List of usage examples for io.netty.buffer ByteBuf readerIndex
public abstract int readerIndex();
From source file:com.mobius.software.mqtt.parser.test.TestParser.java
License:Open Source License
@Test public void testNextLengthIncomplete() throws MalformedMessageException { ByteBuf buf = Unpooled.buffer(2120207); buf.writeByte(0x82);// w ww.j a v a2s . c om buf.writeByte(0x8a); buf.writeByte(0xB4); buf.writeByte(0x81); // one byte missing in length assertNull("Invalid next header length", MQParser.next(buf)); assertEquals("buffer index was not reset", 0, buf.readerIndex()); }
From source file:com.mobius.software.mqtt.parser.test.TestParser.java
License:Open Source License
@Test(expected = MalformedMessageException.class) public void testNextContentIncomplete() throws MalformedMessageException { ByteBuf buf = Unpooled.buffer(2120207); buf.writeByte(0x82);/* www . j a v a 2 s . c o m*/ buf.writeByte(0x8a); buf.writeByte(0xB4); buf.writeByte(0x81); buf.writeByte(0x01); buf.writeBytes(new byte[2120201]); // one byte missing in content MQParser.next(buf); assertEquals("buffer index was not reset", 0, buf.readerIndex()); }
From source file:com.mobius.software.mqtt.parser.test.TestParser.java
License:Open Source License
@Test public void testNextContentIncompleteBufferNotReset() throws MalformedMessageException { ByteBuf buf = Unpooled.buffer(2120207); buf.writeByte(0x82);//from w w w.j av a 2 s. com buf.writeByte(0x8a); buf.writeByte(0xB4); buf.writeByte(0x81); buf.writeByte(0x01); buf.writeBytes(new byte[2120201]); // one byte missing in content try { MQParser.next(buf); } catch (MalformedMessageException e) { } assertEquals("buffer index was not reset", 0, buf.readerIndex()); }
From source file:com.nanxiaoqiang.test.netty.protocol.demo1.codec.MarshallingDecoder.java
License:Apache License
protected Object decode(ByteBuf in) throws Exception { int objectSize = in.readInt();// intdata ByteBuf buf = in.slice(in.readerIndex(), objectSize);// ?Data size?? ByteInput input = new ChannelBufferByteInput(buf); try {//from ww w . j a v a 2s . c o m unmarshaller.start(input); Object obj = unmarshaller.readObject();// marshallingObject unmarshaller.finish(); in.readerIndex(in.readerIndex() + objectSize);// return obj; } finally { unmarshaller.close(); } }
From source file:com.navercorp.nbasearc.gcp.RedisDecoder.java
License:Apache License
void getFrames(ByteBuf in, List<byte[]> out) { while (true) { in.markReaderIndex();/* w w w .ja va 2 s .c om*/ final int stx = in.readerIndex(); if (hasFrame(in) == false) { in.resetReaderIndex(); break; } int length = in.readerIndex() - stx; byte[] readBytes = new byte[length]; in.resetReaderIndex(); in.readBytes(readBytes); out.add(readBytes); } }
From source file:com.navercorp.nbasearc.gcp.RedisDecoder.java
License:Apache License
private int lineLength(ByteBuf in) { int readableBytes = in.readableBytes(); if (readableBytes < 2) { return -1; }// w ww . j a v a 2 s .co m /* CR */ int length = in.bytesBefore(CR_BYTE); if (length < 0) { return -1; } if (readableBytes < length + 2) { return -1; } /* LF */ byte eolLF = in.getByte(in.readerIndex() + length + 1); if (eolLF != LF_BYTE) { throw new RuntimeException("Redis protocol exception; malformed end of line; byte=" + eolLF); } return length + 2; }
From source file:com.necla.simba.server.gateway.server.backend.BackendFrameDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { // length not received yet, return without producing // an output return;//w w w . ja va2 s . c o m } // get calls on ByteBuf don't change the stream // so we leave 'ByteBuf in' unchanged after reading // the length int readerIndex = in.readerIndex(); int length = in.getInt(readerIndex); LOG.debug("got message len=" + length + " readablebytes=" + in.readableBytes()); if (in.readableBytes() < length + 4) return; ByteBuf frame = extractMessage(ctx, in, 4 + readerIndex, length); out.add(frame); in.readerIndex(readerIndex + 4 + length); return; }
From source file:com.necla.simba.server.gateway.server.frontend.FrontendFrameDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { // length not received yet, return without producing // an output return;/* w w w. j a v a 2 s . c o m*/ } // get calls on ByteBuf don't change the stream // so we leave 'ByteBuf in' unchanged after reading // the length int readerIndex = in.readerIndex(); int length = in.getInt(readerIndex); int realLength = length & ~(1 << 30); boolean isCompressed = (length >> 30) > 0; LOG.debug("got message len=" + realLength + " isCompressed=" + isCompressed + " readablebytes=" + in.readableBytes()); if (in.readableBytes() < realLength + 4) return; if (!isCompressed) { ByteBuf frame = extractMessage(ctx, in, 4 + readerIndex, realLength); out.add(frame); } else { ByteBuf frame = in.slice(4 + readerIndex, realLength); LOG.debug("going into decompress"); ByteBuf ret = decompress(ctx, frame); LOG.debug("ret readablebytes=" + ret.readableBytes()); out.add(decompress(ctx, frame)); } in.readerIndex(readerIndex + 4 + realLength); Stats.received(readerIndex + 4 + realLength); return; }
From source file:com.necla.simba.server.gateway.server.frontend.FrontendFrameEncoder.java
License:Apache License
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { LOG.debug("told to send out msg numBytes=" + msg.readableBytes() + " to " + ctx.channel()); int length = msg.readableBytes(); if (DOCOMPRESS) { compress(ctx, msg, out);// w w w .j av a2 s .co m } else { length &= ~(1 << 30); out.writeInt(length); out.writeBytes(msg, msg.readerIndex(), msg.readableBytes()); Stats.sent(length); } }
From source file:com.netflix.iep.http.NetflixJsonObjectDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (state == ST_CORRUPTED) { in.skipBytes(in.readableBytes()); return;/*from ww w. ja v a 2s . co m*/ } if (LOGGER.isTraceEnabled()) { byte[] bytes = new byte[in.readableBytes()]; in.getBytes(in.readerIndex(), bytes, 0, in.readableBytes()); LOGGER.trace("starting [" + in.readerIndex() + ":" + in.readableBytes() + "]:" + new String(bytes)); } // index of next byte to process. int len = this.len; int wrtIdx = in.writerIndex(); for (; in.readerIndex() + len < wrtIdx; len++) { if (len > maxObjectLength) { // buffer size exceeded maxObjectLength; discarding the complete buffer. in.skipBytes(in.readableBytes()); reset(); throw new TooLongFrameException( "object length exceeds " + maxObjectLength + ": " + len + " bytes discarded"); } byte c = in.getByte(in.readerIndex() + len); if (state == ST_DECODING_NORMAL) { decodeByte(c, in, in.readerIndex() + len); // All opening braces/brackets have been closed. That's enough to conclude // that the JSON object/array is complete. if (openBraces == 0) { ByteBuf json = extractObject(ctx, in, in.readerIndex(), len + 1); if (json != null) { out.add(json); } // The JSON object/array was extracted => discard the bytes from // the input buffer. in.readerIndex(in.readerIndex() + len + 1); len = 0; // Reset the object state to get ready for the next JSON object/text // coming along the byte stream. reset(); break; } } else if (state == ST_DECODING_ARRAY_STREAM) { if (len == 0 && Character.isWhitespace(c)) { in.skipBytes(1); len--; } decodeByte(c, in, in.readerIndex() + len); if (!insideString && (openBraces == 1 && c == ',' || openBraces == 0 && c == ']')) { ByteBuf json = extractObject(ctx, in, in.readerIndex(), len); if (json != null) { out.add(json); } in.readerIndex(in.readerIndex() + len + 1); len = 0; if (c == ']') { reset(); } break; } // JSON object/array detected. Accumulate bytes until all braces/brackets are closed. } else if (c == '{' || c == '[') { initDecoding(c); if (state == ST_DECODING_ARRAY_STREAM) { // Discard the array bracket in.skipBytes(1); len--; } // Discard leading spaces in front of a JSON object/array. } else if (Character.isWhitespace(c)) { in.skipBytes(1); len--; } else { state = ST_CORRUPTED; throw new CorruptedFrameException("invalid JSON received at byte position " + (in.readerIndex() + len) + ": " + ByteBufUtil.hexDump(in)); } } this.len = len; if (LOGGER.isTraceEnabled()) { byte[] bytes = new byte[in.readableBytes()]; in.getBytes(in.readerIndex(), bytes, 0, in.readableBytes()); LOGGER.trace("remainder [" + in.readerIndex() + ":" + in.readableBytes() + "]:" + new String(bytes)); } }