Package org.apache.commons.io.input
Class BufferedFileChannelInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.apache.commons.io.input.BufferedFileChannelInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public final class BufferedFileChannelInputStream extends java.io.InputStreamInputStreamimplementation which uses direct buffer to read a file to avoid extra copy of data between Java and native memory which happens when usingBufferedInputStream. Unfortunately, this is not something already available in JDK,sun.nio.ch.ChannelInputStreamsupports 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:
BufferedFileChannelInputStream.Builder
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classBufferedFileChannelInputStream.BuilderBuilds a newBufferedFileChannelInputStream.
-
Field Summary
Fields Modifier and Type Field Description private java.nio.ByteBufferbyteBufferprivate java.nio.channels.FileChannelfileChannel
-
Constructor Summary
Constructors Modifier Constructor Description BufferedFileChannelInputStream(java.io.File file)Deprecated.BufferedFileChannelInputStream(java.io.File file, int bufferSize)Deprecated.privateBufferedFileChannelInputStream(java.nio.channels.FileChannel fileChannel, int bufferSize)BufferedFileChannelInputStream(java.nio.file.Path path)Deprecated.BufferedFileChannelInputStream(java.nio.file.Path path, int bufferSize)Deprecated.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()static BufferedFileChannelInputStream.Builderbuilder()Constructs a newBufferedFileChannelInputStream.Builder.private voidclean(java.nio.ByteBuffer buffer)Attempts to clean up a ByteBuffer if it is direct or memory-mapped.private voidcleanDirectBuffer(java.nio.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)
-
-
-
Constructor Detail
-
BufferedFileChannelInputStream
@Deprecated public BufferedFileChannelInputStream(java.io.File file) throws java.io.IOExceptionDeprecated.Constructs a new instance for the given File.- Parameters:
file- The file to stream.- Throws:
java.io.IOException- If an I/O error occurs
-
BufferedFileChannelInputStream
@Deprecated public BufferedFileChannelInputStream(java.io.File file, int bufferSize) throws java.io.IOExceptionDeprecated.Constructs a new instance for the given File and buffer size.- Parameters:
file- The file to stream.bufferSize- buffer size.- Throws:
java.io.IOException- If an I/O error occurs
-
BufferedFileChannelInputStream
private BufferedFileChannelInputStream(java.nio.channels.FileChannel fileChannel, int bufferSize)
-
BufferedFileChannelInputStream
@Deprecated public BufferedFileChannelInputStream(java.nio.file.Path path) throws java.io.IOExceptionDeprecated.Constructs a new instance for the given Path.- Parameters:
path- The path to stream.- Throws:
java.io.IOException- If an I/O error occurs
-
BufferedFileChannelInputStream
@Deprecated public BufferedFileChannelInputStream(java.nio.file.Path path, int bufferSize) throws java.io.IOExceptionDeprecated.Constructs a new instance for the given Path and buffer size.- Parameters:
path- The path to stream.bufferSize- buffer size.- Throws:
java.io.IOException- If an I/O error occurs
-
-
Method Detail
-
builder
public static BufferedFileChannelInputStream.Builder builder()
Constructs a newBufferedFileChannelInputStream.Builder.- Returns:
- a new
BufferedFileChannelInputStream.Builder. - Since:
- 2.12.0
-
available
public int available() throws java.io.IOException- Overrides:
availablein classjava.io.InputStream- Throws:
java.io.IOException
-
clean
private void clean(java.nio.ByteBuffer buffer)
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
private void cleanDirectBuffer(java.nio.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. 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
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.IOException
-
read
public int read() throws java.io.IOException- Specified by:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
read
public int read(byte[] b, int offset, int len) throws java.io.IOException- Overrides:
readin classjava.io.InputStream- Throws:
java.io.IOException
-
refill
private boolean refill() throws java.io.IOExceptionChecks whether data is left to be read from the input stream.- Returns:
- true if data is left, false otherwise
- Throws:
java.io.IOException- if an I/O error occurs.
-
skip
public long skip(long n) throws java.io.IOException- Overrides:
skipin classjava.io.InputStream- Throws:
java.io.IOException
-
skipFromFileChannel
private long skipFromFileChannel(long n) throws java.io.IOException- Throws:
java.io.IOException
-
-