@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); } }
@Override protected ByteBuf doEncodeMulti(MultiValue<?> multiValue, String transcodingErrorMessage) throws TranscodingException { //initial capacity is very roughly and arbitrarily initialized (4 bytes on average per value) final ByteBufOutputStream out = new ByteBufOutputStream(Unpooled.buffer(4 * multiValue.size())); //Note this OutputStream implementation doesn't implement flush() nor close(), so they are left out. try { for (Iterator<?> iterator = multiValue.iterator(); iterator.hasNext(); ) { Object o = iterator.next(); writeValueIntoStream(out, o); if (iterator.hasNext()) { out.writeBytes(","); } } return out.buffer(); } catch (Exception e) { throw new TranscodingException(transcodingErrorMessage, e); } //changing the OutputStream concrete implementation would probably require to close() in a finally block }
@Override protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception { ObjectOutputStream oos = newObjectOutputStream(new ByteBufOutputStream(out)); try { if (resetInterval != 0) { // Resetting will prevent OOM on the receiving side. writtenObjects ++; if (writtenObjects % resetInterval == 0) { oos.reset(); } } oos.writeObject(msg); oos.flush(); } finally { oos.close(); } } }
@Override protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception { ObjectOutputStream oos = newObjectOutputStream(new ByteBufOutputStream(out)); try { if (resetInterval != 0) { // Resetting will prevent OOM on the receiving side. writtenObjects ++; if (writtenObjects % resetInterval == 0) { oos.reset(); } } oos.writeObject(msg); oos.flush(); } finally { oos.close(); } } }
@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); } }
@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(); } }
@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(); } }
@Override protected void encode( ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { int bodyLen = msg.readableBytes(); int headerLen = CodedOutputStream.computeRawVarint32Size(bodyLen); out.ensureWritable(headerLen + bodyLen); CodedOutputStream headerOut = CodedOutputStream.newInstance(new ByteBufOutputStream(out), headerLen); headerOut.writeRawVarint32(bodyLen); headerOut.flush(); out.writeBytes(msg, msg.readerIndex(), bodyLen); } }
@Override protected void encode( ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { int bodyLen = msg.readableBytes(); int headerLen = CodedOutputStream.computeRawVarint32Size(bodyLen); out.ensureWritable(headerLen + bodyLen); CodedOutputStream headerOut = CodedOutputStream.newInstance(new ByteBufOutputStream(out), headerLen); headerOut.writeRawVarint32(bodyLen); headerOut.flush(); out.writeBytes(msg, msg.readerIndex(), bodyLen); } }