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;
}
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);
}
}
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);
}
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;
}
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;
}
}
Aggregations