对应于 java.io.ByteArrayInputStream 的byte内存输入流,jdk同样提供了java.io. ByteArrayOutputStream byte[]输出流的概念。
功能很简单,就是通过write方法 把字节写入到 byte[]中,取的时候直接toByteArray或者toString() 即可。
注意:此类是线程安全的。
实现方式:
通过对方法 添加synchronized 关键字实现,另外Reader 顶层类则是通过Lock 锁,锁定当前流对象实现的。
功能分析如下:
package java.io;
import java.util.Arrays;
/**
* 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.
翻译:ByteArrayOutputStream 实现了OutputStream 接口,数据是写入到一个 byte[]里,并且buf 是自动增长的随着流的写入。
* The data can be retrieved using <code>toByteArray()</code> and
* <code>toString()</code>.
翻译:ByteArrayOutputStream 缓存的数据,可以通过 toByteArray 和 toString() 方法获取
* <p>
* Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
* this class can be called after the stream has been closed without
* generating an <tt>IOException</tt>.
翻译:关闭ByteArrayOutputStream ,调用close方法是没有意义的。close是空实现,即便是关闭了也可以调用其他的方法。
*
* @author Arthur van Hoff
* @version %I%, %G%
* @since JDK1.0
*/
public class ByteArrayOutputStream extends OutputStream {
/**
* 承载ByteArrayOutputStream 写入的数据
*/
protected byte buf[];
/**
* buf 中当前有效的数据量
*/
protected int count;
/**
* 构造函数,默认创建的是一个长度为32的byte数组
*/
public ByteArrayOutputStream() {
this(32);
}
/**
* 同上
*/
public ByteArrayOutputStream(int size) {
if (size < 0) {
throw new IllegalArgumentException("Negative initial size: "
+ size);
}
buf = new byte[size];
}
/**
* 将b的低八位写入 buf数组里
*/
public synchronized void write(int b) {
int newcount = count + 1;
if (newcount > buf.length) { //如果长度不够,那么将buf的长度扩展2倍。 另外:Arrays.copyOf 这个函数的含义请各位好好体会下,这个功能是数组操作最常用的功能!!
buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
}
buf[count] = (byte)b;
count = newcount; //每写入一个字节,count+1
}
/**
* 将b 数组中的字节,从off开始 读取 len长度的byte数据。
注意:父类可是循环写入的,这里进行覆盖一次性搞定。
* @param b the data. 待写入的byte数组
* @param off data 代写的开始位置
* @param len 写入的长度
*/
public synchronized void write(byte b[], int off, int len) {
if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) > b.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
int newcount = count + len;
if (newcount > buf.length) { //如果需要扩容
buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
}
//否则直接copy即可
System.arraycopy(b, off, buf, count, len); //System.arraycopy 也是操作数组最常用的功能。
count = newcount;
}
/**
* Writes the complete contents of this byte array output stream to
* the specified output stream argument, as if by calling the output
* stream's write method using <code>out.write(buf, 0, count)</code>.
*
以上方法都是写入byte 或者byte[] ,但是writeTo 责任直接把out流中的数据全部写入
buf数组里。
*/
public synchronized void writeTo(OutputStream out) throws IOException {
out.write(buf, 0, count);
}
/**
* 从新设置,count值改0
*/
public synchronized void reset() {
count = 0;
}
/**
返回流中的byte数组数据
*/
public synchronized byte toByteArray()[] {
return Arrays.copyOf(buf, count);
}
/**
* 返回缓存中的数据长度
*
*/
public synchronized int size() {
return count;
}
/**
* 根据默认平台返回特定格式的string字符串
*/
public synchronized String toString() {
return new String(buf, 0, count);
}
/**
* 同上,只是用了特定编码格式
*/
public synchronized String toString(String charsetName)
throws UnsupportedEncodingException
{
return new String(buf, 0, count, charsetName);
}
/**
* 无用的关闭
*
*/
public void close() throws IOException {
}
}
分享到:
相关推荐
a)java.sql b) java.util c) java.math d) java.io 2.不属于java.io包中的接口的一项是(C) a)DataInput b) DataOutput c) DataInputStream d) ObjectInput 3. ByteArrayOutputStream将下列哪一项作为输出流 C a)...
import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import ...
【IT十八掌徐培成】Java基础第16天-04.ByteArrayInputStream-ByteArrayOutputStream.zip
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net....
, java.io.InputStream, java.nio.ByteBuffer, java.io.ByteArrayOutputStream)TXT (java.lang.String, char[], java.nio.CharBuffer, java.lang.StringBuffer, java.lang.StringBuilder)支持的数据源:...
前言 Android端可以对字符串进行压缩,我们在进行大量简单文本传输时...import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutput
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; <br>import com.sun.image.codec.jpeg.JPEGCodec; import ...
import java.io.*; import java.net.InetAddress; import java.net.UnknownHostException; public class ClientDemo { public static void main(String[] args) { try { InputStream in = new FileInputStream...
/** * */ ... import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.i
2、常用21个IO流:FileWriter、FileReader、CharArrayReader、CharArrayWriter、CharSequence、OutputStreamWriter、FileOutputStream、InputStreamReader、PrintWriter、BufferedReader、InputStream、...
代码如下:import java.io.ByteArrayOutputStream;import java.io.InputStream;//从输入流中获取数据并以字节数组返回public class StreamTool { /** * 从输入流获取数据 * @param inputStream * @return * @...
import java.io.ByteArrayOutputStream; import com.fasterxml.jackson.databind.ObjectMapper; import de.undercouch.bson4jackson.BsonFactory; public class ObjectMapperSample { public static void...
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStr
测试环境为Adnroid 2.1...异步图片类 ImageDownloadTask 代码如下: import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; impo
本文实例为大家分享了android通过...import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.
今天要实现的功能是实现专辑倒影效果,这个功能已经属于图像处理方面的了,对图像处理我不怎么在行,等一下会介绍一个很实用的工具类,...import java.io.ByteArrayOutputStream; import java.io.File; import java.
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; impor
SocketHttpRequester.java ...import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; impo
在Java的IO中,所有的stream(包括Input和Out stream)都包括两种类型: 1.1 以字节为导向的stream 以字节为导向的stream,表示以字节为单位从stream中读取或往stream中写入信息。以字节为导向的stream包括下面几种...
代码如下:import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import javax.servlet....