Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.apache.parquet.bytes;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -367,8 +366,18 @@ private StreamBytesInput(InputStream in, int byteCount) {
@Override
public void writeAllTo(OutputStream out) throws IOException {
LOG.debug("write All {} bytes", byteCount);
// TODO: more efficient
out.write(this.toByteArray());
// Transfer in chunks to avoid allocating a byteCount-sized intermediate buffer
byte[] buffer = new byte[Math.min(byteCount, 8192)];
int remaining = byteCount;
while (remaining > 0) {
int toRead = Math.min(remaining, buffer.length);
int n = in.readNBytes(buffer, 0, toRead);
if (n < toRead) {
throw new EOFException("Reached the end of stream with " + (remaining - n) + " bytes left to read");
}
out.write(buffer, 0, n);
remaining -= n;
}
}

@Override
Expand All @@ -395,8 +404,11 @@ void writeInto(ByteBuffer buffer) {

public byte[] toByteArray() throws IOException {
LOG.debug("read all {} bytes", byteCount);
byte[] buf = new byte[byteCount];
new DataInputStream(in).readFully(buf);
byte[] buf = in.readNBytes(byteCount);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hadn't noticed this before. Doesn't look like anything in the JDK has actually subclassed it ... that is there's no AFAIK there's no faster native code version. That's probably because java.nio is the way to go there

if (buf.length != byteCount) {
throw new EOFException(
"Reached the end of stream with " + (byteCount - buf.length) + " bytes left to read");
}
return buf;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ public void testLz4RawHeapDecompressorCanCopyLargePage() throws IOException {
final byte[] raw = new byte[size];
new Random(42).nextBytes(raw);

try (TrackingByteBufferAllocator allocator =
TrackingByteBufferAllocator.wrap(new DirectByteBufferAllocator());
try (TrackingByteBufferAllocator allocator = TrackingByteBufferAllocator.wrap(new DirectByteBufferAllocator());
ByteBufferReleaser releaser = new ByteBufferReleaser(allocator)) {
CodecFactory heapCodecFactory = new CodecFactory(new Configuration(), pageSize);
BytesInputCompressor compressor = heapCodecFactory.getCompressor(CompressionCodecName.LZ4_RAW);
Expand Down
Loading