Java Code Examples for io.netty.buffer.ByteBuf.getUnsignedByte()

The following are Jave code examples for showing how to use getUnsignedByte() of the io.netty.buffer.ByteBuf class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: jsf-sdk   File: LengthFieldBasedFrameDecoder.java   Source Code and License Vote up 8 votes
/**
 * Decodes the specified region of the buffer into an unadjusted frame length.  The default implementation is
 * capable of decoding the specified region into an unsigned 8/16/24/32/64 bit integer.  Override this method to
 * decode the length field encoded differently.  Note that this method must not modify the state of the specified
 * buffer (e.g. {@code readerIndex}, {@code writerIndex}, and the content of the buffer.)
 *
 * @throws DecoderException
 *         if failed to decode the specified region
 */
protected long getUnadjustedFrameLength(ByteBuf buf, int offset, int length, ByteOrder order) {
    buf = buf.order(order);
    long frameLength;
    switch (length) {
        case 1:
            frameLength = buf.getUnsignedByte(offset);
            break;
        case 2:
            frameLength = buf.getUnsignedShort(offset);
            break;
        case 3:
            frameLength = buf.getUnsignedMedium(offset);
            break;
        case 4:
            frameLength = buf.getUnsignedInt(offset);
            break;
        case 8:
            frameLength = buf.getLong(offset);
            break;
        default:
            throw new DecoderException(
                    "unsupported lengthFieldLength: " + lengthFieldLength + " (expected: 1, 2, 3, 4, or 8)");
    }
    return frameLength;
}
 
Example 2
Project: neto   File: ProtocolUnificationHandler.java   Source Code and License Vote up 5 votes
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

    // 프로토콜 확인을 위해서 5byte를 확인한다.
    if (in.readableBytes() < 5) {
        // 다음 inbound 시 버퍼에 이전에 전송된 내용이 쌓이므로 버퍼를 비운다.
        in.clear();
        return;
    }

    if (isSsl(in)) {
        enableSsl(ctx);
    } else {

        if (!isUnificationMode) {
            switchToBinary(ctx);
            return;
        }

        final int magicPacket1 = in.getUnsignedByte(in.readerIndex());
        final int magicPacket2 = in.getUnsignedByte(in.readerIndex() + 1);

        if (isHttp(magicPacket1, magicPacket2)) {
            switchToHttp(ctx);
        } else {
            switchToBinary(ctx);
        }
    }
}
 
Example 3
Project: neoscada   File: APDUDecoder.java   Source Code and License Vote up 4 votes
@Override
protected void decode ( final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out ) throws Exception
{
    if ( logger.isTraceEnabled () )
    {
        logger.trace ( "decode - bytes: {}", ByteBufUtil.hexDump ( in ) );
    }

    if ( in.readableBytes () < Constants.APCI_MIN_LENGTH )
    {
        return;
    }

    final byte start = in.getByte ( in.readerIndex () + 0 );
    if ( start != Constants.START_BYTE )
    {
        throw new DecoderException ( String.format ( "ACPI must start with 0x%02x but did with 0x%02x", Constants.START_BYTE, start ) );
    }

    final short len = in.getUnsignedByte ( in.readerIndex () + 1 );

    if ( len > Constants.APCI_MAX_DATA_LENGTH )
    {
        throw new DecoderException ( String.format ( "APCI has a maximum length of %s bytes, but we received %s", Constants.APCI_MAX_DATA_LENGTH, len ) );
    }

    if ( in.readableBytes () < len + 2 )
    {
        return;
    }

    // we have the full InformationTransfer

    // skip start & len
    in.skipBytes ( 2 );

    // read control fields
    final ByteBuf controlFields = in.readSlice ( 4 );

    final ByteBuf data;
    if ( len > 4 )
    {
        data = Unpooled.copiedBuffer ( in.readSlice ( len - 4 ) ).order ( ByteOrder.LITTLE_ENDIAN );
    }
    else
    {
        data = null;
    }

    // now add the PDU

    out.add ( decode ( controlFields, data ) );
}
 
Example 4
Project: neoscada   File: InformationObjectAddress.java   Source Code and License Vote up 4 votes
public int[] toArray ()
{
    final ByteBuf buf = Unpooled.buffer ( 4 );
    buf.writeMedium ( this.address );
    return new int[] { buf.getUnsignedByte ( 0 ), buf.getUnsignedByte ( 1 ), buf.getUnsignedByte ( 2 ) };
}
 
Example 5
Project: neoscada   File: ASDUAddress.java   Source Code and License Vote up 4 votes
public int[] toArray ()
{
    final ByteBuf buf = Unpooled.buffer ( 2 );
    buf.writeShort ( this.address );
    return new int[] { buf.getUnsignedByte ( 0 ), buf.getUnsignedByte ( 1 ) };
}