Example usage for io.netty.buffer ByteBuf readableBytes

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

Introduction

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

Prototype

public abstract int readableBytes();

Source Link

Document

Returns the number of readable bytes which is equal to (this.writerIndex - this.readerIndex) .

Usage

From source file:com.cloudhopper.smpp.transcoder.PduDecoderTest.java

License:Apache License

@Test
public void decodeDeliverSmWith2BytePayload() throws Exception {
    ByteBuf buffer = BufferHelper.createBuffer(
            "00000040000000050000000000657E4E0002013434393531333631393200040934303430340000000000000000000000000E0001010006000101042400024F6B");

    DeliverSm pdu0 = (DeliverSm) transcoder.decode(buffer);

    Assert.assertEquals(0x40, pdu0.getCommandLength());
    Assert.assertEquals(SmppConstants.CMD_ID_DELIVER_SM, pdu0.getCommandId());
    Assert.assertEquals(0, pdu0.getCommandStatus());
    Assert.assertEquals(6651470, pdu0.getSequenceNumber());
    Assert.assertEquals(true, pdu0.isRequest());
    Assert.assertEquals("", pdu0.getServiceType());
    Assert.assertEquals(0x02, pdu0.getSourceAddress().getTon());
    Assert.assertEquals(0x01, pdu0.getSourceAddress().getNpi());
    Assert.assertEquals("4495136192", pdu0.getSourceAddress().getAddress());
    Assert.assertEquals(0x04, pdu0.getDestAddress().getTon());
    Assert.assertEquals(0x09, pdu0.getDestAddress().getNpi());
    Assert.assertEquals("40404", pdu0.getDestAddress().getAddress());
    Assert.assertEquals(0x00, pdu0.getEsmClass());
    Assert.assertEquals(0x00, pdu0.getProtocolId());
    Assert.assertEquals(0x00, pdu0.getPriority());
    Assert.assertEquals("", pdu0.getScheduleDeliveryTime());
    Assert.assertEquals("", pdu0.getValidityPeriod());
    Assert.assertEquals(0x00, pdu0.getRegisteredDelivery());
    Assert.assertEquals(0x00, pdu0.getReplaceIfPresent());
    Assert.assertEquals(0x00, pdu0.getDataCoding());
    Assert.assertEquals(0x00, pdu0.getDefaultMsgId());
    Assert.assertEquals(0, pdu0.getShortMessageLength());
    Assert.assertArrayEquals(HexUtil.toByteArray(""), pdu0.getShortMessage());

    Assert.assertEquals(3, pdu0.getOptionalParameters().size());
    Tlv tlv0 = pdu0.getOptionalParameter(SmppConstants.TAG_SOURCE_NETWORK_TYPE);
    Assert.assertEquals(0x01, tlv0.getValueAsByte());
    Tlv tlv1 = pdu0.getOptionalParameter(SmppConstants.TAG_DEST_NETWORK_TYPE);
    Assert.assertEquals(0x01, tlv1.getValueAsByte());
    Tlv tlv2 = pdu0.getOptionalParameter(SmppConstants.TAG_MESSAGE_PAYLOAD);
    Assert.assertArrayEquals(HexUtil.toByteArray("4f6b"), tlv2.getValue());

    // interesting -- this example has optional parameters it happened to skip...
    Assert.assertEquals(0, buffer.readableBytes());
}

From source file:com.cloudhopper.smpp.transcoder.PduDecoderTest.java

License:Apache License

