Package com.levigo.jadice.document.io
Class IOUtils
- java.lang.Object
-
- com.levigo.jadice.document.io.IOUtils
-
public final class IOUtils extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static int
COPY_CHUNK_SIZE
static int
DEFAULT_BUFFER_SIZE
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static long
copy(InputStream in, OutputStream out)
static long
copy(InputStream in, OutputStream out, int bufferSize)
static void
copyAndClose(InputStream is, OutputStream os)
Copies all data from input to output.static void
copyStream(SeekableInputStream is, ImageOutputStream os)
static void
deleteAllBelow(File file)
Recursively delete everything below a given directory, but don't remove the directory itself.static void
deleteRecursively(File file)
Recursively delete a file or directory.static long
determineLength(SeekableInputStream is)
Determines and returns the stream length either by checking whether the stream returns a reasonable length (some instances may not be able to do that) or just reading over it, until an EOF is hit.static void
discardStream(InputStream is)
Copies all data from the input stream.static void
drainAsynchronously(InputStream is)
Eagerly drain the given input stream.static void
drainSynchronously(InputStream is)
Eagerly drain the given input stream.static long
forcedDetermineLength(SeekableInputStream is)
Determines and returns the stream length by reading over the complete stream until an EOF was found.static boolean
isBufferManagerActive()
Checks if BufferManager is available and enabledstatic boolean
isTryFileCaching()
Return whether to try to cache data in local files by default.static void
setTryFileCaching(boolean tryFileCaching)
Set whether to try to cache data in local files by default.static SeekableInputStream
wrap(InputStream is)
Wraps the given stream into aSeekableInputStream
.static SeekableInputStream
wrap(InputStream inputStream, boolean tryLocalTmpFileAccess)
Wraps the given stream intoSeekableInputStream
.static SeekableInputStream
wrap(InputStream inputStream, boolean tryLocalTmpFileAccess, boolean ignoreSeekableStream)
Wraps the given stream intoSeekableInputStream
.static SeekableInputStream
wrap(ImageInputStream imageInputStream)
Wraps the givenImageInputStream
into anSeekableInputStream
.
-
-
-
Field Detail
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE
- See Also:
- Constant Field Values
-
COPY_CHUNK_SIZE
public static final int COPY_CHUNK_SIZE
- See Also:
- Constant Field Values
-
-
Method Detail
-
copy
public static long copy(InputStream in, OutputStream out) throws IOException
- Throws:
IOException
-
copy
public static long copy(InputStream in, OutputStream out, int bufferSize) throws IOException
- Throws:
IOException
-
copyAndClose
public static void copyAndClose(InputStream is, OutputStream os) throws IOException
Copies all data from input to output. Closes the streams afterwards.- Throws:
IOException
-
discardStream
public static void discardStream(InputStream is) throws IOException
Copies all data from the input stream. Does NOT close the input stream.- Throws:
IOException
-
deleteRecursively
public static void deleteRecursively(File file)
Recursively delete a file or directory.
-
deleteAllBelow
public static void deleteAllBelow(File file)
Recursively delete everything below a given directory, but don't remove the directory itself.
-
copyStream
public static void copyStream(SeekableInputStream is, ImageOutputStream os) throws IOException
- Throws:
IOException
-
determineLength
public static long determineLength(SeekableInputStream is) throws IOException
Determines and returns the stream length either by checking whether the stream returns a reasonable length (some instances may not be able to do that) or just reading over it, until an EOF is hit. Users of given stream need to deal with synchronization against other users of the same stream instance.- Parameters:
is
- the stream to determine its length- Returns:
- the length, the length returned by
SeekableInputStream.length()
if positive, or the length of the stream determined by reading over it. the length is measured in bytes. - Throws:
IOException
- if an I/O error occurs.
-
forcedDetermineLength
public static long forcedDetermineLength(SeekableInputStream is) throws IOException
Determines and returns the stream length by reading over the complete stream until an EOF was found.Note: Users of given stream need to deal with synchronization against other users of the same stream instance.
- Parameters:
is
- the stream to determine its length- Returns:
- the length of the stream determined by reading over it. The length is measured in bytes.
- Throws:
IOException
- if an I/O error occurs.
-
drainSynchronously
public static void drainSynchronously(InputStream is)
Eagerly drain the given input stream. This method will block until all data has been read and the stream's EOF has been reached.
-
drainAsynchronously
public static void drainAsynchronously(InputStream is)
Eagerly drain the given input stream. Draining will be performed in the background. This method will synchronize on theInputStream
while doing block-wise reads.
-
wrap
public static final SeekableInputStream wrap(InputStream is) throws IOException
Wraps the given stream into aSeekableInputStream
. Checks the given input stream and if necessary wraps it with an appropriateSeekableInputStream
implementation. This method will try to buffer all data into a temporary file if possible. If additionally a BufferManager is available on the classpath and temporary buffering is possible, the buffering of data will be handled by the BufferManager. If temporary buffering is not possible (e.g. due tosecurity restrictions
), all data will be buffered in memory.- Parameters:
is
- the stream to wrap- Returns:
- a
SeekableInputStream
instance wrapping the originalInputStream
- Throws:
IOException
- if an i/o error occurredIllegalArgumentException
- if the givenInputStream
is null
-
wrap
public static final SeekableInputStream wrap(InputStream inputStream, boolean tryLocalTmpFileAccess) throws IOException
Wraps the given stream intoSeekableInputStream
. Checks the given input stream and if necessary wraps it with an appropriateSeekableInputStream
, which is returned. IftryLocalTmpFileAccess
istrue
, this method will try to buffer all data into a temporary file if possible. If additionally a BufferManager is available on the classpath and temporary buffering is possible, the buffering of data will be handled by the BufferManager. If temporary buffering is not possible (e.g. due tosecurity restrictions
), all data will be buffered in memory.- Parameters:
inputStream
- the stream to wraptryLocalTmpFileAccess
- enable/disable temporary file data buffering.- Returns:
- a
SeekableInputStream
instance wrapping the originalInputStream
- Throws:
IOException
- if an i/o error occurredIllegalArgumentException
- if the givenInputStream
is null
-
wrap
public static final SeekableInputStream wrap(InputStream inputStream, boolean tryLocalTmpFileAccess, boolean ignoreSeekableStream) throws IOException
Wraps the given stream intoSeekableInputStream
. Checks the given input stream and if necessary wraps it with an appropriateSeekableInputStream
, which is returned. IftryLocalTmpFileAccess
istrue
, this method will try to buffer all data into a temporary file if possible. If additionally a BufferManager is available on the classpath and temporary buffering is possible, the buffering of data will be handled by the BufferManager. If temporary buffering is not possible (e.g. due tosecurity restrictions
), all data will be buffered in memory.- Parameters:
inputStream
- the stream to wraptryLocalTmpFileAccess
- enable/disable temporary file data buffering.ignoreSeekableStream
- if the given inputStream is already an instance ofSeekableInputStream
it will be ignored and not wrapped any further if this flag is set to true- Returns:
- a
SeekableInputStream
instance wrapping the originalInputStream
- Throws:
IOException
- if an i/o error occurredIllegalArgumentException
- if the givenInputStream
is null
-
wrap
public static SeekableInputStream wrap(ImageInputStream imageInputStream)
Wraps the givenImageInputStream
into anSeekableInputStream
. If the providedImageInputStream
is already an implementation ofSeekableInputStream
, it will not be wrapped and the original instance will be returned. If theImageInputStream
is not an instance ofSeekableInputStream
, a lightweight delegation implementation will be created and returned.- Parameters:
imageInputStream
- theImageInputStream
to wrap- Returns:
- a
SeekableInputStream
instance - Throws:
IllegalArgumentException
- if the providedImageInputStream
is null.
-
setTryFileCaching
public static void setTryFileCaching(boolean tryFileCaching)
Set whether to try to cache data in local files by default. This value is used bywrap(InputStream)
.- Parameters:
tryFileCaching
-true
if local file caching should be tried by default
-
isTryFileCaching
public static boolean isTryFileCaching()
Return whether to try to cache data in local files by default.- Returns:
true
if local file caching is tried by default
-
isBufferManagerActive
public static boolean isBufferManagerActive()
Checks if BufferManager is available and enabled- Returns:
- true if BufferManager is available and enabled
-
-