Package com.helger.commons.io.stream
Class StreamHelper
- java.lang.Object
-
- com.helger.commons.io.stream.StreamHelper
-
@Immutable public final class StreamHelper extends Object
Some very basic IO stream utility stuff. All input stream (=reading) related stuff is quitenullaware, where on writing an output stream may never be null.- Author:
- Philip Helger
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_BUFSIZEbuffer size for copy operationsstatic intEND_OF_STRING_MARKER
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static InputStreamcheckForInvalidFilterInputStream(InputStream aIS)static 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 ESuccesscopyInputStreamToOutputStream(InputStream aIS, boolean bCloseIS, OutputStream aOS, boolean bCloseOS, byte[] aBuffer, Long aLimit, IExceptionCallback<IOException> aExceptionCallback, MutableLong aCopyByteCount)Pass the content of the given input stream to the given output stream.static ESuccesscopyInputStreamToOutputStream(InputStream aIS, OutputStream aOS)Pass the content of the given input stream to the given output stream.static ESuccesscopyInputStreamToOutputStream(InputStream aIS, OutputStream aOS, byte[] aBuffer)Pass the content of the given input stream to the given output stream.static ESuccesscopyInputStreamToOutputStream(InputStream aIS, OutputStream aOS, byte[] aBuffer, MutableLong aCopyByteCount)static ESuccesscopyInputStreamToOutputStream(InputStream aIS, OutputStream aOS, byte[] aBuffer, MutableLong aCopyByteCount, Long aLimit)static ESuccesscopyInputStreamToOutputStream(InputStream aIS, OutputStream aOS, MutableLong aCopyByteCount)Pass the content of the given input stream to the given output stream.static ESuccesscopyInputStreamToOutputStreamAndCloseOS(InputStream aIS, OutputStream aOS)Pass the content of the given input stream to the given output stream.static ESuccesscopyInputStreamToOutputStreamWithLimit(InputStream aIS, OutputStream aOS, long nLimit)Pass the content of the given input stream to the given output stream.static ESuccesscopyInputStreamToOutputStreamWithLimitAndCloseOS(InputStream aIS, OutputStream aOS, long nLimit)Pass the content of the given input stream to the given output stream.static ESuccesscopyReaderToWriter(Reader aReader, boolean bCloseReader, Writer aWriter, boolean bCloseWriter, char[] aBuffer, Long aLimit, IExceptionCallback<IOException> aExceptionCallback, MutableLong aCopyCharCount)Pass the content of the given reader to the given writer.static ESuccesscopyReaderToWriter(Reader aReader, Writer aWriter)Pass the content of the given reader to the given writer.static ESuccesscopyReaderToWriter(Reader aReader, Writer aWriter, char[] aBuffer)Pass the content of the given reader to the given writer.static ESuccesscopyReaderToWriter(Reader aReader, Writer aWriter, char[] aBuffer, MutableLong aCopyCharCount)static ESuccesscopyReaderToWriter(Reader aReader, Writer aWriter, char[] aBuffer, MutableLong aCopyCharCount, Long aLimit)Pass the content of the given reader to the given writer.static ESuccesscopyReaderToWriter(Reader aReader, Writer aWriter, MutableLong aCopyCharCount)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 ESuccesscopyReaderToWriterWithLimit(Reader aReader, Writer aWriter, long nLimit)Pass the content of the given reader to the given writer.static ESuccesscopyReaderToWriterWithLimitAndCloseWriter(Reader aReader, Writer aWriter, long nLimit)Pass the content of the given reader to the given writer.static byte[]createDefaultCopyBufferBytes()static char[]createDefaultCopyBufferChars()static NonBlockingStringReadercreateReader(char[] aChars)static InputStreamReadercreateReader(InputStream aIS, Charset aCharset)static NonBlockingStringReadercreateReader(String sText)static OutputStreamWritercreateWriter(OutputStream aOS, Charset aCharset)static ESuccessflush(Flushable aFlushable)Flush 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)static NonBlockingByteArrayOutputStreamgetCopy(InputStream aIS)Get a byte buffer with all the available content of the passed input stream.static NonBlockingStringWritergetCopy(Reader aReader)static NonBlockingByteArrayOutputStreamgetCopyWithLimit(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 booleanisKnownEOFException(Throwable t)Check 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 Detail
-
DEFAULT_BUFSIZE
public static final int DEFAULT_BUFSIZE
buffer size for copy operations- See Also:
- Constant Field Values
-
END_OF_STRING_MARKER
public static final int END_OF_STRING_MARKER
- See Also:
- Constant Field Values
-
-
Method Detail
-
isKnownEOFException
public static boolean isKnownEOFException(@Nullable Throwable t)
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
public static boolean isKnownEOFException(@Nullable Class<?> aClass)
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
@Nonnull public static ESuccess closeWithoutFlush(@Nullable @WillClose AutoCloseable aCloseable)
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
@Nonnull public static ESuccess close(@Nullable @WillClose AutoCloseable aCloseable)
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
@Nonnull public static ESuccess flush(@Nullable Flushable aFlushable)
Flush the passed object encapsulating the declaredIOException.- Parameters:
aFlushable- The flushable to be flushed. May benull.- Returns:
ESuccess.SUCCESSif the object was successfully flushed.
-
copyInputStreamToOutputStream
@Nonnull public static ESuccess copyInputStreamToOutputStream(@Nullable InputStream aIS, boolean bCloseIS, @Nullable OutputStream aOS, boolean bCloseOS, @Nonnull @Nonempty byte[] aBuffer, @Nullable Long aLimit, @Nullable IExceptionCallback<IOException> aExceptionCallback, @Nullable MutableLong aCopyByteCount)
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.bCloseIS-trueto close the InputStream,falseto leave it open.aOS- The output stream to write to. May benull.bCloseOS-trueto close the OutputStream,falseto leave it open.aBuffer- The buffer to use. May not benull.aLimit- An optional maximum number of bytes to copied from the input stream to the output stream. May benullto indicate no limit, meaning all bytes are copied.aExceptionCallback- The Exception callback to be invoked, if an exception occurs. May not benull.aCopyByteCount- An optional mutable long object that will receive the total number of copied bytes. Note: and optional old value is overwritten. Note: this is only called, if copying was successful, and not in case of an exception.- Returns:
if copying took place,ESuccess.SUCCESSotherwiseESuccess.FAILURE- Since:
- 9.3.6
-
createDefaultCopyBufferBytes
@Nonnull @ReturnsMutableCopy public static byte[] createDefaultCopyBufferBytes()
- Returns:
- A newly created copy buffer using
DEFAULT_BUFSIZE. Nevernull. - Since:
- 9.3.6
-
copyInputStreamToOutputStream
@Nonnull @Deprecated public static ESuccess copyInputStreamToOutputStream(@WillClose @Nullable InputStream aIS, @WillNotClose @Nullable OutputStream aOS, @Nonnull @Nonempty byte[] aBuffer, @Nullable MutableLong aCopyByteCount, @Nullable Long aLimit)
Deprecated.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!aBuffer- The buffer to use. May not benull.aCopyByteCount- An optional mutable long object that will receive the total number of copied bytes. Note: and optional old value is overwritten!aLimit- An optional maximum number of bytes to copied from the input stream to the output stream. May benullto indicate no limit, meaning all bytes are copied.- Returns:
if copying took place,ESuccess.SUCCESSotherwiseESuccess.FAILURE
-
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
-
copyInputStreamToOutputStreamWithLimit
@Nonnull public static ESuccess copyInputStreamToOutputStreamWithLimit(@WillClose @Nullable InputStream aIS, @WillNotClose @Nullable OutputStream aOS, @Nonnegative long nLimit)
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!nLimit- The maximum number of bytes to be copied to the output stream. Must be ≥ 0.- Returns:
if copying took place,ESuccess.SUCCESSotherwiseESuccess.FAILURE
-
copyInputStreamToOutputStreamWithLimitAndCloseOS
@Nonnull public static ESuccess copyInputStreamToOutputStreamWithLimitAndCloseOS(@WillClose @Nullable InputStream aIS, @WillClose @Nullable OutputStream aOS, @Nonnegative long nLimit)
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!nLimit- The maximum number of bytes to be copied to the output stream. Must be ≥ 0.- Returns:
if copying took place,ESuccess.SUCCESSotherwiseESuccess.FAILURE
-
copyInputStreamToOutputStream
@Nonnull public static ESuccess copyInputStreamToOutputStream(@WillClose @Nullable InputStream aIS, @WillNotClose @Nullable OutputStream aOS, @Nullable MutableLong aCopyByteCount)
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!aCopyByteCount- An optional mutable long object that will receive the total number of copied bytes. Note: and optional old value is overwritten!- Returns:
if copying took place,ESuccess.SUCCESSotherwiseESuccess.FAILURE
-
copyInputStreamToOutputStream
@Nonnull public static ESuccess copyInputStreamToOutputStream(@WillClose @Nullable InputStream aIS, @WillNotClose @Nullable OutputStream aOS, @Nonnull byte[] aBuffer)
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!aBuffer- The buffer to use. May not benull.- Returns:
if copying took place,ESuccess.SUCCESSotherwiseESuccess.FAILURE
-
copyInputStreamToOutputStream
@Nonnull @Deprecated public static ESuccess copyInputStreamToOutputStream(@WillClose @Nullable InputStream aIS, @WillNotClose @Nullable OutputStream aOS, @Nonnull @Nonempty byte[] aBuffer, @Nullable MutableLong aCopyByteCount)
Deprecated.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!aBuffer- The buffer to use. May not benull.aCopyByteCount- An optional mutable long object that will receive the total number of copied bytes. Note: and optional old value is overwritten!- Returns:
if copying took place,ESuccess.SUCCESSotherwiseESuccess.FAILURE
-
getAvailable
public static int getAvailable(@Nullable InputStream aIS)
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. The Outputstream is already closed. 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. Since v9.3.6 this method returnsnullif copying fails.
-
getAllBytes
@Nullable public static byte[] getAllBytes(@Nullable IHasInputStream aISP)
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
@Nullable public static byte[] getAllBytes(@Nullable @WillClose InputStream aIS)
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.
-
copyReaderToWriter
@Nonnull public static ESuccess copyReaderToWriter(@Nullable Reader aReader, boolean bCloseReader, @Nullable Writer aWriter, boolean bCloseWriter, @Nonnull @Nonempty char[] aBuffer, @Nullable Long aLimit, @Nullable IExceptionCallback<IOException> aExceptionCallback, @Nullable MutableLong aCopyCharCount)
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.bCloseReader-trueto close the reader,falseto keep it open.aWriter- The writer to write to. May benull.bCloseWriter-trueto close the writer,falseto keep it open.aBuffer- The buffer to use. May not benull.aLimit- An optional maximum number of chars to copied from the reader to the writer. May benullto indicate no limit, meaning all chars are copied.aExceptionCallback- The Exception callback to be invoked, if an exception occurs. May not benull.aCopyCharCount- An optional mutable long object that will receive the total number of copied characters. Note: and optional old value is overwritten!- Returns:
if copying took place,ESuccess.SUCCESSotherwiseESuccess.FAILURE
-
createDefaultCopyBufferChars
@Nonnull @ReturnsMutableCopy public static char[] createDefaultCopyBufferChars()
- Returns:
- A newly created copy buffer using
DEFAULT_BUFSIZE. Nevernull. - Since:
- 9.3.6
-
copyReaderToWriter
@Nonnull public static ESuccess copyReaderToWriter(@Nullable @WillClose Reader aReader, @Nullable @WillNotClose Writer aWriter, @Nonnull @Nonempty char[] aBuffer, @Nullable MutableLong aCopyCharCount, @Nullable Long aLimit)
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!aBuffer- The buffer to use. May not benull.aCopyCharCount- An optional mutable long object that will receive the total number of copied characters. Note: and optional old value is overwritten!aLimit- An optional maximum number of chars to copied from the reader to the writer. May benullto indicate no limit, meaning all chars are copied.- Returns:
if copying took place,ESuccess.SUCCESSotherwiseESuccess.FAILURE
-
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
-
copyReaderToWriterWithLimit
@Nonnull public static ESuccess copyReaderToWriterWithLimit(@WillClose @Nullable Reader aReader, @WillNotClose @Nullable Writer aWriter, long nLimit)
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!nLimit- The maximum number of chars to be copied to the writer. Must be ≥ 0.- Returns:
if copying took place,ESuccess.SUCCESSotherwiseESuccess.FAILURE
-
copyReaderToWriterWithLimitAndCloseWriter
@Nonnull public static ESuccess copyReaderToWriterWithLimitAndCloseWriter(@Nullable @WillClose Reader aReader, @Nullable @WillClose Writer aWriter, @Nonnegative long nLimit)
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!nLimit- The maximum number of chars to be copied to the writer. Must be ≥ 0.- Returns:
if copying took place,ESuccess.SUCCESSotherwiseESuccess.FAILURE
-
copyReaderToWriter
@Nonnull public static ESuccess copyReaderToWriter(@WillClose @Nullable Reader aReader, @WillNotClose @Nullable Writer aWriter, @Nullable MutableLong aCopyCharCount)
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!aCopyCharCount- An optional mutable long object that will receive the total number of copied characters. Note: and optional old value is overwritten!- Returns:
if copying took place,ESuccess.SUCCESSotherwiseESuccess.FAILURE
-
copyReaderToWriter
@Nonnull public static ESuccess copyReaderToWriter(@WillClose @Nullable Reader aReader, @WillNotClose @Nullable Writer aWriter, @Nonnull char[] aBuffer)
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!aBuffer- The buffer to use. May not benull.- Returns:
if copying took place,ESuccess.SUCCESSotherwiseESuccess.FAILURE
-
copyReaderToWriter
@Nonnull @Deprecated public static ESuccess copyReaderToWriter(@WillClose @Nullable Reader aReader, @WillNotClose @Nullable Writer aWriter, @Nonnull @Nonempty char[] aBuffer, @Nullable MutableLong aCopyCharCount)
Deprecated.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!aBuffer- The buffer to use. May not benull.aCopyCharCount- An optional mutable long object that will receive the total number of copied characters. Note: and optional old value is overwritten!- Returns:
if copying took place,ESuccess.SUCCESSotherwiseESuccess.FAILURE
-
getCopy
@Nullable public static NonBlockingStringWriter getCopy(@Nonnull @WillClose Reader aReader)
-
getCopyWithLimit
@Nullable public static NonBlockingStringWriter getCopyWithLimit(@Nonnull @WillClose Reader aReader, @Nonnegative long nLimit)
-
getAllCharacters
@Nullable public static char[] getAllCharacters(@Nullable @WillClose Reader aReader)
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
@Nullable public static String getAllCharactersAsString(@Nullable @WillClose Reader aReader)
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
@Nonnull public static NonBlockingStringReader createReader(@Nonnull String sText)
-
createReader
@Nonnull public static NonBlockingStringReader createReader(@Nonnull char[] aChars)
-
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
public static boolean isBuffered(@Nullable InputStream aIS)
-
getBuffered
@Nullable public static InputStream getBuffered(@Nullable InputStream aIS)
-
isBuffered
public static boolean isBuffered(@Nullable OutputStream aOS)
-
getBuffered
@Nullable public static OutputStream getBuffered(@Nullable OutputStream aOS)
-
checkForInvalidFilterInputStream
@Nullable public static InputStream checkForInvalidFilterInputStream(@Nullable InputStream aIS)
-
writeSafeUTF
public static void writeSafeUTF(@Nonnull DataOutput aDO, @Nullable String sStr) throws IOException
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(DataInput)
-
readSafeUTF
@Nullable public static String readSafeUTF(@Nonnull DataInput aDI) throws IOException
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:
writeSafeUTF(DataOutput, String)
-
-