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 intBUFFER_SIZEstatic java.io.OutputStreamNULL_OUTPUT_STREAMOutputStream that just throws all the bytes away -
Constructor Summary
Constructors Constructor Description Streams() -
Method Summary
Modifier and Type Method Description static longconsumeFully(java.io.InputStream inputStream)Fully consumes the input stream, throwing the bytes away.static voidcopy(byte[] in, java.io.OutputStream out)Copy the contents of the given byte array to the given OutputStream.static intcopy(java.io.Reader in, java.io.Writer out)Copy the contents of the given Reader to the given Writer.static voidcopy(java.lang.String in, java.io.Writer out)Copy the contents of the given String to the given output Writer.static java.lang.StringcopyToString(java.io.Reader in)Copy the contents of the given Reader into a String.static BytesStreamflushOnCloseStream(BytesStream os)Wraps the givenBytesStreamin aStreamOutputthat simply flushes when close is called.static java.io.InputStreamlimitStream(java.io.InputStream in, long limit)Limits the given input stream to the provided number of bytesstatic java.io.InputStreamnoCloseStream(java.io.InputStream stream)Wraps anInputStreamsuch that it'sclosemethod becomes a noopstatic java.io.OutputStreamnoCloseStream(java.io.OutputStream stream)Wraps anOutputStreamsuch that it'sclosemethod becomes a noopstatic java.util.List<java.lang.String>readAllLines(java.io.InputStream input)static voidreadAllLines(java.io.InputStream input, java.util.function.Consumer<java.lang.String> consumer)static BytesReferencereadFully(java.io.InputStream in)Reads all bytes from the givenInputStreamand closes it afterwards.static intreadFully(java.io.InputStream reader, byte[] dest)static intreadFully(java.io.InputStream reader, byte[] dest, int offset, int len)static intreadFully(java.io.Reader reader, char[] dest)static intreadFully(java.io.Reader reader, char[] dest, int offset, int len)Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
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 anInputStreamsuch that it'sclosemethod becomes a noop- Parameters:
stream-InputStreamto wrap- Returns:
- wrapped
InputStream
-
noCloseStream
public static java.io.OutputStream noCloseStream(java.io.OutputStream stream)Wraps anOutputStreamsuch that it'sclosemethod becomes a noop- Parameters:
stream-OutputStreamto wrap- Returns:
- wrapped
OutputStream
-
flushOnCloseStream
Wraps the givenBytesStreamin aStreamOutputthat simply flushes when close is called. -
readFully
Reads all bytes from the givenInputStreamand 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
-