Package org.elasticsearch.common.io
Class Streams
java.lang.Object
org.elasticsearch.common.io.Streams
public abstract class Streams
extends java.lang.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 Summary
Fields Modifier and Type Field Description static int
BUFFER_SIZE
static java.io.OutputStream
NULL_OUTPUT_STREAM
OutputStream that just throws all the bytes away -
Constructor Summary
Constructors Constructor Description Streams()
-
Method Summary
Modifier and Type Method Description static long
consumeFully(java.io.InputStream inputStream)
Fully consumes the input stream, throwing the bytes away.static void
copy(byte[] in, java.io.OutputStream out)
Copy the contents of the given byte array to the given OutputStream.static int
copy(java.io.Reader in, java.io.Writer out)
Copy the contents of the given Reader to the given Writer.static void
copy(java.lang.String in, java.io.Writer out)
Copy the contents of the given String to the given output Writer.static java.lang.String
copyToString(java.io.Reader in)
Copy the contents of the given Reader into a String.static BytesStream
flushOnCloseStream(BytesStream os)
Wraps the givenBytesStream
in aStreamOutput
that simply flushes when close is called.static java.io.InputStream
limitStream(java.io.InputStream in, long limit)
Limits the given input stream to the provided number of bytesstatic java.io.InputStream
noCloseStream(java.io.InputStream stream)
Wraps anInputStream
such that it'sclose
method becomes a noopstatic java.util.List<java.lang.String>
readAllLines(java.io.InputStream input)
static void
readAllLines(java.io.InputStream input, java.util.function.Consumer<java.lang.String> consumer)
static BytesReference
readFully(java.io.InputStream in)
Reads all bytes from the givenInputStream
and closes it afterwards.static int
readFully(java.io.InputStream reader, byte[] dest)
static int
readFully(java.io.InputStream reader, byte[] dest, int offset, int len)
static int
readFully(java.io.Reader reader, char[] dest)
static int
readFully(java.io.Reader reader, char[] dest, int offset, int len)
-
Field Details
-
BUFFER_SIZE
public static final int BUFFER_SIZE- See Also:
- Constant Field Values
-
NULL_OUTPUT_STREAM
public static final java.io.OutputStream NULL_OUTPUT_STREAMOutputStream that just throws all the bytes away
-
-
Constructor Details
-
Streams
public Streams()
-
-
Method Details
-
copy
public static void copy(byte[] in, java.io.OutputStream out) throws java.io.IOExceptionCopy the contents of the given byte array to the given OutputStream. Closes the stream when done.- Parameters:
in
- the byte array to copy fromout
- the OutputStream to copy to- Throws:
java.io.IOException
- in case of I/O errors
-
copy
public static int copy(java.io.Reader in, java.io.Writer out) throws java.io.IOExceptionCopy the contents of the given Reader to the given Writer. Closes both when done.- Parameters:
in
- the Reader to copy fromout
- the Writer to copy to- Returns:
- the number of characters copied
- Throws:
java.io.IOException
- in case of I/O errors
-
copy
public static void copy(java.lang.String in, java.io.Writer out) throws java.io.IOExceptionCopy the contents of the given String to the given output Writer. Closes the write when done.- Parameters:
in
- the String to copy fromout
- the Writer to copy to- Throws:
java.io.IOException
- in case of I/O errors
-
copyToString
public static java.lang.String copyToString(java.io.Reader in) throws java.io.IOExceptionCopy 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:
java.io.IOException
- in case of I/O errors
-
readFully
public static int readFully(java.io.Reader reader, char[] dest) throws java.io.IOException- Throws:
java.io.IOException
-
readFully
public static int readFully(java.io.Reader reader, char[] dest, int offset, int len) throws java.io.IOException- Throws:
java.io.IOException
-
readFully
public static int readFully(java.io.InputStream reader, byte[] dest) throws java.io.IOException- Throws:
java.io.IOException
-
readFully
public static int readFully(java.io.InputStream reader, byte[] dest, int offset, int len) throws java.io.IOException- Throws:
java.io.IOException
-
consumeFully
public static long consumeFully(java.io.InputStream inputStream) throws java.io.IOExceptionFully consumes the input stream, throwing the bytes away. Returns the number of bytes consumed.- Throws:
java.io.IOException
-
readAllLines
public static java.util.List<java.lang.String> readAllLines(java.io.InputStream input) throws java.io.IOException- Throws:
java.io.IOException
-
readAllLines
public static void readAllLines(java.io.InputStream input, java.util.function.Consumer<java.lang.String> consumer) throws java.io.IOException- Throws:
java.io.IOException
-
noCloseStream
public static java.io.InputStream noCloseStream(java.io.InputStream stream)Wraps anInputStream
such that it'sclose
method becomes a noop- Parameters:
stream
-InputStream
to wrap- Returns:
- wrapped
InputStream
-
flushOnCloseStream
Wraps the givenBytesStream
in aStreamOutput
that simply flushes when close is called. -
readFully
Reads all bytes from the givenInputStream
and closes it afterwards.- Throws:
java.io.IOException
-
limitStream
public static java.io.InputStream limitStream(java.io.InputStream in, long limit)Limits the given input stream to the provided number of bytes
-