DataType(int code) { this.code = UnsignedBytes.checkedCast(code); }
/** * Converts a BufferedImage into a ByteBuffer based on 32-bit values * in RGBA byte order * * @param image any type of BufferedImage * @return a ByteBuffer that contains the data in RGBA byte order */ public static ByteBuffer convertToByteBuffer(BufferedImage image) { int width = image.getWidth(); int height = image.getHeight(); ByteBuffer data = ByteBuffer.allocateDirect(4 * width * height); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int argb = image.getRGB(x, y); int r = (argb >> 16) & 0xFF; int g = (argb >> 8) & 0xFF; int b = argb & 0xFF; int a = (argb >> 24) & 0xFF; data.put(UnsignedBytes.checkedCast(r)); data.put(UnsignedBytes.checkedCast(g)); data.put(UnsignedBytes.checkedCast(b)); data.put(UnsignedBytes.checkedCast(a)); } } data.rewind(); return data; } }
/** * Writes this {@code BloomFilter} to an output stream, with a custom format (not Java * serialization). This has been measured to save at least 400 bytes compared to regular * serialization. * * <p>Use {@linkplain #readFrom(InputStream, Funnel)} to reconstruct the written BloomFilter. */ public void writeTo(OutputStream out) throws IOException { // Serial form: // 1 signed byte for the strategy // 1 unsigned byte for the number of hash functions // 1 big endian int, the number of longs in our bitset // N big endian longs of our bitset DataOutputStream dout = new DataOutputStream(out); dout.writeByte(SignedBytes.checkedCast(strategy.ordinal())); dout.writeByte(UnsignedBytes.checkedCast(numHashFunctions)); // note: checked at the c'tor dout.writeInt(bits.data.length()); for (int i = 0; i < bits.data.length(); i++) { dout.writeLong(bits.data.get(i)); } }
private static void assertCastFails(long value) { try { UnsignedBytes.checkedCast(value); fail("Cast to byte should have failed: " + value); } catch (IllegalArgumentException ex) { assertTrue( value + " not found in exception text: " + ex.getMessage(), ex.getMessage().contains(String.valueOf(value))); } }
@Override public byte getTag(final Object o) { if (o == null) { return -1; } final TFieldIdEnum setField = ((TUnion<? extends TUnion<?, ?>, ? extends TFieldIdEnum>) o).getSetField(); return UnsignedBytes.checkedCast((setField.getThriftFieldId() - 1)); }
/** * Writes this {@code BloomFilter} to an output stream, with a custom format (not Java * serialization). This has been measured to save at least 400 bytes compared to regular * serialization. * * <p>Use {@linkplain #readFrom(InputStream, Funnel)} to reconstruct the written BloomFilter. */ public void writeTo(OutputStream out) throws IOException { // Serial form: // 1 signed byte for the strategy // 1 unsigned byte for the number of hash functions // 1 big endian int, the number of longs in our bitset // N big endian longs of our bitset DataOutputStream dout = new DataOutputStream(out); dout.writeByte(SignedBytes.checkedCast(strategy.ordinal())); dout.writeByte(UnsignedBytes.checkedCast(numHashFunctions)); // note: checked at the c'tor dout.writeInt(bits.data.length()); for (int i = 0; i < bits.data.length(); i++) { dout.writeLong(bits.data.get(i)); } }
public void testCheckedCast() { for (byte value : VALUES) { assertEquals(value, UnsignedBytes.checkedCast(UnsignedBytes.toInt(value))); } assertCastFails(256L); assertCastFails(-1L); assertCastFails(Long.MAX_VALUE); assertCastFails(Long.MIN_VALUE); }
private ByteBuffer quantizeColors(byte[] vertex_colors) { ByteBuffer quantizedColors = ByteBuffer.wrap(new byte[vertex_colors.length]); for (int i=0; i<vertex_colors.length; i++) { float c = vertex_colors[i]; quantizedColors.put(UnsignedBytes.checkedCast((int) (c * 255))); } return quantizedColors; }
public byte[] toByteArray() { ByteBuffer buffer = ByteBuffer.allocate(list.size() * 8).order(ByteOrder.LITTLE_ENDIAN); for (A a : list) { Color4f color = a.color; int count = a.count; buffer.putInt(count); buffer.put(UnsignedBytes.checkedCast((int)(color.getR() * 255))); buffer.put(UnsignedBytes.checkedCast((int)(color.getG() * 255))); buffer.put(UnsignedBytes.checkedCast((int)(color.getB() * 255))); buffer.put(UnsignedBytes.checkedCast((int)(color.getA() * 255))); } return buffer.array(); }
private IpmiChannelProtocol(int code, String name, String description) { this.code = UnsignedBytes.checkedCast(code); this.name = name; this.description = description; }
/* pp */ private SensorType(@Nonnegative int code, @Nonnull String description, @CheckForNull Class<? extends SensorSpecificOffset> sensorSpecificOffsetType) { this.code = UnsignedBytes.checkedCast(code); this.description = description; this.sensorSpecificOffsetType = sensorSpecificOffsetType; } /* pp */ private SensorType(@Nonnegative int code, @Nonnull String description) {
/* pp */ SDRDeviceType(@Nonnegative int code, @Nonnull String description, @CheckForNull Class<? extends SDRDeviceSubtype> subtype) { this.code = UnsignedBytes.checkedCast(code); this.description = description; this.subtype = subtype; }
for (int l = 0; l < 4; ++l) { float f = materials.get(4 * c + l); vertexColors.put(4 * k + l, UnsignedBytes.checkedCast((int)(f * 255)));
private IpmiCommandName(@Nonnull String name, @Nonnull IpmiNetworkFunction networkFunction, @Nonnegative int code, @CheckForNull IpmiChannelPrivilegeLevel privilegeLevel, @CheckForNull Class<? extends IpmiRequest> requestType, @CheckForNull Class<? extends IpmiResponse> responseType) { this.name = name; this.networkFunction = networkFunction; this.code = UnsignedBytes.checkedCast(code); this.privilegeLevel = privilegeLevel; this.requestType = requestType; this.responseType = responseType; }
for (int l = 0; l < 4; ++l) { float val = fixColor(materialsAsFloat.get(4 * c + l)); colors.put(4 * k + l, UnsignedBytes.checkedCast((int)(val * 255)));
/** [IPMI2] Section 13.32, page 165. */ @Nonnull public byte[] getAdditionalKey(@Nonnegative int idx) throws NoSuchAlgorithmException { IpmiAuthenticationAlgorithm algorithm = getAuthenticationAlgorithm(); byte[] raw = new byte[algorithm.getHashLength()]; // Should this be 20, or the hash length? Arrays.fill(raw, UnsignedBytes.checkedCast(idx)); return algorithm.hash(ByteBuffer.wrap(raw)); } }
private static void assertBytes(byte[] actual, int... expected) { assertEquals(actual.length, expected.length); byte[] array = new byte[expected.length]; for (int i = 0; i < expected.length; i++) { array[i] = UnsignedBytes.checkedCast(expected[i]); } assertEquals(actual, array); }
private static void assertBytes(byte[] actual, int... expected) { assertEquals(actual.length, expected.length); byte[] array = new byte[expected.length]; for (int i = 0; i < expected.length; i++) { array[i] = UnsignedBytes.checkedCast(expected[i]); } assertEquals(actual, array); }
@Override public byte getTag(final Object o) { if (o == null) { return -1; } final TFieldIdEnum setField = ((TUnion<? extends TUnion<?, ?>, ? extends TFieldIdEnum>) o).getSetField(); return UnsignedBytes.checkedCast((setField.getThriftFieldId() - 1)); }
@Override protected void toWireUnchecked(IpmiPacketContext context, ByteBuffer buffer) { buffer.putInt(IANA_ENTERPRISE_NUMBER.getNumber()); buffer.put(getMessageType().getCode()); buffer.put(getMessageTag()); buffer.put((byte) 0); // reserved buffer.put(UnsignedBytes.checkedCast(getDataWireLength())); toWireData(buffer); }