List of usage examples for io.netty.buffer ByteBuf readableBytes
public abstract int readableBytes();
From source file:com.cloudhopper.smpp.transcoder.DefaultPduTranscoder.java
License:Apache License
@Override public ByteBuf encode(Pdu pdu) throws UnrecoverablePduException, RecoverablePduException { // see if we can map the command status into a message if (pdu instanceof PduResponse) { PduResponse response = (PduResponse) pdu; if (response.getResultMessage() == null) { response.setResultMessage(context.lookupResultMessage(pdu.getCommandStatus())); }/*from w ww . j a v a2 s .co m*/ } // if the pdu length hasn't been assigned yet, calculate it now // NOTE: it may be safest to recalculate it, but we won't since the SmppSession // should really be the only interface creating PDUs if (!pdu.hasCommandLengthCalculated()) { pdu.calculateAndSetCommandLength(); } // create the buffer and add the header ByteBuf buffer = Unpooled.buffer(pdu.getCommandLength()); buffer.order(ByteOrder.BIG_ENDIAN); buffer.writeInt(pdu.getCommandLength()); buffer.writeInt(pdu.getCommandId()); buffer.writeInt(pdu.getCommandStatus()); buffer.writeInt(pdu.getSequenceNumber()); // add mandatory body (a noop if no body exists) pdu.writeBody(buffer); // add optional parameters (a noop if none exist) pdu.writeOptionalParameters(buffer, context); // NOTE: at this point, the entire buffer written MUST match the command length // from earlier -- if it doesn't match, the our encoding process went awry if (buffer.readableBytes() != pdu.getCommandLength()) { throw new NotEnoughDataInBufferException( "During PDU encoding the expected commandLength did not match the actual encoded (a serious error with our own encoding process)", pdu.getCommandLength(), buffer.readableBytes()); } return buffer; }
From source file:com.cloudhopper.smpp.transcoder.DefaultPduTranscoder.java
License:Apache License
@Override public Pdu decode(ByteBuf buffer) throws UnrecoverablePduException, RecoverablePduException { // wait until the length prefix is available if (buffer.readableBytes() < SmppConstants.PDU_INT_LENGTH) { return null; }//w ww. ja v a2 s .co m // parse the command length (first 4 bytes) int commandLength = buffer.getInt(buffer.readerIndex()); //logger.trace("PDU commandLength [" + commandLength + "]"); // valid command length is >= 16 bytes if (commandLength < SmppConstants.PDU_HEADER_LENGTH) { throw new UnrecoverablePduException( "Invalid PDU length [0x" + HexUtil.toHexString(commandLength) + "] parsed"); } // wait until the whole pdu is available (entire pdu) if (buffer.readableBytes() < commandLength) { return null; } // at this point, we have the entire PDU and length already in the buffer // we'll create a new "view" of this PDU and read the data from the actual buffer // NOTE: this should be super fast since the underlying byte array doesn't get copied ByteBuf buffer0 = buffer.readSlice(commandLength); return doDecode(commandLength, buffer0); }
From source file:com.cloudhopper.smpp.transcoder.PduDecoderTest.java
License:Apache License
@Test public void decodeLessThan4Bytes() throws Exception { ByteBuf buffer = BufferHelper.createBuffer("000000"); EnquireLink pdu0 = (EnquireLink) transcoder.decode(buffer); Assert.assertNull(pdu0);/* w w w . j a va 2s .c o m*/ // make sure the entire buffer is still there we started with Assert.assertEquals(3, buffer.readableBytes()); }
From source file:com.cloudhopper.smpp.transcoder.PduDecoderTest.java
License:Apache License
@Test public void decodeOnly4Bytes() throws Exception { ByteBuf buffer = BufferHelper.createBuffer("00000010"); EnquireLink pdu0 = (EnquireLink) transcoder.decode(buffer); Assert.assertNull(pdu0);//from w ww . j a v a 2s . c om // make sure the entire buffer is still there we started with Assert.assertEquals(4, buffer.readableBytes()); }
From source file:com.cloudhopper.smpp.transcoder.PduDecoderTest.java
License:Apache License
@Test public void decodeLessThan16Bytes() throws Exception { ByteBuf buffer = BufferHelper.createBuffer("0000001000000015000000000a342e"); EnquireLink pdu0 = (EnquireLink) transcoder.decode(buffer); Assert.assertNull(pdu0);/* w ww .ja va2s . c o m*/ // make sure the entire buffer is still there we started with Assert.assertEquals(15, buffer.readableBytes()); }
From source file:com.cloudhopper.smpp.transcoder.PduDecoderTest.java
License:Apache License
@Test public void decodeUnsupportedRequestCommandId() throws Exception { ByteBuf buffer = BufferHelper.createBuffer("0000001000000110000000000a342ee7"); try {/*w w w. j a v a 2s .c o m*/ EnquireLink pdu0 = (EnquireLink) transcoder.decode(buffer); Assert.fail(); } catch (UnknownCommandIdException e) { // correct behavior } // an unsupported command id is recoverable and all the bytes should have been read Assert.assertEquals(0, buffer.readableBytes()); }
From source file:com.cloudhopper.smpp.transcoder.PduDecoderTest.java
License:Apache License
@Test public void decodeUnsupportedResponseCommandId() throws Exception { ByteBuf buffer = BufferHelper.createBuffer("0000001080000110000000000a342ee7"); try {//from w w w. j a v a 2 s. c o m EnquireLink pdu0 = (EnquireLink) transcoder.decode(buffer); Assert.fail(); } catch (UnknownCommandIdException e) { // correct behavior } // an unsupported command id is recoverable and all the bytes should have been read Assert.assertEquals(0, buffer.readableBytes()); }
From source file:com.cloudhopper.smpp.transcoder.PduDecoderTest.java
License:Apache License
@Test public void decodeBadPduButSkipAllDataInBuffer() throws Exception { ByteBuf buffer = BufferHelper .createBuffer("0000001100000110000000000a342ee70F0000001000000015000000000a342ee7"); try {//from w w w . j a v a2 s. c om EnquireLink pdu0 = (EnquireLink) transcoder.decode(buffer); Assert.fail(); } catch (UnknownCommandIdException e) { // correct behavior (this should fail) } // at this point, the first PDU command id wasn't valid, but we still // read the entire PDU and the next PDU was able to be parsed okay EnquireLink pdu0 = (EnquireLink) transcoder.decode(buffer); Assert.assertEquals(16, pdu0.getCommandLength()); Assert.assertEquals(0x00000015, pdu0.getCommandId()); Assert.assertEquals(0, pdu0.getCommandStatus()); Assert.assertEquals(171192039, pdu0.getSequenceNumber()); // an unsupported command id is recoverable and all the bytes should have been read Assert.assertEquals(0, buffer.readableBytes()); }
From source file:com.cloudhopper.smpp.transcoder.PduDecoderTest.java
License:Apache License
@Test public void decodeMaxSequenceNumber() throws Exception { ByteBuf buffer = BufferHelper.createBuffer("0000001000000015000000007fffffff"); EnquireLink pdu0 = (EnquireLink) transcoder.decode(buffer); Assert.assertEquals(16, pdu0.getCommandLength()); Assert.assertEquals(SmppConstants.CMD_ID_ENQUIRE_LINK, pdu0.getCommandId()); Assert.assertEquals(0, pdu0.getCommandStatus()); Assert.assertEquals(2147483647, pdu0.getSequenceNumber()); Assert.assertEquals(true, pdu0.isRequest()); Assert.assertEquals(0, buffer.readableBytes()); }
From source file:com.cloudhopper.smpp.transcoder.PduDecoderTest.java
License:Apache License
@Test public void decodeTooBigSequenceNumber() throws Exception { ByteBuf buffer = BufferHelper.createBuffer("00000010000000150000000080000000"); try {/* www.j a va 2 s .com*/ EnquireLink pdu0 = (EnquireLink) transcoder.decode(buffer); // this should work now that we support 32 bits for a sequence number } catch (UnrecoverablePduException e) { Assert.fail(); } // despite having too large a sequence number, all the bytes should have been read Assert.assertEquals(0, buffer.readableBytes()); }