Search in sources :

Example 1 with BinaryMemory

use of php.runtime.memory.BinaryMemory in project jphp by jphp-compiler.

the class OutputBuffer method doFlush.

protected void doFlush(boolean flush) throws Throwable {
    /*if (buffer.size() == 0)
            return;*/
    if (flush) {
        isFlushed = true;
        if (status != HANDLER_START && status != HANDLER_FLUSH)
            status = HANDLER_WRITE;
    } else {
        if (status == HANDLER_FINAL)
            status |= HANDLER_CLEAN;
        else
            status = HANDLER_CLEAN;
    }
    if (invoker != null) {
        invoker.setTrace(trace == null ? TraceInfo.UNKNOWN : trace);
        Memory[] args;
        if (!binaryInBuffer) {
            args = new Memory[] { new StringMemory(buffer.toString(environment.getDefaultCharset().name())), null };
        } else
            args = new Memory[] { new BinaryMemory(buffer.toByteArray()), null };
        args[1] = LongMemory.valueOf(status);
        invoker.setTrace(trace == null ? TraceInfo.UNKNOWN : trace);
        Memory result;
        incLock();
        try {
            result = invoker.call(args).toValue();
        } finally {
            decLock();
        }
        if (result != Memory.FALSE) {
            byte[] data = result instanceof BinaryMemory ? result.getBinaryBytes(environment.getDefaultCharset()) : result.toString().getBytes(environment.getDefaultCharset());
            if (flush) {
                if (output == null)
                    parentOutput.write(data);
                else
                    output.write(data);
                reset();
                status = HANDLER_FLUSH;
            }
            return;
        }
    }
    if (flush) {
        status |= HANDLER_FLUSH;
        if (output == null)
            parentOutput.write(buffer.toByteArray());
        else
            buffer.writeTo(output);
        reset();
    }
}
Also used : BinaryMemory(php.runtime.memory.BinaryMemory) Memory(php.runtime.Memory) BinaryMemory(php.runtime.memory.BinaryMemory) LongMemory(php.runtime.memory.LongMemory) StringMemory(php.runtime.memory.StringMemory) StringMemory(php.runtime.memory.StringMemory)

Example 2 with BinaryMemory

use of php.runtime.memory.BinaryMemory in project jphp by jphp-compiler.

the class BinaryCharArrayMemory method valueOfIndex.

@Override
public Memory valueOfIndex(TraceInfo trace, String index) {
    int _index = -1;
    Memory tmp = StringMemory.toLong(index);
    if (tmp != null)
        _index = tmp.toInteger();
    if (_index >= 0 && _index < buffer.length())
        return new BinaryMemory(buffer.charAt(_index));
    else
        return CONST_EMPTY_STRING;
}
Also used : BinaryMemory(php.runtime.memory.BinaryMemory) Memory(php.runtime.Memory) BinaryMemory(php.runtime.memory.BinaryMemory) StringMemory(php.runtime.memory.StringMemory)

Example 3 with BinaryMemory

use of php.runtime.memory.BinaryMemory in project jphp by jphp-compiler.

the class PSqlStatement method bind.

@Signature
public void bind(Environment env, int index, Memory value) throws SQLException {
    if (value.instanceOf(WrapTime.class)) {
        WrapTime time = value.toObject(WrapTime.class);
        statement.setDate(index + 1, new Date(time.getDate().getTime()), time.getCalendar());
    } else if (value.instanceOf(Stream.class)) {
        statement.setBlob(index + 1, Stream.getInputStream(env, value));
    } else if (value.toValue() instanceof BinaryMemory) {
        statement.setBytes(index + 1, value.getBinaryBytes(env.getDefaultCharset()));
    } else {
        if (value.isNull()) {
            statement.setNull(index + 1, Types.NULL);
        } else {
            switch(value.getRealType()) {
                case INT:
                    statement.setLong(index + 1, value.toLong());
                    break;
                case DOUBLE:
                    statement.setDouble(index + 1, value.toDouble());
                    break;
                case BOOL:
                    statement.setBoolean(index + 1, value.toBoolean());
                    break;
                default:
                    statement.setString(index + 1, value.toString());
            }
        }
    }
}
Also used : WrapTime(php.runtime.ext.core.classes.time.WrapTime) BinaryMemory(php.runtime.memory.BinaryMemory) Stream(php.runtime.ext.core.classes.stream.Stream) InputStream(java.io.InputStream) Signature(php.runtime.annotation.Reflection.Signature)

Example 4 with BinaryMemory

use of php.runtime.memory.BinaryMemory in project jphp by jphp-compiler.

the class BinUtils method of.

@FastMethod
@Signature(@Arg("value"))
public static Memory of(Environment env, Memory... args) {
    if (ParameterEntity.checkTypeHinting(env, args[0], HintType.TRAVERSABLE)) {
        ForeachIterator iterator = args[0].getNewIterator(env, false, false);
        ByteArrayOutputStream tmp = new ByteArrayOutputStream();
        while (iterator.next()) {
            tmp.write(iterator.getValue().toInteger());
        }
        return new BinaryMemory(tmp.toByteArray());
    }
    return new BinaryMemory(args[0].getBinaryBytes(env.getDefaultCharset()));
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) BinaryMemory(php.runtime.memory.BinaryMemory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FastMethod(php.runtime.annotation.Runtime.FastMethod)

Example 5 with BinaryMemory

use of php.runtime.memory.BinaryMemory in project jphp by jphp-compiler.

the class ResourceStream method readFully.

@Override
@Signature
public Memory readFully(Environment env, Memory... args) throws IOException {
    ByteArrayOutputStream tmp = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int read;
    while ((read = stream.read(buf)) > 0) {
        tmp.write(buf, 0, read);
        position += read;
    }
    return new BinaryMemory(tmp.toByteArray());
}
Also used : BinaryMemory(php.runtime.memory.BinaryMemory) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

BinaryMemory (php.runtime.memory.BinaryMemory)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Memory (php.runtime.Memory)2 StringMemory (php.runtime.memory.StringMemory)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ByteBuffer (java.nio.ByteBuffer)1 Signature (php.runtime.annotation.Reflection.Signature)1 FastMethod (php.runtime.annotation.Runtime.FastMethod)1 CriticalException (php.runtime.exceptions.CriticalException)1 Stream (php.runtime.ext.core.classes.stream.Stream)1 WrapTime (php.runtime.ext.core.classes.time.WrapTime)1 ForeachIterator (php.runtime.lang.ForeachIterator)1 LongMemory (php.runtime.memory.LongMemory)1