Search in sources :

Example 21 with Buffer

use of org.fusesource.hawtbuf.Buffer in project fabric8 by jboss-fuse.

the class OpenWireFormat method marshal.

public synchronized Buffer marshal(Object command) throws IOException {
    if (cacheEnabled) {
        runMarshallCacheEvictionSweep();
    }
    // MarshallAware ma = null;
    // // If not using value caching, then the marshaled form is always the
    // // same
    // if (!cacheEnabled && ((DataStructure)command).isMarshallAware()) {
    // ma = (MarshallAware)command;
    // }
    Buffer sequence = null;
    if (sequence == null) {
        int size = 1;
        if (command != null) {
            DataStructure c = (DataStructure) command;
            byte type = c.getDataStructureType();
            DataStreamMarshaller dsm = (DataStreamMarshaller) dataMarshallers[type & 0xFF];
            if (dsm == null) {
                throw new IOException("Unknown data type: " + type);
            }
            if (tightEncodingEnabled) {
                BooleanStream bs = new BooleanStream();
                size += dsm.tightMarshal1(this, c, bs);
                size += bs.marshalledSize();
                bytesOut.restart(size);
                if (!sizePrefixDisabled) {
                    bytesOut.writeInt(size);
                }
                bytesOut.writeByte(type);
                bs.marshal(bytesOut);
                dsm.tightMarshal2(this, c, bytesOut, bs);
                sequence = bytesOut.toBuffer();
            } else {
                bytesOut.restart();
                if (!sizePrefixDisabled) {
                    // we don't know the final size
                    bytesOut.writeInt(0);
                // yet but write this here for
                // now.
                }
                bytesOut.writeByte(type);
                dsm.looseMarshal(this, c, bytesOut);
                sequence = bytesOut.toBuffer();
                if (!sizePrefixDisabled) {
                    size = sequence.getLength() - 4;
                    int pos = sequence.offset;
                    sequence.offset = 0;
                    BufferEditor.big(sequence).writeInt(size);
                    sequence.offset = pos;
                }
            }
        } else {
            bytesOut.restart(5);
            bytesOut.writeInt(size);
            bytesOut.writeByte(NULL_TYPE);
            sequence = bytesOut.toBuffer();
        }
    // if( ma!=null ) {
    // ma.setCachedMarshalledForm(this, sequence);
    // }
    }
    return sequence;
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) DataStructure(io.fabric8.gateway.handlers.detecting.protocol.openwire.command.DataStructure) IOException(java.io.IOException)

Example 22 with Buffer

use of org.fusesource.hawtbuf.Buffer in project fabric8 by jboss-fuse.

the class OpenWireFormat method marshal.

public synchronized void marshal(Object o, DataByteArrayOutputStream dataOut) throws IOException {
    if (cacheEnabled) {
        runMarshallCacheEvictionSweep();
    }
    int size = 1;
    if (o != null) {
        DataStructure c = (DataStructure) o;
        byte type = c.getDataStructureType();
        DataStreamMarshaller dsm = (DataStreamMarshaller) dataMarshallers[type & 0xFF];
        if (dsm == null) {
            throw new IOException("Unknown data type: " + type);
        }
        if (tightEncodingEnabled) {
            BooleanStream bs = new BooleanStream();
            size += dsm.tightMarshal1(this, c, bs);
            size += bs.marshalledSize();
            if (!sizePrefixDisabled) {
                dataOut.writeInt(size);
            }
            dataOut.writeByte(type);
            bs.marshal(dataOut);
            dsm.tightMarshal2(this, c, dataOut, bs);
        } else {
            DataByteArrayOutputStream looseOut = dataOut;
            if (!sizePrefixDisabled) {
                bytesOut.restart();
                looseOut = bytesOut;
            }
            looseOut.writeByte(type);
            dsm.looseMarshal(this, c, looseOut);
            if (!sizePrefixDisabled) {
                Buffer sequence = bytesOut.toBuffer();
                dataOut.writeInt(sequence.getLength());
                dataOut.write(sequence.getData(), sequence.getOffset(), sequence.getLength());
            }
        }
    } else {
        if (!sizePrefixDisabled) {
            dataOut.writeInt(size);
        }
        dataOut.writeByte(NULL_TYPE);
    }
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) DataByteArrayOutputStream(org.fusesource.hawtbuf.DataByteArrayOutputStream) DataStructure(io.fabric8.gateway.handlers.detecting.protocol.openwire.command.DataStructure) IOException(java.io.IOException)

