Class StreamUtil

java.lang.Object
org.frankframework.util.StreamUtil

@NullMarked public class StreamUtil extends Object
Functions to read and write from one stream to another. Be careful: Util classes should NOT depend on the Servlet-API
Author:
Gerrit van Brakel
  • Field Details

    • DEFAULT_CHARSET

      public static final Charset DEFAULT_CHARSET
    • DEFAULT_INPUT_STREAM_ENCODING

      public static final String DEFAULT_INPUT_STREAM_ENCODING
    • AUTO_DETECT_CHARSET

      public static final String AUTO_DETECT_CHARSET
      See Also:
    • BUFFER_SIZE

      public static final int BUFFER_SIZE
      See Also:
  • Method Details

    • readerToString

      public static String readerToString(Reader reader, @Nullable String endOfLineString) throws IOException
      Throws:
      IOException
    • streamToString

      public static String streamToString(InputStream stream, @Nullable String endOfLineString, String streamEncoding) throws IOException
      Throws:
      IOException
    • getCharsetDetectingInputStreamReader

      public static BufferedReader getCharsetDetectingInputStreamReader(InputStream inputStream) throws IOException
      Return a Reader that reads the InputStream in the character set specified by the BOM. If no BOM is found, the default character set UTF-8 is used.
      Throws:
      IOException
    • getCharsetDetectingInputStreamReader

      public static BufferedReader getCharsetDetectingInputStreamReader(InputStream inputStream, String defaultCharset) throws IOException
      Return a Reader that reads the InputStream in the character set specified by the BOM. If no BOM is found, a default character set is used.
      Throws:
      IOException
    • copyStream

      public static long copyStream(InputStream in, OutputStream out, int chunkSize) throws IOException
      Copy an InputStream to the OutputStream. After copying, the InputStream is closed but the OutputStream is not.
      Parameters:
      in - The InputStream from which to read bytes to copy.
      out - The OutputStream target to which to write the byte from in.
      chunkSize - The size of the buffer used for copying.
      Returns:
      The number of bytes copied.
      Throws:
      IOException - Thrown if any exception occurs while reading or writing from either stream.
    • copyPartialStream

      public static long copyPartialStream(InputStream in, OutputStream out, long maxBytesToCopy, int chunkSize) throws IOException
      Copy a maximum of maxBytesToCopy of an InputStream to the OutputStream. If a negative number is passed, then the full stream is copied. The InputStream is not closed.
      Parameters:
      in - The InputStream from which to read bytes to copy.
      out - The OutputStream target to which to write the byte from in.
      maxBytesToCopy - The maximum nr of bytes to copy. If a negative number, then the entire InputStream will be copied.
      chunkSize - The size of the buffer used for copying.
      Throws:
      IOException - Thrown if any exception occurs while reading or writing from either stream.
    • copyReaderToWriter

      public static void copyReaderToWriter(Reader reader, Writer writer, int chunkSize) throws IOException
      Throws:
      IOException
    • copyPartialReader

      public static long copyPartialReader(Reader in, Writer out, long maxCharsToCopy, int chunkSize) throws IOException
      Throws:
      IOException
    • streamToStream

      public static void streamToStream(@Nullable InputStream input, @NonNull OutputStream output) throws IOException
      Throws:
      IOException
    • streamToStream

      public static void streamToStream(@Nullable InputStream input, OutputStream output, byte @Nullable [] eof) throws IOException
      Writes the content of an input stream to an output stream by copying the buffer of input stream to the buffer of the output stream. If eof is specified, appends the eof(could represent a new line) to the outputstream Closes the input stream if specified.

      Example:

              String test = "test";
              ByteArrayInputStream bais = new ByteArrayInputStream(test.getBytes());
              OutputStream baos = new ByteArrayOutputStream();
              Misc.streamToStream(bais, baos);
              System.out.println(baos.toString()); // prints "test"
          

      Throws:
      IOException - exception to be thrown if an I/O exception occurs
    • streamToBytes

      public static byte[] streamToBytes(InputStream inputStream) throws IOException
      Writes the content of an input stream to a byte array.

      Example:

              String test = "test";
              ByteArrayInputStream bais = new ByteArrayInputStream(test.getBytes());
              byte[] arr = Misc.streamToBytes(bais);
              System.out.println(new String(arr, StandardCharsets.UTF_8)); // prints "test"
          

      Throws:
      IOException
    • readerToWriter

      public static void readerToWriter(Reader reader, Writer writer) throws IOException
      Copies the content of a reader to the buffer of a writer.

      Example:

              Reader reader = new StringReader("test");
              Writer writer = new StringWriter();
              Misc.readerToWriter(reader, writer, true);
              System.out.println(writer.toString)); // prints "test"
          

      Throws:
      IOException
    • readerToString

      public static String readerToString(Reader reader, @Nullable String endOfLineString, boolean xmlEncode) throws IOException
      Copies the content of a reader into a string, adds specified string to the end of the line, if specified.

      Example:

              Reader r = new StringReader(" WeAreFrank' \n";
              String s = Misc.readerToString(r, "!!", true);
              System.out.println(s);
              // prints "<root> WeAreFrank!!</root>"
          

      Parameters:
      xmlEncode - if set to true, applies XML encodings to the content of the reader
      Throws:
      IOException
    • readerToString

      public static String readerToString(Reader reader, @Nullable String endOfLineString, boolean xmlEncode, int initialCapacity) throws IOException
      Throws:
      IOException
    • streamToString

      public static String streamToString(InputStream stream) throws IOException
      Convert the contents of the InputStream to a String, using the DEFAULT_INPUT_STREAM_ENCODING and no encoding of XML special characters.
      Returns:
      String that's included in the stream
      Throws:
      IOException
      See Also:
    • streamToString

      public static String streamToString(InputStream stream, String streamEncoding) throws IOException
      Convert the contents of the InputStream to a String, using the given encoding and no encoding of XML special characters.
      Throws:
      IOException
      See Also:
    • streamToString

      public static String streamToString(InputStream stream, @Nullable String endOfLineString, boolean xmlEncode) throws IOException
      Convert the contents of the InputStream to a String, using the DEFAULT_INPUT_STREAM_ENCODING and optional end-of-line string and optionally encoding of XML special characters.
      Throws:
      IOException
      See Also:
    • streamToString

      public static String streamToString(InputStream stream, @Nullable String endOfLineString, String streamEncoding, boolean xmlEncode) throws IOException
      Convert the contents of the InputStream to a String, using the given encoding, end-of-line string and optionally encoding of XML special characters.
      Throws:
      IOException
      See Also:
    • resourceToString

      public static String resourceToString(URL resource) throws IOException
      Load a classpath resource from the given classpath URL and return the contents as a string.
      Throws:
      IOException
      See Also:
    • resourceToString

      public static String resourceToString(URL resource, @Nullable String endOfLineString) throws IOException
      Load a classpath resource from the given classpath URL and return the contents as a string using the optional endOfLineString if given.
      Throws:
      IOException
      See Also:
    • resourceToString

      public static String resourceToString(URL resource, @Nullable String endOfLineString, boolean xmlEncode) throws IOException
      Load a classpath resource from the given classpath URL and return the contents as a string using the optional endOfLineString if given and optionally encoding XML special characters.
      Throws:
      IOException
      See Also: