Example usage for io.netty.util ByteProcessor FIND_NUL

List of usage examples for io.netty.util ByteProcessor FIND_NUL

Introduction

In this page you can find the example usage for io.netty.util ByteProcessor FIND_NUL.

Prototype

ByteProcessor FIND_NUL

To view the source code for io.netty.util ByteProcessor FIND_NUL.

Click Source Link

Document

Aborts on a NUL (0x00) .

Usage

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 www.  java2  s. 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();/*from   www  . j  ava  2  s.c om*/
        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:org.eclipse.milo.opcua.stack.core.serialization.OpcUaBinaryStreamDecoder.java

License:Open Source License

private String readNullTerminatedString(Charset charset) {
    int indexOfNull = buffer.forEachByte(ByteProcessor.FIND_NUL);

    if (indexOfNull == -1) {
        throw new UaSerializationException(StatusCodes.Bad_DecodingError, "null terminator not found");
    }//from   www.j  av a2s.  com

    int index = buffer.readerIndex();
    int length = indexOfNull - index;
    String str = buffer.toString(index, length, charset);
    buffer.skipBytes(length + 1);

    return str;
}