@Override public int read() throws IOException { if (buffer.isReadable()) { return buffer.readByte() & 0xff; } return -1; }
/** * Creates a new buffer which wraps the specified buffer's readable bytes. * A modification on the specified buffer's content will be visible to the * returned buffer. * @param buffer The buffer to wrap. Reference count ownership of this variable is transferred to this method. * @return The readable portion of the {@code buffer}, or an empty buffer if there is no readable portion. * The caller is responsible for releasing this buffer. */ public static ByteBuf wrappedBuffer(ByteBuf buffer) { if (buffer.isReadable()) { return buffer.slice(); } else { buffer.release(); return EMPTY_BUFFER; } }
private void decodeHeartbeat(ByteBuf in, List<Object> out) { while (in.isReadable()) { if (in.readByte() == Packet.HB_PACKET_BYTE) { out.add(Packet.HB_PACKET); } else { in.readerIndex(in.readerIndex() - 1); break; } } }
@Override public Object decode(ByteBuf buf, State state) throws IOException { List<Object> result = new ArrayList<Object>(3); Object key = MapCacheEventCodec.this.decode(buf, state, codec.getMapKeyDecoder()); result.add(key); Object value = MapCacheEventCodec.this.decode(buf, state, codec.getMapValueDecoder()); result.add(value); if (buf.isReadable()) { Object oldValue = MapCacheEventCodec.this.decode(buf, state, codec.getMapValueDecoder()); result.add(oldValue); } return result; } };
while (in.isReadable()) { final int startIndex = in.readerIndex(); incompleteBuffer.ensureWritable(in.readableBytes()); incompleteBuffer.writeBytes(in); } else { if (null != incompleteBuffer) { line = incompleteBuffer.toString(encoding) + lineBuf.toString(encoding); incompleteBuffer.release(); incompleteBuffer = null; } else { line = lineBuf.toString(encoding); out.add(line); in.skipBytes(1); // Skip new line character.
private void fetchEncoderOutput(List<Object> out) { for (;;) { ByteBuf buf = encoder.readOutbound(); if (buf == null) { break; } if (!buf.isReadable()) { buf.release(); continue; } out.add(new DefaultHttpContent(buf)); } }
@Override public Object decode(ByteBuf buf, State state) throws IOException { byte type = buf.readByte(); if (type == 0x0) { return new LocalCachedMapClear(); byte[] value = new byte[valueLen]; buf.readBytes(value); entries.add(new LocalCachedMapUpdate.Entry(key, value)); if (!buf.isReadable()) { break; byte len = buf.readByte(); CharSequence requestId = buf.readCharSequence(len, CharsetUtil.UTF_8); long timeout = buf.readLong(); byte len = buf.readByte(); CharSequence requestId = buf.readCharSequence(len, CharsetUtil.UTF_8); int hashesCount = buf.readInt();
private List<RawMessageExtend> deserializeRawMessagesExtend(RemotingCommand request) { final ByteBuf body = request.getBody(); if (body.readableBytes() == 0) return Collections.emptyList(); List<RawMessageExtend> messages = Lists.newArrayList(); while (body.isReadable()) { messages.add(doDeserializeRawMessagesExtend(body)); } return messages; }
private List<BaseMessage> deserializeBaseMessage(ByteBuf input) { if (input.readableBytes() == 0) return Collections.emptyList(); List<BaseMessage> result = Lists.newArrayList(); long pullLogOffset = input.readLong(); //ignore consumer offset input.readLong(); while (input.isReadable()) { BaseMessage message = new BaseMessage(); byte flag = input.readByte(); input.skipBytes(8 + 8); String subject = PayloadHolderUtils.readString(input); String messageId = PayloadHolderUtils.readString(input); readTags(input, message, flag); int bodyLen = input.readInt(); ByteBuf body = input.readSlice(bodyLen); HashMap<String, Object> attrs = deserializeMapWrapper(subject, messageId, body); message.setMessageId(messageId); message.setSubject(subject); message.setAttrs(attrs); message.setProperty(BaseMessage.keys.qmq_pullOffset, pullLogOffset); result.add(message); pullLogOffset++; } return result; }
public static NetFlowV9Packet parsePacket(ByteBuf bb, NetFlowV9FieldTypeRegistry typeRegistry, Map<Integer, NetFlowV9Template> cache, @Nullable NetFlowV9OptionTemplate optionTemplate) { final int dataLength = bb.readableBytes(); final NetFlowV9Header header = parseHeader(bb); NetFlowV9OptionTemplate optTemplate = optionTemplate; List<NetFlowV9BaseRecord> records = new ArrayList<>(); while (bb.isReadable()) { bb.markReaderIndex(); int flowSetId = bb.readUnsignedShort();
if (!in.isReadable()) { return; final int length = Math.min(in.readableBytes(), blockCompressor.availableSize()); final int bytesWritten = blockCompressor.write(in, in.readerIndex(), length); in.skipBytes(bytesWritten); if (!blockCompressor.isFull()) { if (in.isReadable()) { break; } else {
@Override protected Object decode( ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() >= MESSAGE_HEADER) { int length = Integer.parseInt(buf.toString(2, 2, StandardCharsets.US_ASCII)) + 5; if (buf.readableBytes() >= length) { ByteBuf frame = buf.readRetainedSlice(length); while (buf.isReadable() && buf.getUnsignedByte(buf.readerIndex()) != '$') { buf.readByte(); } return frame; } } return null; }
/** * Return a buffer from HTTP/2 codec that Vert.x can use: * * - if it's a direct buffer (coming likely from OpenSSL) : we get a heap buffer version * - if it's a composite buffer we do the same * - otherwise we increase the ref count */ static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) { if (buf == Unpooled.EMPTY_BUFFER) { return buf; } if (buf.isDirect() || buf instanceof CompositeByteBuf) { if (buf.isReadable()) { ByteBuf buffer = allocator.heapBuffer(buf.readableBytes()); buffer.writeBytes(buf); return buffer; } else { return Unpooled.EMPTY_BUFFER; } } return buf.retain(); }
protected long transfer(ByteBuf source, WritableByteChannel target, MultipartState sourceFullyWrittenState) throws IOException { int transferred = 0; if (target instanceof GatheringByteChannel) { transferred = source.readBytes((GatheringByteChannel) target, source.readableBytes()); } else { for (ByteBuffer byteBuffer : source.nioBuffers()) { int len = byteBuffer.remaining(); int written = target.write(byteBuffer); transferred += written; if (written != len) { // couldn't write full buffer, exit loop break; } } // assume this is a basic single ByteBuf source.readerIndex(source.readerIndex() + transferred); } if (source.isReadable()) { slowTarget = true; } else { state = sourceFullyWrittenState; } return transferred; }
public static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) { if (buf == Unpooled.EMPTY_BUFFER) { return buf; } if (buf.isDirect() || buf instanceof CompositeByteBuf) { try { if (buf.isReadable()) { ByteBuf buffer = allocator.heapBuffer(buf.readableBytes()); buffer.writeBytes(buf); return buffer; } else { return Unpooled.EMPTY_BUFFER; } } finally { buf.release(); } } return buf; }
@Override protected void channelRead0(ChannelHandlerContext ctx, ByteBuf buf) throws Exception { updateNow(); while (buf.isReadable()) { if (incomingBuffer.remaining() > buf.readableBytes()) { int newLimit = incomingBuffer.position() + buf.readableBytes(); incomingBuffer.limit(newLimit);
if (!in.isReadable()) { return NOT_ENOUGH_INPUT; int copies = length / offset; for (; copies > 0; copies--) { out.readerIndex(initialIndex - offset); out.readBytes(out, offset); out.readerIndex(initialIndex - offset); out.readBytes(out, length % offset); out.readerIndex(initialIndex - offset); out.readBytes(out, length);
@Override public String readLine() throws IOException { if (!buffer.isReadable()) { return null; } if (lineBuf != null) { lineBuf.setLength(0); } loop: do { int c = buffer.readUnsignedByte(); switch (c) { case '\n': break loop; case '\r': if (buffer.isReadable() && (char) buffer.getUnsignedByte(buffer.readerIndex()) == '\n') { buffer.skipBytes(1); } break loop; default: if (lineBuf == null) { lineBuf = new StringBuilder(); } lineBuf.append((char) c); } } while (buffer.isReadable()); return lineBuf != null && lineBuf.length() > 0 ? lineBuf.toString() : StringUtil.EMPTY_STRING; }
/** * Skip one empty line * * @return True if one empty line was skipped */ private boolean skipOneLine() { if (!undecodedChunk.isReadable()) { return false; } byte nextByte = undecodedChunk.readByte(); if (nextByte == HttpConstants.CR) { if (!undecodedChunk.isReadable()) { undecodedChunk.readerIndex(undecodedChunk.readerIndex() - 1); return false; } nextByte = undecodedChunk.readByte(); if (nextByte == HttpConstants.LF) { return true; } undecodedChunk.readerIndex(undecodedChunk.readerIndex() - 2); return false; } if (nextByte == HttpConstants.LF) { return true; } undecodedChunk.readerIndex(undecodedChunk.readerIndex() - 1); return false; }
private static void sendResponse(Channel channel, int device, long id, int crc) { if (channel != null) { ByteBuf response = Unpooled.buffer(); response.writeByte(device); response.writeByte(MSG_ACK); response.writeIntLE((int) id); response.writeShortLE(crc); response.writeShortLE(Checksum.crc16( Checksum.CRC16_XMODEM, response.nioBuffer())); ByteBuf encoded = Unpooled.buffer(); encoded.writeByte(0x01); // header while (response.isReadable()) { int b = response.readByte(); if (b == 0x01 || b == 0x04 || b == 0x10 || b == 0x11 || b == 0x13) { encoded.writeByte(0x10); b += 0x20; } encoded.writeByte(b); } response.release(); encoded.writeByte(0x04); // ending channel.writeAndFlush(new NetworkMessage(encoded, channel.remoteAddress())); } }