@Override public String toString() { return String.format(Locale.ROOT, "%,d bytes, block size: %,d, blocks: %,d", size(), blockSize(), blocks.size()); }
@Override public long getFilePointer() { ensureOpen(); return delegate.size(); }
final IndexOutput createOutput(BiFunction<String, ByteBuffersDataOutput, IndexInput> outputToInput) throws IOException { if (content != null) { throw new IOException("Can only write to a file once: " + fileName); } String clazzName = ByteBuffersDirectory.class.getSimpleName(); String outputName = String.format(Locale.ROOT, "%s output (file=%s)", clazzName, fileName); return new ByteBuffersIndexOutput( bbOutputSupplier.get(), outputName, fileName, new CRC32(), (output) -> { content = outputToInput.apply(fileName, output); cachedLength = output.size(); }); } }
/** * Return a contiguous array with the current content written to the output. The returned * array is always a copy (can be mutated). */ public byte[] toArrayCopy() { if (blocks.size() == 0) { return EMPTY_BYTE_ARRAY; } // We could try to detect single-block, array-based ByteBuffer here // and use Arrays.copyOfRange, but I don't think it's worth the extra // instance checks. byte [] arr = new byte[Math.toIntExact(size())]; int offset = 0; for (ByteBuffer bb : toBufferList()) { int len = bb.remaining(); bb.get(arr, offset, len); offset += len; } return arr; }
@Override public String toString() { return String.format(Locale.ROOT, "%,d bytes, block size: %,d, blocks: %,d", size(), blockSize(), blocks.size()); }
@Override public long getFilePointer() { ensureOpen(); return delegate.size(); }
final IndexOutput createOutput(BiFunction<String, ByteBuffersDataOutput, IndexInput> outputToInput) throws IOException { if (content != null) { throw new IOException("Can only write to a file once: " + fileName); } String clazzName = ByteBuffersDirectory.class.getSimpleName(); String outputName = String.format(Locale.ROOT, "%s output (file=%s)", clazzName, fileName); return new ByteBuffersIndexOutput( bbOutputSupplier.get(), outputName, fileName, new CRC32(), (output) -> { content = outputToInput.apply(fileName, output); cachedLength = output.size(); }); } }
/** * Return a contiguous array with the current content written to the output. The returned * array is always a copy (can be mutated). */ public byte[] toArrayCopy() { if (blocks.size() == 0) { return EMPTY_BYTE_ARRAY; } // We could try to detect single-block, array-based ByteBuffer here // and use Arrays.copyOfRange, but I don't think it's worth the extra // instance checks. byte [] arr = new byte[Math.toIntExact(size())]; int offset = 0; for (ByteBuffer bb : toBufferList()) { int len = bb.remaining(); bb.get(arr, offset, len); offset += len; } return arr; }