List of usage examples for io.netty.buffer ByteBuf markReaderIndex
public abstract ByteBuf markReaderIndex();
From source file:alluxio.worker.block.io.LocalFileBlockWriterTest.java
License:Apache License
@Test public void appendByteBuf() throws Exception { ByteBuf buffer = Unpooled.wrappedBuffer(BufferUtils.getIncreasingByteBuffer(TEST_BLOCK_SIZE)); buffer.markReaderIndex(); Assert.assertEquals(TEST_BLOCK_SIZE, mWriter.append(buffer)); buffer.resetReaderIndex();// w w w . j ava 2s .c o m Assert.assertEquals(TEST_BLOCK_SIZE, mWriter.append(buffer)); mWriter.close(); Assert.assertEquals(2 * TEST_BLOCK_SIZE, new File(mTestFilePath).length()); ByteBuffer result = ByteBuffer.wrap(Files.readAllBytes(Paths.get(mTestFilePath))); result.position(0).limit(TEST_BLOCK_SIZE); BufferUtils.equalIncreasingByteBuffer(0, TEST_BLOCK_SIZE, result.slice()); result.position(TEST_BLOCK_SIZE).limit(TEST_BLOCK_SIZE * 2); BufferUtils.equalIncreasingByteBuffer(0, TEST_BLOCK_SIZE, result.slice()); }
From source file:at.yawk.accordion.netty.Framer.java
License:Mozilla Public License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { if (msg.readableBytes() < 2) { // no (full) length header received yet, wait return;//from w w w .jav a 2 s . c o m } // mark so we can reset here if the message isn't complete yet msg.markReaderIndex(); int messageLength = msg.readUnsignedShort(); if (messageLength > msg.readableBytes()) { // length header received but message not yet complete, wait msg.resetReaderIndex(); return; } // message complete, queue for handling ByteBuf message = msg.readBytes(messageLength); out.add(message); }
From source file:at.yawk.dbus.protocol.codec.MessageHeaderCodec.java
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf rawBuf, List<Object> out) throws Exception { if (toRead != 0) { if (rawBuf.readableBytes() < toRead) { return; }//from ww w . ja v a 2 s .com ByteBuf slice = rawBuf.slice().order(byteOrder); slice.writerIndex(slice.readerIndex() + toRead); slice.retain(); AlignableByteBuf decoding = AlignableByteBuf.decoding(slice); log.trace("INBOUND {}", decoding); out.add(decoding); rawBuf.readerIndex(rawBuf.readerIndex() + toRead); toRead = 0; } if (rawBuf.readableBytes() < MIN_HEADER_LENGTH) { return; } rawBuf.markReaderIndex(); byte endianness = rawBuf.readByte(); ByteOrder order; switch (endianness) { case 'l': order = ByteOrder.LITTLE_ENDIAN; break; case 'B': order = ByteOrder.BIG_ENDIAN; break; default: throw new DecoderException("Unknown byte order byte " + endianness); } AlignableByteBuf buf = AlignableByteBuf.decoding(rawBuf.resetReaderIndex().order(order)); buf.getBuffer().markReaderIndex(); buf.readByte(); // skip endianness byte we read above @Nullable MessageType type = MessageType.byId(buf.readByte()); byte flags = buf.readByte(); byte majorProtocolVersion = buf.readByte(); if (majorProtocolVersion != PROTOCOL_VERSION) { throw new DecoderException("Unsupported major protocol version " + majorProtocolVersion); } long bodyLength = buf.readUnsignedInt(); int serial = buf.readInt(); MessageHeader header = new MessageHeader(); header.setByteOrder(order); header.setMessageType(type); header.setNoReplyExpected((flags & NO_REPLY_EXPECTED) != 0); header.setNoAutoStart((flags & NO_AUTO_START) != 0); header.setAllowInteractiveAuthorization((flags & ALLOW_INTERACTIVE_AUTHORIZATION) != 0); header.setMajorProtocolVersion(majorProtocolVersion); header.setMessageBodyLength(bodyLength); header.setSerial(serial); header.setHeaderFields(new EnumMap<>(HeaderField.class)); ArrayObject headers = (ArrayObject) tryDecode(HEADER_FIELD_LIST_TYPE, buf); if (headers == null) { // not enough data buf.getBuffer().resetReaderIndex(); return; } for (DbusObject struct : headers.getValues()) { HeaderField field = HeaderField.byId(struct.get(0).byteValue()); if (field != null) { DbusObject value = struct.get(1).getValue(); if (!value.getType().equals(field.getType())) { throw new DecoderException("Invalid header type on " + field + ": got " + value.getType() + " but expected " + field.getType()); } header.getHeaderFields().put(field, value); } } if (type != null) { checkRequiredHeaderFieldsPresent(header); } if (!buf.canAlignRead(8)) { buf.getBuffer().resetReaderIndex(); return; } buf.alignRead(8); toRead = Math.toIntExact(header.getMessageBodyLength()); byteOrder = order; out.add(header); }
From source file:at.yawk.votifier.LineSplitter.java
License:Mozilla Public License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { in.markReaderIndex(); int lineLength = 0; boolean found = false; while (in.isReadable()) { if (in.readByte() == '\n') { found = true;//from ww w.j a va 2 s . c o m break; } lineLength++; } in.resetReaderIndex(); if (found) { byte[] line = new byte[lineLength]; in.readBytes(line); in.readByte(); // newline out.add(new String(line, StandardCharsets.UTF_8)); } }
From source file:cloudeventbus.codec.Decoder.java
License:Open Source License
@Override public Frame decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { in.markReaderIndex(); final int frameLength = indexOf(in, Codec.DELIMITER); // Frame hasn't been fully read yet. if (frameLength < 0) { if (in.readableBytes() > maxMessageSize) { throw new TooLongFrameException("Frame exceeds maximum size"); }/*from w w w .j a v a 2 s.c o m*/ in.resetReaderIndex(); return null; } // Empty frame, discard and continue decoding if (frameLength == 0) { in.skipBytes(Codec.DELIMITER.length); return decode(ctx, in); } if (frameLength > maxMessageSize) { throw new TooLongFrameException("Frame exceeds maximum size"); } final String command = in.readBytes(frameLength).toString(CharsetUtil.UTF_8); in.skipBytes(Codec.DELIMITER.length); final String[] parts = command.split("\\s+"); final char frameTypeChar = parts[0].charAt(0); final FrameType frameType = FrameType.getFrameType(frameTypeChar); if (frameType == null) { throw new DecodingException("Invalid frame type " + frameTypeChar); } LOGGER.debug("Decoding frame of type {}", frameType); final int argumentsLength = parts.length - 1; switch (frameType) { case AUTH_RESPONSE: assertArgumentsLength(3, argumentsLength, "authentication response"); final CertificateChain certificates = new CertificateChain(); final byte[] rawCertificates = Base64.decodeBase64(parts[1].getBytes()); CertificateStoreLoader.load(new ByteArrayInputStream(rawCertificates), certificates); final byte[] salt = Base64.decodeBase64(parts[2]); final byte[] digitalSignature = Base64.decodeBase64(parts[3]); return new AuthenticationResponseFrame(certificates, salt, digitalSignature); case AUTHENTICATE: assertArgumentsLength(1, argumentsLength, "authentication request"); final byte[] challenge = Base64.decodeBase64(parts[1]); return new AuthenticationRequestFrame(challenge); case ERROR: if (parts.length == 0) { throw new DecodingException("Error is missing error code"); } final Integer errorNumber = Integer.valueOf(parts[1]); final ErrorFrame.Code errorCode = ErrorFrame.Code.lookupCode(errorNumber); int messageIndex = 1; messageIndex = skipWhiteSpace(messageIndex, command); while (messageIndex < command.length() && Character.isDigit(command.charAt(messageIndex))) { messageIndex++; } messageIndex = skipWhiteSpace(messageIndex, command); final String errorMessage = command.substring(messageIndex).trim(); if (errorMessage.length() > 0) { return new ErrorFrame(errorCode, errorMessage); } else { return new ErrorFrame(errorCode); } case GREETING: assertArgumentsLength(3, argumentsLength, "greeting"); final int version = Integer.valueOf(parts[1]); final String agent = parts[2]; final long id = Long.valueOf(parts[3]); return new GreetingFrame(version, agent, id); case PING: return PingFrame.PING; case PONG: return PongFrame.PONG; case PUBLISH: if (argumentsLength < 2 || argumentsLength > 3) { throw new DecodingException( "Expected message frame to have 2 or 3 arguments. It has " + argumentsLength + "."); } final String messageSubject = parts[1]; final String replySubject; final Integer messageLength; if (parts.length == 3) { replySubject = null; messageLength = Integer.valueOf(parts[2]); } else { replySubject = parts[2]; messageLength = Integer.valueOf(parts[3]); } if (in.readableBytes() < messageLength + Codec.DELIMITER.length) { // If we haven't received the entire message body (plus the CRLF), wait until it arrives. in.resetReaderIndex(); return null; } final ByteBuf messageBytes = in.readBytes(messageLength); final String messageBody = new String(messageBytes.array(), CharsetUtil.UTF_8); in.skipBytes(Codec.DELIMITER.length); // Ignore the CRLF after the message body. return new PublishFrame(new Subject(messageSubject), replySubject == null ? null : new Subject(replySubject), messageBody); case SERVER_READY: return ServerReadyFrame.SERVER_READY; case SUBSCRIBE: assertArgumentsLength(1, argumentsLength, "subscribe"); return new SubscribeFrame(new Subject(parts[1])); case UNSUBSCRIBE: assertArgumentsLength(1, argumentsLength, "unsubscribe"); return new UnsubscribeFrame(new Subject(parts[1])); default: throw new DecodingException("Unknown frame type " + frameType); } }
From source file:cloudfoundry.norouter.f5.dropsonde.LineEventDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext context, ByteBuf buffer, List<Object> out) throws Exception { buffer.markReaderIndex(); try {/*from w w w. j a va2 s .co m*/ final String command = nextString(buffer); // Extract event type switch (command) { case "LOG": decodeLogEvent(context, buffer, out); break; default: throw new IllegalStateException("Unknown command " + command); } } catch (Exception e) { buffer.resetReaderIndex(); throw new DecoderException("Invalid line event: " + buffer.toString(StandardCharsets.UTF_8), e); } }
From source file:cn.ifengkou.hestia.serialize.MessageDecoder.java
License:Apache License
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { if (in.readableBytes() < MessageDecoder.MESSAGE_LENGTH) { return;/*from w w w . j av a 2 s. co m*/ } in.markReaderIndex(); int messageLength = in.readInt(); if (messageLength < 0) { ctx.close(); } if (in.readableBytes() < messageLength) { in.resetReaderIndex(); return; } else { byte[] messageBody = new byte[messageLength]; in.readBytes(messageBody); try { Object obj = messageCodecUtil.decode(messageBody); out.add(obj); } catch (IOException ex) { LOGGER.error("messageCodeUtil decode failed!", ex); } } }
From source file:com.antsdb.saltedfish.server.mysql.packet.AuthPacket.java
License:Open Source License
/** * Check if response is version 41/*from w w w .j ava 2 s . c o m*/ * @param in * @return */ private boolean checkResponseVer41(ByteBuf in) { // response V41 has at least 32 bytes if (packetLength >= 32) { in.markReaderIndex(); byte[] reserved = new byte[23]; // 23 bytes from index 9 (+4 head offset) are all zero for V41 in.getBytes(9 + 4, reserved, 0, 23); in.resetReaderIndex(); if (Arrays.equals(reserved, new byte[23])) { return true; } } return false; }
From source file:com.antsdb.saltedfish.server.mysql.packet.QueryPacket.java
License:Open Source License
@Override public void read(MysqlServerHandler handler, ByteBuf in) { in.markReaderIndex(); sql = BufferUtils.readString(in);//w w w. j a v a 2 s . c om in.resetReaderIndex(); this.buf = BufferUtils.readStringCrazy(handler.getDecoder(), in); if (_log.isTraceEnabled()) _log.trace("Query:" + sql); }
From source file:com.antsdb.saltedfish.server.mysql.PacketDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // do we have length field in buffer ? if (!in.isReadable(4)) { return;/*from www. j ava 2 s . co m*/ } // do we have entire packet in the buffer? in.markReaderIndex(); int size = BufferUtils.readLongInt(in); int sequence = in.readByte() & 0xff; if (size == 0) { out.add(new ShutdownPacket()); return; } if (!in.isReadable(size)) { in.resetReaderIndex(); return; } // is very large packet? this.handler.packetSequence = sequence; if (size == MAX_PACKET_SIZE) { if (this.largePacket == null) { this.largePacket = ctx.alloc().directBuffer(); } this.largePacket.writeBytes(in, MAX_PACKET_SIZE); return; } if (this.largePacket != null) { this.largePacket.writeBytes(in, size); } // parse packet if (this.largePacket == null) { int pos = in.readerIndex(); try { RecievePacket packet = readPacket(in, size); out.add(packet); } finally { in.readerIndex(pos + size); } } else { RecievePacket packet = readPacket(this.largePacket, size); out.add(packet); this.largePacket.release(); this.largePacket = null; } }