Example usage for io.netty.buffer ByteBuf readByte

List of usage examples for io.netty.buffer ByteBuf readByte

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf readByte.

Prototype

public abstract byte readByte();

Source Link

Document

Gets a byte at the current readerIndex and increases the readerIndex by 1 in this buffer.

Usage

From source file:com.cloudhopper.smpp.pdu.BaseSm.java

License:Apache License

@Override
public void readBody(ByteBuf buffer) throws UnrecoverablePduException, RecoverablePduException {
    this.serviceType = ChannelBufferUtil.readNullTerminatedString(buffer);
    this.sourceAddress = ChannelBufferUtil.readAddress(buffer);
    this.destAddress = ChannelBufferUtil.readAddress(buffer);
    this.esmClass = buffer.readByte();
    this.protocolId = buffer.readByte();
    this.priority = buffer.readByte();
    this.scheduleDeliveryTime = ChannelBufferUtil.readNullTerminatedString(buffer);
    this.validityPeriod = ChannelBufferUtil.readNullTerminatedString(buffer);
    this.registeredDelivery = buffer.readByte();
    this.replaceIfPresent = buffer.readByte();
    this.dataCoding = buffer.readByte();
    this.defaultMsgId = buffer.readByte();
    // this is always an unsigned version of the short message length
    short shortMessageLength = buffer.readUnsignedByte();
    this.shortMessage = new byte[shortMessageLength];
    buffer.readBytes(this.shortMessage);
}

From source file:com.cloudhopper.smpp.pdu.DataSm.java

License:Apache License

@Override
public void readBody(ByteBuf buffer) throws UnrecoverablePduException, RecoverablePduException {
    this.serviceType = ChannelBufferUtil.readNullTerminatedString(buffer);
    this.sourceAddress = ChannelBufferUtil.readAddress(buffer);
    this.destAddress = ChannelBufferUtil.readAddress(buffer);
    this.esmClass = buffer.readByte();
    this.registeredDelivery = buffer.readByte();
    this.dataCoding = buffer.readByte();
}

From source file:com.cloudhopper.smpp.pdu.QuerySmResp.java

License:Apache License

@Override
public void readBody(ByteBuf buffer) throws UnrecoverablePduException, RecoverablePduException {
    this.messageId = ChannelBufferUtil.readNullTerminatedString(buffer);
    this.finalDate = ChannelBufferUtil.readNullTerminatedString(buffer);
    this.messageState = buffer.readByte();
    this.errorCode = buffer.readByte();
}

From source file:com.cloudhopper.smpp.type.Address.java

License:Apache License

public void read(ByteBuf buffer) throws UnrecoverablePduException, RecoverablePduException {
    this.ton = buffer.readByte();
    this.npi = buffer.readByte();
    this.address = ChannelBufferUtil.readNullTerminatedString(buffer);
}

From source file:com.cloudhopper.smpp.util.ChannelBufferUtil.java

License:Apache License

/**
 * Reads a C-String (null terminated) from a buffer.  This method will
 * attempt to find the null byte and read all data up to and including
 * the null byte.  The returned String does not include the null byte.
 * Will throw an exception if no null byte is found and it runs out of data
 * in the buffer to read.//  w  w  w  .j av  a2 s.c  o  m
 * @param buffer
 * @return
 * @throws TerminatingNullByteNotFoundException
 */