@Test
public void decodeDeliverSmWith1BytePayload() throws Exception {
    ByteBuf buffer = BufferHelper.createBuffer(
            "0000003F000000050000000000657E4E0002013434393531333631393200040934303430340000000000000000000000000E0001010006000101042400014F");

    DeliverSm pdu0 = (DeliverSm) transcoder.decode(buffer);

    Assert.assertEquals(0x3F, pdu0.getCommandLength());
    Assert.assertEquals(SmppConstants.CMD_ID_DELIVER_SM, pdu0.getCommandId());
    Assert.assertEquals(0, pdu0.getCommandStatus());
    Assert.assertEquals(6651470, pdu0.getSequenceNumber());
    Assert.assertEquals(true, pdu0.isRequest());
    Assert.assertEquals("", pdu0.getServiceType());
    Assert.assertEquals(0x02, pdu0.getSourceAddress().getTon());
    Assert.assertEquals(0x01, pdu0.getSourceAddress().getNpi());
    Assert.assertEquals("4495136192", pdu0.getSourceAddress().getAddress());
    Assert.assertEquals(0x04, pdu0.getDestAddress().getTon());
    Assert.assertEquals(0x09, pdu0.getDestAddress().getNpi());
    Assert.assertEquals("40404", pdu0.getDestAddress().getAddress());
    Assert.assertEquals(0x00, pdu0.getEsmClass());
    Assert.assertEquals(0x00, pdu0.getProtocolId());
    Assert.assertEquals(0x00, pdu0.getPriority());
    Assert.assertEquals("", pdu0.getScheduleDeliveryTime());
    Assert.assertEquals("", pdu0.getValidityPeriod());
    Assert.assertEquals(0x00, pdu0.getRegisteredDelivery());
    Assert.assertEquals(0x00, pdu0.getReplaceIfPresent());
    Assert.assertEquals(0x00, pdu0.getDataCoding());
    Assert.assertEquals(0x00, pdu0.getDefaultMsgId());
    Assert.assertEquals(0, pdu0.getShortMessageLength());
    Assert.assertArrayEquals(HexUtil.toByteArray(""), pdu0.getShortMessage());

    Assert.assertEquals(3, pdu0.getOptionalParameters().size());
    Tlv tlv0 = pdu0.getOptionalParameter(SmppConstants.TAG_SOURCE_NETWORK_TYPE);
    Assert.assertEquals(0x01, tlv0.getValueAsByte());
    Tlv tlv1 = pdu0.getOptionalParameter(SmppConstants.TAG_DEST_NETWORK_TYPE);
    Assert.assertEquals(0x01, tlv1.getValueAsByte());
    Tlv tlv2 = pdu0.getOptionalParameter(SmppConstants.TAG_MESSAGE_PAYLOAD);
    Assert.assertArrayEquals(HexUtil.toByteArray("4f"), tlv2.getValue());

    // interesting -- this example has optional parameters it happened to skip...
    Assert.assertEquals(0, buffer.readableBytes());
}

From source file:com.cloudhopper.smpp.transcoder.PduDecoderTest.java

License:Apache License

@Test
public void decodeDeliverSmWithNoPayload() throws Exception {
    ByteBuf buffer = BufferHelper.createBuffer(
            "0000003E000000050000000000657E4E0002013434393531333631393200040934303430340000000000000000000000000E000101000600010104240000");

    DeliverSm pdu0 = (DeliverSm) transcoder.decode(buffer);

    Assert.assertEquals(0x3E, pdu0.getCommandLength());
    Assert.assertEquals(SmppConstants.CMD_ID_DELIVER_SM, pdu0.getCommandId());
    Assert.assertEquals(0, pdu0.getCommandStatus());
    Assert.assertEquals(6651470, pdu0.getSequenceNumber());
    Assert.assertEquals(true, pdu0.isRequest());
    Assert.assertEquals("", pdu0.getServiceType());
    Assert.assertEquals(0x02, pdu0.getSourceAddress().getTon());
    Assert.assertEquals(0x01, pdu0.getSourceAddress().getNpi());
    Assert.assertEquals("4495136192", pdu0.getSourceAddress().getAddress());
    Assert.assertEquals(0x04, pdu0.getDestAddress().getTon());
    Assert.assertEquals(0x09, pdu0.getDestAddress().getNpi());
    Assert.assertEquals("40404", pdu0.getDestAddress().getAddress());
    Assert.assertEquals(0x00, pdu0.getEsmClass());
    Assert.assertEquals(0x00, pdu0.getProtocolId());
    Assert.assertEquals(0x00, pdu0.getPriority());
    Assert.assertEquals("", pdu0.getScheduleDeliveryTime());
    Assert.assertEquals("", pdu0.getValidityPeriod());
    Assert.assertEquals(0x00, pdu0.getRegisteredDelivery());
    Assert.assertEquals(0x00, pdu0.getReplaceIfPresent());
    Assert.assertEquals(0x00, pdu0.getDataCoding());
    Assert.assertEquals(0x00, pdu0.getDefaultMsgId());
    Assert.assertEquals(0, pdu0.getShortMessageLength());
    Assert.assertArrayEquals(HexUtil.toByteArray(""), pdu0.getShortMessage());

    Assert.assertEquals(3, pdu0.getOptionalParameters().size());
    Tlv tlv0 = pdu0.getOptionalParameter(SmppConstants.TAG_SOURCE_NETWORK_TYPE);
    Assert.assertEquals(0x01, tlv0.getValueAsByte());
    Tlv tlv1 = pdu0.getOptionalParameter(SmppConstants.TAG_DEST_NETWORK_TYPE);
    Assert.assertEquals(0x01, tlv1.getValueAsByte());
    Tlv tlv2 = pdu0.getOptionalParameter(SmppConstants.TAG_MESSAGE_PAYLOAD);
    Assert.assertArrayEquals(HexUtil.toByteArray(""), tlv2.getValue());

    // interesting -- this example has optional parameters it happened to skip...
    Assert.assertEquals(0, buffer.readableBytes());
}

