List of usage examples for io.netty.buffer ByteBufOutputStream write
@Override public void write(int b) throws IOException
From source file:com.addthis.hydra.store.skiplist.Page.java
License:Apache License
public byte[] encode(ByteBufOutputStream out, boolean record) { SkipListCacheMetrics metrics = parent.metrics; parent.numPagesEncoded.getAndIncrement(); try {//from www. ja va 2 s . c o m OutputStream os = out; out.write(gztype | FLAGS_HAS_ESTIMATES | FLAGS_IS_SPARSE); switch (gztype) { case 0: break; case 1: os = new DeflaterOutputStream(out, new Deflater(gzlevel)); break; case 2: os = new GZOut(out, gzbuf, gzlevel); break; case 3: os = new LZFOutputStream(out); break; case 4: os = new SnappyOutputStream(out); break; default: throw new RuntimeException("invalid gztype: " + gztype); } DataOutputStream dos = new DataOutputStream(os); byte[] firstKeyEncoded = keyCoder.keyEncode(firstKey); byte[] nextFirstKeyEncoded = keyCoder.keyEncode(nextFirstKey); updateHistogram(metrics.encodeNextFirstKeySize, nextFirstKeyEncoded.length, record); Varint.writeUnsignedVarInt(size, dos); Varint.writeUnsignedVarInt(firstKeyEncoded.length, dos); dos.write(firstKeyEncoded); Varint.writeUnsignedVarInt(nextFirstKeyEncoded.length, dos); if (nextFirstKeyEncoded.length > 0) { dos.write(nextFirstKeyEncoded); } for (int i = 0; i < size; i++) { byte[] keyEncoded = keyCoder.keyEncode(keys.get(i)); byte[] rawVal = rawValues.get(i); if (rawVal == null || encodeType != KeyCoder.EncodeType.SPARSE) { fetchValue(i); rawVal = keyCoder.valueEncode(values.get(i), KeyCoder.EncodeType.SPARSE); } updateHistogram(metrics.encodeKeySize, keyEncoded.length, record); updateHistogram(metrics.encodeValueSize, rawVal.length, record); Varint.writeUnsignedVarInt(keyEncoded.length, dos); dos.write(keyEncoded); Varint.writeUnsignedVarInt(rawVal.length, dos); dos.write(rawVal); } Varint.writeUnsignedVarInt((estimateTotal > 0 ? estimateTotal : 1), dos); Varint.writeUnsignedVarInt((estimates > 0 ? estimates : 1), dos); switch (gztype) { case 1: ((DeflaterOutputStream) os).finish(); break; case 2: ((GZOut) os).finish(); break; case 4: os.flush(); break; } os.flush(); os.close(); dos.close(); ByteBuf buffer = out.buffer(); byte[] returnValue = new byte[out.writtenBytes()]; buffer.readBytes(returnValue); buffer.clear(); updateHistogram(metrics.numberKeysPerPage, size, record); updateHistogram(metrics.encodePageSize, returnValue.length, record); return returnValue; } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:com.addthis.hydra.store.skiplist.SparsePage.java
License:Apache License
public byte[] encode(ByteBufOutputStream out, boolean record) { SkipListCacheMetrics metrics = parent.metrics; parent.numPagesEncoded.getAndIncrement(); try {/*from w w w .j a v a2s . co m*/ OutputStream os = out; out.write(gztype | FLAGS_HAS_ESTIMATES | FLAGS_IS_SPARSE); switch (gztype) { case 0: break; case 1: os = new DeflaterOutputStream(out, new Deflater(gzlevel)); break; case 2: os = new GZOut(out, gzbuf, gzlevel); break; case 3: os = new LZFOutputStream(out); break; case 4: os = new SnappyOutputStream(out); break; default: throw new RuntimeException("invalid gztype: " + gztype); } DataOutputStream dos = new DataOutputStream(os); byte[] firstKeyEncoded = keyCoder.keyEncode(firstKey); byte[] nextFirstKeyEncoded = keyCoder.keyEncode(nextFirstKey); updateHistogram(metrics.encodeNextFirstKeySize, nextFirstKeyEncoded.length, record); Varint.writeUnsignedVarInt(size, dos); Varint.writeUnsignedVarInt(firstKeyEncoded.length, dos); dos.write(firstKeyEncoded); Varint.writeUnsignedVarInt(nextFirstKeyEncoded.length, dos); if (nextFirstKeyEncoded.length > 0) { dos.write(nextFirstKeyEncoded); } for (int i = 0; i < size; i++) { byte[] keyEncoded = keyCoder.keyEncode(keys.get(i)); byte[] rawVal = rawValues.get(i); if (rawVal == null || encodeType != PageEncodeType.SPARSE) { fetchValue(i); rawVal = keyCoder.valueEncode(values.get(i), PageEncodeType.SPARSE); } updateHistogram(metrics.encodeKeySize, keyEncoded.length, record); updateHistogram(metrics.encodeValueSize, rawVal.length, record); Varint.writeUnsignedVarInt(keyEncoded.length, dos); dos.write(keyEncoded); Varint.writeUnsignedVarInt(rawVal.length, dos); dos.write(rawVal); } Varint.writeUnsignedVarInt((estimateTotal > 0 ? estimateTotal : 1), dos); Varint.writeUnsignedVarInt((estimates > 0 ? estimates : 1), dos); switch (gztype) { case 1: ((DeflaterOutputStream) os).finish(); break; case 2: ((GZOut) os).finish(); break; } os.flush(); // flush should be called by dos.close(), but better safe than sorry dos.close(); ByteBuf buffer = out.buffer(); byte[] returnValue = new byte[out.writtenBytes()]; buffer.readBytes(returnValue); buffer.clear(); updateHistogram(metrics.numberKeysPerPage, size, record); updateHistogram(metrics.encodePageSize, returnValue.length, record); return returnValue; } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:com.ebay.jetstream.messaging.transport.netty.serializer.KryoObjectEncoder.java
License:MIT License
@Override protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception { KryoContext kryoContext = kryoContextHolder.get(); Kryo kryo = kryoContext.getKryo();//from w w w.jav a2s. c om Output output = kryoContext.getOut(); output.clear(); ByteBufOutputStream bout = new ByteBufOutputStream(out); int startIdx = out.writerIndex(); bout.write(LENGTH_PLACEHOLDER); output.setOutputStream(bout); output.writeByte(StreamMessageDecoder.KRYO_STREAM_VERSION); kryo.writeClassAndObject(output, msg); output.flush(); bout.flush(); bout.close(); output.close(); int endIdx = out.writerIndex(); out.setInt(startIdx, endIdx - startIdx - 4); }
From source file:io.pravega.shared.protocol.netty.CommandEncoder.java
License:Open Source License
@SneakyThrows(IOException.class) private void writeMessage(AppendBlock block, ByteBuf out) { int startIdx = out.writerIndex(); ByteBufOutputStream bout = new ByteBufOutputStream(out); bout.writeInt(block.getType().getCode()); bout.write(LENGTH_PLACEHOLDER); block.writeFields(bout);// w ww .ja v a2 s. c om bout.flush(); bout.close(); int endIdx = out.writerIndex(); int fieldsSize = endIdx - startIdx - TYPE_PLUS_LENGTH_SIZE; out.setInt(startIdx + TYPE_SIZE, fieldsSize + currentBlockSize); }
From source file:io.pravega.shared.protocol.netty.CommandEncoder.java
License:Open Source License
@SneakyThrows(IOException.class) private int writeMessage(WireCommand msg, ByteBuf out) { int startIdx = out.writerIndex(); ByteBufOutputStream bout = new ByteBufOutputStream(out); bout.writeInt(msg.getType().getCode()); bout.write(LENGTH_PLACEHOLDER); msg.writeFields(bout);//from ww w . j a va 2 s.co m bout.flush(); bout.close(); int endIdx = out.writerIndex(); int fieldsSize = endIdx - startIdx - TYPE_PLUS_LENGTH_SIZE; out.setInt(startIdx + TYPE_SIZE, fieldsSize); return endIdx - startIdx; }
From source file:org.teiid.transport.ObjectEncoder.java
License:Apache License
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { ByteBuf out = allocateBuffer(ctx, this.estimatedLength, this.preferDirect); int startIdx = out.writerIndex(); ByteBufOutputStream bout = new ByteBufOutputStream(out); bout.write(LENGTH_PLACEHOLDER); final CompactObjectOutputStream oout = new CompactObjectOutputStream(bout); try {/*from w w w. j ava 2 s . c o m*/ oout.writeObject(msg); ExternalizeUtil.writeCollection(oout, oout.getReferences()); oout.flush(); oout.close(); int endIdx = out.writerIndex(); out.setInt(startIdx, endIdx - startIdx - 4); if (out.isReadable()) { ctx.write(out, promise); for (InputStream is : oout.getStreams()) { ctx.write(new AnonymousChunkedStream(new BufferedInputStream(is, CHUNK_SIZE)), promise); } } else { out.release(); ctx.write(Unpooled.EMPTY_BUFFER, promise); } ctx.flush(); out = null; } catch (Throwable t) { throw new FailedWriteException(msg, t); } finally { if (out != null) { out.release(); } } }
From source file:org.tinygroup.nettyremote.codec.serialization.HessianEncoder.java
License:GNU General Public License
@Override protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception { int startIdx = out.writerIndex(); ByteBufOutputStream bout = new ByteBufOutputStream(out); bout.write(LENGTH_PLACEHOLDER); // ObjectOutputStream oout = new CompactObjectOutputStream(bout); // oout.writeObject(msg); // oout.flush(); // oout.close(); SerializerFactory serializerFactory = new SerializerFactory(); serializerFactory.addFactory(new BigDecimalSerializerFactory()); HessianOutput hout = new HessianOutput(bout); hout.setSerializerFactory(serializerFactory); hout.writeObject(msg);/*from ww w . ja va2 s. c o m*/ int endIdx = out.writerIndex(); out.setInt(startIdx, endIdx - startIdx - 4); }