Package com.badlogic.gdx.utils
Class StreamUtils
- java.lang.Object
-
- com.badlogic.gdx.utils.StreamUtils
-
public final class StreamUtils extends java.lang.Object
Provides utility methods to copy streams.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
StreamUtils.OptimizedByteArrayOutputStream
A ByteArrayOutputStream which avoids copying of the byte array if possible.
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_BUFFER_SIZE
static byte[]
EMPTY_BYTES
-
Constructor Summary
Constructors Constructor Description StreamUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
closeQuietly(java.io.Closeable c)
Close and ignore all errors.static void
copyStream(java.io.InputStream input, java.io.OutputStream output)
Allocates a 4096 byte[] for use as a temporary buffer and callscopyStream(InputStream, OutputStream, byte[])
.static void
copyStream(java.io.InputStream input, java.io.OutputStream output, byte[] buffer)
Copy the data from anInputStream
to anOutputStream
, using the specified byte[] as a temporary buffer.static void
copyStream(java.io.InputStream input, java.io.OutputStream output, int bufferSize)
Allocates a byte[] of the specified size for use as a temporary buffer and callscopyStream(InputStream, OutputStream, byte[])
.static void
copyStream(java.io.InputStream input, java.nio.ByteBuffer output)
Allocates a 4096 byte[] for use as a temporary buffer and callscopyStream(InputStream, OutputStream, byte[])
.static int
copyStream(java.io.InputStream input, java.nio.ByteBuffer output, byte[] buffer)
Copy the data from anInputStream
to aByteBuffer
, using the specified byte[] as a temporary buffer.static void
copyStream(java.io.InputStream input, java.nio.ByteBuffer output, int bufferSize)
Allocates a byte[] of the specified size for use as a temporary buffer and callscopyStream(InputStream, ByteBuffer, byte[])
.static byte[]
copyStreamToByteArray(java.io.InputStream input)
Copy the data from anInputStream
to a byte array.static byte[]
copyStreamToByteArray(java.io.InputStream input, int estimatedSize)
Copy the data from anInputStream
to a byte array.static java.lang.String
copyStreamToString(java.io.InputStream input)
CallscopyStreamToString(InputStream, int, String)
using the input'savailable
size and the platform's default charset.static java.lang.String
copyStreamToString(java.io.InputStream input, int estimatedSize)
CallscopyStreamToString(InputStream, int, String)
using the platform's default charset.static java.lang.String
copyStreamToString(java.io.InputStream input, int estimatedSize, java.lang.String charset)
Copy the data from anInputStream
to a string using the specified charset.
-
-
-
Field Detail
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE
- See Also:
- Constant Field Values
-
EMPTY_BYTES
public static final byte[] EMPTY_BYTES
-
-
Method Detail
-
copyStream
public static void copyStream(java.io.InputStream input, java.io.OutputStream output) throws java.io.IOException
Allocates a 4096 byte[] for use as a temporary buffer and callscopyStream(InputStream, OutputStream, byte[])
.- Throws:
java.io.IOException
-
copyStream
public static void copyStream(java.io.InputStream input, java.io.OutputStream output, int bufferSize) throws java.io.IOException
Allocates a byte[] of the specified size for use as a temporary buffer and callscopyStream(InputStream, OutputStream, byte[])
.- Throws:
java.io.IOException
-
copyStream
public static void copyStream(java.io.InputStream input, java.io.OutputStream output, byte[] buffer) throws java.io.IOException
Copy the data from anInputStream
to anOutputStream
, using the specified byte[] as a temporary buffer. The stream is not closed.- Throws:
java.io.IOException
-
copyStream
public static void copyStream(java.io.InputStream input, java.nio.ByteBuffer output) throws java.io.IOException
Allocates a 4096 byte[] for use as a temporary buffer and callscopyStream(InputStream, OutputStream, byte[])
.- Throws:
java.io.IOException
-
copyStream
public static void copyStream(java.io.InputStream input, java.nio.ByteBuffer output, int bufferSize) throws java.io.IOException
Allocates a byte[] of the specified size for use as a temporary buffer and callscopyStream(InputStream, ByteBuffer, byte[])
.- Throws:
java.io.IOException
-
copyStream
public static int copyStream(java.io.InputStream input, java.nio.ByteBuffer output, byte[] buffer) throws java.io.IOException
Copy the data from anInputStream
to aByteBuffer
, using the specified byte[] as a temporary buffer. The buffer's limit is increased by the number of bytes copied, the position is left unchanged. The stream is not closed.- Parameters:
output
- Must be a direct Buffer with native byte order and the buffer MUST be large enough to hold all the bytes in the stream. No error checking is performed.- Returns:
- the number of bytes copied.
- Throws:
java.io.IOException
-
copyStreamToByteArray
public static byte[] copyStreamToByteArray(java.io.InputStream input) throws java.io.IOException
Copy the data from anInputStream
to a byte array. The stream is not closed.- Throws:
java.io.IOException
-
copyStreamToByteArray
public static byte[] copyStreamToByteArray(java.io.InputStream input, int estimatedSize) throws java.io.IOException
Copy the data from anInputStream
to a byte array. The stream is not closed.- Parameters:
estimatedSize
- Used to allocate the output byte[] to possibly avoid an array copy.- Throws:
java.io.IOException
-
copyStreamToString
public static java.lang.String copyStreamToString(java.io.InputStream input) throws java.io.IOException
CallscopyStreamToString(InputStream, int, String)
using the input'savailable
size and the platform's default charset.- Throws:
java.io.IOException
-
copyStreamToString
public static java.lang.String copyStreamToString(java.io.InputStream input, int estimatedSize) throws java.io.IOException
CallscopyStreamToString(InputStream, int, String)
using the platform's default charset.- Throws:
java.io.IOException
-
copyStreamToString
public static java.lang.String copyStreamToString(java.io.InputStream input, int estimatedSize, @Null java.lang.String charset) throws java.io.IOException
Copy the data from anInputStream
to a string using the specified charset.- Parameters:
estimatedSize
- Used to allocate the output buffer to possibly avoid an array copy.charset
- May be null to use the platform's default charset.- Throws:
java.io.IOException
-
closeQuietly
public static void closeQuietly(java.io.Closeable c)
Close and ignore all errors.
-
-