Class Streams

org.elasticsearch.common.io.Streams

public abstract class Streams extends
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 Summary

    Fields
    Modifier and Type
    Field
    Description
    static int
     
    static
    OutputStream that just throws all the bytes away
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static long
     inputStream)
    Fully consumes the input stream, throwing the bytes away.
    static void
    copy​(byte[] in,  out)
    Copy the contents of the given byte array to the given OutputStream.
    static int
     in,  out)
    Copy the contents of the given Reader to the given Writer.
    static void
     in,  out)
    Copy the contents of the given String to the given output Writer.
    static
     in)
    Copy the contents of the given Reader into a String.
    Wraps the given BytesStream in a StreamOutput that simply flushes when close is called.
    static
     in, long limit)
    Limits the given input stream to the provided number of bytes
    static
     stream)
    Wraps an such that it's close method becomes a noop
    static
     stream)
    Wraps an such that it's close method becomes a noop
    static <>
     input)
     
    static void
     input, <> consumer)
     
     in)
    Reads all bytes from the given and closes it afterwards.
    static int
     reader, byte[] dest)
     
    static int
     reader, byte[] dest, int offset, int len)
     
    static int
     reader, char[] dest)
     
    static int
     reader, char[] dest, int offset, int len)
     

    Methods inherited from class java.lang.

    , , , , , , , , , ,
  • Field Details

    • BUFFER_SIZE

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

      public static final  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,  out) throws
      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:
      - in case of I/O errors
    • copy

      public static int copy( in,  out) throws
      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:
      - in case of I/O errors
    • copy

      public static void copy( in,  out) throws
      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:
      - in case of I/O errors
    • copyToString

      public static  copyToString( in) throws
      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:
      - in case of I/O errors
    • readFully

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

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

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

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

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

      public static <> readAllLines( input) throws
      Throws:
    • readAllLines

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

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

      public static  noCloseStream( stream)
      Wraps an 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  in) throws
      Reads all bytes from the given and closes it afterwards.
      Throws:
    • limitStream

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