/** * Read data from a set of contiguous buffers. All data buffers except for the last one * must have an identical remaining number of bytes in the buffer (that is a power of two). The last * buffer can be of an arbitrary remaining length. */ public ByteBuffersDataInput(List<ByteBuffer> buffers) { ensureAssumptions(buffers); this.blocks = buffers.stream().map(buf -> buf.asReadOnlyBuffer()).toArray(ByteBuffer[]::new); if (blocks.length == 1) { this.blockBits = 32; this.blockMask = ~0; } else { final int blockBytes = determineBlockPage(buffers); this.blockBits = Integer.numberOfTrailingZeros(blockBytes); this.blockMask = (1 << blockBits) - 1; } this.size = Arrays.stream(blocks).mapToLong(block -> block.remaining()).sum(); // The initial "position" of this stream is shifted by the position of the first block. this.offset = blocks[0].position(); this.pos = offset; }
private static void ensureAssumptions(List<ByteBuffer> buffers) { if (buffers.isEmpty()) { throw new IllegalArgumentException("Buffer list must not be empty."); } if (buffers.size() == 1) { // Special case of just a single buffer, conditions don't apply. } else { final int blockPage = determineBlockPage(buffers); // First buffer decides on block page length. if (!isPowerOfTwo(blockPage)) { throw new IllegalArgumentException("The first buffer must have power-of-two position() + remaining(): 0x" + Integer.toHexString(blockPage)); } // Any block from 2..last-1 should have the same page size. for (int i = 1, last = buffers.size() - 1; i < last; i++) { ByteBuffer buffer = buffers.get(i); if (buffer.position() != 0) { throw new IllegalArgumentException("All buffers except for the first one must have position() == 0: " + buffer); } if (i != last && buffer.remaining() != blockPage) { throw new IllegalArgumentException("Intermediate buffers must share an identical remaining() power-of-two block size: 0x" + Integer.toHexString(blockPage)); } } } }
long absEnd = Math.toIntExact(absStart + length); int blockBytes = ByteBuffersDataInput.determineBlockPage(buffers); int blockBits = Integer.numberOfTrailingZeros(blockBytes); int blockMask = (1 << blockBits) - 1;
/** * Read data from a set of contiguous buffers. All data buffers except for the last one * must have an identical remaining number of bytes in the buffer (that is a power of two). The last * buffer can be of an arbitrary remaining length. */ public ByteBuffersDataInput(List<ByteBuffer> buffers) { ensureAssumptions(buffers); this.blocks = buffers.stream().map(buf -> buf.asReadOnlyBuffer()).toArray(ByteBuffer[]::new); if (blocks.length == 1) { this.blockBits = 32; this.blockMask = ~0; } else { final int blockBytes = determineBlockPage(buffers); this.blockBits = Integer.numberOfTrailingZeros(blockBytes); this.blockMask = (1 << blockBits) - 1; } this.size = Arrays.stream(blocks).mapToLong(block -> block.remaining()).sum(); // The initial "position" of this stream is shifted by the position of the first block. this.offset = blocks[0].position(); this.pos = offset; }
private static void ensureAssumptions(List<ByteBuffer> buffers) { if (buffers.isEmpty()) { throw new IllegalArgumentException("Buffer list must not be empty."); } if (buffers.size() == 1) { // Special case of just a single buffer, conditions don't apply. } else { final int blockPage = determineBlockPage(buffers); // First buffer decides on block page length. if (!isPowerOfTwo(blockPage)) { throw new IllegalArgumentException("The first buffer must have power-of-two position() + remaining(): 0x" + Integer.toHexString(blockPage)); } // Any block from 2..last-1 should have the same page size. for (int i = 1, last = buffers.size() - 1; i < last; i++) { ByteBuffer buffer = buffers.get(i); if (buffer.position() != 0) { throw new IllegalArgumentException("All buffers except for the first one must have position() == 0: " + buffer); } if (i != last && buffer.remaining() != blockPage) { throw new IllegalArgumentException("Intermediate buffers must share an identical remaining() power-of-two block size: 0x" + Integer.toHexString(blockPage)); } } } }
long absEnd = Math.toIntExact(absStart + length); int blockBytes = ByteBuffersDataInput.determineBlockPage(buffers); int blockBits = Integer.numberOfTrailingZeros(blockBytes); int blockMask = (1 << blockBits) - 1;