List of usage examples for io.netty.buffer ByteBuf getUnsignedByte
public abstract short getUnsignedByte(int index);
From source file:org.opendaylight.protocol.pcep.impl.subobject.XROIpv6PrefixSubobjectParser.java
License:Open Source License
@Override public Subobject parseSubobject(final ByteBuf buffer, final boolean mandatory) throws PCEPDeserializerException { Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); final SubobjectBuilder builder = new SubobjectBuilder(); builder.setMandatory(mandatory);/* ww w . j a va 2s . co m*/ if (buffer.readableBytes() != CONTENT6_LENGTH) { throw new PCEPDeserializerException( "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";"); } final int length = buffer.getUnsignedByte(PREFIX6_F_OFFSET); final IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix( new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH), length))); builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build()); buffer.skipBytes(PREFIX_F_LENGTH); builder.setAttribute(Attribute.forValue(buffer.readUnsignedByte())); return builder.build(); }
From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROIpv4PrefixSubobjectParser.java
License:Open Source License
@Override public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean loose) throws RSVPParsingException { Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); final SubobjectContainerBuilder builder = new SubobjectContainerBuilder(); builder.setLoose(loose);/*from w w w . ja va2 s . c o m*/ if (buffer.readableBytes() != CONTENT4_LENGTH) { throw new RSVPParsingException( "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";"); } final int length = buffer.getUnsignedByte(PREFIX4_F_OFFSET); final IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix( new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH), length))); builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build()); return builder.build(); }
From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.ero.EROIpv6PrefixSubobjectParser.java
License:Open Source License
@Override public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean loose) throws RSVPParsingException { Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); final SubobjectContainerBuilder builder = new SubobjectContainerBuilder(); builder.setLoose(loose);/* w w w . j a v a 2 s . com*/ if (buffer.readableBytes() != CONTENT_LENGTH) { throw new RSVPParsingException( "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";"); } final int length = buffer.getUnsignedByte(PREFIX_F_OFFSET); final IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix( new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH), length))); builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build()); return builder.build(); }
From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.rro.RROIpv4PrefixSubobjectParser.java
License:Open Source License
@Override public SubobjectContainer parseSubobject(final ByteBuf buffer) throws RSVPParsingException { Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); if (buffer.readableBytes() != CONTENT4_LENGTH) { throw new RSVPParsingException( "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";"); }//w w w . j av a 2 s .com final SubobjectContainerBuilder builder = new SubobjectContainerBuilder(); final int length = buffer.getUnsignedByte(PREFIX4_F_OFFSET); final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.record.route.subobjects.subobject.type.ip.prefix._case.IpPrefix prefix = new IpPrefixBuilder() .setIpPrefix(new IpPrefix( Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH), length))) .build(); buffer.skipBytes(PREFIX_F_LENGTH); final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE); builder.setProtectionAvailable(flags.get(LPA_F_OFFSET)); builder.setProtectionInUse(flags.get(LPIU_F_OFFSET)); builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix).build()); return builder.build(); }
From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.rro.RROIpv6PrefixSubobjectParser.java
License:Open Source License
@Override public SubobjectContainer parseSubobject(final ByteBuf buffer) throws RSVPParsingException { Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); final SubobjectContainerBuilder builder = new SubobjectContainerBuilder(); if (buffer.readableBytes() != CONTENT_LENGTH) { throw new RSVPParsingException( "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";"); }//from w w w .j av a 2s . co m final int length = buffer.getUnsignedByte(PREFIX_F_OFFSET); final IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix( new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH), length))); buffer.skipBytes(PREFIX_F_LENGTH); final BitArray flags = BitArray.valueOf(buffer, FLAGS_SIZE); builder.setProtectionAvailable(flags.get(LPA_F_OFFSET)); builder.setProtectionInUse(flags.get(LPIU_F_OFFSET)); builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build()); return builder.build(); }
From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROIpv4PrefixSubobjectParser.java
License:Open Source License
@Override public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean mandatory) throws RSVPParsingException { Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); final SubobjectContainerBuilder builder = new SubobjectContainerBuilder(); builder.setMandatory(mandatory);//w w w.j ava 2 s . c o m if (buffer.readableBytes() != CONTENT4_LENGTH) { throw new RSVPParsingException( "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";"); } final int length = buffer.getUnsignedByte(PREFIX4_F_OFFSET); final IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix( new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH), length))); builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build()); buffer.skipBytes(PREFIX_F_LENGTH); builder.setAttribute(ExcludeRouteSubobjects.Attribute.forValue(buffer.readUnsignedByte())); return builder.build(); }
From source file:org.opendaylight.protocol.rsvp.parser.impl.subobject.xro.XROIpv6PrefixSubobjectParser.java
License:Open Source License
@Override public SubobjectContainer parseSubobject(final ByteBuf buffer, final boolean mandatory) throws RSVPParsingException { Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); final SubobjectContainerBuilder builder = new SubobjectContainerBuilder(); builder.setMandatory(mandatory);// w w w. ja v a 2s. c o m if (buffer.readableBytes() != CONTENT6_LENGTH) { throw new RSVPParsingException( "Wrong length of array of bytes. Passed: " + buffer.readableBytes() + ";"); } final int length = buffer.getUnsignedByte(PREFIX6_F_OFFSET); final IpPrefixBuilder prefix = new IpPrefixBuilder().setIpPrefix( new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(buffer, Ipv6Util.IPV6_LENGTH), length))); builder.setSubobjectType(new IpPrefixCaseBuilder().setIpPrefix(prefix.build()).build()); buffer.skipBytes(PREFIX_F_LENGTH); builder.setAttribute(ExcludeRouteSubobjects.Attribute.forValue(buffer.readUnsignedByte())); return builder.build(); }
From source file:org.opendaylight.protocol.rsvp.parser.spi.subobjects.EROSubobjectListParser.java
License:Open Source License
public List<SubobjectContainer> parseList(final ByteBuf buffer) throws RSVPParsingException { // Explicit approval of empty ERO Preconditions.checkArgument(buffer != null, "Array of bytes is mandatory. Can't be null."); final List<SubobjectContainer> subs = new ArrayList<>(); while (buffer.isReadable()) { final boolean loose = ((buffer.getUnsignedByte(buffer.readerIndex()) & (1 << Values.FIRST_BIT_OFFSET)) != 0) ? true : false; final int type = (buffer.readUnsignedByte() & Values.BYTE_MAX_VALUE_BYTES) & ~(1 << Values.FIRST_BIT_OFFSET); final int length = buffer.readUnsignedByte() - HEADER_LENGTH; if (length > buffer.readableBytes()) { throw new RSVPParsingException( "Wrong length specified. Passed: " + length + "; Expected: <= " + buffer.readableBytes()); }/* ww w. j av a 2 s . co m*/ LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer)); final SubobjectContainer sub = this.subobjReg.parseSubobject(type, buffer.readSlice(length), loose); if (sub == null) { LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type); } else { LOG.debug("Subobject was parsed. {}", sub); subs.add(sub); } } return subs; }
From source file:org.opendaylight.protocol.rsvp.parser.spi.subobjects.XROSubobjectListParser.java
License:Open Source License
public List<SubobjectContainer> parseList(final ByteBuf byteBuf) throws RSVPParsingException { final List<SubobjectContainer> subs = new ArrayList<>(); while (byteBuf.isReadable()) { final boolean mandatory = ((byteBuf.getUnsignedByte(byteBuf.readerIndex()) & (1 << Values.FIRST_BIT_OFFSET)) != 0) ? true : false; final int type = UnsignedBytes.checkedCast( (byteBuf.readUnsignedByte() & Values.BYTE_MAX_VALUE_BYTES) & ~(1 << Values.FIRST_BIT_OFFSET)); final int length = byteBuf.readUnsignedByte() - HEADER_LENGHT; if (length > byteBuf.readableBytes()) { throw new RSVPParsingException( "Wrong length specified. Passed: " + length + "; Expected: <= " + byteBuf.readableBytes()); }/*from w ww .ja v a 2s . c o m*/ LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(byteBuf)); final SubobjectContainer sub = this.subobjReg.parseSubobject(type, byteBuf.readSlice(length), mandatory); if (sub == null) { LOG.warn("Unknown subobject type: {}. Ignoring subobject.", type); } else { LOG.debug("Subobject was parsed. {}", sub); subs.add(sub); } } return subs; }
From source file:org.traccar.helper.BcdUtil.java
License:Apache License
public static int readInteger(ByteBuf buf, int digits) { int result = 0; for (int i = 0; i < digits / 2; i++) { int b = buf.readUnsignedByte(); result *= 10;/*from w w w . j av a 2s . c o m*/ result += b >>> 4; result *= 10; result += b & 0x0f; } if (digits % 2 != 0) { int b = buf.getUnsignedByte(buf.readerIndex()); result *= 10; result += b >>> 4; } return result; }