org.more.util.io.output
类 ByteArrayOutputStream

java.lang.Object
  继承者 java.io.OutputStream
      继承者 org.more.util.io.output.ByteArrayOutputStream
所有已实现的接口:
Closeable, Flushable

public class ByteArrayOutputStream
extends OutputStream

This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it.

The data can be retrieved using toByteArray() and toString().

Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

This is an alternative implementation of the ByteArrayOutputStream class. The original implementation only allocates 32 bytes at the beginning. As this class is designed for heavy duty it starts at 1024 bytes. In contrast to the original it doesn't reallocate the whole memory block but allocates additional buffers. This way no buffers need to be garbage collected and the contents don't have to be copied to the new buffer. This class is designed to behave exactly like the original. The only exception is the deprecated toString(int) method that has been ignored.

版本:
$Id: ByteArrayOutputStream.java 1304052 2012-03-22 20:55:29Z ggregory $

构造方法摘要
ByteArrayOutputStream()
          Creates a new byte array output stream.
ByteArrayOutputStream(int size)
          Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
 
方法摘要
 void close()
          Closing a ByteArrayOutputStream has no effect.
 void reset()
           
 int size()
          Return the current size of the byte array.
static InputStream toBufferedInputStream(InputStream input)
          Fetches entire contents of an InputStream and represent same data as result InputStream.
 byte[] toByteArray()
          Gets the curent contents of this byte stream as a byte array.
 String toString()
          Gets the curent contents of this byte stream as a string.
 String toString(String enc)
          Gets the curent contents of this byte stream as a string using the specified encoding.
 void write(byte[] b, int off, int len)
          Write the bytes to byte array.
 int write(InputStream in)
          Writes the entire contents of the specified input stream to this byte stream.
 void write(int b)
          Write a byte to byte array.
 void writeTo(OutputStream out)
          Writes the entire contents of this byte stream to the specified output stream.
 
从类 java.io.OutputStream 继承的方法
flush, write
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

构造方法详细信息

ByteArrayOutputStream

public ByteArrayOutputStream()
Creates a new byte array output stream. The buffer capacity is initially 1024 bytes, though its size increases if necessary.


ByteArrayOutputStream

public ByteArrayOutputStream(int size)
Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.

参数:
size - the initial size
抛出:
IllegalArgumentException - if size is negative
方法详细信息

write

public void write(byte[] b,
                  int off,
                  int len)
Write the bytes to byte array.

覆盖:
OutputStream 中的 write
参数:
b - the bytes to write
off - The start offset
len - The number of bytes to write

write

public void write(int b)
Write a byte to byte array.

指定者:
OutputStream 中的 write
参数:
b - the byte to write

write

public int write(InputStream in)
          throws IOException
Writes the entire contents of the specified input stream to this byte stream. Bytes from the input stream are read directly into the internal buffers of this streams.

参数:
in - the input stream to read from
返回:
total number of bytes read from the input stream (and written to this stream)
抛出:
IOException - if an I/O error occurs while reading the input stream
从以下版本开始:
1.4

size

public int size()
Return the current size of the byte array.

返回:
the current size of the byte array

close

public void close()
           throws IOException
Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

指定者:
接口 Closeable 中的 close
覆盖:
OutputStream 中的 close
抛出:
IOException - never (this method should not declare this exception but it has to now due to backwards compatability)

reset

public void reset()
另请参见:
ByteArrayOutputStream.reset()

writeTo

public void writeTo(OutputStream out)
             throws IOException
Writes the entire contents of this byte stream to the specified output stream.

参数:
out - the output stream to write to
抛出:
IOException - if an I/O error occurs, such as if the stream is closed
另请参见:
ByteArrayOutputStream.writeTo(OutputStream)

toBufferedInputStream

public static InputStream toBufferedInputStream(InputStream input)
                                         throws IOException
Fetches entire contents of an InputStream and represent same data as result InputStream.

This method is useful where,

It can be used in favor of toByteArray(), since it avoids unnecessary allocation and copy of byte[].
This method buffers the input internally, so there is no need to use a BufferedInputStream.

参数:
input - Stream to be fully buffered.
返回:
A fully buffered stream.
抛出:
IOException - if an I/O error occurs
从以下版本开始:
2.0

toByteArray

public byte[] toByteArray()
Gets the curent contents of this byte stream as a byte array. The result is independent of this stream.

返回:
the current contents of this output stream, as a byte array
另请参见:
ByteArrayOutputStream.toByteArray()

toString

public String toString()
Gets the curent contents of this byte stream as a string.

覆盖:
Object 中的 toString
返回:
the contents of the byte array as a String
另请参见:
ByteArrayOutputStream.toString()

toString

public String toString(String enc)
                throws UnsupportedEncodingException
Gets the curent contents of this byte stream as a string using the specified encoding.

参数:
enc - the name of the character encoding
返回:
the string converted from the byte array
抛出:
UnsupportedEncodingException - if the encoding is not supported
另请参见:
ByteArrayOutputStream.toString(String)


Copyright © 2014. All rights reserved.