use of io.netty.buffer.ByteBufOutputStream in project CorfuDB by CorfuDB.
the class JSONSerializer method serialize.
/**
* Serialize an object into a given byte buffer.
*
* @param o The object to serialize.
* @param b The bytebuf to serialize it into.
*/
@Override
public void serialize(Object o, ByteBuf b) {
String className = o == null ? "null" : o.getClass().getName();
if (className.endsWith(ICorfuSMR.CORFUSMR_SUFFIX)) {
String SMRClass = className.split("\\$")[0];
className = "CorfuObject";
byte[] classNameBytes = className.getBytes();
b.writeShort(classNameBytes.length);
b.writeBytes(classNameBytes);
byte[] SMRClassNameBytes = SMRClass.getBytes();
b.writeShort(SMRClassNameBytes.length);
b.writeBytes(SMRClassNameBytes);
UUID id = ((ICorfuSMR) o).getCorfuStreamID();
log.trace("Serializing a CorfuObject of type {} as a stream pointer to {}", SMRClass, id);
b.writeLong(id.getMostSignificantBits());
b.writeLong(id.getLeastSignificantBits());
} else {
byte[] classNameBytes = className.getBytes();
b.writeShort(classNameBytes.length);
b.writeBytes(classNameBytes);
if (o == null) {
return;
}
try (ByteBufOutputStream bbos = new ByteBufOutputStream(b)) {
try (OutputStreamWriter osw = new OutputStreamWriter(bbos)) {
gson.toJson(o, o.getClass(), osw);
}
} catch (IOException ie) {
log.error("Exception during serialization!", ie);
}
}
}
use of io.netty.buffer.ByteBufOutputStream in project hbase by apache.
the class NettyRpcDuplexHandler method writeRequest.
private void writeRequest(ChannelHandlerContext ctx, Call call, ChannelPromise promise) throws IOException {
id2Call.put(call.id, call);
ByteBuf cellBlock = cellBlockBuilder.buildCellBlock(codec, compressor, call.cells, ctx.alloc());
CellBlockMeta cellBlockMeta;
if (cellBlock != null) {
CellBlockMeta.Builder cellBlockMetaBuilder = CellBlockMeta.newBuilder();
cellBlockMetaBuilder.setLength(cellBlock.writerIndex());
cellBlockMeta = cellBlockMetaBuilder.build();
} else {
cellBlockMeta = null;
}
RequestHeader requestHeader = IPCUtil.buildRequestHeader(call, cellBlockMeta);
int sizeWithoutCellBlock = IPCUtil.getTotalSizeWhenWrittenDelimited(requestHeader, call.param);
int totalSize = cellBlock != null ? sizeWithoutCellBlock + cellBlock.writerIndex() : sizeWithoutCellBlock;
ByteBuf buf = ctx.alloc().buffer(sizeWithoutCellBlock + 4);
buf.writeInt(totalSize);
ByteBufOutputStream bbos = new ByteBufOutputStream(buf);
requestHeader.writeDelimitedTo(bbos);
if (call.param != null) {
call.param.writeDelimitedTo(bbos);
}
if (cellBlock != null) {
ChannelPromise withoutCellBlockPromise = ctx.newPromise();
ctx.write(buf, withoutCellBlockPromise);
ChannelPromise cellBlockPromise = ctx.newPromise();
ctx.write(cellBlock, cellBlockPromise);
PromiseCombiner combiner = new PromiseCombiner();
combiner.addAll(withoutCellBlockPromise, cellBlockPromise);
combiner.finish(promise);
} else {
ctx.write(buf, promise);
}
}
use of io.netty.buffer.ByteBufOutputStream in project netty by netty.
the class LzmaFrameEncoder method encode.
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
final int length = in.readableBytes();
InputStream bbIn = null;
ByteBufOutputStream bbOut = null;
try {
bbIn = new ByteBufInputStream(in);
bbOut = new ByteBufOutputStream(out);
bbOut.writeByte(properties);
bbOut.writeInt(littleEndianDictionarySize);
bbOut.writeLong(Long.reverseBytes(length));
encoder.code(bbIn, bbOut, -1, -1, null);
} finally {
if (bbIn != null) {
bbIn.close();
}
if (bbOut != null) {
bbOut.close();
}
}
}
use of io.netty.buffer.ByteBufOutputStream in project netty by netty.
the class ObjectEncoder method encode.
@Override
protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
int startIdx = out.writerIndex();
ByteBufOutputStream bout = new ByteBufOutputStream(out);
ObjectOutputStream oout = null;
try {
bout.write(LENGTH_PLACEHOLDER);
oout = new CompactObjectOutputStream(bout);
oout.writeObject(msg);
oout.flush();
} finally {
if (oout != null) {
oout.close();
} else {
bout.close();
}
}
int endIdx = out.writerIndex();
out.setInt(startIdx, endIdx - startIdx - 4);
}
use of io.netty.buffer.ByteBufOutputStream in project netty by netty.
the class ObjectEncoderOutputStream method writeObject.
@Override
public void writeObject(Object obj) throws IOException {
ByteBuf buf = Unpooled.buffer(estimatedLength);
try {
ObjectOutputStream oout = new CompactObjectOutputStream(new ByteBufOutputStream(buf));
try {
oout.writeObject(obj);
oout.flush();
} finally {
oout.close();
}
int objectSize = buf.readableBytes();
writeInt(objectSize);
buf.getBytes(0, this, objectSize);
} finally {
buf.release();
}
}
Aggregations