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