Class Streams

java.lang.Object
com.mastfrog.util.streams.Streams

public final class Streams extends Object
Utilities methods for working with input and output streams.
  • Method Details

    • channel

      public GeneralByteChannel channel(byte[] bytes)
      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

      public static int copy(InputStream in, OutputStream out, int bufferSize) throws IOException
      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 stream
      out - The output stream
      bufferSize - The number of bytes to fetch in a single loop
      Returns:
      Number of bytes copied from one stream to the other
      Throws:
      IOException
    • copy

      public static int copy(InputStream in, OutputStream out) throws IOException
      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 stream
      out - The output stream
      Returns:
      Number of bytes copied from one stream to the other
      Throws:
      IOException
    • copy

      public static int copy(InputStream in, OutputStream out, OutputStream otherOut) throws IOException
      Writes the input stream to two output streams.
      Parameters:
      in - The input stream
      out - The output stream
      otherOut - Another output stream
      Returns:
      the number of bytes copied
      Throws:
      IOException - if something goes wrong
    • readUTF8String

      public static String readUTF8String(InputStream in) throws IOException
      Read a UTF-8 string from a stream.
      Parameters:
      in - An input stream
      Returns:
      A string
      Throws:
      IOException - If something goes wrong
    • readAsciiString

      public static String readAsciiString(InputStream in) throws IOException
      Read an ASCII string from a stream.
      Parameters:
      in - An input stream
      Returns:
      A string
      Throws:
      IOException - If something goes wrong
    • readResourceAsUTF8

      public static String readResourceAsUTF8(Class<?> relativeTo, String filename) throws IOException
      Read a UTF-8 string from a resource relative to a class file.
      Parameters:
      relativeTo - The class
      filename - The file name or relative path
      Returns:
      A string, or null if no such resource exists
      Throws:
      IOException - If something goes wrong
    • readString

      public static String readString(InputStream in) throws IOException
      Reads a stream as a string.
      Parameters:
      in - The input stream
      Returns:
      The string
      Throws:
      IOException
    • readString

      public static String readString(InputStream in, int bufferSize) throws IOException
      Read a string with a fixed buffer size
      Parameters:
      in - An input stream
      bufferSize - A buffer size, non-negative; if zero, no buffering
      Returns:
      A string
      Throws:
      IOException - if something goes wrong
    • readString

      public static String readString(InputStream in, String charset, int bufferSize) throws IOException
      Read a string with a specified charset and a fixed buffer size
      Parameters:
      in - An input stream
      charset - A character set
      bufferSize - A buffer size, non-negative; if zero, no buffering
      Returns:
      A string
      Throws:
      IOException - if something goes wrong
    • readString

      public static String readString(InputStream in, Charset charset, int bufferSize) throws IOException
      Read a string with a specified charset and a fixed buffer size
      Parameters:
      in - An input stream
      charset - A character set
      bufferSize - A buffer size, non-negative; if zero, no buffering
      Returns:
      A string
      Throws:
      IOException - if something goes wrong
    • readString

      public static String readString(InputStream in, String charset) throws IOException
      Read a string with a specified charset
      Parameters:
      in - An input stream
      charset - A character set
      Returns:
      A string
      Throws:
      IOException - if something goes wrong
    • readString

      public static String readString(InputStream in, Charset charset) throws IOException
      Read a string with a specified charset
      Parameters:
      in - An input stream
      charset - A character set
      Returns:
      A string
      Throws:
      IOException - if something goes wrong
    • readSql

      public static String[] readSql(InputStream is)
      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

      public static String readString(InputStream in, CharSequence encoding) throws IOException
      Reads a string using a character encoding.
      Parameters:
      in - The input
      encoding - The character encoding of the input data
      Returns:
      The string
      Throws:
      IOException
    • readString

      public static String readString(Reader in) throws IOException
      Reads all input from a reader into a string.
      Parameters:
      in - The input
      Returns:
      The string
      Throws:
      IOException
    • locate

      public static InputStream[] locate(String location)
      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

      public static void writeString(String s, File to) throws IOException
      Throws:
      IOException
    • asInputStream

      public static InputStream asInputStream(ByteBuffer buf)
      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 calling asInputStream(true, buf).
      Parameters:
      buf - A ByteBuffer
      Returns:
      An InputStream for reading the byte buffer
    • asInputStream

      public static InputStream asInputStream(boolean rewind, ByteBuffer buf)
      Get a ByteBuffer as an InputStream. The passed buffer will be wrapped as a read-only buffer. If rewind is 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, call buf.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

      public static InputStream asInputStream(ReadableByteChannel channel)
    • asByteChannel

      public static ReadableByteChannel asByteChannel(InputStream in)
    • asByteBuffer

      public static ByteBuffer asByteBuffer(InputStream in) throws IOException
      Throws:
      IOException
    • nullOutputStream

      public static OutputStream nullOutputStream()
    • nullWriter

      public static Writer nullWriter()
      Get a writer that black-holes anything printed to it.
      Returns:
      A writer
    • nullPrintStream

      public static PrintStream nullPrintStream()
      Get a print-stream that black-holes anything printed to it.
      Returns:
      A print stream
    • asInputStream

      public static InputStream asInputStream(Iterable<ByteBuffer> buffers)
    • forByteBuffers

      public static InputStream forByteBuffers(ByteBuffer... iter)
    • forByteBuffers

      public static InputStream forByteBuffers(Iterable<ByteBuffer> iter)
    • asOutputStream

      public static OutputStream asOutputStream(ByteBuffer buffer)
    • copyFile

      public static void copyFile(File orig, File nue, boolean create) throws IOException
      Throws:
      IOException
    • streamForURL

      public static InputStream streamForURL(URL url)
    • teeSystemOut

      public static PrintStream teeSystemOut(PrintStream other)
    • tee

      public static PrintStream tee(PrintStream... streams)
    • link

      public static void link(File original, File target) throws IOException
      Create a relative symbolic link between two files
      Parameters:
      original - The original
      target - The target, which should not yet exist
      Throws:
      IOException - if something goes wrong