Java Code Examples for com.google.common.primitives.UnsignedBytes#checkedCast()

The following examples show how to use com.google.common.primitives.UnsignedBytes#checkedCast() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XROSubobjectListParser.java    From bgpcep with Eclipse Public License 1.0 6 votes vote down vote up
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;
        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());
        }
        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("Parsing failed for subobject type: {}. Ignoring subobject.", type);
        } else {
            LOG.debug("Subobject was parsed. {}", sub);
            subs.add(sub);
        }
    }
    return subs;
}
 
Example 2
Source File: Stateful07LspObjectParser.java    From bgpcep with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void serializeObject(final Object object, final ByteBuf buffer) {
    checkArgument(object instanceof Lsp, "Wrong instance of PCEPObject. Passed %s . Needed LspObject.",
        object.getClass());
    final Lsp specObj = (Lsp) object;
    final ByteBuf body = Unpooled.buffer();
    final PlspId plspId = specObj.getPlspId();
    checkArgument(plspId != null, "PLSP-ID not present");
    body.writeMedium(plspId.getValue().intValue() << FOUR_BITS_SHIFT);
    final BitArray flags = serializeFlags(specObj);
    byte op = 0;
    if (specObj.getOperational() != null) {
        op = UnsignedBytes.checkedCast(specObj.getOperational().getIntValue());
        op = (byte) (op << FOUR_BITS_SHIFT);
    }
    final byte[] res = flags.array();
    res[res.length - 1] = (byte) (res[res.length - 1] | op);
    body.writeByte(res[res.length - 1]);
    serializeTlvs(specObj.getTlvs(), body);
    ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
 
Example 3
Source File: OrcTestingUtil.java    From presto with Apache License 2.0 5 votes vote down vote up
public static byte[] octets(int... values)
{
    byte[] bytes = new byte[values.length];
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = UnsignedBytes.checkedCast(values[i]);
    }
    return bytes;
}
 
Example 4
Source File: TestDecimals.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Slice sliceFromBytes(int... bytes)
{
    byte[] buffer = new byte[bytes.length];
    for (int i = 0; i < bytes.length; i++) {
        buffer[i] = UnsignedBytes.checkedCast(bytes[i]);
    }
    return Slices.wrappedBuffer(buffer);
}
 
Example 5
Source File: Layer2AttributesExtCom.java    From bgpcep with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf body) {
    Preconditions.checkArgument(extendedCommunity instanceof Layer2AttributesExtendedCommunityCase,
            "The extended community %s is not EsImportRouteExtendedCommunityCaseCase type.",
            extendedCommunity);
    final Layer2AttributesExtendedCommunity extCom = ((Layer2AttributesExtendedCommunityCase) extendedCommunity)
            .getLayer2AttributesExtendedCommunity();
    final BitArray flags = new BitArray(FLAGS_SIZE);
    flags.set(PRIMARY_PE_OFFSET, extCom.isPrimaryPe());
    flags.set(BACKUP_PE_OFFSET, extCom.isBackupPe());
    flags.set(CONTROL_WORD_OFFSET, extCom.isControlWord());

    final byte[] res = flags.array();
    byte aux = 0;
    final OperationalMode modeOfOperation = extCom.getModeOfOperation();
    if (modeOfOperation != null) {
        aux = UnsignedBytes.checkedCast(modeOfOperation.getIntValue());
        aux = (byte) (aux << THREE_BITS_SHIFT);
        res[res.length - 1] = (byte) (res[res.length - 1] | aux);
    }

    final NormalizationType normalizationType = extCom.getOperatingPer();
    if (normalizationType != null) {
        aux = UnsignedBytes.checkedCast(normalizationType.getIntValue());
        aux = (byte) (aux << FIVE_BITS_SHIFT);
        res[res.length - 1] = (byte) (res[res.length - 1] | aux);
    }
    body.writeShort(res[res.length - 1]);
    ByteBufUtils.writeOrZero(body, extCom.getL2Mtu());
    body.writeZero(RESERVED);
}
 
Example 6
Source File: DbKeyTest.java    From exonum-java-binding with Apache License 2.0 5 votes vote down vote up
/**
 * The method intentionally doesn't check if type & numSignificantBits are valid, so that
 * it may be used to test how DbKey handles invalid inputs.
 */
