StreamUtil

public class StreamUtil
extends Object

java.lang.Object
   ↳ com.android.tradefed.util.StreamUtil


Utility class for managing input streams.

Summary

Public methods

static String calculateBase64Md5(InputStream inputSource)

Helper method to calculate base64 md5 for a inputStream.

static long calculateCrc32(InputStream inStream)

Helper method to calculate CRC-32 for an ERROR(/InputStream).

static String calculateMd5(InputStream inputSource)

Helper method to calculate md5 for a inputStream.

static void cancel(InputStreamSource outputSource)

Cancels the given InputStreamSource if non-null.

static void close(Closeable closeable)

Closes the given ERROR(/Closeable).

static void closeGZipStream(GZIPOutputStream outStream)

Closes given gzip output stream.

static void closeStream(OutputStream out)

This method is deprecated. use close(Closeable) instead.

static void closeStream(InputStream in)

This method is deprecated. use close(Closeable) instead.

static void closeZipStream(ZipOutputStream outStream)

Closes given zip output stream.

static void copyFileToStream(File file, OutputStream outStream)

Copies contents of file to outStream.

static void copyStreamToWriter(InputStream inStream, Writer writer)

Copies contents of inStream to writer.

static void copyStreams(InputStream inStream, OutputStream outStream, long offset, long size)

Copies contents of origStream to destStream starting at a given offset with a specific size.

static void copyStreams(InputStream inStream, OutputStream outStream, int offset)

Copies contents of origStream to destStream.

static void copyStreams(InputStream inStream, OutputStream outStream)

Copies contents of origStream to destStream.

static int countLinesFromSource(InputStreamSource source)

Count number of lines in an InputStreamSource

static void flushAndCloseStream(OutputStream outStream)

Attempts to flush the given output stream, and then closes it.

static BufferedReader getBufferedReaderFromStreamSrc(InputStreamSource stream)

Return a BuffferedReader to read the contents from the given InputstreamSource.

static ByteArrayList getByteArrayListFromSource(InputStreamSource source)

Retrieves a ByteArrayList from an InputStreamSource.

static ByteArrayList getByteArrayListFromStream(InputStream stream)

Retrieves a ByteArrayList from a byte stream.

static String getStackTrace(Throwable throwable)

Gets the stack trace as a String.

static String getStringFromSource(InputStreamSource source)

Retrieves a String from an InputStreamSource.

static String getStringFromStream(InputStream stream)

Retrieves a String from a character stream.

static String getStringFromStream(InputStream stream, long length)

Retrieves a String from a character stream.

static OutputStream nullOutputStream()

Create a ERROR(/OutputStream) that discards all writes.

Public methods

calculateBase64Md5

public static String calculateBase64Md5 (InputStream inputSource)

Helper method to calculate base64 md5 for a inputStream. The inputStream will be consumed and closed.

Parameters
inputSource InputStream: used to create inputStream

Returns
String base64 md5 of the stream

Throws
IOException

calculateCrc32

public static long calculateCrc32 (InputStream inStream)

Helper method to calculate CRC-32 for an ERROR(/InputStream). The stream will be consumed and closed. It is recommended to provide a buffered stream.

Parameters
inStream InputStream: the ERROR(/InputStream)

Returns
long CRC-32 of the stream

Throws
IOException

calculateMd5

public static String calculateMd5 (InputStream inputSource)

Helper method to calculate md5 for a inputStream. The inputStream will be consumed and closed.

Parameters
inputSource InputStream: used to create inputStream

Returns
String md5 of the stream

Throws
IOException

cancel

public static void cancel (InputStreamSource outputSource)

Cancels the given InputStreamSource if non-null.

Parameters
outputSource InputStreamSource

close

public static void close (Closeable closeable)

Closes the given ERROR(/Closeable).

Parameters
closeable Closeable: the ERROR(/Closeable). No action taken if null.

closeGZipStream

public static void closeGZipStream (GZIPOutputStream outStream)

Closes given gzip output stream.

Parameters
outStream GZIPOutputStream: the ERROR(/ZipOutputStream). No action taken if outStream is null.

closeStream

public static void closeStream (OutputStream out)

This method is deprecated.
use close(Closeable) instead.

Parameters
out OutputStream

closeStream

public static void closeStream (InputStream in)

This method is deprecated.
use close(Closeable) instead.

Parameters
in InputStream

closeZipStream

public static void closeZipStream (ZipOutputStream outStream)

Closes given zip output stream.

