public final class BufferedFileChannelInputStream
extends java.io.InputStream
InputStream
implementation which uses direct buffer to read a file to avoid extra copy of data between Java and native memory which happens when
using BufferedInputStream
. Unfortunately, this is not something already available in JDK, sun.nio.ch.ChannelInputStream
supports
reading a file using NIO, but does not support buffering.
To build an instance, see BufferedFileChannelInputStream.Builder
.
This class was ported and adapted from Apache Spark commit 933dc6cb7b3de1d8ccaf73d124d6eb95b947ed19 where it was called NioBufferedFileInputStream
.
Modifier and Type | Class and Description |
---|---|
static class |
BufferedFileChannelInputStream.Builder
Builds a new
BufferedFileChannelInputStream instance. |
Modifier and Type | Field and Description |
---|---|
private java.nio.ByteBuffer |
byteBuffer |
private java.nio.channels.FileChannel |
fileChannel |
Constructor and Description |
---|
BufferedFileChannelInputStream(java.io.File file)
Deprecated.
|
BufferedFileChannelInputStream(java.io.File file,
int bufferSize)
Deprecated.
|
BufferedFileChannelInputStream(java.nio.file.Path path)
Deprecated.
|
BufferedFileChannelInputStream(java.nio.file.Path path,
int bufferSize)
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
int |
available() |
static BufferedFileChannelInputStream.Builder |
builder()
Constructs a new
BufferedFileChannelInputStream.Builder . |
private void |
clean(java.nio.ByteBuffer buffer)
Attempts to clean up a ByteBuffer if it is direct or memory-mapped.
|
private void |
cleanDirectBuffer(java.nio.ByteBuffer buffer)
In Java 8, the type of
sun.nio.ch.DirectBuffer.cleaner() was sun.misc.Cleaner , and it was possible to access the method
sun.misc.Cleaner.clean() to invoke it. |
void |
close() |
int |
read() |
int |
read(byte[] b,
int offset,
int len) |
private boolean |
refill()
Checks whether data is left to be read from the input stream.
|
long |
skip(long n) |
private long |
skipFromFileChannel(long n) |
private final java.nio.ByteBuffer byteBuffer
private final java.nio.channels.FileChannel fileChannel
@Deprecated public BufferedFileChannelInputStream(java.io.File file) throws java.io.IOException
builder()
, BufferedFileChannelInputStream.Builder
, and BufferedFileChannelInputStream.Builder.get()
file
- The file to stream.java.io.IOException
- If an I/O error occurs@Deprecated public BufferedFileChannelInputStream(java.io.File file, int bufferSize) throws java.io.IOException
builder()
, BufferedFileChannelInputStream.Builder
, and BufferedFileChannelInputStream.Builder.get()
file
- The file to stream.bufferSize
- buffer size.java.io.IOException
- If an I/O error occurs@Deprecated public BufferedFileChannelInputStream(java.nio.file.Path path) throws java.io.IOException
builder()
, BufferedFileChannelInputStream.Builder
, and BufferedFileChannelInputStream.Builder.get()
path
- The path to stream.java.io.IOException
- If an I/O error occurs@Deprecated public BufferedFileChannelInputStream(java.nio.file.Path path, int bufferSize) throws java.io.IOException
builder()
, BufferedFileChannelInputStream.Builder
, and BufferedFileChannelInputStream.Builder.get()
path
- The path to stream.bufferSize
- buffer size.java.io.IOException
- If an I/O error occurspublic static BufferedFileChannelInputStream.Builder builder()
BufferedFileChannelInputStream.Builder
.BufferedFileChannelInputStream.Builder
.public int available() throws java.io.IOException
available
in class java.io.InputStream
java.io.IOException
private void clean(java.nio.ByteBuffer buffer)
buffer
- the buffer to clean.private void cleanDirectBuffer(java.nio.ByteBuffer buffer)
sun.nio.ch.DirectBuffer.cleaner()
was sun.misc.Cleaner
, and it was possible to access the method
sun.misc.Cleaner.clean()
to invoke it. The type changed to jdk.internal.ref.Cleaner
in later JDKs, and the clean()
method is not
accessible even with reflection. However sun.misc.Unsafe
added an invokeCleaner()
method in JDK 9+ and this is still accessible with
reflection.buffer
- the buffer to clean. must be a DirectBuffer.public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.InputStream
java.io.IOException
public int read() throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
public int read(byte[] b, int offset, int len) throws java.io.IOException
read
in class java.io.InputStream
java.io.IOException
private boolean refill() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public long skip(long n) throws java.io.IOException
skip
in class java.io.InputStream
java.io.IOException
private long skipFromFileChannel(long n) throws java.io.IOException
java.io.IOException