Top Description Fields Constructors Methods
org.apache.pdfbox.io

public final Class IOUtils

extends Object
Class Inheritance
Static Imports
java.lang.invoke.MethodHandles.constant, .MethodHandles.dropArguments, .MethodHandles.filterReturnValue, .MethodHandles.guardWithTest, .MethodHandles.lookup, .MethodType.methodType, java.util.Objects.nonNull

This class contains various I/O-related methods.

Field Summary

Modifier and TypeField and Description
private static final Log
LOG

Log instance.

private static final RandomAccessStreamCache.StreamCacheCreateFunction
private static final Optional<Consumer<ByteBuffer>>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static IOException

Returns:

the IOException is there was any but only if initialException is null
closeAndLogException
(Closeable
to be closed
closeable
,
Log
the logger to be used so that logging appears under that log instance
logger
,
String
the name to appear in the log output
resourceName
,
IOException
if set, this exception will be returned even where there is another exception while closing the IO resource
initialException
)

Try to close an IO resource and log and return if there was an exception.

public static void
closeQuietly(Closeable
to be closed
closeable
)

Null safe close of the given Closeable suppressing any exception.

public static long

Returns:

the number of bytes that have been copied
copy
(InputStream
the input stream
input
,
OutputStream
the output stream
output
)

Copies all the contents from the given input stream to the given output stream.

public static RandomAccessStreamCache.StreamCacheCreateFunction

Returns:

a function to create an instance of a memory only StreamCache using unrestricted main memory
createMemoryOnlyStreamCache
()

Provides a function to create an instance of a memory only StreamCache using unrestricted main memory.

public static RandomAccessStreamCache.StreamCacheCreateFunction

Returns:

a function to create an instance of a temp file only StreamCache using unrestricted size
createTempFileOnlyStreamCache
()

Provides a function to create an instance of a temp file only StreamCache using unrestricted size.

private static Consumer<ByteBuffer>
newBufferCleaner(final Class<?> unmappableBufferClass, final MethodHandle unmapper)

public static long

Returns:

the number of bytes written to the buffer
populateBuffer
(InputStream
the input stream to read from
in
,
byte[]
the buffer to fill
buffer
)

Populates the given buffer with data read from the input stream.

public static byte[]

Returns:

the byte array
toByteArray
(InputStream
the input stream to read from.
in
)

Reads the input stream and returns its contents as a byte array.

public static void
unmap(ByteBuffer
the buffer to be unmapped
buf
)

Unmap memory mapped byte buffers.

private static Consumer<ByteBuffer>
unmapper()

This is adapted from org.apache.lucene.store.MMapDirectory

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

LOGback to summary
private static final Log LOG

Log instance.

streamCacheback to summary
private static final RandomAccessStreamCache.StreamCacheCreateFunction streamCache
UNMAPPERback to summary
private static final Optional<Consumer<ByteBuffer>> UNMAPPER

Constructor Detail

IOUtilsback to summary
private IOUtils()

Method Detail

closeAndLogExceptionback to summary
public static IOException closeAndLogException(Closeable closeable, Log logger, String resourceName, 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:Closeable

to be closed

logger:Log

the logger to be used so that logging appears under that log instance

resourceName:String

the name to appear in the log output

initialException:IOException

if set, this exception will be returned even where there is another exception while closing the IO resource

Returns:IOException

the IOException is there was any but only if initialException is null

closeQuietlyback to summary
public static void closeQuietly(Closeable closeable)

Null safe close of the given Closeable suppressing any exception.

Parameters
closeable:Closeable

to be closed

copyback to summary
public static long copy(InputStream input, OutputStream output) throws IOException

Copies all the contents from the given input stream to the given output stream.

Parameters
input:InputStream

the input stream

output:OutputStream

the output stream

Returns:long

the number of bytes that have been copied

Exceptions
IOException:
if an I/O error occurs
createMemoryOnlyStreamCacheback to summary
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:RandomAccessStreamCache.StreamCacheCreateFunction

a function to create an instance of a memory only StreamCache using unrestricted main memory

createTempFileOnlyStreamCacheback to summary
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:RandomAccessStreamCache.StreamCacheCreateFunction

a function to create an instance of a temp file only StreamCache using unrestricted size

newBufferCleanerback to summary
private static Consumer<ByteBuffer> newBufferCleaner(final Class<?> unmappableBufferClass, final MethodHandle unmapper)
populateBufferback to summary
public static long populateBuffer(InputStream in, byte[] buffer) throws IOException

Populates 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:InputStream

the input stream to read from

buffer:byte[]

the buffer to fill

Returns:long

the number of bytes written to the buffer

Exceptions
IOException:
if an I/O error occurs
toByteArrayback to summary
public static byte[] toByteArray(InputStream in) throws IOException

Reads the input stream and returns its contents as a byte array.

Parameters
in:InputStream

the input stream to read from.

Returns:byte[]

the byte array

Exceptions
IOException:
if an I/O error occurs
unmapback to summary
public static void unmap(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:ByteBuffer

the buffer to be unmapped

unmapperback to summary
private static Consumer<ByteBuffer> unmapper()

This is adapted from org.apache.lucene.store.MMapDirectory