From source file:com.cloudhopper.smpp.transcoder.PduDecoderTest.java

License:Apache License

@Test
public void decodeDeliverSmWithDeliveryReceiptThatFailedFromEndToEnd() throws Exception {
    ByteBuf buffer = BufferHelper.createBuffer(
            "000000A2000000050000000000116AD500010134343935313336313932303537000501475442616E6B000400000000010000006E69643A3934323531343330393233207375623A30303120646C7672643A303031207375626D697420646174653A3039313130343031323420646F6E6520646174653A3039313130343031323420737461743A41434345505444206572723A31303720746578743A20323646313032");

    DeliverSm pdu0 = (DeliverSm) transcoder.decode(buffer);

    Assert.assertEquals(162, pdu0.getCommandLength());
    Assert.assertEquals(SmppConstants.CMD_ID_DELIVER_SM, pdu0.getCommandId());
    Assert.assertEquals(0, pdu0.getCommandStatus());
    Assert.assertEquals(1141461, pdu0.getSequenceNumber());
    Assert.assertEquals(true, pdu0.isRequest());
    Assert.assertEquals("", pdu0.getServiceType());
    Assert.assertEquals(0x01, pdu0.getSourceAddress().getTon());
    Assert.assertEquals(0x01, pdu0.getSourceAddress().getNpi());
    Assert.assertEquals("4495136192057", pdu0.getSourceAddress().getAddress());
    Assert.assertEquals(0x05, pdu0.getDestAddress().getTon());
    Assert.assertEquals(0x01, pdu0.getDestAddress().getNpi());
    Assert.assertEquals("GTBank", pdu0.getDestAddress().getAddress());
    Assert.assertEquals(0x04, pdu0.getEsmClass());
    Assert.assertEquals(0x00, pdu0.getProtocolId());
    Assert.assertEquals(0x00, pdu0.getPriority());
    Assert.assertEquals("", pdu0.getScheduleDeliveryTime());
    Assert.assertEquals("", pdu0.getValidityPeriod());
    Assert.assertEquals(0x01, pdu0.getRegisteredDelivery());
    Assert.assertEquals(0x00, pdu0.getReplaceIfPresent());
    Assert.assertEquals(0x00, pdu0.getDataCoding());
    Assert.assertEquals(0x00, pdu0.getDefaultMsgId());
    Assert.assertEquals(110, pdu0.getShortMessageLength());
    Assert.assertArrayEquals(HexUtil.toByteArray(
            "69643a3934323531343330393233207375623a30303120646c7672643a303031207375626d697420646174653a3039313130343031323420646f6e6520646174653a3039313130343031323420737461743a41434345505444206572723a31303720746578743a20323646313032"),
            pdu0.getShortMessage());/*from   w w w. j  a va  2 s . c o m*/

    Assert.assertEquals(0, pdu0.getOptionalParameterCount());

    // interesting -- this example has optional parameters it happened to skip...
    Assert.assertEquals(0, buffer.readableBytes());
}

