ByteArrayOutputStream 和 ByteArrayInputStream

来源:互联网 发布:河北网络干部学院辅助 编辑:程序博客网 时间:2024/05/17 00:12

ByteArrayOutputStream and ByteArrayInputStream


Byte arrays are often useful as stream destinations and sources. The ByteArrayOutputStream class
lets you write a stream of bytes to a byte array; the ByteArrayInputStream class lets you read a
stream of bytes from a byte array.
ByteArrayOutputStream declares two constructors. Each constructor creates a byte array output
stream with an internal byte array; a copy of this array can be returned by calling

ByteArrayOutputStream’s byte[] toByteArray() method.

每个构造器建立一个字节数组输出流带有一个内部的字节数组;这个数组的副本可能通过调用ByteArrayOutputStream’s byte[] toByteArray() 方法返回。


 ByteArrayOutputStream() creates a byte array output stream with an internal
byte array whose initial size is 32 bytes. This array grows as necessary.
 ByteArrayOutputStream(int size) creates a byte array output stream with an
internal byte array whose initial size is specified by size and grows as necessary.
This constructor throws IllegalArgumentException when size is less than zero.
The following example uses ByteArrayOutputStream() to create a byte array output stream with an
internal byte array set to the default size:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteArrayInputStream also declares a pair of constructors. Each constructor creates a byte array
input stream based on the specified byte array and also keeps track of the next byte to read from the
array and the number of bytes to read.
 ByteArrayInputStream(byte[] ba) creates a byte array input stream that uses
ba as its byte array (ba is used directly; a copy isn’t created). The position is set
to 0 and the number of bytes to read is set to ba.length.
 ByteArrayInputStream(byte[] ba, int offset, int count) creates a byte
array input stream that uses ba as its byte array (no copy is made). The position
is set to offset and the number of bytes to read is set to count.
The following example uses ByteArrayInputStream(byte[]) to create a byte array input stream
whose source is a copy of the previous byte array output stream’s byte array:
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());


ByteArrayOutputStream and ByteArrayInputStream are useful in a scenario where you need to

convert an image to an array of bytes, process these bytes in some manner, and convert the bytes

back to the image.

For example, suppose you’re writing an Android-based image-processing application. You decode
a file containing the image into an Android-specific android.graphics.BitMap instance, compress
this instance into a ByteArrayOutputStream instance, obtain a copy of the byte array output stream’s
array, process this array in some manner, convert this array to a ByteArrayInputStream instance, and
use the byte array input stream to decode these bytes into another BitMap instance, as follows:
String pathname = . . . ; // Assume a legitimate pathname to an image.
Bitmap bm = BitmapFactory.decodeFile(pathname);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (bm.compress(Bitmap.CompressFormat.PNG, 100, baos))
{
byte[] imageBytes = baos.toByteArray();
// Do something with imageBytes.
bm = BitMapFactory.decodeStream(new ByteArrayInputStream(imageBytes));
}
This example obtains an image file’s pathname and then calls the concrete android.graphics.
BitmapFactory class’s Bitmap decodeFile(String pathname) class method. This method decodes
the image file identified by pathname into a bitmap and returns a Bitmap instance that represents this
bitmap.
After creating a ByteArrayOutputStream object, the example uses the returned BitMap instance to
call BitMap’s boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream)
method to write a compressed version of the bitmap to the byte array output stream:
 format identifies the format of the compressed image. I’ve chosen to use the
popular Portable Network Graphics (PNG) format.
 quality hints to the compressor as to how much compression is required.
This value ranges from 0 through 100, where 0 means maximum compression
at the expense of quality and 100 means maximum quality at the expense
of compression. Formats such as PNG ignore quality because they employ
lossless compression.
 stream identifies the stream on which to write the compressed image data.
When compress() returns true, which means that it successfully compressed the image onto the byte
array output stream in the PNG format, the ByteArrayOutputStream object’s toByteArray() method
is called to create and return a byte array with the image’s bytes.
Continuing, the array is processed, a ByteArrayInputStream object is created with the processed bytes
serving as the source of this stream, and BitmapFactory’s BitMap decodeStream(InputStream is)
class method is called to convert the byte array input stream’s source of bytes to a BitMap instance.


0 0
原创粉丝点击