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
FieldsModifier and TypeFieldDescriptionstatic intstatic OutputStreamOutputStream that just throws all the bytes away -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic longconsumeFully(InputStream inputStream)Fully consumes the input stream, throwing the bytes away.static voidcopy(byte[] in, OutputStream out)Copy the contents of the given byte array to the given OutputStream.static intCopy the contents of the given Reader to the given Writer.static voidCopy the contents of the given String to the given output Writer.static StringcopyToString(Reader in)Copy the contents of the given Reader into a String.static BytesStreamWraps the givenBytesStreamin aStreamOutputthat simply flushes when close is called.static InputStreamlimitStream(InputStream in, long limit)Limits the given input stream to the provided number of bytesstatic InputStreamnoCloseStream(InputStream stream)Wraps anInputStreamsuch that it'sclosemethod becomes a noopstatic OutputStreamnoCloseStream(OutputStream stream)Wraps anOutputStreamsuch that it'sclosemethod becomes a noopreadAllLines(InputStream input)static voidreadAllLines(InputStream input, Consumer<String> consumer)static BytesReferencereadFully(InputStream in)Reads all bytes from the givenInputStreamand closes it afterwards.static intreadFully(InputStream reader, byte[] dest)static intreadFully(InputStream reader, byte[] dest, int offset, int len)static intstatic 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 anInputStreamsuch that it'sclosemethod becomes a noop- Parameters:
stream-InputStreamto wrap- Returns:
- wrapped
InputStream
-
noCloseStream
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:
IOException
-
limitStream
Limits the given input stream to the provided number of bytes
-