From source file:com.cloudhopper.smpp.transcoder.PduDecoderTest.java

License:Apache License

@Test
public void decodeDeliveryReceipt0() throws Exception {
    ByteBuf buffer = BufferHelper.createBuffer(
            "000000EB00000005000000000000000100010134343935313336313932300000013430343034000400000000000003009069643A3132366538356136656465616331613032303230303939333132343739353634207375623A30303120646C7672643A303031207375626D697420646174653A3130303231393136333020646F6E6520646174653A3130303231393136333020737461743A44454C49565244206572723A30303020546578743A48656C6C6F2020202020202020202020202020200427000102001E0021313236653835613665646561633161303230323030393933313234373935363400");

    DeliverSm pdu0 = (DeliverSm) transcoder.decode(buffer);

    Assert.assertEquals(235, pdu0.getCommandLength());
    Assert.assertEquals(SmppConstants.CMD_ID_DELIVER_SM, pdu0.getCommandId());
    Assert.assertEquals(0, pdu0.getCommandStatus());
    Assert.assertEquals(1, pdu0.getSequenceNumber());
    Assert.assertEquals(true, pdu0.isRequest());
    Assert.assertEquals("", pdu0.getServiceType());
    Assert.assertEquals(0x01, pdu0.getSourceAddress().getTon());
    Assert.assertEquals(0x01, pdu0.getSourceAddress().getNpi());
    Assert.assertEquals("44951361920", pdu0.getSourceAddress().getAddress());
    Assert.assertEquals(0x00, pdu0.getDestAddress().getTon());
    Assert.assertEquals(0x01, pdu0.getDestAddress().getNpi());
    Assert.assertEquals("40404", pdu0.getDestAddress().getAddress());
    Assert.assertEquals(0x04, pdu0.getEsmClass());
    Assert.assertEquals(0x00, pdu0.getProtocolId());
    Assert.assertEquals(0x00, pdu0.getPriority());
    Assert.assertEquals("", pdu0.getScheduleDeliveryTime());
    Assert.assertEquals("", pdu0.getValidityPeriod());
    Assert.assertEquals(0x00, pdu0.getRegisteredDelivery());
    Assert.assertEquals(0x00, pdu0.getReplaceIfPresent());
    Assert.assertEquals(0x03, pdu0.getDataCoding());
    Assert.assertEquals(0x00, pdu0.getDefaultMsgId());
    Assert.assertEquals(144, pdu0.getShortMessageLength());
    //Assert.assertArrayEquals(HexUtil.toByteArray("69643a3934323531343330393233207375623a30303120646c7672643a303031207375626d697420646174653a3039313130343031323420646f6e6520646174653a3039313130343031323420737461743a41434345505444206572723a31303720746578743a20323646313032"), pdu0.getShortMessage());

    Assert.assertEquals(2, pdu0.getOptionalParameterCount());

    // interesting -- this example has optional parameters it happened to skip...
    Assert.assertEquals(0, buffer.readableBytes());
}

From source file:com.cloudhopper.smpp.transcoder.PduDecoderTest.java

License:Apache License