Example 23 with Buffer

use of org.fusesource.hawtbuf.Buffer in project fabric8 by jboss-fuse.

the class OpenWireFormat method tightMarshalNestedObject1.

// public void debug(String msg) {
// String t = (Thread.currentThread().getName()+" ").substring(0, 40);
// System.out.println(t+": "+msg);
// }
public int tightMarshalNestedObject1(DataStructure o, BooleanStream bs) throws IOException {
    bs.writeBoolean(o != null);
    if (o == null) {
        return 0;
    }
    if (o.isMarshallAware()) {
        // MarshallAware ma = (MarshallAware)o;
        Buffer sequence = null;
        // sequence=ma.getCachedMarshalledForm(this);
        bs.writeBoolean(sequence != null);
        if (sequence != null) {
            return 1 + sequence.getLength();
        }
    }
    byte type = o.getDataStructureType();
    DataStreamMarshaller dsm = (DataStreamMarshaller) dataMarshallers[type & 0xFF];
    if (dsm == null) {
        throw new IOException("Unknown data type: " + type);
    }
    return 1 + dsm.tightMarshal1(this, o, bs);
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) IOException(java.io.IOException)

Example 24 with Buffer

use of org.fusesource.hawtbuf.Buffer in project fabric8 by jboss-fuse.

the class BaseDataStreamMarshaller method looseUnmarshalBuffer.

protected Buffer looseUnmarshalBuffer(DataByteArrayInputStream dataIn) throws IOException {
    Buffer rc = null;
    if (dataIn.readBoolean()) {
        int size = dataIn.readInt();
        byte[] t = new byte[size];
        dataIn.readFully(t);
        rc = new Buffer(t, 0, size);
    }
    return rc;
}
Also used : UTF8Buffer(org.fusesource.hawtbuf.UTF8Buffer) Buffer(org.fusesource.hawtbuf.Buffer)

Example 25 with Buffer

use of org.fusesource.hawtbuf.Buffer in project fabric8 by jboss-fuse.

the class BaseDataStreamMarshaller method tightUnmarshalString.

@SuppressWarnings("deprecation")
protected UTF8Buffer tightUnmarshalString(DataByteArrayInputStream dataIn, BooleanStream bs) throws IOException {
    if (bs.readBoolean()) {
        // ignored for now.
        boolean ascii = bs.readBoolean();
        int size = dataIn.readShort();
        if (size == 0) {
            return new UTF8Buffer("");
        } else {
            Buffer buffer = dataIn.readBuffer(size);
            return buffer.utf8();
        }
    } else {
        return null;
    }
}
Also used : UTF8Buffer(org.fusesource.hawtbuf.UTF8Buffer) Buffer(org.fusesource.hawtbuf.Buffer) UTF8Buffer(org.fusesource.hawtbuf.UTF8Buffer)

Aggregations

Buffer (org.fusesource.hawtbuf.Buffer)45 IOException (java.io.IOException)17 ByteBuffer (java.nio.ByteBuffer)10 Exchange (org.apache.camel.Exchange)9 Test (org.junit.Test)9 Transaction (org.fusesource.hawtdb.api.Transaction)8 UTF8Buffer (org.fusesource.hawtbuf.UTF8Buffer)6 SortedIndex (org.fusesource.hawtdb.api.SortedIndex)6 InflaterInputStream (java.util.zip.InflaterInputStream)5 ByteArrayInputStream (org.fusesource.hawtbuf.ByteArrayInputStream)5 BufferState (io.fabric8.dosgi.io.ProtocolCodec.BufferState)4 OpenwireException (io.fabric8.gateway.handlers.detecting.protocol.openwire.support.OpenwireException)3 DataStructure (io.fabric8.gateway.handlers.detecting.protocol.openwire.command.DataStructure)2 RandomAccessFile (java.io.RandomAccessFile)2 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)2 BufferEditor (org.fusesource.hawtbuf.BufferEditor)2 ByteArrayOutputStream (org.fusesource.hawtbuf.ByteArrayOutputStream)2 DataByteArrayOutputStream (org.fusesource.hawtbuf.DataByteArrayOutputStream)2 Index (org.fusesource.hawtdb.api.Index)2 ObjectSerializationStrategy (io.fabric8.dosgi.api.ObjectSerializationStrategy)1