Class StreamHelper

java.lang.Object
com.helger.base.io.stream.StreamHelper

@Immutable public class StreamHelper extends Object
  • Field Details

    • DEFAULT_BUFSIZE

      public static final int DEFAULT_BUFSIZE
      buffer size for copy operations
      See Also:
    • END_OF_STRING_MARKER

      public static final int END_OF_STRING_MARKER
      See Also:
  • Constructor Details

    • StreamHelper

      protected StreamHelper()
  • Method Details

    • createDefaultCopyBufferBytes

      @Nonnull @ReturnsMutableCopy public static byte[] createDefaultCopyBufferBytes()
      Returns:
      A newly created copy buffer using DEFAULT_BUFSIZE. Never null.
      Since:
      9.3.6
    • createDefaultCopyBufferChars

      @Nonnull @ReturnsMutableCopy public static char[] createDefaultCopyBufferChars()
      Returns:
      A newly created copy buffer using DEFAULT_BUFSIZE. Never null.
      Since:
      9.3.6
    • 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)
    • 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 be null.
      Returns:
      true if 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 be null.
      Returns:
      true if it is a known EOF exception class.
    • internalGetPropagatableException

      @Nullable protected static Exception internalGetPropagatableException(@Nonnull Exception ex)
    • 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 be null.
      Returns:
      ESuccess.SUCCESS if the object was successfully closed.
    • close

      @Nonnull public static ESuccess close(@Nullable @WillClose AutoCloseable aCloseable)
      Close the passed stream by encapsulating the declared IOException. If the passed object also implements the Flushable interface, it is tried to be flushed before it is closed.
      Parameters:
      aCloseable - The object to be closed. May be null.
      Returns:
      ESuccess if the object was successfully closed.
    • flush

      @Nonnull public static ESuccess flush(@Nullable Flushable aFlushable)
      Flush the passed object encapsulating the declared IOException.
      Parameters:
      aFlushable - The flushable to be flushed. May be null.
      Returns:
      ESuccess.SUCCESS if the object was successfully flushed.
    • 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)
    • isBuffered

      public static boolean isBuffered(@Nullable Reader aReader)
    • getBuffered

      @Nullable public static Reader getBuffered(@Nullable Reader aReader)
    • isBuffered

      public static boolean isBuffered(@Nullable Writer aWriter)
    • getBuffered

      @Nullable public static Writer getBuffered(@Nullable Writer aWriter)
    • 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 be null. Automatically closed!
      aOS - The output stream to write to. May be null. Not automatically closed!
      Returns:
      ESuccess.SUCCESS if copying took place, ESuccess.FAILURE otherwise
    • 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 be null. Automatically closed!
      aOS - The output stream to write to. May be null. Automatically closed!
      Returns:
      ESuccess.SUCCESS if copying took place, ESuccess.FAILURE otherwise
    • copyByteStream

      @Nonnull public static StreamHelper.CopyByteStreamBuilder copyByteStream()
      Returns:
      A new StreamHelper.CopyByteStreamBuilder. Never null.
    • 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 be null.
      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 be null.
      Returns:
      A new NonBlockingByteArrayOutputStream with all available content inside. The OutputStream must be closed by the caller since v10. Since v9.3.6 this method returns null if 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 be null.
      nLimit - The maximum number of bytes to be copied to the output stream. Must be ≥ 0.
      Returns:
      A new NonBlockingByteArrayOutputStream with all available content inside. The OutputStream must be closed by the caller since v10. Since v9.3.6 this method returns null if 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 be null .
      Returns:
      The byte array or null if the parameter or the resolved input stream is null.
    • 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 be null.
      Returns:
      The byte array or null if the input stream is null.
    • 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 be null .
      aCharset - The charset to use. May not be null .
      Returns:
      The String or null if the parameter or the resolved input stream is null.
    • 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 be null.
      aCharset - The charset to use. May not be null .
      Returns:
      The String or null if the input stream is null.
    • 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 be null. Automatically closed!
      aWriter - The writer to write to. May be null. Not automatically closed!
      Returns:
      ESuccess.SUCCESS if copying took place, ESuccess.FAILURE otherwise
    • 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 be null. Automatically closed!
      aWriter - The writer to write to. May be null. Automatically closed!
      Returns:
      ESuccess.SUCCESS if copying took place, ESuccess.FAILURE otherwise
    • copyCharStream

      @Nonnull public static StreamHelper.CopyCharStreamBuilder copyCharStream()
      Returns:
      A new StreamHelper.CopyCharStreamBuilder. Never null.
    • 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 be null.
      Returns:
      The character array or null if the reader is null.
    • 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 be null.
      Returns:
      The character array or null if the reader is null.
    • writeStream

      @Nonnull public static ESuccess writeStream(@WillClose @Nonnull OutputStream aOS, @Nonnull byte[] aBuf, @Nonnegative int nOfs, @Nonnegative int nLen)
      Write bytes to an OutputStream.
      Parameters:
      aOS - The output stream to write to. May not be null. Is closed independent of error or success.
      aBuf - The byte array from which is to be written. May not be null.
      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 an OutputStream.
      Parameters:
      aOS - The output stream to write to. May not be null. Is closed independent of error or success.
      aBuf - The byte array to be written. May not be null.
      Returns:
      ESuccess
    • writeStream

      @Nonnull public static ESuccess writeStream(@WillClose @Nonnull OutputStream aOS, @Nonnull String sContent, @Nonnull Charset aCharset)
      Write bytes to an OutputStream.
      Parameters:
      aOS - The output stream to write to. May not be null. Is closed independent of error or success.
      sContent - The string to be written. May not be null.
      aCharset - The charset to be used, to convert the String to a byte array.
      Returns:
      ESuccess
    • 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 be null.
      aBuffer - The buffer to write to. May not be null. 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 be null.
      aBuffer - The buffer to write to. May not be null. 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
    • checkForInvalidFilterInputStream

      @Nullable public static InputStream checkForInvalidFilterInputStream(@Nullable InputStream aIS)
    • writeSafeUTF

      public static void writeSafeUTF(@Nonnull DataOutput aDO, @Nullable String sStr) throws IOException
      Because DataOutputStream.writeUTF(String) has a limit of 64KB this methods provides a similar solution but simply writing the bytes.
      Parameters:
      aDO - DataOutput to write to. May not be null.
      sStr - The string to be written. May be null.
      Throws:
      IOException - on write error
      See Also:
    • readSafeUTF

      @Nullable public static String readSafeUTF(@Nonnull DataInput aDI) throws IOException
      Because DataOutputStream.writeUTF(String) has a limit of 64KB this methods provides a similar solution for reading like DataInputStream.readUTF() but what was written in writeSafeUTF(DataOutput, String).
      Parameters:
      aDI - DataInput to read from. May not be null.
      Returns:
      The read string. May be null.
      Throws:
      IOException - on read error
      See Also: