static ByteBuf expandCumulation(ByteBufAllocator alloc, ByteBuf cumulation, int readable) { ByteBuf oldCumulation = cumulation; cumulation = alloc.buffer(oldCumulation.readableBytes() + readable); cumulation.writeBytes(oldCumulation); oldCumulation.release(); return cumulation; }
/** * Read the given amount of bytes into a new {@link ByteBuf} that is allocated from the {@link ByteBufAllocator}. */ public static ByteBuf readBytes(ByteBufAllocator alloc, ByteBuf buffer, int length) { boolean release = true; ByteBuf dst = alloc.buffer(length); try { buffer.readBytes(dst); release = false; return dst; } finally { if (release) { dst.release(); } } }
private ByteBuf responseToByteBuf(ChannelHandlerContext ctx, String string) { ByteBuf bytebuf = ctx.alloc().buffer(3 + string.length()); bytebuf.writeByte(0xFF); bytebuf.writeShort(string.length()); try { bytebuf.writeBytes(string.getBytes("UTF-16BE")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return bytebuf; } }
public static byte[] hash128toArray(ByteBuf objectState) { long[] hash = hash128(objectState); ByteBuf buf = ByteBufAllocator.DEFAULT.buffer((2 * Long.SIZE) / Byte.SIZE); try { buf.writeLong(hash[0]).writeLong(hash[1]); byte[] dst = new byte[buf.readableBytes()]; buf.readBytes(dst); return dst; } finally { buf.release(); } }
public static ByteBuf getNoAuthResponse(ByteBufAllocator alloc, String error) { byte[] errorAr = stringToBytes(error); ByteBuf response = alloc.buffer(errorAr.length + 25); response.writeByte(ERROR_ID); response.writeBytes(noAuth); response.writeBytes(errorAr); response.writeBytes(CRLFar); return response; }
private Object[] copy(Object[] params) { List<Object> result = new ArrayList<Object>(); for (Object object : params) { if (object instanceof ByteBuf) { ByteBuf b = ((ByteBuf) object); ByteBuf nb = ByteBufAllocator.DEFAULT.buffer(b.readableBytes()); int ri = b.readerIndex(); nb.writeBytes(b); b.readerIndex(ri); result.add(nb); } else { result.add(object); } } return result.toArray(); }
@Override protected ByteBuf extractFrame(ChannelHandlerContext ctx, ByteBuf buffer, int index, int length) { // This is a work around on https://github.com/netty/netty/commit/55fbf007f04fbba7bf50028f3c8b35d6c5ea5947 // Right now we need a copy when sending a message on the server otherwise messages won't be resent to the client ByteBuf frame = ctx.alloc().buffer(length); frame.writeBytes(buffer, index, length); return frame.skipBytes(DataConstants.SIZE_INT); } }
private HttpData serializeTrailersAsMessage(HttpHeaders trailers) { final ByteBuf serialized = ctx.alloc().buffer(); boolean success = false; try { serialized.writeByte(TRAILERS_FRAME_HEADER); // Skip, we'll set this after serializing the headers. serialized.writeInt(0); for (Map.Entry<AsciiString, String> trailer : trailers) { encodeHeader(trailer.getKey(), trailer.getValue(), serialized); } final int messageSize = serialized.readableBytes() - 5; serialized.setInt(1, messageSize); success = true; } finally { if (!success) { serialized.release(); } } return new ByteBufHttpData(serialized, true); }
@Override public ByteBuf copy(int index, int length) { checkIndex(index, length); boolean release = true; ByteBuf buf = alloc().buffer(length); try { buf.writeBytes(this, index, length); release = false; return buf; } finally { if (release) { buf.release(); } } }
private ByteBuf lazyLoadContentBuffer() { if (contentBuffer == null) { contentBuffer = ByteBufAllocator.DEFAULT.buffer((int) getContentLength()); contentBuffer.writeBytes(EXTRA_BYTES).writeBytes(boundary).writeBytes(EXTRA_BYTES).writeBytes(CRLF_BYTES); } return contentBuffer; }
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { return; } in.markReaderIndex(); int length = in.order(ByteOrder.LITTLE_ENDIAN).readInt(); if (in.readableBytes() < length) { in.resetReaderIndex(); return; } ByteBuf buf = ctx.alloc().buffer(length); in.readBytes(buf, length); out.add(buf.retain()); } }
private void handleHandshake(ChannelHandlerContext ctx, DatagramPacket packet, int sessionId) { int challengeToken = queryServer.generateChallengeToken(packet.sender()); ByteBuf out = ctx.alloc().buffer(); out.writeByte(ACTION_HANDSHAKE); out.writeInt(sessionId); writeString(out, String.valueOf(challengeToken)); ctx.write(new DatagramPacket(out, packet.sender())); }
/** * @see <a href="https://github.com/logstash-plugins/logstash-input-beats/blob/master/PROTOCOL.md#ack-frame-type">'ack' frame type</a> */ private void sendACK(Channel channel) throws IOException { if (sequenceNum == windowSize) { final ByteBuf buffer = channel.alloc().buffer(6); buffer.writeByte(PROTOCOL_VERSION); buffer.writeByte(FRAME_ACK); buffer.writeInt((int) sequenceNum); LOG.trace("Sending ACK for sequence number {} on channel {}", sequenceNum, channel); channel.writeAndFlush(buffer); } }
static ByteBuf expandCumulation(ByteBufAllocator alloc, ByteBuf cumulation, int readable) { ByteBuf oldCumulation = cumulation; cumulation = alloc.buffer(oldCumulation.readableBytes() + readable); cumulation.writeBytes(oldCumulation); oldCumulation.release(); return cumulation; }
private void sendResponse(ChannelHandlerContext ctx, int requestId, int type, String payload) { ByteBuf buf = ctx.alloc().buffer().order(ByteOrder.LITTLE_ENDIAN); buf.writeInt(requestId); buf.writeInt(type); buf.writeBytes(payload.getBytes(StandardCharsets.UTF_8)); buf.writeByte(0); buf.writeByte(0); ctx.write(buf); }
public static byte[] hash128toArray(ByteBuf objectState) { long[] hash = hash128(objectState); ByteBuf buf = ByteBufAllocator.DEFAULT.buffer((2 * Long.SIZE) / Byte.SIZE); try { buf.writeLong(hash[0]).writeLong(hash[1]); byte[] dst = new byte[buf.readableBytes()]; buf.readBytes(dst); return dst; } finally { buf.release(); } }
public static ByteBuf getIntegerResponse(ByteBufAllocator alloc, int integer) { ByteBuf response = alloc.buffer(15); response.writeByte(INTEGER_ID); response.writeBytes(intToBytes(integer)); response.writeBytes(CRLFar); return response; }
private Object[] copy(Object[] params) { List<Object> result = new ArrayList<Object>(); for (Object object : params) { if (object instanceof ByteBuf) { ByteBuf b = ((ByteBuf) object); ByteBuf nb = ByteBufAllocator.DEFAULT.buffer(b.readableBytes()); int ri = b.readerIndex(); nb.writeBytes(b); b.readerIndex(ri); result.add(nb); } else { result.add(object); } } return result.toArray(); }
/** * Read the given amount of bytes into a new {@link ByteBuf} that is allocated from the {@link ByteBufAllocator}. */ public static ByteBuf readBytes(ByteBufAllocator alloc, ByteBuf buffer, int length) { boolean release = true; ByteBuf dst = alloc.buffer(length); try { buffer.readBytes(dst); release = false; return dst; } finally { if (release) { dst.release(); } } }