@Test
public void decodeSubmitSmWith255ByteShortMessage() throws Exception {
    String text255 = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum in orci magna. Etiam auctor ultrices lacus vel suscipit. Maecenas eget faucibus purus. Etiam aliquet mollis fermentum. Proin vel augue arcu. Praesent venenatis tristique ante turpis duis.";
    byte[] text255Bytes = text255.getBytes("ISO-8859-1");

    ByteBuf buffer = BufferHelper.createBuffer(
            "00000130000000040000000000004FE80001013430343034000101343439353133363139323000000000000001000000FF"
                    + HexUtil.toHexString(text255Bytes));

    SubmitSm pdu0 = (SubmitSm) transcoder.decode(buffer);

    Assert.assertEquals(304, pdu0.getCommandLength());
    Assert.assertEquals(SmppConstants.CMD_ID_SUBMIT_SM, pdu0.getCommandId());
    Assert.assertEquals(0, pdu0.getCommandStatus());
    Assert.assertEquals(20456, pdu0.getSequenceNumber());
    Assert.assertEquals(true, pdu0.isRequest());
    Assert.assertEquals("", pdu0.getServiceType());
    Assert.assertEquals(0x01, pdu0.getSourceAddress().getTon());
    Assert.assertEquals(0x01, pdu0.getSourceAddress().getNpi());
    Assert.assertEquals("40404", pdu0.getSourceAddress().getAddress());
    Assert.assertEquals(0x01, pdu0.getDestAddress().getTon());
    Assert.assertEquals(0x01, pdu0.getDestAddress().getNpi());
    Assert.assertEquals("44951361920", pdu0.getDestAddress().getAddress());
    Assert.assertEquals(0x00, pdu0.getEsmClass());
    Assert.assertEquals(0x00, pdu0.getProtocolId());
    Assert.assertEquals(0x00, pdu0.getPriority());
    Assert.assertEquals("", pdu0.getScheduleDeliveryTime());
    Assert.assertEquals("", pdu0.getValidityPeriod());
    Assert.assertEquals(0x01, pdu0.getRegisteredDelivery());
    Assert.assertEquals(0x00, pdu0.getReplaceIfPresent());
    Assert.assertEquals(0x00, pdu0.getDataCoding());
    Assert.assertEquals(0x00, pdu0.getDefaultMsgId());
    Assert.assertEquals(255, pdu0.getShortMessage().length);
    Assert.assertArrayEquals(text255Bytes, pdu0.getShortMessage());

    Assert.assertEquals(null, pdu0.getOptionalParameters());

    // interesting -- this example has optional parameters it happened to skip...
    Assert.assertEquals(0, buffer.readableBytes());
}

From source file:com.cloudhopper.smpp.transcoder.PduDecoderTest.java

License:Apache License

@Test
public void decodeDataSM() throws Exception {
    ByteBuf buffer = BufferHelper.createBuffer(
            "000000300000010300000000000000000001013535353237313030303000000139363935000001000424000454657374");

    DataSm pdu0 = (DataSm) transcoder.decode(buffer);

    Assert.assertEquals(0x30, pdu0.getCommandLength());
    Assert.assertEquals(SmppConstants.CMD_ID_DATA_SM, pdu0.getCommandId());
    Assert.assertEquals(0, pdu0.getCommandStatus());
    Assert.assertEquals(0, pdu0.getSequenceNumber());
    Assert.assertEquals(true, pdu0.isRequest());
    Assert.assertEquals("", pdu0.getServiceType());
    Assert.assertEquals(0x01, pdu0.getSourceAddress().getTon());
    Assert.assertEquals(0x01, pdu0.getSourceAddress().getNpi());
    Assert.assertEquals("5552710000", pdu0.getSourceAddress().getAddress());
    Assert.assertEquals(0x00, pdu0.getDestAddress().getTon());
    Assert.assertEquals(0x01, pdu0.getDestAddress().getNpi());
    Assert.assertEquals("9695", pdu0.getDestAddress().getAddress());
    Assert.assertEquals(0x00, pdu0.getEsmClass());
    Assert.assertEquals(0x01, pdu0.getRegisteredDelivery());
    Assert.assertEquals(0x00, pdu0.getDataCoding());

    // these are NOT actually part of data_sm's, but included for compatability
    // with submit_sm and delivery_sm
    Assert.assertEquals(0x00, pdu0.getProtocolId());
    Assert.assertEquals(0x00, pdu0.getPriority());
    Assert.assertEquals(null, pdu0.getScheduleDeliveryTime());
    Assert.assertEquals(null, pdu0.getValidityPeriod());
    Assert.assertEquals(0x00, pdu0.getReplaceIfPresent());
    Assert.assertEquals(0x00, pdu0.getDefaultMsgId());
    Assert.assertEquals(0, pdu0.getShortMessageLength());
    Assert.assertNull(pdu0.getShortMessage());

    Tlv tlv0 = pdu0.getOptionalParameter(SmppConstants.TAG_MESSAGE_PAYLOAD);
    Assert.assertArrayEquals("Test".getBytes("ISO-8859-1"), tlv0.getValue());

    // interesting -- this example has optional parameters it happened to skip...
    Assert.assertEquals(0, buffer.readableBytes());
}

