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.
This class was ported and adapted from Apache Spark commit 933dc6cb7b3de1d8ccaf73d124d6eb95b947ed19 where it was
called NioBufferedFileInputStream
.
Modifier and Type | Field and Description |
---|---|
private java.nio.ByteBuffer |
byteBuffer |
private static java.lang.Class<?> |
DIRECT_BUFFER_CLASS |
private java.nio.channels.FileChannel |
fileChannel |
Constructor and Description |
---|
BufferedFileChannelInputStream(java.io.File file)
Constructs a new instance for the given File.
|
BufferedFileChannelInputStream(java.io.File file,
int bufferSizeInBytes)
Constructs a new instance for the given File and buffer size.
|
BufferedFileChannelInputStream(java.nio.file.Path path)
Constructs a new instance for the given Path.
|
BufferedFileChannelInputStream(java.nio.file.Path path,
int bufferSizeInBytes)
Constructs a new instance for the given Path and buffer size.
|
Modifier and Type | Method and Description |
---|---|
int |
available() |
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 DirectBuffer.cleaner() was sun.misc.Cleaner, and it was possible to access the method
sun.misc.Cleaner.clean() to invoke it.
|
void |
close() |
private static java.lang.Class<?> |
getDirectBufferClass() |
private static boolean |
isDirectBuffer(java.lang.Object object) |
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
private static final java.lang.Class<?> DIRECT_BUFFER_CLASS
public BufferedFileChannelInputStream(java.io.File file) throws java.io.IOException
file
- The file to stream.java.io.IOException
- If an I/O error occurspublic BufferedFileChannelInputStream(java.io.File file, int bufferSizeInBytes) throws java.io.IOException
file
- The file to stream.bufferSizeInBytes
- buffer size.java.io.IOException
- If an I/O error occurspublic BufferedFileChannelInputStream(java.nio.file.Path path) throws java.io.IOException
path
- The path to stream.java.io.IOException
- If an I/O error occurspublic BufferedFileChannelInputStream(java.nio.file.Path path, int bufferSizeInBytes) throws java.io.IOException
path
- The path to stream.bufferSizeInBytes
- buffer size.java.io.IOException
- If an I/O error occursprivate static java.lang.Class<?> getDirectBufferClass()
private static boolean isDirectBuffer(java.lang.Object object)
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)
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
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