/** * See https://github.com/netty/netty/issues/445 */ @Test public void testGetUnsignedByte() { ReplayingDecoderBuffer buffer = new ReplayingDecoderBuffer(releaseLater(Unpooled.copiedBuffer("TestBuffer", CharsetUtil.ISO_8859_1))); boolean error; int i = 0; try { for (;;) { buffer.getUnsignedByte(i); i++; } } catch (Signal e) { error = true; } assertTrue(error); assertEquals(10, i); }
/** * See https://github.com/netty/netty/issues/445 */ @Test public void testGetByte() { ReplayingDecoderBuffer buffer = new ReplayingDecoderBuffer(releaseLater(Unpooled.copiedBuffer("TestBuffer", CharsetUtil.ISO_8859_1))); boolean error; int i = 0; try { for (;;) { buffer.getByte(i); i++; } } catch (Signal e) { error = true; } assertTrue(error); assertEquals(10, i); }
@Override public String parse(ByteBuf buffer) { AppendableCharSequence sequence = sequenceReference.get(); int readerIndex = buffer.readerIndex(); try { super.parse(buffer); } catch (Signal e) { e.expect(REPLAYING_SIGNAL); buffer.readerIndex(readerIndex + size); return sequence.toString(); } return sequence.toString(); }
@Override public Optional<byte[]> parse(ByteBuf in) { for (;;) { switch (state) { case START: char c = ((char) in.readUnsignedByte()); if (c == '{') { in.readerIndex(in.readerIndex() - 1); expectedSize = sizeParser.parse(in); state = State.SKIP_CRLF; return Optional.empty(); } else if (Character.isWhitespace(c)) { continue; } else { in.readerIndex(in.readerIndex() - 1); state = State.PARSE_STRING; continue; } case SKIP_CRLF: in.readBytes(2); state = State.PARSE_SIZE; continue; case PARSE_STRING: return Optional.of(stringParser.parse(in).getBytes(StandardCharsets.UTF_8)); case PARSE_SIZE: if (buf == null) { buf = PooledByteBufAllocator.DEFAULT.buffer(expectedSize); } pos = in.readerIndex(); try { while (size < expectedSize) { buf.writeByte((char) in.readUnsignedByte()); inc(); } } catch (Signal e) { e.expect(REPLAYING_SIGNAL); in.readerIndex(pos); return Optional.empty(); } byte[] result = new byte[buf.readableBytes()]; buf.getBytes(buf.readerIndex(), result); reset(); return Optional.of(result); } } }