private static byte[] createDbKey(int type, byte[] keySlice, int numSignificantBits) {
  byte[] rawDbKey = new byte[DbKey.DB_KEY_SIZE];
  rawDbKey[0] = UnsignedBytes.checkedCast(type);
  System.arraycopy(keySlice, 0, rawDbKey, 1, Math.min(DbKey.KEY_SIZE, keySlice.length));
  rawDbKey[DbKey.DB_KEY_SIZE - 1] = UnsignedBytes.checkedCast(numSignificantBits);
  return rawDbKey;
}
 
Example 7
Source File: SensorSpecificOffset.java    From ipmi4j with Apache License 2.0 4 votes vote down vote up
private TimestampClockType(@Nonnegative int code, @Nonnull String description) {
    this.code = UnsignedBytes.checkedCast(code);
    this.description = description;
}
 
Example 8
Source File: SensorSpecificOffset.java    From ipmi4j with Apache License 2.0 4 votes vote down vote up
private DriveSlot(@Nonnegative int code, @Nonnull String description) {
    this.code = UnsignedBytes.checkedCast(code);
    this.description = description;
}
 
Example 9
Source File: SensorSpecificOffset.java    From ipmi4j with Apache License 2.0 4 votes vote down vote up
private ConfigurationErrorData3(@Nonnegative int code, @Nonnull String description) {
    this.code = UnsignedBytes.checkedCast(code);
    this.description = description;
}
 
Example 10
Source File: SensorSpecificOffset.java    From ipmi4j with Apache License 2.0 4 votes vote down vote up
private Chipset(@Nonnegative int code, @Nonnull String description) {
    this.code = UnsignedBytes.checkedCast(code);
    this.description = description;
}
 
Example 11
Source File: GetChannelInfoResponse.java    From ipmi4j with Apache License 2.0 4 votes vote down vote up
ChannelSessionSupport(@Nonnegative int code) {
    this.code = UnsignedBytes.checkedCast(code);
}
 
Example 12
Source File: SensorSpecificOffset.java    From ipmi4j with Apache License 2.0 4 votes vote down vote up
private LogType(@Nonnegative int code, @Nonnull String description) {
    this.code = UnsignedBytes.checkedCast(code);
    this.description = description;
}
 
Example 13
Source File: SensorSpecificOffset.java    From ipmi4j with Apache License 2.0 4 votes vote down vote up
private PlatformSecurity(@Nonnegative int code, @Nonnull String description) {
    this.code = UnsignedBytes.checkedCast(code);
    this.description = description;
}
 
Example 14
Source File: GenericOffset.java    From ipmi4j with Apache License 2.0 4 votes vote down vote up
private Performance(@Nonnegative int code, @Nonnull String description) {
    this.code = UnsignedBytes.checkedCast(code);
    this.description = description;
}
 
Example 15
Source File: SensorSpecificOffset.java    From ipmi4j with Apache License 2.0 4 votes vote down vote up
private BootError(@Nonnegative int code, @Nonnull String description) {
    this.code = UnsignedBytes.checkedCast(code);
    this.description = description;
}
 
Example 16
Source File: SensorSpecificOffset.java    From ipmi4j with Apache License 2.0 4 votes vote down vote up
private PowerUnit(@Nonnegative int code, @Nonnull String description) {
    this.code = UnsignedBytes.checkedCast(code);
    this.description = description;
}
 
Example 17
Source File: ResourceValue.java    From apkfile with Apache License 2.0 4 votes vote down vote up
Type(int code) {
    this.code = UnsignedBytes.checkedCast(code);
}
 
Example 18
Source File: GenericOffset.java    From ipmi4j with Apache License 2.0 4 votes vote down vote up
private Presence(@Nonnegative int code, @Nonnull String description) {
    this.code = UnsignedBytes.checkedCast(code);
    this.description = description;
}
 
Example 19
Source File: SDRDeviceSubtype.java    From ipmi4j with Apache License 2.0 4 votes vote down vote up
Heceta(int code, String description) {
    this.code = UnsignedBytes.checkedCast(code);
    this.description = description;
}
 
Example 20
Source File: GetChannelInfoResponse.java    From ipmi4j with Apache License 2.0 4 votes vote down vote up
ChannelInterruptType(@Nonnegative int code) {
    this.code = UnsignedBytes.checkedCast(code);
}