java.lang.Object
kala.compress.utils.IOUtils
Utility functions.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default buffer size (8192) to use in copy methods.static final LinkOption[]Empty array of typeLinkOption.static final OpenOption[]Empty array of typeOpenOption.private static final byte[] -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateIOUtils()Private constructor to prevent instantiation of this utility class. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidCloses the givenCloseableas a null-safe operation.static voidcloseQuietly(Closeable closeable) Closes the given Closeable and swallows any IOException that may occur.static longcopy(InputStream input, OutputStream output) Copies the content of a InputStream into an OutputStream.static longcopy(InputStream input, OutputStream output, int bufferSize) Copies the content of a InputStream into an OutputStreamstatic longcopyRange(InputStream input, long len, OutputStream output) Copies part of the content of a InputStream into an OutputStream.static longcopyRange(InputStream input, long len, OutputStream output, int bufferSize) Copies part of the content of a InputStream into an OutputStreamstatic intreadFully(InputStream input, byte[] array) Reads as much from input as possible to fill the given array.static intreadFully(InputStream input, byte[] array, int offset, int length) Reads as much from input as possible to fill the given array with the given amount of bytes.static voidreadFully(ReadableByteChannel channel, ByteBuffer byteBuffer) Readsb.remaining()bytes from the given channel starting at the current channel's position.static byte[]readRange(InputStream input, int length) Gets part of the contents of anInputStreamas abyte[].static byte[]readRange(ReadableByteChannel input, int length) Gets part of the contents of anReadableByteChannelas abyte[].static longskip(InputStream input, long toSkip) Skips bytes from an input byte stream.static byte[]toByteArray(InputStream input) Gets the contents of anInputStreamas abyte[].
-
Field Details
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZEThe default buffer size (8192) to use in copy methods.- Since:
- 1.27.1-0
- See Also:
-
EMPTY_OPEN_OPTIONS
Empty array of typeOpenOption.- Since:
- 1.27.1-0
-
EMPTY_LINK_OPTIONS
Empty array of typeLinkOption.- Since:
- 1.21
-
SKIP_BUFFER
private static final byte[] SKIP_BUFFER
-
-
Constructor Details
-
IOUtils
private IOUtils()Private constructor to prevent instantiation of this utility class.
-
-
Method Details
-
close
Closes the givenCloseableas a null-safe operation.- Parameters:
closeable- The resource to close, may be null.- Throws:
IOException- if an I/O error occurs.- Since:
- 1.27.1-0
-
closeQuietly
Closes the given Closeable and swallows any IOException that may occur.- Parameters:
closeable- Closeable to close, can be null- Since:
- 1.7
-
copy
Copies the content of a InputStream into an OutputStream. Uses a default buffer size of 8192 bytes.- Parameters:
input- theInputStreamto read.output- theOutputStreamto write.- Returns:
- the number of bytes copied.
- Throws:
IOException- if an error occurs.NullPointerException- if theinputoroutputisnull.
-
copy
Copies the content of a InputStream into an OutputStream- Parameters:
input- theInputStreamto read.output- theOutputStreamto write.bufferSize- the buffer size to use, must be bigger than 0.- Returns:
- the number of bytes copied.
- Throws:
IOException- if an error occurs.NullPointerException- if theinputoroutputisnull.
-
copyRange
Copies part of the content of a InputStream into an OutputStream. Uses a default buffer size of 8024 bytes.- Parameters:
input- the InputStream to copylen- maximum amount of bytes to copyoutput- the target Stream- Returns:
- the number of bytes copied
- Throws:
IOException- if an error occurs- Since:
- 1.21
-
copyRange
public static long copyRange(InputStream input, long len, OutputStream output, int bufferSize) throws IOException Copies part of the content of a InputStream into an OutputStream- Parameters:
input- the InputStream to copylen- maximum amount of bytes to copyoutput- the target, may be null to simulate output to dev/null on Linux and NUL on WindowsbufferSize- the buffer size to use, must be bigger than 0- Returns:
- the number of bytes copied
- Throws:
IOException- if an error occursIllegalArgumentException- if bufferSize is smaller than or equal to 0- Since:
- 1.21
-
readFully
Reads as much from input as possible to fill the given array.This method may invoke read repeatedly to fill the array and only read less bytes than the length of the array if the end of the stream has been reached.
- Parameters:
input- stream to read fromarray- buffer to fill- Returns:
- the number of bytes actually read
- Throws:
IOException- on error
-
readFully
public static int readFully(InputStream input, byte[] array, int offset, int length) throws IOException Reads as much from input as possible to fill the given array with the given amount of bytes.This method may invoke read repeatedly to read the bytes and only read less bytes than the requested length if the end of the stream has been reached.
- Parameters:
input- stream to read fromarray- buffer to filloffset- offset into the buffer to start filling atlength- of bytes to read- Returns:
- the number of bytes actually read
- Throws:
IOException- if an I/O error has occurred
-
readFully
Readsb.remaining()bytes from the given channel starting at the current channel's position.This method reads repeatedly from the channel until the requested number of bytes are read. This method blocks until the requested number of bytes are read, the end of the channel is detected, or an exception is thrown.
- Parameters:
channel- the channel to read frombyteBuffer- the buffer into which the data is read.- Throws:
IOException- if an I/O error occurs.EOFException- if the channel reaches the end before reading all the bytes.
-
readRange
Gets part of the contents of anInputStreamas abyte[].- Parameters:
input- theInputStreamto read fromlength- maximum amount of bytes to copy- Returns:
- the requested byte array
- Throws:
NullPointerException- if the input is nullIOException- if an I/O error occurs- Since:
- 1.21
-
readRange
Gets part of the contents of anReadableByteChannelas abyte[].- Parameters:
input- theReadableByteChannelto read fromlength- maximum amount of bytes to copy- Returns:
- the requested byte array
- Throws:
NullPointerException- if the input is nullIOException- if an I/O error occurs- Since:
- 1.21
-
skip
Skips bytes from an input byte stream.This method will only skip less than the requested number of bytes if the end of the input stream has been reached.
- Parameters:
input- stream to skip bytes intoSkip- the number of bytes to skip- Returns:
- the number of bytes actually skipped
- Throws:
IOException- on error
-
toByteArray
Gets the contents of anInputStreamas abyte[].This method buffers the input internally, so there is no need to use a
BufferedInputStream.- Parameters:
input- theInputStreamto read from- Returns:
- the requested byte array
- Throws:
NullPointerException- if the input is nullIOException- if an I/O error occurs- Since:
- 1.5
-