From source file:com.cloudhopper.smpp.transcoder.PduDecoderTest.java

License:Apache License

@Test
public void decodeCancelSm() throws Exception {
    ByteBuf buffer = BufferHelper.createBuffer(
            "0000002D000000080000000000004FE80031323334350001013430343034000101343439353133363139323000");

    CancelSm pdu0 = (CancelSm) transcoder.decode(buffer);

    Assert.assertEquals(45, pdu0.getCommandLength());
    Assert.assertEquals(SmppConstants.CMD_ID_CANCEL_SM, pdu0.getCommandId());
    Assert.assertEquals(0, pdu0.getCommandStatus());
    Assert.assertEquals(20456, pdu0.getSequenceNumber());
    Assert.assertEquals(true, pdu0.isRequest());
    Assert.assertEquals("", pdu0.getServiceType());
    Assert.assertEquals(0x01, pdu0.getSourceAddress().getTon());
    Assert.assertEquals(0x01, pdu0.getSourceAddress().getNpi());
    Assert.assertEquals("40404", pdu0.getSourceAddress().getAddress());
    Assert.assertEquals(0x01, pdu0.getDestAddress().getTon());
    Assert.assertEquals(0x01, pdu0.getDestAddress().getNpi());
    Assert.assertEquals("44951361920", pdu0.getDestAddress().getAddress());
    Assert.assertEquals("12345", pdu0.getMessageId());

    Assert.assertEquals(null, pdu0.getOptionalParameters());

    // interesting -- this example has optional parameters it happened to skip...
    Assert.assertEquals(0, buffer.readableBytes());
}

From source file:com.cloudhopper.smpp.transcoder.PduDecoderTest.java

License:Apache License

@Test
public void decodeQuerySm() throws Exception {
    ByteBuf buffer = BufferHelper.createBuffer("0000001E000000030000000000004FE83132333435000101343034303400");

    QuerySm pdu0 = (QuerySm) transcoder.decode(buffer);

    Assert.assertEquals(30, pdu0.getCommandLength());
    Assert.assertEquals(SmppConstants.CMD_ID_QUERY_SM, pdu0.getCommandId());
    Assert.assertEquals(0, pdu0.getCommandStatus());
    Assert.assertEquals(20456, pdu0.getSequenceNumber());
    Assert.assertEquals(true, pdu0.isRequest());
    Assert.assertEquals(0x01, pdu0.getSourceAddress().getTon());
    Assert.assertEquals(0x01, pdu0.getSourceAddress().getNpi());
    Assert.assertEquals("40404", pdu0.getSourceAddress().getAddress());
    Assert.assertEquals("12345", pdu0.getMessageId());

    Assert.assertEquals(null, pdu0.getOptionalParameters());

    // interesting -- this example has optional parameters it happened to skip...
    Assert.assertEquals(0, buffer.readableBytes());
}

From source file:com.cloudhopper.smpp.transcoder.PduDecoderTest.java

License:Apache License

@Test
public void decodeCancelSmResp() throws Exception {
    ByteBuf buffer = BufferHelper.createBuffer("00000010800000080000000000004FE8");

    CancelSmResp pdu0 = (CancelSmResp) transcoder.decode(buffer);

    Assert.assertEquals(16, pdu0.getCommandLength());
    Assert.assertEquals(SmppConstants.CMD_ID_CANCEL_SM_RESP, pdu0.getCommandId());
    Assert.assertEquals(0, pdu0.getCommandStatus());
    Assert.assertEquals(20456, pdu0.getSequenceNumber());
    Assert.assertEquals(true, pdu0.isResponse());

    Assert.assertEquals(0, buffer.readableBytes());
}