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()); }
ByteBuffer buffer = ByteBuffer.allocate( chunkSize + CHUNK_HEADER_SIZE ); buffer.putShort( chunkSize ); buffer.put( value.byteValue() ); buffer.putShort( value.shortValue() ); buffer.putInt( value.intValue() ); buffer.putLong( value.longValue() ); buffer.flip(); return ByteBufUtil.hexDump( buffer.array() );
/** * Adds metadata at current position (position is moved forward). Does not flip or reset. * @return The passed <code>destination</code> with metadata added. */ private ByteBuffer addMetaData(final ByteBuffer destination, boolean includeNextBlockMetadata) { destination.put(this.fileContext.isUseHBaseChecksum() ? (byte) 1 : (byte) 0); destination.putLong(this.offset); if (includeNextBlockMetadata) { destination.putInt(this.nextBlockOnDiskSize); } return destination; }
@Override public AppendMessageResult<Void> doAppend(long baseOffset, ByteBuffer targetBuffer, int freeSpace, ConsumerLogMessage message) { workingBuffer.clear(); final long wroteOffset = baseOffset + targetBuffer.position(); workingBuffer.flip(); workingBuffer.limit(CONSUMER_LOG_UNIT_BYTES); workingBuffer.putLong(System.currentTimeMillis()); workingBuffer.putLong(message.getOffset()); workingBuffer.putInt(message.getSize()); workingBuffer.putShort(message.getHeaderSize()); targetBuffer.put(workingBuffer.array(), 0, CONSUMER_LOG_UNIT_BYTES); return new AppendMessageResult<>(AppendMessageStatus.SUCCESS, wroteOffset, CONSUMER_LOG_UNIT_BYTES); } }
@Test public void testUpdateLong() { final long value = Integer.MAX_VALUE + 1; final ByteBuffer buffer = ByteBuffer.allocate(8); buffer.putLong(value); Checksum crc1 = new Crc32(); Checksum crc2 = new Crc32(); Checksums.updateLong(crc1, value); crc2.update(buffer.array(), buffer.arrayOffset(), 8); assertEquals("Crc values should be the same", crc1.getValue(), crc2.getValue()); }
@Override public void reset() { // Clear the entire usedFlagBuffer final int usedFlagBufferCapacity = usedFlagBuffer.capacity(); // putLong() instead of put() can boost the performance of clearing the buffer final int n = (usedFlagBufferCapacity / Long.BYTES) * Long.BYTES; for (int i = 0; i < n; i += Long.BYTES) { usedFlagBuffer.putLong(i, 0L); } for (int i = n; i < usedFlagBufferCapacity; i++) { usedFlagBuffer.put(i, (byte) 0); } }
private void write( StoreChannel channel, long time, long identifier, long version, long lastCommittedTxId, long indexVersion ) throws IOException { buf.clear(); buf.putLong( time ).putLong( identifier ).putLong( version ).putLong( lastCommittedTxId ).putLong( indexVersion ); buf.flip(); channel.writeAll( buf, 0 ); channel.force( true ); }
/** * Serializes histogram fields that are common to both the full and sparse encoding modes. * * @param buf Destination buffer */ private void writeByteBufferCommonFields(ByteBuffer buf) { buf.putDouble(lowerLimit); buf.putDouble(upperLimit); buf.putInt(numBuckets); buf.put((byte) outlierHandlingMode.ordinal()); buf.putLong(count); buf.putLong(lowerOutlierCount); buf.putLong(upperOutlierCount); buf.putLong(missingValueCount); buf.putDouble(max); buf.putDouble(min); }
@Override public void write(FileChannel fileChannel) throws IOException { byteBuffer().position(0); byteBuffer().putShort(magic); byteBuffer().putLong(fileLength); byteBuffer().putInt(totalNum.get()); byteBuffer().putInt(aliveNum.get()); byteBuffer().putInt(isFull); byteBuffer().putLong(storeTxLogRecordId); byteBuffer().flip(); fileChannel.position(0); fileChannel.write(byteBuffer()); fileChannel.force(true); }
private MemoryRecords buildOverflowBatch(int remaining) { // We do not have any records left to down-convert. Construct an overflow message for the length remaining. // This message will be ignored by the consumer because its length will be past the length of maximum // possible response size. // DefaultRecordBatch => // BaseOffset => Int64 // Length => Int32 // ... ByteBuffer overflowMessageBatch = ByteBuffer.allocate( Math.max(MIN_OVERFLOW_MESSAGE_LENGTH, Math.min(remaining + 1, MAX_READ_SIZE))); overflowMessageBatch.putLong(-1L); // Fill in the length of the overflow batch. A valid batch must be at least as long as the minimum batch // overhead. overflowMessageBatch.putInt(Math.max(remaining + 1, DefaultRecordBatch.RECORD_BATCH_OVERHEAD)); log.debug("Constructed overflow message batch for partition {} with length={}", topicPartition(), remaining); return MemoryRecords.readableRecords(overflowMessageBatch); }
/** * @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)); } } }