@Override public byte readByte(long pos) { pos += offset; return blocks[blockIndex(pos)].get(blockOffset(pos)); }
@Override public byte readByte() throws EOFException { try { ByteBuffer block = blocks[blockIndex(pos)]; byte v = block.get(blockOffset(pos)); pos++; return v; } catch (IndexOutOfBoundsException e) { if (pos >= size()) { throw new EOFException(); } else { throw e; // Something is wrong. } } }
@Override public void readBytes(byte[] arr, int off, int len) throws EOFException { try { while (len > 0) { ByteBuffer block = blocks[blockIndex(pos)].duplicate(); block.position(blockOffset(pos)); int chunk = Math.min(len, block.remaining()); if (chunk == 0) { throw new EOFException(); } // Update pos early on for EOF detection, then try to get buffer content. pos += chunk; block.get(arr, off, chunk); len -= chunk; off += chunk; } } catch (BufferUnderflowException | ArrayIndexOutOfBoundsException e) { if (pos >= size()) { throw new EOFException(); } else { throw e; // Something is wrong. } } }
@Override public short readShort(long pos) { long absPos = offset + pos; int blockOffset = blockOffset(absPos); if (blockOffset + Short.BYTES <= blockMask) { return blocks[blockIndex(absPos)].getShort(blockOffset); } else { return (short) ((readByte(pos ) & 0xFF) << 8 | (readByte(pos + 1) & 0xFF)); } }
try { while (len > 0) { ByteBuffer block = blocks[blockIndex(pos)].duplicate(); int blockOffset = blockOffset(pos); block.position(blockOffset);
@Override public long readLong(long pos) { long absPos = offset + pos; int blockOffset = blockOffset(absPos); if (blockOffset + Long.BYTES <= blockMask) { return blocks[blockIndex(absPos)].getLong(blockOffset); } else { return (((long) readInt(pos)) << 32) | (readInt(pos + 4) & 0xFFFFFFFFL); } }
@Override public int readInt(long pos) { long absPos = offset + pos; int blockOffset = blockOffset(absPos); if (blockOffset + Integer.BYTES <= blockMask) { return blocks[blockIndex(absPos)].getInt(blockOffset); } else { return ((readByte(pos ) ) << 24 | (readByte(pos + 1) & 0xFF) << 16 | (readByte(pos + 2) & 0xFF) << 8 | (readByte(pos + 3) & 0xFF)); } }
@Override public byte readByte(long pos) { pos += offset; return blocks[blockIndex(pos)].get(blockOffset(pos)); }
@Override public byte readByte() throws EOFException { try { ByteBuffer block = blocks[blockIndex(pos)]; byte v = block.get(blockOffset(pos)); pos++; return v; } catch (IndexOutOfBoundsException e) { if (pos >= size()) { throw new EOFException(); } else { throw e; // Something is wrong. } } }
@Override public void readBytes(byte[] arr, int off, int len) throws EOFException { try { while (len > 0) { ByteBuffer block = blocks[blockIndex(pos)].duplicate(); block.position(blockOffset(pos)); int chunk = Math.min(len, block.remaining()); if (chunk == 0) { throw new EOFException(); } // Update pos early on for EOF detection, then try to get buffer content. pos += chunk; block.get(arr, off, chunk); len -= chunk; off += chunk; } } catch (BufferUnderflowException | ArrayIndexOutOfBoundsException e) { if (pos >= size()) { throw new EOFException(); } else { throw e; // Something is wrong. } } }
@Override public short readShort(long pos) { long absPos = offset + pos; int blockOffset = blockOffset(absPos); if (blockOffset + Short.BYTES <= blockMask) { return blocks[blockIndex(absPos)].getShort(blockOffset); } else { return (short) ((readByte(pos ) & 0xFF) << 8 | (readByte(pos + 1) & 0xFF)); } }
try { while (len > 0) { ByteBuffer block = blocks[blockIndex(pos)].duplicate(); int blockOffset = blockOffset(pos); block.position(blockOffset);
@Override public long readLong(long pos) { long absPos = offset + pos; int blockOffset = blockOffset(absPos); if (blockOffset + Long.BYTES <= blockMask) { return blocks[blockIndex(absPos)].getLong(blockOffset); } else { return (((long) readInt(pos)) << 32) | (readInt(pos + 4) & 0xFFFFFFFFL); } }
@Override public int readInt(long pos) { long absPos = offset + pos; int blockOffset = blockOffset(absPos); if (blockOffset + Integer.BYTES <= blockMask) { return blocks[blockIndex(absPos)].getInt(blockOffset); } else { return ((readByte(pos ) ) << 24 | (readByte(pos + 1) & 0xFF) << 16 | (readByte(pos + 2) & 0xFF) << 8 | (readByte(pos + 3) & 0xFF)); } }