static public String readNullTerminatedString(ByteBuf buffer) throws TerminatingNullByteNotFoundException {
    // maximum possible length are the readable bytes in buffer
    int maxLength = buffer.readableBytes();

    // if there are no readable bytes, return null
    if (maxLength == 0) {
        return null;
    }

    // the reader index is defaulted to the readerIndex
    int offset = buffer.readerIndex();
    int zeroPos = 0;

    // search for NULL byte until we hit end or find it
    while ((zeroPos < maxLength) && (buffer.getByte(zeroPos + offset) != 0x00)) {
        zeroPos++;
    }

    if (zeroPos >= maxLength) {
        // a NULL byte was not found
        throw new TerminatingNullByteNotFoundException(
                "Terminating null byte not found after searching [" + maxLength + "] bytes");
    }

    // at this point, we found a terminating zero
    String result = null;
    if (zeroPos > 0) {
        // read a new byte array
        byte[] bytes = new byte[zeroPos];
        buffer.readBytes(bytes);
        try {
            result = new String(bytes, "ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            logger.error("Impossible error", e);
        }
    } else {
        result = "";
    }

    // at this point, we have just one more byte to skip over (the null byte)
    byte b = buffer.readByte();
    if (b != 0x00) {
        logger.error("Impossible error: last byte read SHOULD have been a null byte, but was [" + b + "]");
    }

    return result;
}

From source file:com.cloudhopper.smpp.util.ChannelBufferUtilTest.java

License:Apache License

@Test
public void readNullTerminatedString() throws Exception {
    // normal case with a termination zero
    ByteBuf buffer0 = BufferHelper.createBuffer("343439353133363139323000");
    String str0 = ChannelBufferUtil.readNullTerminatedString(buffer0);
    Assert.assertEquals("44951361920", str0);
    // make sure the entire buffer is still there we started with
    Assert.assertEquals(0, buffer0.readableBytes());

    // another case with an extra byte after NULL byte
    buffer0 = BufferHelper.createBuffer("343439353133363139323000FF");
    str0 = ChannelBufferUtil.readNullTerminatedString(buffer0);
    Assert.assertEquals("44951361920", str0);
    // make sure the entire buffer is still there we started with
    Assert.assertEquals(1, buffer0.readableBytes());

    // another case with an first extra byte
    buffer0 = BufferHelper.createBuffer("39343439353133363139323000");
    buffer0.readByte(); // skip 1 byte
    str0 = ChannelBufferUtil.readNullTerminatedString(buffer0);
    Assert.assertEquals("44951361920", str0);
    // make sure the entire buffer is still there we started with
    Assert.assertEquals(0, buffer0.readableBytes());

    // another case with an first extra byte and last extra byte
    buffer0 = BufferHelper.createBuffer("39343439353133363139323000FF");
    buffer0.readByte(); // skip 1 byte
    str0 = ChannelBufferUtil.readNullTerminatedString(buffer0);
    Assert.assertEquals("44951361920", str0);
    // make sure the entire buffer is still there we started with
    Assert.assertEquals(1, buffer0.readableBytes());

    // another case with an empty string
    buffer0 = BufferHelper.createBuffer("00");
    str0 = ChannelBufferUtil.readNullTerminatedString(buffer0);
    Assert.assertEquals("", str0);
    // make sure the entire buffer is still there we started with
    Assert.assertEquals(0, buffer0.readableBytes());

    // another case with an empty string and last extra byte
    buffer0 = BufferHelper.createBuffer("0039");
    str0 = ChannelBufferUtil.readNullTerminatedString(buffer0);
    Assert.assertEquals("", str0);
    // make sure the entire buffer is still there we started with
    Assert.assertEquals(1, buffer0.readableBytes());

    // no bytes left to read in buffer will return null
    buffer0 = BufferHelper.createBuffer("");
    str0 = ChannelBufferUtil.readNullTerminatedString(buffer0);
    Assert.assertNull(str0);//  w  ww  .jav a  2  s  .c o m
    Assert.assertEquals(0, buffer0.readableBytes());

    // no terminating zero
    try {
        buffer0 = BufferHelper.createBuffer("39");
        str0 = ChannelBufferUtil.readNullTerminatedString(buffer0);
        Assert.fail();
    } catch (TerminatingNullByteNotFoundException e) {
        // correct behavior
        Assert.assertEquals(1, buffer0.readableBytes());
    }

    // unsupported latin-1 chars?
    buffer0 = BufferHelper.createBuffer("0100");
    str0 = ChannelBufferUtil.readNullTerminatedString(buffer0);
    // correct behavior
    Assert.assertEquals(0, buffer0.readableBytes());
}

From source file:com.codeabovelab.dm.platform.http.async.ByteBufHolderAdapter.java

License:Apache License

@Override
public int readByte(ByteBufHolder chunk) {
    ByteBuf buf = chunk.content();
    if (buf.readableBytes() == 0) {
        return ChunkedInputStream.EOF;
    }/*from   w  w w .j  a  va 2  s .  com*/
    return buf.readByte();
}

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;// ww  w .j av a 2s  .c o  m
    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  w  ww .  j a  va  2s  .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.comphenix.protocol.injector.netty.WirePacket.java

License:Open Source License

public static int readVarInt(ByteBuf input) {
    checkNotNull(input, "input cannot be null!");

    int i = 0;/*from w w  w  .j  a  v a 2 s .  co m*/
    int j = 0;

    byte b0;

    do {
        b0 = input.readByte();
        i |= (b0 & 127) << j++ * 7;
        if (j > 5) {
            throw new RuntimeException("VarInt too big");
        }
    } while ((b0 & 128) == 128);

    return i;
}