Package org.apache.commons.io.input
Class BufferedFileChannelInputStream
java.lang.Object
java.io.InputStream
org.apache.commons.io.input.BufferedFileChannelInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
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, use BufferedFileChannelInputStream.Builder.
This class was ported and adapted from Apache Spark commit 933dc6cb7b3de1d8ccaf73d124d6eb95b947ed19 where it was called NioBufferedFileInputStream.
- Since:
- 2.9.0
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionDeprecated.BufferedFileChannelInputStream(File file, int bufferSize) Deprecated.Deprecated.BufferedFileChannelInputStream(Path path, int bufferSize) Deprecated. -
Method Summary
Modifier and TypeMethodDescriptionintbuilder()Constructs a newBufferedFileChannelInputStream.Builder.private voidclean(ByteBuffer buffer) Attempts to clean up a ByteBuffer if it is direct or memory-mapped.private voidcleanDirectBuffer(ByteBuffer buffer) In Java 8, the type ofsun.nio.ch.DirectBuffer.cleaner()wassun.misc.Cleaner, and it was possible to access the methodsun.misc.Cleaner.clean()to invoke it.voidclose()intread()intread(byte[] b, int offset, int len) private booleanrefill()Checks whether data is left to be read from the input stream.longskip(long n) private longskipFromFileChannel(long n) Methods inherited from class java.io.InputStream
mark, markSupported, read, reset
-
Field Details
-
byteBuffer
-
fileChannel
-
-
Constructor Details
-
BufferedFileChannelInputStream
Deprecated.Constructs a new instance for the given File.- Parameters:
file- The file to stream.- Throws:
IOException- If an I/O error occurs
-
BufferedFileChannelInputStream
Deprecated.Constructs a new instance for the given File and buffer size.- Parameters:
file- The file to stream.bufferSize- buffer size.- Throws:
IOException- If an I/O error occurs
-
BufferedFileChannelInputStream
Deprecated.Constructs a new instance for the given Path.- Parameters:
path- The path to stream.- Throws:
IOException- If an I/O error occurs
-
BufferedFileChannelInputStream
Deprecated.Constructs a new instance for the given Path and buffer size.- Parameters:
path- The path to stream.bufferSize- buffer size.- Throws:
IOException- If an I/O error occurs
-
-
Method Details
-
builder
Constructs a newBufferedFileChannelInputStream.Builder.- Returns:
- a new
BufferedFileChannelInputStream.Builder. - Since:
- 2.12.0
-
available
- Overrides:
availablein classInputStream- Throws:
IOException
-
clean
Attempts to clean up a ByteBuffer if it is direct or memory-mapped. This uses an *unsafe* Sun API that will cause errors if one attempts to read from the disposed buffer. However, neither the bytes allocated to direct buffers nor file descriptors opened for memory-mapped buffers put pressure on the garbage collector. Waiting for garbage collection may lead to the depletion of off-heap memory or huge numbers of open files. There's unfortunately no standard API to manually dispose of these kinds of buffers.- Parameters:
buffer- the buffer to clean.
-
cleanDirectBuffer
In Java 8, the type ofsun.nio.ch.DirectBuffer.cleaner()wassun.misc.Cleaner, and it was possible to access the methodsun.misc.Cleaner.clean()to invoke it. The type changed tojdk.internal.ref.Cleanerin later JDKs, and theclean()method is not accessible even with reflection. Howeversun.misc.Unsafeadded aninvokeCleaner()method in JDK 9+ and this is still accessible with reflection.- Parameters:
buffer- the buffer to clean. must be a DirectBuffer.
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
read
- Specified by:
readin classInputStream- Throws:
IOException
-
read
- Overrides:
readin classInputStream- Throws:
IOException
-
refill
Checks whether data is left to be read from the input stream.- Returns:
- true if data is left, false otherwise
- Throws:
IOException- if an I/O error occurs.
-
skip
- Overrides:
skipin classInputStream- Throws:
IOException
-
skipFromFileChannel
- Throws:
IOException
-
builder(),BufferedFileChannelInputStream.Builder, andBufferedFileChannelInputStream.Builder.get()