@GwtIncompatible // Shorts.toByteArray public void testToByteArray() { assertTrue(Arrays.equals(new byte[] {0x23, 0x45}, Shorts.toByteArray((short) 0x2345))); assertTrue( Arrays.equals(new byte[] {(byte) 0xFE, (byte) 0xDC}, Shorts.toByteArray((short) 0xFEDC))); }
@GwtIncompatible // Shorts.fromByteArray, Shorts.toByteArray public void testByteArrayRoundTrips() { Random r = new Random(5); byte[] b = new byte[Shorts.BYTES]; // total overkill, but, it takes 0.1 sec so why not... for (int i = 0; i < 10000; i++) { short num = (short) r.nextInt(); assertEquals(num, Shorts.fromByteArray(Shorts.toByteArray(num))); r.nextBytes(b); assertTrue(Arrays.equals(b, Shorts.toByteArray(Shorts.fromByteArray(b)))); } }
/** * Takes a short value and splits it into 2 bytes * @param val - the value to split * @return - a 2 byte array */ public static byte[] shortToBytes(final short val) { return Shorts.toByteArray(val); }
@Override public byte[] toByteArray(final Short value) { return Shorts.toByteArray((short) (value ^ 0x8000)); }
/** Create a bytestring containing the short s */ public static ByteString copyFrom(short s) { return ByteString.copyFrom(Shorts.toByteArray(s)); }
@Override public QpidByteBuffer putShort(final int index, final short value) { byte[] valueArray = Shorts.toByteArray(value); return put(index, valueArray); }
@Override public final QpidByteBuffer putShort(final short value) { byte[] valueArray = Shorts.toByteArray(value); return put(valueArray); }
@NotNull public static UnsignedByteArray from(final short s) { return from(Shorts.toByteArray((short) (s ^ Short.MIN_VALUE))); }
@NotNull public static UnsignedByteArray from(final short s) { return from(Shorts.toByteArray((short) (s ^ Short.MIN_VALUE))); }
public void setShortValue(@Nonnegative int value) { Preconditions.checkArgument(value >> Short.SIZE == 0, "out of range: %s", value); setData(Shorts.toByteArray((short) value)); }
@Override public void allocate(short owner, long start, int num) { final DatabaseEntry key = new DatabaseEntry(Longs.toByteArray(start + num - 1)); final DatabaseEntry value = new DatabaseEntry(Shorts.toByteArray(owner)); final Transaction txn = null; // env.beginTransaction(null, null); try { OperationStatus status = allocationDirectory.putNoOverwrite(txn, key, value); if (status != OperationStatus.SUCCESS) { LOG.debug("Bad status: {}", status); throw new AssertionError(); } if (txn != null) txn.commit(); } catch (Exception e) { LOG.error("Exception during DB operation. Aborting transaction.", e); if (txn != null) txn.abort(); throw Throwables.propagate(e); } }
@GwtIncompatible // Shorts.toByteArray public void testToByteArray() { assertTrue(Arrays.equals( new byte[] {0x23, 0x45}, Shorts.toByteArray((short) 0x2345))); assertTrue(Arrays.equals( new byte[] {(byte) 0xFE, (byte) 0xDC}, Shorts.toByteArray((short) 0xFEDC))); }
@GwtIncompatible // Shorts.fromByteArray, Shorts.toByteArray public void testByteArrayRoundTrips() { Random r = new Random(5); byte[] b = new byte[Shorts.BYTES]; // total overkill, but, it takes 0.1 sec so why not... for (int i = 0; i < 10000; i++) { short num = (short) r.nextInt(); assertEquals(num, Shorts.fromByteArray(Shorts.toByteArray(num))); r.nextBytes(b); assertTrue(Arrays.equals(b, Shorts.toByteArray(Shorts.fromByteArray(b)))); } }
@Override public void write(long id, short owner, long version, byte[] data, Object txn) { if (LOG.isDebugEnabled()) LOG.debug("WRITE " + hex(id) + " ver: " + version + " data: " + (data != null ? "(" + data.length + " bytes)" : "null")); final DatabaseEntry key = new DatabaseEntry(Longs.toByteArray(id)); final DatabaseEntry dbEntry = new DatabaseEntry(); entryBinding.objectToEntry(new MainMemoryEntry(version, data), dbEntry); mainStore.put((Transaction) txn, key, dbEntry); // try to write owner, but only if nonexistent (i.e will happen at first put only) ownerDirectory.putNoOverwrite((Transaction) txn, key, new DatabaseEntry(Shorts.toByteArray(owner))); }
OperationStatus status; value.setData(Shorts.toByteArray(newNode)); if (oldNode < 0) { status = ownerDirectory.putNoOverwrite(txn, key, value); value.setData(Shorts.toByteArray(newNode)); ownerDirectory.put(txn, key, value); txn.commit();
final DatabaseEntry sKey = new DatabaseEntry(Shorts.toByteArray(node)); final DatabaseEntry pKey = new DatabaseEntry(); final DatabaseEntry data = new DatabaseEntry();