java.io.ByteArrayInputStream
ByteArrayInputStream
ByteArrayInputStream是从输入流中按字节读入的类。他有一个内部的缓冲区来保存从流中读入的字节,一个内部的计数器来跟踪read()方法要读取的字节
Constructor
public ByteArrayInputStream(byte buf[])
构造函数,变量bug指向输入的字节数组buf[],pos默认为0代表从第0个开始读入,构造的流的长度默认为buf[]的长度
public ByteArrayInputStream(byte buf[]) {
this.buf = buf;
this.pos = 0;
this.count = buf.length;
}
public ByteArrayInputStream(byte buf[], int offset, int 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;
}
Method
synchronized int available()
void close()
synchronized void mark(int readlimit)
boolean markSupported()
synchronized int read()
synchronized int read(byte[] buffer, int offset, int length)
synchronized void reset()
synchronized long skip(long byteCount)
read()
read()方法会读取下一个字节并返回一个int值。
由于buf[pos++]是byte类型,返回时Java会自动提升至int,所以为了确保不返回-1,将b与0xff与运算。 即读取到b为-1时,2进制表示为11111111
byte——》int 11111111——》11111111 11111111 11111111 11111111
与0xff做与运算 ——》00000000 00000000 00000000 11111111
public synchronized int read() {
return (pos < count) ? (buf[pos++] & 0xff) : -1;
read(byte b[], int off, int len)方法会将多个字节读取到b[]数组中,并返回读取到的字节的大小。其中,off代表从当前位置跳过off个开始读取,len表示读取的最大的数量。
从源码中可以看到read()方法主要是用System.arraycopy()方法复制数组
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;
}
int avail = count - pos;
if (len > avail) {
len = avail;
}
if (len <= 0) {
return 0;
}
System.arraycopy(buf, pos, b, off, len);
pos += len;
return len;
}
available()
available方法返回输入流中的字节长度
public synchronized int available() {
return count - pos;
}
markSupported()/ mark() / reset()
ByteInputStream类中有一变量mark(默认为0)指向输入流的一个字节,使用mark()函数可以改变this.mark 指向的位置。使用reset()可以使输入流从mark的位置重新开始。
markSupported()检测输入流是否支持mark()/reset()。
public boolean markSupported() {
return true;
}
public void mark(int readAheadLimit) {
mark = pos;
}
public synchronized void reset() {
pos = mark;
}
例程
import java.io.*;
public class testIO {
private static final byte[] ArrayLetters = {
0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A
};
private static String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main(String[] args){
try{
testByteInputStream();
}catch(Exception e){
e.printStackTrace();
}
}
private static void testByteInputStream() throws Exception {
File f = new File("testIO.txt");
ByteArrayInputStream is = new ByteArrayInputStream(ArrayLetters);
System.out.println("is.available = "+is.available());
byte[] b = new byte[5];
is.read(b, 0, 5);
System.out.println(new String(b));
if(is.markSupported()){
is.mark(5);
is.read(b, 0, 5);
System.out.println(new String(b));
is.reset();
is.read(b, 0, 5);
System.out.println(new String(b));
is.reset();
is.skip(2);
is.read(b, 0, 5);
System.out.println(new String(b));
}
is.close();
}
}