Package org.frankframework.util
Class StreamUtil
java.lang.Object
org.frankframework.util.StreamUtil
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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic long
copyPartialStream
(InputStream in, OutputStream out, long maxBytesToCopy, int chunkSize) static void
copyReaderToWriter
(Reader reader, Writer writer, int chunkSize) static long
copyStream
(InputStream in, OutputStream out, int chunkSize) Copy anInputStream
to theOutputStream
.static InputStream
dontClose
(InputStream stream) static OutputStream
dontClose
(OutputStream stream) static Reader
static void
fileToStream
(String filename, OutputStream output) Copies the content of the specified file to an output stream.static BufferedReader
getCharsetDetectingInputStreamReader
(InputStream inputStream) Return a Reader that reads the InputStream in the character set specified by the BOM.static BufferedReader
getCharsetDetectingInputStreamReader
(InputStream inputStream, String defaultCharset) Return a Reader that reads the InputStream in the character set specified by the BOM.static String
readerToString
(Reader reader, String endOfLineString) static String
readerToString
(Reader reader, String endOfLineString, boolean xmlEncode) Copies the content of a reader into a string, adds specified string to the end of the line, if specified.static String
readerToString
(Reader reader, String endOfLineString, boolean xmlEncode, int initialCapacity) static void
readerToWriter
(Reader reader, Writer writer) Copies the content of a reader to the buffer of a writer.static String
resourceToString
(URL resource) static String
resourceToString
(URL resource, String endOfLineString) static String
resourceToString
(URL resource, String endOfLineString, boolean xmlEncode) static byte[]
streamToByteArray
(InputStream inputStream, boolean skipBOM) static byte[]
streamToBytes
(InputStream inputStream) Writes the content of an input stream to a byte array.static void
streamToFile
(InputStream inputStream, File file) Writes the content of an input stream to a specified file.static void
streamToStream
(InputStream input, OutputStream output) static void
streamToStream
(InputStream input, OutputStream output, byte[] eof) 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.static String
streamToString
(InputStream stream) static String
streamToString
(InputStream stream, String streamEncoding) static String
streamToString
(InputStream stream, String endOfLineString, boolean xmlEncode) static String
streamToString
(InputStream stream, String endOfLineString, String streamEncoding) static String
streamToString
(InputStream stream, String endOfLineString, String streamEncoding, boolean xmlEncode) static void
stringToFile
(String string, String fileName) Writes the string to a file.static InputStream
urlToStream
(URL url, int timeoutMs)
-
Field Details
-
DEFAULT_CHARSET
-
DEFAULT_INPUT_STREAM_ENCODING
-
AUTO_DETECT_CHARSET
- See Also:
-
BUFFER_SIZE
public static final int BUFFER_SIZE- See Also:
-
log
protected static org.apache.logging.log4j.Logger log
-
-
Constructor Details
-
StreamUtil
public StreamUtil()
-
-
Method Details
-
dontClose
-
dontClose
-
dontClose
-
urlToStream
- Throws:
IOException
-
readerToString
- Throws:
IOException
-
streamToString
public static String streamToString(InputStream stream, String endOfLineString, String streamEncoding) throws IOException - Throws:
IOException
-
streamToByteArray
- 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
Copy anInputStream
to theOutputStream
. After copying, theInputStream
is closed but theOutputStream
is not.- Parameters:
in
- TheInputStream
from which to read bytes to copy.out
- TheOutputStream
target to which to write the byte fromin
.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 ofmaxBytesToCopy
of anInputStream
to theOutputStream
. If a negative number is passed, then the full stream is copied. TheInputStream
is not closed.- Parameters:
in
- TheInputStream
from which to read bytes to copy.out
- TheOutputStream
target to which to write the byte fromin
.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
-
fileToStream
Copies the content of the specified file to an output stream.Example:
OutputStream os = new ByteArrayOutputStream Misc.fileToStream(someFileName, os); System.out.println(os.toString) // prints the content of the output stream // that's copied from the file.
- Throws:
IOException
- exception to be thrown if an I/O exception occurs
-
streamToStream
public static void streamToStream(@Nullable InputStream input, @Nonnull OutputStream output) throws IOException - Throws:
IOException
-
streamToStream
public static void streamToStream(InputStream input, OutputStream output, byte[] 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
-
streamToFile
Writes the content of an input stream to a specified file.Example:
String test = "test"; ByteArrayInputStream bais = new ByteArrayInputStream(test.getBytes()); Misc.streamToFile(bais, file); // "test" copied inside the file.
- Throws:
IOException
- exception to be thrown if an I/O exception occurs
-
streamToBytes
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
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, 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, String endOfLineString, boolean xmlEncode, int initialCapacity) throws IOException - Throws:
IOException
-
streamToString
- Returns:
- String that's included in the stream
- Throws:
IOException
- See Also:
-
streamToString
- Throws:
IOException
- See Also:
-
streamToString
public static String streamToString(InputStream stream, String endOfLineString, boolean xmlEncode) throws IOException - Throws:
IOException
- See Also:
-
streamToString
public static String streamToString(InputStream stream, String endOfLineString, String streamEncoding, boolean xmlEncode) throws IOException - Throws:
IOException
- See Also:
-
resourceToString
public static String resourceToString(URL resource, String endOfLineString, boolean xmlEncode) throws IOException - Throws:
IOException
- See Also:
-
resourceToString
- Throws:
IOException
- See Also:
-
resourceToString
- Throws:
IOException
- See Also:
-
stringToFile
Writes the string to a file.- Throws:
IOException
-