private static byte[] createUniqIDBuffer() { ByteBuffer buffer = ByteBuffer.allocate(4 + 2); long current = System.currentTimeMillis(); if (current >= nextStartTime) { setStartTime(current); } buffer.position(0); buffer.putInt((int) (System.currentTimeMillis() - startTime)); buffer.putShort((short) COUNTER.getAndIncrement()); return buffer.array(); }
public void testByteBuffer() throws IOException { byte[] string = "Hello".getBytes(); ByteBuffer buf = ByteBuffer.allocate(1024); buf.order(ByteOrder.LITTLE_ENDIAN); buf.putInt(123); buf.putChar('z'); buf.putShort((short) 2017); buf.putFloat(3.1415f); buf.put(string); buf.putLong(9876543210L); buf.putDouble(3.14159265); buf.put((byte) 5); buf.flip(); // important // TestUtils.showBuffer(buf); assertEquals(123, buf.getInt()); assertEquals('z', buf.getChar()); assertEquals(2017, buf.getShort()); assertEquals(3.1415f, buf.getFloat()); byte[] so = new byte[string.length]; buf.get(so); assertTrue(TestUtils.sameBytes(string, so)); assertEquals(9876543210L, buf.getLong()); assertEquals(3.14159265, buf.getDouble()); assertEquals((byte) 5, buf.get()); }
private static ByteBuffer createMockTCPHeader() { ByteBuffer buffer = ByteBuffer.allocate(20); buffer.putShort((short) 0x1234); // source port buffer.putShort((short) 0x5678); // destination port buffer.putInt(0x111); // sequence number buffer.putInt(0x222); // acknowledgment number buffer.putShort((short) (5 << 12)); // data offset + flags(0) buffer.putShort((short) 0); // window (don't care for these tests) buffer.putShort((short) 0); // checksum buffer.putShort((short) 0); // urgent pointer buffer.flip(); return buffer; }
public int storeInByteBuffer(ByteBuffer buffer, int position) { buffer.position(position); buffer.putShort((short) (((isLeaf ? 0x1 : 0x0) << 15) | getChildren().size())); for (float v : getMinCoordinates()) { buffer.putFloat(v); } for (float v : getMaxCoordinates()) { buffer.putFloat(v); } byte[] bytes = bitmap.toBytes(); buffer.putInt(bytes.length); buffer.put(bytes); int pos = buffer.position(); int childStartOffset = pos + getChildren().size() * Integer.BYTES; for (Node child : getChildren()) { buffer.putInt(pos, childStartOffset); childStartOffset = child.storeInByteBuffer(buffer, childStartOffset); pos += Integer.BYTES; } return childStartOffset; }
@Override public void doEncode() { // super.doEncode(); byteBuffer.put(this.identified ? (byte) 1 : (byte) 0); if (this.version != null) { byte[] bs = version.getBytes(UTF8); byteBuffer.putShort((short) bs.length); if (bs.length > 0) { byteBuffer.put(bs); } } else { byteBuffer.putShort((short) 0); } }
@Test public void testParseUnknownType() { ByteBuffer buffer = ByteBuffer.allocate(32); buffer.putShort(ControlRecordType.CURRENT_CONTROL_RECORD_KEY_VERSION); buffer.putShort((short) 337); buffer.flip(); ControlRecordType type = ControlRecordType.parse(buffer); assertEquals(ControlRecordType.UNKNOWN, type); }
/** * @param states Cache states. */ private static void putCacheStates(ByteBuffer buf, Map<Integer, CacheState> states) { buf.putShort((short)states.size()); for (Map.Entry<Integer, CacheState> entry : states.entrySet()) { buf.putInt(entry.getKey()); CacheState state = entry.getValue(); // Need 2 bytes for the number of partitions. buf.putShort((short)state.size()); for (int i = 0; i < state.size(); i++) { buf.putShort((short)state.partitionByIndex(i)); buf.putLong(state.partitionSizeByIndex(i)); buf.putLong(state.partitionCounterByIndex(i)); } } }
public void testBufferWrite() throws IOException { File f = newTestFile(); byte[] string = "Hello".getBytes(); ByteBuffer in = ByteBuffer.allocate(1024); in.order(ByteOrder.LITTLE_ENDIAN); in.putInt(123); in.putChar('z'); in.putShort((short) 2017); in.putFloat(3.1415f); in.putLong(9876543210L); in.putDouble(3.14159265); in.put((byte) 5); in.put(string);