Package org.elasticsearch.common.io
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
Modifier and TypeFieldDescriptionstatic int
static
OutputStream that just throws all the bytes away -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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.static BytesStream
Wraps the givenBytesStream
in aStreamOutput
that simply flushes when close is called.static
in, long limit)
Limits the given input stream to the provided number of bytesstatic
stream)
Wraps an such that it'sclose
method becomes a noopstatic
stream)
Wraps an such that it'sclose
method becomes a noopstatic <>
input)
static void
input, <> consumer)
static BytesReference
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_STREAMOutputStream that just throws all the bytes away
-
-
Constructor Details
-
Streams
public Streams()
-
-
Method Details
-
copy
public static void copy(byte[] in, out) throwsCopy 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:
- in case of I/O errors
-
copy
public static int copy( in, out) throwsCopy 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:
- in case of I/O errors
-
copy
public static void copy( in, out) throwsCopy 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:
- in case of I/O errors
-
copyToString
public static copyToString( in) throwsCopy 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) throwsFully 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'sclose
method becomes a noop- Parameters:
stream
-InputStream
to wrap- Returns:
- wrapped
InputStream
-
noCloseStream
public static noCloseStream( stream)Wraps an 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
public static in) throwsReads 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
-