List of usage examples for io.netty.buffer ByteBuf forEachByte
public abstract int forEachByte(ByteProcessor processor);
From source file:at.yawk.dbus.protocol.auth.CommandCodec.java
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { int crlfPos = in.forEachByte(new CRLFFinder()); if (crlfPos == -1) { return;//from w w w . ja v a2 s .c o m } int start = in.readerIndex(); int end = crlfPos - 1; // remove trailing crlf String commandString = in.toString(start, end - start, CHARSET); log.trace("Received message {}", commandString); in.readerIndex(crlfPos + 1); int sep = commandString.indexOf(' '); String commandName = sep == -1 ? commandString : commandString.substring(0, sep); Function<List<String>, Command> factory = FACTORIES.get(commandName); if (factory != null) { List<String> args; if (sep == -1) { args = Collections.emptyList(); } else { args = Arrays.asList(commandString.substring(sep + 1).split(" ")); } out.add(factory.apply(args)); } }
From source file:at.yawk.dbus.protocol.LoggingInboundAdapter.java
private void accept(ByteBuf msg) { msg.forEachByte(value -> { append(value); return true; }); flush(); }
From source file:com.codnos.dbgp.internal.handlers.DBGpCommandDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> objects) throws Exception { int nullPosition = in.forEachByte(ByteProcessor.FIND_NUL); if (nullPosition < 0) return;/*from w w w .ja v a2s .c om*/ int length = nullPosition - in.readerIndex(); ByteBuf msgBuffer = in.readBytes(length); in.readByte(); objects.add(msgBuffer.toString(UTF_8)); msgBuffer.release(); }
From source file:com.codnos.dbgp.internal.handlers.DBGpResponseDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> objects) throws Exception { final int length = in.readableBytes(); LOGGER.fine("got something from engine (" + length + " bytes)"); int nullPosition = in.forEachByte(ByteProcessor.FIND_NUL); int readerIndex = in.readerIndex(); int numberOfBytes = nullPosition - readerIndex; LOGGER.fine("found nullposition on " + nullPosition + " and readerIndex is " + readerIndex + " calculated number of bytes " + numberOfBytes); if (numberOfBytes <= 0) { LOGGER.fine("not enough to read, finishing"); in.resetReaderIndex();// w ww .j av a2 s . com return; } if (nullPosition > length) { LOGGER.fine("have null position further than length, finishing"); in.resetReaderIndex(); return; } ByteBuf sizeBuf = in.readBytes(numberOfBytes); in.readByte(); String sizeBufAsString = sizeBuf.toString(UTF_8); int size = Integer.parseInt(sizeBufAsString); int expectedSize = sizeBuf.readableBytes() + NULL_BYTE_SIZE + size + NULL_BYTE_SIZE; if (length < expectedSize) { LOGGER.fine("don't have the whole message yet (expected " + expectedSize + "), finishing"); in.resetReaderIndex(); sizeBuf.release(); return; } ByteBuf messageBuf = in.readBytes(size); in.readByte(); objects.add(messageBuf.toString(UTF_8)); sizeBuf.release(); messageBuf.release(); }
From source file:com.couchbase.client.core.endpoint.util.ByteBufJsonHelper.java
License:Apache License
/** * Finds the position of the correct closing character, taking into account the fact that before the correct one, * other sub section with same opening and closing characters can be encountered. * * This implementation starts for the current {@link ByteBuf#readerIndex() readerIndex}. * * @param buf the {@link ByteBuf} where to search for the end of a section enclosed in openingChar and closingChar. * @param openingChar the section opening char, used to detect a sub-section. * @param closingChar the section closing char, used to detect the end of a sub-section / this section. * @return the section closing position or -1 if not found. *//*www . j a v a 2s . co m*/ public static int findSectionClosingPosition(ByteBuf buf, char openingChar, char closingChar) { return buf.forEachByte(new ClosingPositionBufProcessor(openingChar, closingChar, true)); }
From source file:com.couchbase.client.core.endpoint.util.ByteBufJsonHelper.java
License:Apache License
/** * Finds the position of the split character, taking into account the fact that the character * could be found escaped in strings (such occurrences are ignored). * * This implementation starts for the current {@link ByteBuf#readerIndex() readerIndex}. * * @param buf the {@link ByteBuf} where to search for the split character. * @param splitChar the split character to detect. * @return the split character position or -1 if not found. *//* w w w . j a va 2s .c o m*/ public static final int findSplitPosition(ByteBuf buf, char splitChar) { return buf.forEachByte(new SplitPositionBufProcessor(splitChar, true)); }
From source file:com.couchbase.client.core.endpoint.util.ClosingPositionBufProcessorTest.java
License:Apache License
@Test public void shouldFindClosingInSimpleSection() { ByteBuf source = Unpooled.copiedBuffer("{ this is simple }", CharsetUtil.UTF_8); int closingPos = source.forEachByte(new ClosingPositionBufProcessor('{', '}')); assertEquals(17, closingPos);/* w w w . j a v a 2 s. c o m*/ assertEquals(0, source.readerIndex()); }
From source file:com.couchbase.client.core.endpoint.util.ClosingPositionBufProcessorTest.java
License:Apache License
@Test public void shouldNotFindClosingInBrokenSection() { ByteBuf source = Unpooled.copiedBuffer("{ this is simple", CharsetUtil.UTF_8); int closingPos = source.forEachByte(new ClosingPositionBufProcessor('{', '}')); assertEquals(-1, closingPos);/*from www . j a va 2 s .co m*/ assertEquals(0, source.readerIndex()); }
From source file:com.couchbase.client.core.endpoint.util.ClosingPositionBufProcessorTest.java
License:Apache License
@Test public void shouldFindClosingInSectionWithSubsection() { ByteBuf source = Unpooled.copiedBuffer("{ this is { simple } }", CharsetUtil.UTF_8); int closingPos = source.forEachByte(new ClosingPositionBufProcessor('{', '}')); assertEquals(21, closingPos);/*from w ww . ja v a 2 s . c o m*/ assertEquals(0, source.readerIndex()); }
From source file:com.couchbase.client.core.endpoint.util.ClosingPositionBufProcessorTest.java
License:Apache License
@Test public void shouldNotFindClosingInBrokenSectionWithCompleteSubSection() { ByteBuf source = Unpooled.copiedBuffer("{ this is { complex } oups", CharsetUtil.UTF_8); int closingPos = source.forEachByte(new ClosingPositionBufProcessor('{', '}')); assertEquals(-1, closingPos);/*from w w w. j av a 2 s . c om*/ assertEquals(0, source.readerIndex()); }