ByteArrayInputStream 包含一个内部缓冲区,该缓冲区包含从流中读取的字节。内部计数器跟踪 read 方法要提供的下一个字节。关闭 ByteArrayInputStream 无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOException。
ByteArrayInputStream.java源码如下(注释已被删除):
public class ByteArrayInputStream extends InputStream { // 由该流的创建者提供的 byte 数组 protected byte buf[]; // 要从输入流缓冲区中读取的下一个字符的索引 protected int pos; // 流中当前的标记位置 protected int mark = 0; // 比输入流缓冲区中最后一个有效字符的索引大一的索引 protected int count; public ByteArrayInputStream(byte buf[]) { this.buf = buf; this.pos = 0; this.count = buf.length; } public ByteArrayInputStream(byte buf[], int offset, int length) { this.buf = buf; this.pos = offset; this.count = Math.min(offset + length, buf.length); this.mark = offset; } // 一次读取一个字节 public synchronized int read() { return (pos < count) ? (buf[pos++] & 0xff) : -1; } // 一次读取一个字节数组 public synchronized int read(byte b[], int off, int len) { if (b == null) { throw new NullPointerException(); } else if (off < 0 || len < 0 || len > b.length - off) { throw new IndexOutOfBoundsException(); } if (pos >= count) { return -1; } if (pos + len > count) { len = count - pos; } if (len <= 0) { return 0; } System.arraycopy(buf, pos, b, off, len); pos += len; return len; } public synchronized long skip(long n) { if (pos + n > count) { n = count - pos; } if (n < 0) { return 0; } pos += n; return n; } public synchronized int available() { return count - pos; } public boolean markSupported() { return true; } public void mark(int readAheadLimit) { mark = pos; } public synchronized void reset() { pos = mark; } // 空的关闭实现 public void close() throws IOException {} }
实例:将程序中的一串字符串转换成InputStream字节流,然后利用BufferedInputStream输入流在控制信息打印。只要你得到字符串的字节流你就拥有通过字节流程操作这些字符串的所有权利。代码如下:
import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; public class ByteArrayInputStreamTest01 { public static void main(String[] args) throws Exception { ByteArrayInputStream input = null; BufferedInputStream buffer = null; try { String str = "https://www.hxstrive.com"; input = new ByteArrayInputStream(str.getBytes()); buffer = new BufferedInputStream(input); byte[] bytes = new byte[1024]; int len = -1; while ( (len = buffer.read(bytes)) > 0 ) { System.out.println(new String(bytes, 0, len)); } } finally { if ( null != buffer) { buffer.close(); } } } } // 结果:https://www.hxstrive.com
其他一些功能需要读者自己动手去实践,如:设置mark和skip等。如:使用mark和reset重新输出www.hxstrive.com。
public void t2() throws Exception { ByteArrayInputStream input = null; BufferedInputStream buffer = null; try { String str = "https://www.hxstrive.com"; input = new ByteArrayInputStream(str.getBytes()); buffer = new BufferedInputStream(input); int read, index = 0; while ( (read = buffer.read()) != -1 ) { System.out.print( (char)read ); if ( 6 == index ) { // 标记www的开始位置 buffer.mark(-1); } index ++; } // 结果:https://www.hxstrive.com System.out.println(" markSupported=" + buffer.markSupported()); // 重置输入流的position,输出www.hxstrive.com buffer.reset(); while ( (read = buffer.read()) != -1 ) { System.out.print( (char)read ); } // 结果:www.hxstrive.com } finally { if ( null != buffer) { buffer.close(); } } }