Package org.apache.pdfbox.io
Class IOUtils
- java.lang.Object
-
- org.apache.pdfbox.io.IOUtils
-
public final class IOUtils extends java.lang.ObjectThis class contains various I/O-related methods.
-
-
Field Summary
Fields Modifier and Type Field Description private static org.apache.commons.logging.LogLOGLog instance.private static RandomAccessStreamCache.StreamCacheCreateFunctionstreamCacheprivate static java.util.Optional<java.util.function.Consumer<java.nio.ByteBuffer>>UNMAPPER
-
Constructor Summary
Constructors Modifier Constructor Description privateIOUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.io.IOExceptioncloseAndLogException(java.io.Closeable closeable, org.apache.commons.logging.Log logger, java.lang.String resourceName, java.io.IOException initialException)Try to close an IO resource and log and return if there was an exception.static voidcloseQuietly(java.io.Closeable closeable)Null safe close of the givenCloseablesuppressing any exception.static longcopy(java.io.InputStream input, java.io.OutputStream output)Copies all the contents from the given input stream to the given output stream.static RandomAccessStreamCache.StreamCacheCreateFunctioncreateMemoryOnlyStreamCache()Provides a function to create an instance of a memory only StreamCache using unrestricted main memory.static RandomAccessStreamCache.StreamCacheCreateFunctioncreateTempFileOnlyStreamCache()Provides a function to create an instance of a temp file only StreamCache using unrestricted size.private static java.util.function.Consumer<java.nio.ByteBuffer>newBufferCleaner(java.lang.Class<?> unmappableBufferClass, java.lang.invoke.MethodHandle unmapper)static longpopulateBuffer(java.io.InputStream in, byte[] buffer)Populates the given buffer with data read from the input stream.static byte[]toByteArray(java.io.InputStream in)Reads the input stream and returns its contents as a byte array.static voidunmap(java.nio.ByteBuffer buf)Unmap memory mapped byte buffers.private static java.util.function.Consumer<java.nio.ByteBuffer>unmapper()This is adapted from org.apache.lucene.store.MMapDirectory
-
-
-
Field Detail
-
LOG
private static final org.apache.commons.logging.Log LOG
Log instance.
-
streamCache
private static final RandomAccessStreamCache.StreamCacheCreateFunction streamCache
-
UNMAPPER
private static final java.util.Optional<java.util.function.Consumer<java.nio.ByteBuffer>> UNMAPPER
-
-
Method Detail
-
toByteArray
public static byte[] toByteArray(java.io.InputStream in) throws java.io.IOExceptionReads the input stream and returns its contents as a byte array.- Parameters:
in- the input stream to read from.- Returns:
- the byte array
- Throws:
java.io.IOException- if an I/O error occurs
-
copy
public static long copy(java.io.InputStream input, java.io.OutputStream output) throws java.io.IOExceptionCopies all the contents from the given input stream to the given output stream.- Parameters:
input- the input streamoutput- the output stream- Returns:
- the number of bytes that have been copied
- Throws:
java.io.IOException- if an I/O error occurs
-
populateBuffer
public static long populateBuffer(java.io.InputStream in, byte[] buffer) throws java.io.IOExceptionPopulates the given buffer with data read from the input stream. If the data doesn't fit the buffer, only the data that fits in the buffer is read. If the data is less than fits in the buffer, the buffer is not completely filled.- Parameters:
in- the input stream to read frombuffer- the buffer to fill- Returns:
- the number of bytes written to the buffer
- Throws:
java.io.IOException- if an I/O error occurs
-
closeQuietly
public static void closeQuietly(java.io.Closeable closeable)
Null safe close of the givenCloseablesuppressing any exception.- Parameters:
closeable- to be closed
-
closeAndLogException
public static java.io.IOException closeAndLogException(java.io.Closeable closeable, org.apache.commons.logging.Log logger, java.lang.String resourceName, java.io.IOException initialException)Try to close an IO resource and log and return if there was an exception.An exception is only returned if the IOException passed in is null.
- Parameters:
closeable- to be closedlogger- the logger to be used so that logging appears under that log instanceresourceName- the name to appear in the log outputinitialException- if set, this exception will be returned even where there is another exception while closing the IO resource- Returns:
- the IOException is there was any but only if initialException is null
-
unmap
public static void unmap(java.nio.ByteBuffer buf)
Unmap memory mapped byte buffers. This is a hack waiting for a proper JVM provided solution expected in java 10 https://bugs.openjdk.java.net/browse/JDK-4724038 The issue here is that even when closed, memory mapped byte buffers hold a lock on the underlying file until GC is executes and this in turns result in an error if the user tries to move or delete the file.- Parameters:
buf- the buffer to be unmapped
-
unmapper
private static java.util.function.Consumer<java.nio.ByteBuffer> unmapper()
This is adapted from org.apache.lucene.store.MMapDirectory- Returns:
-
newBufferCleaner
private static java.util.function.Consumer<java.nio.ByteBuffer> newBufferCleaner(java.lang.Class<?> unmappableBufferClass, java.lang.invoke.MethodHandle unmapper)
-
createMemoryOnlyStreamCache
public static RandomAccessStreamCache.StreamCacheCreateFunction createMemoryOnlyStreamCache()
Provides a function to create an instance of a memory only StreamCache using unrestricted main memory. RandomAccessReadWriteBuffer is used as current default implementation.- Returns:
- a function to create an instance of a memory only StreamCache using unrestricted main memory
-
createTempFileOnlyStreamCache
public static RandomAccessStreamCache.StreamCacheCreateFunction createTempFileOnlyStreamCache()
Provides a function to create an instance of a temp file only StreamCache using unrestricted size. ScratchFile is used as current default implementation.- Returns:
- a function to create an instance of a temp file only StreamCache using unrestricted size
-
-