Parameters
outStream ZipOutputStream: the ERROR(/ZipOutputStream). No action taken if outStream is null.

copyFileToStream

public static void copyFileToStream (File file, 
                OutputStream outStream)

Copies contents of file to outStream. It is recommended to provide a buffered stream.

Parameters
file File: the ERROR(/File)

outStream OutputStream: the ERROR(/OutputStream)

Throws
IOException

copyStreamToWriter

public static void copyStreamToWriter (InputStream inStream, 
                Writer writer)

Copies contents of inStream to writer.

Recommended to provide a buffered stream for input and output

Parameters
inStream InputStream: the ERROR(/InputStream)

writer Writer: the ERROR(/Writer) destination

Throws
IOException

copyStreams

public static void copyStreams (InputStream inStream, 
                OutputStream outStream, 
                long offset, 
                long size)

Copies contents of origStream to destStream starting at a given offset with a specific size.

Recommended to provide a buffered stream for input and output

Parameters
inStream InputStream: the ERROR(/InputStream)

outStream OutputStream: the ERROR(/OutputStream)

offset long: the offset of when to start copying the data.

size long: the number of bytes to copy. A negative value means to copy all content.

Throws
IOException

copyStreams

public static void copyStreams (InputStream inStream, 
                OutputStream outStream, 
                int offset)

Copies contents of origStream to destStream.

Recommended to provide a buffered stream for input and output

Parameters
inStream InputStream: the ERROR(/InputStream)

outStream OutputStream: the ERROR(/OutputStream)

offset int: the offset of when to start copying the data.

Throws
IOException

copyStreams

public static void copyStreams (InputStream inStream, 
                OutputStream outStream)

Copies contents of origStream to destStream.

Recommended to provide a buffered stream for input and output

Parameters
inStream InputStream: the ERROR(/InputStream)

outStream OutputStream: the ERROR(/OutputStream)

Throws
IOException

countLinesFromSource

public static int countLinesFromSource (InputStreamSource source)

Count number of lines in an InputStreamSource

Parameters
source InputStreamSource: the InputStreamSource

Returns
int number of lines

Throws
if failure occurred reading the stream

flushAndCloseStream

public static void flushAndCloseStream (OutputStream outStream)

Attempts to flush the given output stream, and then closes it.

Parameters
outStream OutputStream: the ERROR(/OutputStream). No action taken if outStream is null.

getBufferedReaderFromStreamSrc

public static BufferedReader getBufferedReaderFromStreamSrc (InputStreamSource stream)

Return a BuffferedReader to read the contents from the given InputstreamSource.

Parameters
stream InputStreamSource: the InputStreamSource

Returns
BufferedReader a BuffferedReader

getByteArrayListFromSource

public static ByteArrayList getByteArrayListFromSource (InputStreamSource source)

Retrieves a ByteArrayList from an InputStreamSource.

Parameters
source InputStreamSource: the InputStreamSource

Returns
ByteArrayList a ByteArrayList containing the stream contents

Throws
if failure occurred reading the stream

getByteArrayListFromStream

public static ByteArrayList getByteArrayListFromStream (InputStream stream)

Retrieves a ByteArrayList from a byte stream.

Parameters
stream InputStream: the ERROR(/InputStream)

Returns
ByteArrayList a ByteArrayList containing the stream contents

Throws
if failure occurred reading the stream

getStackTrace

public static String getStackTrace (Throwable throwable)

Gets the stack trace as a String.

Parameters
throwable Throwable: the Throwable to convert.

Returns
String a String stack trace

getStringFromSource

public static String getStringFromSource (InputStreamSource source)

Retrieves a String from an InputStreamSource.

Parameters
source InputStreamSource: the InputStreamSource

Returns
String a String containing the stream contents

Throws
if failure occurred reading the stream

getStringFromStream

public static String getStringFromStream (InputStream stream)

Retrieves a String from a character stream.

Parameters
stream InputStream: the ERROR(/InputStream)

Returns
String a String containing the stream contents

Throws
if failure occurred reading the stream

getStringFromStream

public static String getStringFromStream (InputStream stream, 
                long length)

Retrieves a String from a character stream.

Parameters
stream InputStream: the ERROR(/InputStream)

length long: the size of the content to read, set to 0 to read all contents

Returns
String a String containing the stream contents

Throws
if failure occurred reading the stream

nullOutputStream

public static OutputStream nullOutputStream ()

Create a ERROR(/OutputStream) that discards all writes.

Returns
OutputStream