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 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 copyStreamToWriter(InputStream inStream, Writer writer)

Copies contents of inStream to writer.

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 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

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

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.

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

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.

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)

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

nullOutputStream

public static OutputStream nullOutputStream ()

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

Returns
OutputStream