Package com.helger.commons.io.stream
Class StreamHelper
java.lang.Object
com.helger.commons.io.stream.StreamHelper
Some very basic IO stream utility stuff. All input stream (=reading) related
stuff is quite
null aware, where on writing an output stream may
never be null.- Author:
- Philip Helger
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA simple builder to copy an InputStream (StreamHelper.CopyByteStreamBuilder.from(InputStream)) to an OutputStream (StreamHelper.CopyByteStreamBuilder.to(OutputStream)) with certain parameters.static classA simple builder to copy a Reader (StreamHelper.CopyCharStreamBuilder.from(Reader)) to an Writer (StreamHelper.CopyCharStreamBuilder.to(Writer)) with certain parameters. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intbuffer size for copy operationsstatic final int -
Method Summary
Modifier and TypeMethodDescriptionstatic InputStreamstatic ESuccessclose(AutoCloseable aCloseable) Close the passed stream by encapsulating the declaredIOException.static ESuccesscloseWithoutFlush(AutoCloseable aCloseable) Close the passed object, without trying to call flush on it.static ESuccessPass the content of the given input stream to the given output stream.static ESuccessPass the content of the given input stream to the given output stream.static ESuccesscopyReaderToWriter(Reader aReader, Writer aWriter) Pass the content of the given reader to the given writer.static ESuccesscopyReaderToWriterAndCloseWriter(Reader aReader, Writer aWriter) Pass the content of the given reader to the given writer.static byte[]static char[]static NonBlockingStringReadercreateReader(char[] aChars) static InputStreamReadercreateReader(InputStream aIS, Charset aCharset) static NonBlockingStringReadercreateReader(String sText) static OutputStreamWritercreateWriter(OutputStream aOS, Charset aCharset) static ESuccessFlush the passed object encapsulating the declaredIOException.static byte[]getAllBytes(IHasInputStream aISP) Read all bytes from the passed input stream into a byte array.static byte[]getAllBytes(InputStream aIS) Read all bytes from the passed input stream into a byte array.static StringgetAllBytesAsString(IHasInputStream aISP, Charset aCharset) Read all bytes from the passed input stream into a string.static StringgetAllBytesAsString(InputStream aIS, Charset aCharset) Read all bytes from the passed input stream into a string.static char[]getAllCharacters(Reader aReader) Read all characters from the passed reader into a char array.static StringgetAllCharactersAsString(Reader aReader) Read all characters from the passed reader into a String.static intgetAvailable(InputStream aIS) Get the number of available bytes in the passed input stream.static InputStreamgetBuffered(InputStream aIS) static OutputStreamgetBuffered(OutputStream aOS) static ReadergetBuffered(Reader aReader) static WritergetBuffered(Writer aWriter) getCopy(InputStream aIS) Get a byte buffer with all the available content of the passed input stream.static NonBlockingStringWritergetCopyWithLimit(InputStream aIS, long nLimit) Get a byte buffer with all the available content of the passed input stream.static NonBlockingStringWritergetCopyWithLimit(Reader aReader, long nLimit) static booleanisBuffered(InputStream aIS) static booleanisBuffered(OutputStream aOS) static booleanisBuffered(Reader aReader) static booleanisBuffered(Writer aWriter) static booleanisKnownEOFException(Class<?> aClass) Check if the passed class is a known EOF exception class.static booleanCheck if the passed exception is a known EOF exception.static intreadFully(InputStream aIS, byte[] aBuffer) Read the whole buffer from the input stream.static intreadFully(InputStream aIS, byte[] aBuffer, int nOfs, int nLen) Read the whole buffer from the input stream.static StringreadSafeUTF(DataInput aDI) BecauseDataOutputStream.writeUTF(String)has a limit of 64KB this methods provides a similar solution for reading likeDataInputStream.readUTF()but what was written inwriteSafeUTF(DataOutput, String).static ICommonsList<String> readStreamLines(IHasInputStream aISP, Charset aCharset) Get the content of the passed Spring resource as one big string in the passed character set.static ICommonsList<String> readStreamLines(IHasInputStream aISP, Charset aCharset, int nLinesToSkip, int nLinesToRead) Get the content of the passed Spring resource as one big string in the passed character set.static ICommonsList<String> readStreamLines(InputStream aIS, Charset aCharset) Get the content of the passed stream as a list of lines in the passed character set.static ICommonsList<String> readStreamLines(InputStream aIS, Charset aCharset, int nLinesToSkip, int nLinesToRead) Get the content of the passed stream as a list of lines in the passed character set.static voidreadStreamLines(InputStream aIS, Charset aCharset, int nLinesToSkip, int nLinesToRead, Consumer<? super String> aLineCallback) Read the content of the passed stream line by line and invoking a callback on all matching lines.static voidreadStreamLines(InputStream aIS, Charset aCharset, Consumer<? super String> aLineCallback) Read the complete content of the passed stream and pass each line separately to the passed callback.static voidreadStreamLines(InputStream aIS, Charset aCharset, List<String> aTargetList) Get the content of the passed stream as a list of lines in the passed character set.static voidreadUntilEOF(InputStream aIS, byte[] aBuffer, ObjIntConsumer<? super byte[]> aConsumer) static voidreadUntilEOF(InputStream aIS, ObjIntConsumer<? super byte[]> aConsumer) static voidreadUntilEOF(Reader aReader, char[] aBuffer, ObjIntConsumer<? super char[]> aConsumer) static voidreadUntilEOF(Reader aReader, ObjIntConsumer<? super char[]> aConsumer) static voidskipFully(InputStream aIS, long nBytesToSkip) Fully skip the passed amounts in the input stream.static voidwriteSafeUTF(DataOutput aDO, String sStr) BecauseDataOutputStream.writeUTF(String)has a limit of 64KB this methods provides a similar solution but simply writing the bytes.static ESuccesswriteStream(OutputStream aOS, byte[] aBuf) Write bytes to anOutputStream.static ESuccesswriteStream(OutputStream aOS, byte[] aBuf, int nOfs, int nLen) Write bytes to anOutputStream.static ESuccesswriteStream(OutputStream aOS, String sContent, Charset aCharset) Write bytes to anOutputStream.
-
Field Details
-
DEFAULT_BUFSIZE
public static final int DEFAULT_BUFSIZEbuffer size for copy operations- See Also:
-
END_OF_STRING_MARKER
public static final int END_OF_STRING_MARKER- See Also:
-
-
Method Details
-
isKnownEOFException
Check if the passed exception is a known EOF exception.- Parameters:
t- The throwable/exception to be checked. May benull.- Returns:
trueif it is a user-created EOF exception
-
isKnownEOFException
Check if the passed class is a known EOF exception class.- Parameters:
aClass- The class to be checked. May benull.- Returns:
trueif it is a known EOF exception class.
-
closeWithoutFlush
Close the passed object, without trying to call flush on it.- Parameters:
aCloseable- The object to be closed. May benull.- Returns:
ESuccess.SUCCESSif the object was successfully closed.
-
close
Close the passed stream by encapsulating the declaredIOException. If the passed object also implements theFlushableinterface, it is tried to be flushed before it is closed.- Parameters:
aCloseable- The object to be closed. May benull.- Returns:
ESuccessif the object was successfully closed.
-
flush
Flush the passed object encapsulating the declaredIOException.- Parameters:
aFlushable- The flushable to be flushed. May benull.- Returns:
ESuccess.SUCCESSif the object was successfully flushed.
-
createDefaultCopyBufferBytes
- Returns:
- A newly created copy buffer using
DEFAULT_BUFSIZE. Nevernull. - Since:
- 9.3.6
-
copyInputStreamToOutputStream
@Nonnull public static ESuccess copyInputStreamToOutputStream(@WillClose @Nullable InputStream aIS, @WillNotClose @Nullable OutputStream aOS) Pass the content of the given input stream to the given output stream. The input stream is automatically closed, whereas the output stream stays open!- Parameters:
aIS- The input stream to read from. May benull. Automatically closed!aOS- The output stream to write to. May benull. Not automatically closed!- Returns:
if copying took place,ESuccess.SUCCESSotherwiseESuccess.FAILURE
-
copyInputStreamToOutputStreamAndCloseOS
@Nonnull public static ESuccess copyInputStreamToOutputStreamAndCloseOS(@WillClose @Nullable InputStream aIS, @WillClose @Nullable OutputStream aOS) Pass the content of the given input stream to the given output stream. Both the input stream and the output stream are automatically closed.- Parameters:
aIS- The input stream to read from. May benull. Automatically closed!aOS- The output stream to write to. May benull. Automatically closed!- Returns:
if copying took place,ESuccess.SUCCESSotherwiseESuccess.FAILURE
-
copyByteStream
- Returns:
- A new
StreamHelper.CopyByteStreamBuilder. Nevernull.
-
getAvailable
Get the number of available bytes in the passed input stream.- Parameters:
aIS- The input stream to use. May benull.- Returns:
- 0 in case of an error or if the parameter was
null.
-
getCopy
@Nullable public static NonBlockingByteArrayOutputStream getCopy(@Nonnull @WillClose InputStream aIS) Get a byte buffer with all the available content of the passed input stream.- Parameters:
aIS- The source input stream. May not benull.- Returns:
- A new
NonBlockingByteArrayOutputStreamwith all available content inside. TheOutputStreammust be closed by the caller since v10. Since v9.3.6 this method returnsnullif copying fails.
-
getCopyWithLimit
@Nullable public static NonBlockingByteArrayOutputStream getCopyWithLimit(@Nonnull @WillClose InputStream aIS, @Nonnegative long nLimit) Get a byte buffer with all the available content of the passed input stream.- Parameters:
aIS- The source input stream. May not benull.nLimit- The maximum number of bytes to be copied to the output stream. Must be ≥ 0.- Returns:
- A new
NonBlockingByteArrayOutputStreamwith all available content inside. TheOutputStreammust be closed by the caller since v10. Since v9.3.6 this method returnsnullif copying fails.
-
getAllBytes
Read all bytes from the passed input stream into a byte array.- Parameters:
aISP- The input stream provider to read from. May benull.- Returns:
- The byte array or
nullif the parameter or the resolved input stream isnull.
-
getAllBytes
Read all bytes from the passed input stream into a byte array.- Parameters:
aIS- The input stream to read from. May benull.- Returns:
- The byte array or
nullif the input stream isnull.
-
getAllBytesAsString
@Nullable public static String getAllBytesAsString(@Nullable IHasInputStream aISP, @Nonnull @Nonempty Charset aCharset) Read all bytes from the passed input stream into a string.- Parameters:
aISP- The input stream provider to read from. May benull.aCharset- The charset to use. May not benull.- Returns:
- The String or
nullif the parameter or the resolved input stream isnull.
-
getAllBytesAsString
@Nullable public static String getAllBytesAsString(@Nullable @WillClose InputStream aIS, @Nonnull @Nonempty Charset aCharset) Read all bytes from the passed input stream into a string.- Parameters:
aIS- The input stream to read from. May benull.aCharset- The charset to use. May not benull.- Returns:
- The String or
nullif the input stream isnull.
-
createDefaultCopyBufferChars
- Returns:
- A newly created copy buffer using
DEFAULT_BUFSIZE. Nevernull. - Since:
- 9.3.6
-
copyReaderToWriter
@Nonnull public static ESuccess copyReaderToWriter(@WillClose @Nullable Reader aReader, @WillNotClose @Nullable Writer aWriter) Pass the content of the given reader to the given writer. The reader is automatically closed, whereas the writer stays open!- Parameters:
aReader- The reader to read from. May benull. Automatically closed!aWriter- The writer to write to. May benull. Not automatically closed!- Returns:
if copying took place,ESuccess.SUCCESSotherwiseESuccess.FAILURE
-
copyReaderToWriterAndCloseWriter
@Nonnull public static ESuccess copyReaderToWriterAndCloseWriter(@Nullable @WillClose Reader aReader, @Nullable @WillClose Writer aWriter) Pass the content of the given reader to the given writer. The reader and the writer are automatically closed!- Parameters:
aReader- The reader to read from. May benull. Automatically closed!aWriter- The writer to write to. May benull. Automatically closed!- Returns:
if copying took place,ESuccess.SUCCESSotherwiseESuccess.FAILURE
-
copyCharStream
- Returns:
- A new
StreamHelper.CopyCharStreamBuilder. Nevernull.
-
getCopy
-
getCopyWithLimit
@Nullable public static NonBlockingStringWriter getCopyWithLimit(@Nonnull @WillClose Reader aReader, @Nonnegative long nLimit) -
getAllCharacters
Read all characters from the passed reader into a char array.- Parameters:
aReader- The reader to read from. May benull.- Returns:
- The character array or
nullif the reader isnull.
-
getAllCharactersAsString
Read all characters from the passed reader into a String.- Parameters:
aReader- The reader to read from. May benull.- Returns:
- The character array or
nullif the reader isnull.
-
readStreamLines
@Nullable @ReturnsMutableCopy public static ICommonsList<String> readStreamLines(@Nullable IHasInputStream aISP, @Nonnull Charset aCharset) Get the content of the passed Spring resource as one big string in the passed character set.- Parameters:
aISP- The resource to read. May not benull.aCharset- The character set to use. May not benull.- Returns:
nullif the resolved input stream isnull, the content otherwise.
-
readStreamLines
@Nullable @ReturnsMutableCopy public static ICommonsList<String> readStreamLines(@Nullable IHasInputStream aISP, @Nonnull Charset aCharset, @Nonnegative int nLinesToSkip, @CheckForSigned int nLinesToRead) Get the content of the passed Spring resource as one big string in the passed character set.- Parameters:
aISP- The resource to read. May benull.aCharset- The character set to use. May not benull.nLinesToSkip- The 0-based index of the first line to read. Pass in 0 to indicate to read everything.nLinesToRead- The number of lines to read. Pass inCGlobal.ILLEGAL_UINTto indicate that all lines should be read. If the number passed here exceeds the number of lines in the file, nothing happens.- Returns:
nullif the resolved input stream isnull, the content otherwise.
-
readStreamLines
@Nullable @ReturnsMutableCopy public static ICommonsList<String> readStreamLines(@WillClose @Nullable InputStream aIS, @Nonnull @Nonempty Charset aCharset) Get the content of the passed stream as a list of lines in the passed character set.- Parameters:
aIS- The input stream to read from. May benull.aCharset- The character set to use. May not benull.- Returns:
nullif the input stream isnull, the content lines otherwise.
-
readStreamLines
public static void readStreamLines(@WillClose @Nullable InputStream aIS, @Nonnull Charset aCharset, @Nonnull List<String> aTargetList) Get the content of the passed stream as a list of lines in the passed character set.- Parameters:
aIS- The input stream to read from. May benull.aCharset- The character set to use. May not benull.aTargetList- The list to be filled with the lines. May not benull.
-
readStreamLines
@Nullable @ReturnsMutableCopy public static ICommonsList<String> readStreamLines(@WillClose @Nullable InputStream aIS, @Nonnull Charset aCharset, @Nonnegative int nLinesToSkip, @CheckForSigned int nLinesToRead) Get the content of the passed stream as a list of lines in the passed character set.- Parameters:
aIS- The input stream to read from. May benull.aCharset- The character set to use. May not benull.nLinesToSkip- The 0-based index of the first line to read. Pass in 0 to indicate to read everything.nLinesToRead- The number of lines to read. Pass inCGlobal.ILLEGAL_UINTto indicate that all lines should be read. If the number passed here exceeds the number of lines in the file, nothing happens.- Returns:
nullif the input stream isnull, the content lines otherwise.
-
readStreamLines
public static void readStreamLines(@WillClose @Nullable InputStream aIS, @Nonnull @Nonempty Charset aCharset, @Nonnull Consumer<? super String> aLineCallback) Read the complete content of the passed stream and pass each line separately to the passed callback.- Parameters:
aIS- The input stream to read from. May benull.aCharset- The character set to use. May not benull.aLineCallback- The callback that is invoked for all read lines. Each passed line does NOT contain the line delimiter!
-
readStreamLines
public static void readStreamLines(@WillClose @Nullable InputStream aIS, @Nonnull @Nonempty Charset aCharset, @Nonnegative int nLinesToSkip, int nLinesToRead, @Nonnull Consumer<? super String> aLineCallback) Read the content of the passed stream line by line and invoking a callback on all matching lines.- Parameters:
aIS- The input stream to read from. May benull.aCharset- The character set to use. May not benull.nLinesToSkip- The 0-based index of the first line to read. Pass in 0 to indicate to read everything.nLinesToRead- The number of lines to read. Pass inCGlobal.ILLEGAL_UINTto indicate that all lines should be read. If the number passed here exceeds the number of lines in the file, nothing happens.aLineCallback- The callback that is invoked for all read lines. Each passed line does NOT contain the line delimiter! Note: it is not invoked for skipped lines!
-
writeStream
@Nonnull public static ESuccess writeStream(@WillClose @Nonnull OutputStream aOS, @Nonnull byte[] aBuf, @Nonnegative int nOfs, @Nonnegative int nLen) Write bytes to anOutputStream.- Parameters:
aOS- The output stream to write to. May not benull. Is closed independent of error or success.aBuf- The byte array from which is to be written. May not benull.nOfs- The 0-based index to the first byte in the array to be written. May not be < 0.nLen- The non-negative amount of bytes to be written. May not be < 0.- Returns:
ESuccess
-
writeStream
@Nonnull public static ESuccess writeStream(@WillClose @Nonnull OutputStream aOS, @Nonnull byte[] aBuf) Write bytes to anOutputStream.- Parameters:
aOS- The output stream to write to. May not benull. Is closed independent of error or success.aBuf- The byte array to be written. May not benull.- Returns:
ESuccess
-
writeStream
@Nonnull public static ESuccess writeStream(@WillClose @Nonnull OutputStream aOS, @Nonnull String sContent, @Nonnull Charset aCharset) Write bytes to anOutputStream.- Parameters:
aOS- The output stream to write to. May not benull. Is closed independent of error or success.sContent- The string to be written. May not benull.aCharset- The charset to be used, to convert the String to a byte array.- Returns:
ESuccess
-
createReader
-
createReader
-
createReader
@Nullable public static InputStreamReader createReader(@Nullable InputStream aIS, @Nonnull Charset aCharset) -
createWriter
@Nullable public static OutputStreamWriter createWriter(@Nullable OutputStream aOS, @Nonnull Charset aCharset) -
skipFully
public static void skipFully(@Nonnull InputStream aIS, @Nonnegative long nBytesToSkip) throws IOException Fully skip the passed amounts in the input stream. Only forward skipping is possible!- Parameters:
aIS- The input stream to skip in.nBytesToSkip- The number of bytes to skip. Must be ≥ 0.- Throws:
IOException- In case something goes wrong internally
-
readFully
@Nonnegative public static int readFully(@Nonnull InputStream aIS, @Nonnull byte[] aBuffer) throws IOException Read the whole buffer from the input stream.- Parameters:
aIS- The input stream to read from. May not benull.aBuffer- The buffer to write to. May not benull. Must be ≥ than the content to be read.- Returns:
- The number of read bytes
- Throws:
IOException- In case reading fails
-
readFully
@Nonnegative public static int readFully(@Nonnull @WillNotClose InputStream aIS, @Nonnull byte[] aBuffer, @Nonnegative int nOfs, @Nonnegative int nLen) throws IOException Read the whole buffer from the input stream.- Parameters:
aIS- The input stream to read from. May not benull.aBuffer- The buffer to write to. May not benull. Must be ≥ than the content to be read.nOfs- The offset into the destination buffer to use. May not be < 0.nLen- The number of bytes to read into the destination buffer to use. May not be < 0.- Returns:
- The number of read bytes
- Throws:
IOException- In case reading fails
-
readUntilEOF
public static void readUntilEOF(@Nonnull @WillClose InputStream aIS, @Nonnull ObjIntConsumer<? super byte[]> aConsumer) throws IOException - Throws:
IOException
-
readUntilEOF
public static void readUntilEOF(@Nonnull @WillClose InputStream aIS, @Nonnull byte[] aBuffer, @Nonnull ObjIntConsumer<? super byte[]> aConsumer) throws IOException - Throws:
IOException
-
readUntilEOF
public static void readUntilEOF(@Nonnull @WillClose Reader aReader, @Nonnull ObjIntConsumer<? super char[]> aConsumer) throws IOException - Throws:
IOException
-
readUntilEOF
public static void readUntilEOF(@Nonnull @WillClose Reader aReader, @Nonnull char[] aBuffer, @Nonnull ObjIntConsumer<? super char[]> aConsumer) throws IOException - Throws:
IOException
-
isBuffered
-
getBuffered
-
isBuffered
-
getBuffered
-
isBuffered
-
getBuffered
-
isBuffered
-
getBuffered
-
checkForInvalidFilterInputStream
-
writeSafeUTF
BecauseDataOutputStream.writeUTF(String)has a limit of 64KB this methods provides a similar solution but simply writing the bytes.- Parameters:
aDO-DataOutputto write to. May not benull.sStr- The string to be written. May benull.- Throws:
IOException- on write error- See Also:
-
readSafeUTF
BecauseDataOutputStream.writeUTF(String)has a limit of 64KB this methods provides a similar solution for reading likeDataInputStream.readUTF()but what was written inwriteSafeUTF(DataOutput, String).- Parameters:
aDI-DataInputto read from. May not benull.- Returns:
- The read string. May be
null. - Throws:
IOException- on read error- See Also:
-