List of usage examples for com.google.common.primitives UnsignedBytes checkedCast
public static byte checkedCast(long value)
From source file:org.opendaylight.protocol.bgp.flowspec.Util.java
/** * Given the integer values, this method instead of writing the value * in 4B field, compresses the value to lowest required byte field * depending on the value./* w w w .j a v a2 s.c o m*/ * * @param value integer to be written * @param buffer ByteBuf where the value will be written */ protected static void writeShortest(final int value, final ByteBuf buffer) { if (value <= Values.UNSIGNED_BYTE_MAX_VALUE) { buffer.writeByte(UnsignedBytes.checkedCast(value)); } else if (value <= Values.UNSIGNED_SHORT_MAX_VALUE) { ByteBufWriteUtil.writeUnsignedShort(value, buffer); } else if (value <= Values.UNSIGNED_INT_MAX_VALUE) { ByteBufWriteUtil.writeUnsignedInt(UnsignedInts.toLong(value), buffer); } else { buffer.writeLong(value); } }
From source file:org.opendaylight.protocol.bgp.flowspec.handlers.Util.java
/** * Given the integer values, this method instead of writing the value * in 4B field, compresses the value to lowest required byte field * depending on the value.//from w ww. java2s . c om * * @param value integer to be written * @param buffer ByteBuf where the value will be written */ public static void writeShortest(final int value, final ByteBuf buffer) { if (value <= Values.UNSIGNED_BYTE_MAX_VALUE) { buffer.writeByte(UnsignedBytes.checkedCast(value)); } else if (value <= Values.UNSIGNED_SHORT_MAX_VALUE) { ByteBufWriteUtil.writeUnsignedShort(value, buffer); } else if (value <= Values.UNSIGNED_INT_MAX_VALUE) { ByteBufWriteUtil.writeUnsignedInt(UnsignedInts.toLong(value), buffer); } else { buffer.writeLong(value); } }
From source file:org.terasology.rendering.assets.texture.TextureDataFactory.java
/** * Create TextureData for a Texture all of a single color. * @param color to use for creating TextureData * @return TextureData created using specified color *//* w ww . j a v a2 s.c om*/ public static TextureData newInstance(Color color) { byte red = UnsignedBytes.checkedCast(color.r()); byte green = UnsignedBytes.checkedCast(color.g()); byte blue = UnsignedBytes.checkedCast(color.b()); byte alpha = UnsignedBytes.checkedCast(color.a()); ByteBuffer data = ByteBuffer.allocateDirect(4 * TEXTURE_WIDTH * TEXTURE_HEIGHT); for (int width = 0; width < TEXTURE_WIDTH; width++) { for (int height = 0; height < TEXTURE_HEIGHT; height++) { data.put(red).put(green).put(blue).put(alpha); } } // The buffer must be reset back to the initial position before passing it onward. data.rewind(); return new TextureData(TEXTURE_WIDTH, TEXTURE_HEIGHT, new ByteBuffer[] { data }, WrapMode.REPEAT, FilterMode.NEAREST); }
From source file:com.jadarstudios.developercapes.cape.CapeConfigManager.java
public static int claimId(int id) throws InvalidCapeConfigIdException { if (id <= 0) throw new InvalidCapeConfigIdException("The config ID must be a positive non-zero integer"); try {//from w w w . ja va 2 s . co m UnsignedBytes.checkedCast(id); } catch (IllegalArgumentException e) { e.printStackTrace(); } boolean isRegistered = availableIds.get(id); if (isRegistered) throw new InvalidCapeConfigIdException(String.format("The config ID %d is already claimed.", id)); availableIds.set(id); return id; }
From source file:de.TheJulianJES.UtilsPlus.Core.fancy.cape.CapeConfigManager.java
public static int claimId(int id) throws InvalidCapeConfigIdException { if (id <= 0) { throw new InvalidCapeConfigIdException("The config ID must be a positive non-zero integer"); }//from w w w . j a v a 2s . c o m try { UnsignedBytes.checkedCast(id); } catch (IllegalArgumentException e) { e.printStackTrace(); } boolean isRegistered = availableIds.get(id); if (isRegistered) { throw new InvalidCapeConfigIdException(String.format("The config ID %d is already claimed.", id)); } availableIds.set(id); return id; }
From source file:org.apache.directory.server.dhcp.options.ByteOption.java
public void setByteValue(@Nonnegative int value) { setData(new byte[] { UnsignedBytes.checkedCast(value) }); }
From source file:org.opendaylight.protocol.rsvp.parser.spi.subobjects.XROSubobjectListParser.java
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()); }//w w w . j a v a2s.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:se.sics.caracaldb.utils.CustomSerialisers.java
public static void serialiseKey(Key key, ByteBuf w) { byte[] id = key.getArray(); BitBuffer bbuf = BitBuffer.create((key instanceof Key.Inf), (id == null)); boolean byteFlag = (id != null) && (id.length <= Key.BYTE_KEY_SIZE); bbuf.write(byteFlag);/*from w ww . j a v a2s . co m*/ byte[] flags = bbuf.finalise(); w.writeBytes(flags); if (id != null) { if (byteFlag) { w.writeByte(UnsignedBytes.checkedCast(id.length)); } else { w.writeInt(id.length); } w.writeBytes(id); } }
From source file:org.opendaylight.protocol.pcep.impl.object.AbstractXROWithSubobjectsParser.java
protected List<Subobject> parseSubobjects(final ByteBuf buffer) throws PCEPDeserializerException { Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty."); final List<Subobject> subs = new ArrayList<>(); while (buffer.isReadable()) { final boolean mandatory = ((buffer.getUnsignedByte(buffer.readerIndex()) & (1 << Values.FIRST_BIT_OFFSET)) != 0) ? true : false; final int type = UnsignedBytes.checkedCast( (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 PCEPDeserializerException( "Wrong length specified. Passed: " + length + "; Expected: <= " + buffer.readableBytes()); }// ww w . ja v a 2s .co m LOG.debug("Attempt to parse subobject from bytes: {}", ByteBufUtil.hexDump(buffer)); final Subobject sub = this.subobjReg.parseSubobject(type, buffer.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.apache.hadoop.hive.serde2.objectinspector.ThriftUnionObjectInspector.java
@Override public byte getTag(final Object o) { if (o == null) { return -1; }/*from w w w . j a v a2s . co m*/ final TFieldIdEnum setField = ((TUnion<? extends TUnion<?, ?>, ? extends TFieldIdEnum>) o).getSetField(); return UnsignedBytes.checkedCast((setField.getThriftFieldId() - 1)); }