Package com.mastfrog.util.streams
Class Streams
java.lang.Object
com.mastfrog.util.streams.Streams
Utilities methods for working with input and output streams.
-
Method Summary
Modifier and TypeMethodDescriptionstatic ByteBufferstatic ReadableByteChannelstatic InputStreamasInputStream(boolean rewind, ByteBuffer buf) Get a ByteBuffer as an InputStream.static InputStreamasInputStream(Iterable<ByteBuffer> buffers) static InputStreamasInputStream(ByteBuffer buf) Get a ByteBuffer as an InputStream.static InputStreamasInputStream(ReadableByteChannel channel) static OutputStreamasOutputStream(ByteBuffer buffer) channel(byte[] bytes) Create a ReadableByteChannel over an array of bytes, for impedance matching array-oriented APIs to NIO channels.static intcopy(InputStream in, OutputStream out) Writes the input stream to the output stream.static intcopy(InputStream in, OutputStream out, int bufferSize) Writes the input stream to the output stream.static intcopy(InputStream in, OutputStream out, OutputStream otherOut) Writes the input stream to two output streams.static voidstatic InputStreamforByteBuffers(Iterable<ByteBuffer> iter) static InputStreamforByteBuffers(ByteBuffer... iter) static voidCreate a relative symbolic link between two filesstatic InputStream[]Locate an input stream for the provided location, possibly multiple if it is a resource from the classpath that can be found at multiple locations.static OutputStreamstatic PrintStreamGet a print-stream that black-holes anything printed to it.static WriterGet a writer that black-holes anything printed to it.static StringRead an ASCII string from a stream.static StringreadResourceAsUTF8(Class<?> relativeTo, String filename) Read a UTF-8 string from a resource relative to a class file.static String[]readSql(InputStream is) Reads SQL from an input stream.static StringReads a stream as a string.static StringreadString(InputStream in, int bufferSize) Read a string with a fixed buffer sizestatic StringreadString(InputStream in, CharSequence encoding) Reads a string using a character encoding.static StringreadString(InputStream in, String charset) Read a string with a specified charsetstatic StringreadString(InputStream in, String charset, int bufferSize) Read a string with a specified charset and a fixed buffer sizestatic StringreadString(InputStream in, Charset charset) Read a string with a specified charsetstatic StringreadString(InputStream in, Charset charset, int bufferSize) Read a string with a specified charset and a fixed buffer sizestatic StringreadString(Reader in) Reads all input from a reader into a string.static StringRead a UTF-8 string from a stream.static InputStreamstreamForURL(URL url) static PrintStreamtee(PrintStream... streams) static PrintStreamteeSystemOut(PrintStream other) static voidwriteString(String s, File to)
-
Method Details
-
channel
Create a ReadableByteChannel over an array of bytes, for impedance matching array-oriented APIs to NIO channels.- Parameters:
bytes- An array of bytes- Returns:
- A channel
-
copy
Writes the input stream to the output stream. Input is done without a Reader object, meaning that the input is copied in its raw form.- Parameters:
in- The input streamout- The output streambufferSize- The number of bytes to fetch in a single loop- Returns:
- Number of bytes copied from one stream to the other
- Throws:
IOException
-
copy
Writes the input stream to the output stream. Input is done without a Reader object, meaning that the input is copied in its raw form.- Parameters:
in- The input streamout- The output stream- Returns:
- Number of bytes copied from one stream to the other
- Throws:
IOException
-
copy
Writes the input stream to two output streams.- Parameters:
in- The input streamout- The output streamotherOut- Another output stream- Returns:
- the number of bytes copied
- Throws:
IOException- if something goes wrong
-
readUTF8String
Read a UTF-8 string from a stream.- Parameters:
in- An input stream- Returns:
- A string
- Throws:
IOException- If something goes wrong
-
readAsciiString
Read an ASCII string from a stream.- Parameters:
in- An input stream- Returns:
- A string
- Throws:
IOException- If something goes wrong
-
readResourceAsUTF8
Read a UTF-8 string from a resource relative to a class file.- Parameters:
relativeTo- The classfilename- The file name or relative path- Returns:
- A string, or null if no such resource exists
- Throws:
IOException- If something goes wrong
-
readString
Reads a stream as a string.- Parameters:
in- The input stream- Returns:
- The string
- Throws:
IOException
-
readString
Read a string with a fixed buffer size- Parameters:
in- An input streambufferSize- A buffer size, non-negative; if zero, no buffering- Returns:
- A string
- Throws:
IOException- if something goes wrong
-
readString
Read a string with a specified charset and a fixed buffer size- Parameters:
in- An input streamcharset- A character setbufferSize- A buffer size, non-negative; if zero, no buffering- Returns:
- A string
- Throws:
IOException- if something goes wrong
-
readString
Read a string with a specified charset and a fixed buffer size- Parameters:
in- An input streamcharset- A character setbufferSize- A buffer size, non-negative; if zero, no buffering- Returns:
- A string
- Throws:
IOException- if something goes wrong
-
readString
Read a string with a specified charset- Parameters:
in- An input streamcharset- A character set- Returns:
- A string
- Throws:
IOException- if something goes wrong
-
readString
Read a string with a specified charset- Parameters:
in- An input streamcharset- A character set- Returns:
- A string
- Throws:
IOException- if something goes wrong
-
readSql
Reads SQL from an input stream. Returns statements separated by semi-colons, skipping comments. The input stream will be closed by this invocation.- Parameters:
is- input stream- Returns:
- SQL array of sql strings, possibly empty, never null
-
readString
Reads a string using a character encoding.- Parameters:
in- The inputencoding- The character encoding of the input data- Returns:
- The string
- Throws:
IOException
-
readString
Reads all input from a reader into a string.- Parameters:
in- The input- Returns:
- The string
- Throws:
IOException
-
locate
Locate an input stream for the provided location, possibly multiple if it is a resource from the classpath that can be found at multiple locations.- Parameters:
location- either a URL or a path that can be found on the class path or as a file- Returns:
- input streams or null if not found
-
writeString
- Throws:
IOException
-
asInputStream
Get a ByteBuffer as an InputStream. The passed buffer will be wrapped as a read-only buffer. The position, mark and limit of the passed buffer will remain unmodified, and the returned InputStream will read the byte buffer from position 0, not its current position. Equivalent of callingasInputStream(true, buf).- Parameters:
buf- A ByteBuffer- Returns:
- An InputStream for reading the byte buffer
-
asInputStream
Get a ByteBuffer as an InputStream. The passed buffer will be wrapped as a read-only buffer. Ifrewindis true, the passed buffer is rewound to position 0 before creating the stream. The mark and position of the buffer are modified by operations on the stream - if you don't want that, callbuf.duplicate()to create a buffer to pass here.- Parameters:
rewind- If true, rewind the buffer (modifying it)buf- A ByteBuffer- Returns:
- An InputStream for reading the byte buffer
-
asInputStream
-
asByteChannel
-
asByteBuffer
- Throws:
IOException
-
nullOutputStream
-
nullWriter
Get a writer that black-holes anything printed to it.- Returns:
- A writer
-
nullPrintStream
Get a print-stream that black-holes anything printed to it.- Returns:
- A print stream
-
asInputStream
-
forByteBuffers
-
forByteBuffers
-
asOutputStream
-
copyFile
- Throws:
IOException
-
streamForURL
-
teeSystemOut
-
tee
-
link
Create a relative symbolic link between two files- Parameters:
original- The originaltarget- The target, which should not yet exist- Throws:
IOException- if something goes wrong
-