Class Streams

java.lang.Object
org.elasticsearch.common.io.Streams

public abstract class Streams extends Object
Simple utility methods for file and stream copying. All copy methods use a block size of 4096 bytes, and close all affected streams when done.

Mainly for use within the framework, but also useful for application code.

  • Field Details

    • BUFFER_SIZE

      public static final int BUFFER_SIZE
      See Also:
      Constant Field Values
    • NULL_OUTPUT_STREAM

      public static final OutputStream NULL_OUTPUT_STREAM
      OutputStream that just throws all the bytes away
  • Constructor Details

    • Streams

      public Streams()
  • Method Details

    • copy

      public static void copy(byte[] in, OutputStream out) throws IOException
      Copy the contents of the given byte array to the given OutputStream. Closes the stream when done.
      Parameters:
      in - the byte array to copy from
      out - the OutputStream to copy to
      Throws:
      IOException - in case of I/O errors
    • copy

      public static int copy(Reader in, Writer out) throws IOException
      Copy the contents of the given Reader to the given Writer. Closes both when done.
      Parameters:
      in - the Reader to copy from
      out - the Writer to copy to
      Returns:
      the number of characters copied
      Throws:
      IOException - in case of I/O errors
    • copy

      public static void copy(String in, Writer out) throws IOException
      Copy the contents of the given String to the given output Writer. Closes the write when done.
      Parameters:
      in - the String to copy from
      out - the Writer to copy to
      Throws:
      IOException - in case of I/O errors
    • copyToString

      public static String copyToString(Reader in) throws IOException
      Copy the contents of the given Reader into a String. Closes the reader when done.
      Parameters:
      in - the reader to copy from
      Returns:
      the String that has been copied to
      Throws:
      IOException - in case of I/O errors
    • readFully

      public static int readFully(Reader reader, char[] dest) throws IOException
      Throws:
      IOException
    • readFully

      public static int readFully(Reader reader, char[] dest, int offset, int len) throws IOException
      Throws:
      IOException
    • readFully

      public static int readFully(InputStream reader, byte[] dest) throws IOException
      Throws:
      IOException
    • readFully

      public static int readFully(InputStream reader, byte[] dest, int offset, int len) throws IOException
      Throws:
      IOException
    • consumeFully

      public static long consumeFully(InputStream inputStream) throws IOException
      Fully consumes the input stream, throwing the bytes away. Returns the number of bytes consumed.
      Throws:
      IOException
    • readAllLines

      public static List<String> readAllLines(InputStream input) throws IOException
      Throws:
      IOException
    • readAllLines

      public static void readAllLines(InputStream input, Consumer<String> consumer) throws IOException
      Throws:
      IOException
    • noCloseStream

      public static InputStream noCloseStream(InputStream stream)
      Wraps an InputStream such that it's close method becomes a noop
      Parameters:
      stream - InputStream to wrap
      Returns:
      wrapped InputStream
    • noCloseStream

      public static OutputStream noCloseStream(OutputStream stream)
      Wraps an OutputStream such that it's close method becomes a noop
      Parameters:
      stream - OutputStream to wrap
      Returns:
      wrapped OutputStream
    • flushOnCloseStream

      public static BytesStream flushOnCloseStream(BytesStream os)
      Wraps the given BytesStream in a StreamOutput that simply flushes when close is called.
    • readFully

      public static BytesReference readFully(InputStream in) throws IOException
      Reads all bytes from the given InputStream and closes it afterwards.
      Throws:
      IOException
    • limitStream

      public static InputStream limitStream(InputStream in, long limit)
      Limits the given input stream to the provided number of bytes