Refine search
@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, 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 ByteBuf encode(Object in) throws IOException { ByteBuf out = ByteBufAllocator.DEFAULT.buffer(); try { ByteBufOutputStream result = new ByteBufOutputStream(out); ObjectOutputStream outputStream = new ObjectOutputStream(result); outputStream.writeObject(in); outputStream.close(); return result.buffer(); } catch (IOException e) { out.release(); throw e; } } };
@Override public ByteBuf encode(Object in) throws IOException { ByteBuf out = ByteBufAllocator.DEFAULT.buffer(); try { ByteBufOutputStream os = new ByteBufOutputStream(out); mapObjectMapper.writeValue((OutputStream)os, in); return os.buffer(); } catch (IOException e) { out.release(); throw e; } } };
@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(); } } }
@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(); } } }
/** * Allocates a buffer and serializes the server failure into it. * * <p>The cause must not be or contain any user types as causes. * * @param alloc ByteBuf allocator for the buffer to serialize message into * @param cause Failure cause * @return Serialized server failure message * @throws IOException Serialization failures are forwarded */ public static ByteBuf serializeServerFailure(ByteBufAllocator alloc, Throwable cause) throws IOException { ByteBuf buf = alloc.ioBuffer(); // Frame length is set at end buf.writeInt(0); writeHeader(buf, KvStateRequestType.SERVER_FAILURE); try (ByteBufOutputStream bbos = new ByteBufOutputStream(buf); ObjectOutputStream out = new ObjectOutputStream(bbos)) { out.writeObject(cause); } // Set frame length int frameLength = buf.readableBytes() - 4; buf.setInt(0, frameLength); return buf; }
try { out.discardReadBytes(); objectOutstream = new ObjectOutputStream(new ByteBufOutputStream(out)); objectOutstream.writeObject(testResult); objectOutstream.flush(); ctx.flush(); return;
@Override ByteBuf write(ByteBufAllocator allocator) throws IOException { final ByteBuf result = allocateBuffer(allocator, ID); try (ObjectOutputStream oos = new ObjectOutputStream(new ByteBufOutputStream(result))) { oos.writeObject(cause); if (receiverId != null) { result.writeBoolean(true); receiverId.writeTo(result); } else { result.writeBoolean(false); } // Update frame length... result.setInt(0, result.readableBytes()); return result; } catch (Throwable t) { result.release(); if (t instanceof IOException) { throw (IOException) t; } else { throw new IOException(t); } } }
/** * 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) { try (ByteBufOutputStream bbos = new ByteBufOutputStream(b)) { try (ObjectOutputStream oos = new ObjectOutputStream(bbos)) { oos.writeObject(o); } } catch (IOException ie) { log.error("Exception during serialization!", ie); } } }
@Override public void toBinary(Object o, ByteBuf buf) { try { Closer closer = Closer.create(); // TODO: Convert to try-with-resources once Java6 is faded out try { ByteBufOutputStream bout = closer.register(new ByteBufOutputStream(buf)); ObjectOutputStream oout = closer.register(new CompactObjectOutputStream(bout)); oout.writeObject(o); oout.flush(); } catch (Throwable e) { // must catch Throwable throw closer.rethrow(e); } finally { closer.close(); } } catch (IOException ex) { Serializers.LOG.error("JavaSerializer: Could not Serialize object of type " + o.getClass(), ex); } }
public static byte[] compressedFrame(ByteBuf uncompressed) { final ByteBuf compressed = Unpooled.buffer(); try (ByteBufInputStream is = new ByteBufInputStream(uncompressed, true); GZIPOutputStream os = new GZIPOutputStream(new ByteBufOutputStream(compressed))) { ByteStreams.copy(is, os); } catch (IOException e) { throw new UncheckedIOException(e); } final ByteBuf buf = Unpooled.buffer(); buf.writeByte(1); buf.writeInt(compressed.readableBytes()); buf.writeBytes(compressed); compressed.release(); final byte[] result = ByteBufUtil.getBytes(buf); buf.release(); return result; }
return Unpooled.EMPTY_BUFFER; final ByteBuf buf = alloc.buffer(serializedSize); boolean success = false; try { message.writeTo(CodedOutputStream.newInstance(buf.nioBuffer(0, serializedSize))); buf.writerIndex(serializedSize); success = true; } finally { if (!success) { buf.release(); final ByteBuf buf = alloc.buffer(); boolean success = false; try (ByteBufOutputStream os = new ByteBufOutputStream(buf)) { jsonMarshaller.writeValue(message, os); success = true;
@Override public void reset() { if (bytesOut != null) { ByteBuf writeBuf = bytesOut.buffer(); Binary body = new Binary(writeBuf.array(), writeBuf.arrayOffset(), writeBuf.readableBytes()); setBody(new Data(body)); try { bytesOut.close(); } catch (IOException e) { } bytesOut = null; } else if (bytesIn != null) { try { bytesIn.close(); } catch (IOException e) { } bytesIn = null; } }
ByteBuf buf = ctx.alloc().buffer(); OutputStream os = new ByteBufOutputStream(buf); CodedOutputStream cos = CodedOutputStream.newInstance(os); final CompositeByteBuf cbb = ctx.alloc().compositeBuffer(msg.dBodies.length + 1); cbb.addComponent(buf); int bufLength = buf.readableBytes(); for (ByteBuf b : msg.dBodies) { cbb.addComponent(b); bufLength += b.readableBytes();
@Override public Body stream(Action<? super OutputStream> action) throws Exception { ByteBuf byteBuf = byteBufAllocator.buffer(); try (OutputStream outputStream = new ByteBufOutputStream(byteBuf)) { action.execute(outputStream); } catch (Throwable t) { byteBuf.release(); throw t; } setBodyByteBuf(byteBuf); return this; }
private FullHttpRequest createFullHttpRequest(HttpHeaders headers) { io.netty.handler.codec.http.HttpMethod nettyMethod = io.netty.handler.codec.http.HttpMethod.valueOf(this.method.name()); String authority = this.uri.getRawAuthority(); String path = this.uri.toString().substring(this.uri.toString().indexOf(authority) + authority.length()); FullHttpRequest nettyRequest = new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, nettyMethod, path, this.body.buffer()); nettyRequest.headers().set(HttpHeaders.HOST, this.uri.getHost() + ":" + getPort(this.uri)); nettyRequest.headers().set(HttpHeaders.CONNECTION, "close"); headers.forEach((headerName, headerValues) -> nettyRequest.headers().add(headerName, headerValues)); if (!nettyRequest.headers().contains(HttpHeaders.CONTENT_LENGTH) && this.body.buffer().readableBytes() > 0) { nettyRequest.headers().set(HttpHeaders.CONTENT_LENGTH, this.body.buffer().readableBytes()); } return nettyRequest; }
@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); bout.flush(); bout.close(); int endIdx = out.writerIndex(); int fieldsSize = endIdx - startIdx - TYPE_PLUS_LENGTH_SIZE; out.setInt(startIdx + TYPE_SIZE, fieldsSize + currentBlockSize); }
private ByteBuf writeCompressed(ByteBuf message) throws IOException { final CompositeByteBuf compressed = alloc.compositeBuffer(); try (OutputStream compressingStream = compressor.compress(new ByteBufOutputStream(compressed))) { compressingStream.write(ByteBufUtil.getBytes(message)); } finally { message.release(); } return write(compressed, true); }
public void fixedNumberEviction(int numEvictions) { ByteBufOutputStream byteStream = new ByteBufOutputStream(PooledByteBufAllocator.DEFAULT.buffer()); try { for (int i = 0; i < numEvictions; i++) { doEvictPage(byteStream); } } finally { byteStream.buffer().release(); } }