ByteArrayInputStream源码分析

转自:

ByteArrayInputStream 是字节数组输入流。它继承于InputStream。
它包含一个内部缓冲区,该缓冲区包含从流中读取的字节;通俗点说,它的内部缓冲区就是一个字节数组,而ByteArrayInputStream本质就是通过字节数组来实现的。
我们都知道,InputStream通过read()向外提供接口,供它们来读取字节数据;而ByteArrayInputStream 的内部额外的定义了一个计数器,它被用来跟踪 read() 方法要读取的下一个字节。

public abstract class InputStream implements Closeable {  
 
    // 能skip的大小 
    private static final int MAX_SKIP_BUFFER_SIZE = 2048;  
 
    // 从输入流中读取数据的下一个字节。 
    public abstract int read() throws IOException;  
 
    // 将数据从输入流读入 byte 数组。 
    public int read(byte b[]) throws IOException {  
        return read(b, 0, b.length);  
    }  
 
    // 将最多 len 个数据字节从此输入流读入 byte 数组。 
    public int read(byte b[], int off, int len) throws IOException {  
        if (b == null) {  
            throw new NullPointerException();  
        } else if (off < 0 || len < 0 || len > b.length - off) {  
            throw new IndexOutOfBoundsException();  
        } else if (len == 0) {  
            return 0;  
        }  
 
        int c = read();  
        if (c == -1) {  
            return -1;  
        }  
        b[off] = (byte)c;  
 
        int i = 1;  
        try {  
            for (; i < len ; i++) {  
                c = read();  
                if (c == -1) {  
                    break;  
                }  
                b[off + i] = (byte)c;  
            }  
        } catch (IOException ee) {  
        }  
        return i;  
    }  
 
    // 跳过输入流中的n个字节 
    public long skip(long n) throws IOException {  
 
        long remaining = n;  
        int nr;  
 
        if (n <= 0) {  
            return 0;  
        }  
 
        int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);  
        byte[] skipBuffer = new byte[size];  
        while (remaining > 0) {  
            nr = read(skipBuffer, 0, (int)Math.min(size, remaining));  
            if (nr < 0) {  
                break;  
            }  
            remaining -= nr;  
        }  
 
        return n - remaining;  
    }  
 
    public int available() throws IOException {  
        return 0;  
    }  
 
    public void close() throws IOException {}  
 
    public synchronized void mark(int readlimit) {}  
 
    public synchronized void reset() throws IOException {  
        throw new IOException("mark/reset not supported");  
    }  
 
    public boolean markSupported() {  
        return false;  
    }  
} 

public class ByteArrayInputStream extends InputStream {  
 
    // 保存字节输入流数据的字节数组  
    protected byte buf[];  
 
    // 下一个会被读取的字节的索引  
    protected int pos;  
 
    // 标记的索引  
    protected int mark = 0;  
 
    // 字节流的长度  
    protected int count;  
 
    // 构造函数:创建一个内容为buf的字节流  
    public ByteArrayInputStream(byte buf[]) {  
        // 初始化“字节流对应的字节数组为buf”  
        this.buf = buf;  
        // 初始化“下一个要被读取的字节索引号为0”  
        this.pos = 0;  
        // 初始化“字节流的长度为buf的长度”  
        this.count = buf.length;  
    }  
 
    // 构造函数:创建一个内容为buf的字节流,并且是从offset开始读取数据,读取的长度为length  
    public ByteArrayInputStream(byte buf[], int offset, int length) {  
        // 初始化“字节流对应的字节数组为buf”  
        this.buf = buf;  
        // 初始化“下一个要被读取的字节索引号为offset”  
        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;  
    }  
 
    // 将“字节流的数据写入到字节数组b中”  
    // off是“字节数组b的偏移地址”,表示从数组b的off开始写入数据  
    // len是“写入的字节长度”  
    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;  
    }  
 
    // 跳过“字节流”中的n个字节。  
    public synchronized long skip(long n) {  
        long k = count - pos;  
        if (n < k) {  
            k = n < 0 ? 0 : n;  
        }  
 
        pos += k;  
        return k;  
    }  
 
    // “能否读取字节流的下一个字节”  
    public synchronized int available() {  
        return count - pos;  
    }  
 
    // 是否支持“标签”  
    public boolean markSupported() {  
        return true;  
    }  
 
    // 保存当前位置。readAheadLimit在此处没有任何实际意义  
    public void mark(int readAheadLimit) {  
        mark = pos;  
    }  
 
    // 重置“字节流的读取索引”为“mark所标记的位置”  
    public synchronized void reset() {  
        pos = mark;  
    }  
 
    public void close() throws IOException {  
    }  
} 


说明

ByteArrayInputStream实际上是通过“字节数组”去保存数据。
(01) 通过ByteArrayInputStream(byte buf[]) 或 ByteArrayInputStream(byte buf[], int offset, int length) ,我们可以根据buf数组来创建字节流对象。
(02) read()的作用是从字节流中“读取下一个字节”。
(03) read(byte[] buffer, int offset, int length)的作用是从字节流读取字节数据,并写入到字节数组buffer中。offset是将字节写入到buffer的起始位置,length是写入的字节的长度。
(04) markSupported()是判断字节流是否支持“标记功能”。它一直返回true。
(05) mark(int readlimit)的作用是记录标记位置。记录标记位置之后,某一时刻调用reset()则将“字节流下一个被读取的位置”重置到“mark(int readlimit)所标记的位置”;也就是说,reset()之后再读取字节流时,是从mark(int readlimit)所标记的位置开始读取。


测试

public class ByteArrayInputStreamTest {  
 
    private static final int LEN = 5;  
    // 对应英文字母“abcddefghijklmnopqrsttuvwxyz” 
    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 
    };  
 
    public static void main(String[] args) {  
        String tmp = new String(ArrayLetters);  
        System.out.println("ArrayLetters="+tmp);  
 
        tesByteArrayInputStream() ;  
    }  
 
    /** 
     * ByteArrayInputStream的API测试函数 
     */ 
    private static void tesByteArrayInputStream() {  
        // 创建ByteArrayInputStream字节流,内容是ArrayLetters数组 
        ByteArrayInputStream bais = new ByteArrayInputStream(ArrayLetters);  
 
        // 从字节流中读取5个字节 
        for (int i=0; i<LEN; i++) {  
            // 若能继续读取下一个字节,则读取下一个字节 
            if (bais.available() >= 0) {  
                // 读取“字节流的下一个字节” 
                int tmp = bais.read();  
                System.out.printf("%d : 0x%s\n", i, Integer.toHexString(tmp));  
            }  
        }  
 
        // 若“该字节流”不支持标记功能,则直接退出 
        if (!bais.markSupported()) {  
            System.out.println("make not supported!");  
            return ;  
        }  
 
        // 标记“字节流中下一个被读取的位置”。即--标记“0x66”,因为因为前面已经读取了5个字节,所以下一个被读取的位置是第6个字节” 
        // (01), ByteArrayInputStream类的mark(0)函数中的“参数0”是没有实际意义的。 
        // (02), mark()与reset()是配套的,reset()会将“字节流中下一个被读取的位置”重置为“mark()中所保存的位置” 
        bais.mark(0);  
 
        // 跳过5个字节。跳过5个字节后,字节流中下一个被读取的值应该是“0x6B”。 
        bais.skip(5);  
 
        // 从字节流中读取5个数据。即读取“0x6B, 0x6C, 0x6D, 0x6E, 0x6F” 
        byte[] buf = new byte[LEN];  
        bais.read(buf, 0, LEN);  
        // 将buf转换为String字符串。“0x6B, 0x6C, 0x6D, 0x6E, 0x6F”对应字符是“klmno” 
        String str1 = new String(buf);  
        System.out.printf("str1=%s\n", str1);  
      
        // 重置“字节流”:即,将“字节流中下一个被读取的位置”重置到“mark()所标记的位置”,即0x66。 
        bais.reset();  
        // 从“重置后的字节流”中读取5个字节到buf中。即读取“0x66, 0x67, 0x68, 0x69, 0x6A” 
        bais.read(buf, 0, LEN);  
        // 将buf转换为String字符串。“0x66, 0x67, 0x68, 0x69, 0x6A”对应字符是“fghij” 
        String str2 = new String(buf);  
        System.out.printf("str2=%s\n", str2);  
    }  
} 



<think>好的,我现在需要回答用户关于如何在Java中创建和使用ByteArrayInputStream的问题,并提供示例代码。首先,我应该回顾用户提供的引用内容,看看哪些信息可以利用。 用户给出的引用中,引用[1]提到了ByteArrayInputStream的不同使用场景,比如内存数据读取、数据传输、测试调试和数据解析。引用[2]和[3]提供了代码示例和源码分析,特别是提到close()方法无效。引用[4]详细说明了构造函数和方法,比如read()。引用[5]展示了StreamAPI的使用,虽然不直接相关,但可能对处理数据有帮助。接下来,我需要整理创建ByteArrayInputStream的步骤。通常,创建需要字节数组作为数据源。根据引用[4],有两种构造函数:一种直接使用字节数组,另一种指定偏移量和长度。我应该分别举例说明这两种情况。然后,使用方法部分,比如read()、available()、skip()和reset()。需要解释每个方法的作用,并给出代码示例。例如,读取单个字节用read(),查看可用字节用available(),跳过部分数据用skip(),重置读取位置用reset()。这些方法在引用[4]中有提到,需要正确展示。 另外,用户可能需要知道如何处理异常或者资源管理,但根据引用[3],close()方法没有实际作用,所以不需要特别处理。这一点应该在注意事项中提到,避免用户担心资源泄漏。 示例代码部分,引用[2]和[3]提供了基础的读取示例,可以借鉴。比如从字节数组读取数据,转换成字符串输出。同时,可以展示使用偏移量和长度的构造函数,比如从数组的某个位置开始读取指定长度的数据。另外,结合引用[5]的StreamAPI,可能可以展示如何将ByteArrayInputStream与其他Java特性结合使用,比如用BufferedReader读取文本数据,但需要确认是否相关。不过用户主要问的是创建和使用,可能不需要复杂例子,保持简单明了更好。最后,相关问题部分需要根据内容生成,比如构造方法区别、异常处理、与其他流的区别、重置方法的作用等。这些问题应覆盖用户可能进一步询问的方向。 检查LaTeX格式是否符合要求,但问题中没有数学公式,所以可能不需要。但如果有数学表达式,需用$...$包裹。不过这里代码示例较多,注意代码块的格式是否正确,比如使用反引号包裹,并标明语言类型。 确保回答结构清晰,分步骤说明创建和使用,代码示例正确,注意事项和常见问题解答。同时,引用标记要正确添加在相关段落末尾,例如构造函数说明引用[4],close()方法引用[3]等。</think>### 如何在Java中创建和使用ByteArrayInputStream #### 1. 创建ByteArrayInputStream ByteArrayInputStream是基于字节数组的输入流,构造方法有两种: 1. **直接使用字节数组** ```java byte[] data = "Hello".getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(data); ``` 2. **指定偏移量和长度** ```java byte[] data = {1, 2, 3, 4, 5}; int offset = 1; // 从索引1开始 int length = 3; // 读取3个字节 ByteArrayInputStream bais = new ByteArrayInputStream(data, offset, length); ``` 此时流包含的字节为`2,3,4`[^4]。 --- #### 2. 核心使用方法 ##### 2.1 读取单个字节 ```java int byteValue; while ((byteValue = bais.read()) != -1) { System.out.print((char) byteValue); // 输出字符形式 } ``` ##### 2.2 读取字节到数组 ```java byte[] buffer = new byte[1024]; int bytesRead = bais.read(buffer, 0, buffer.length); ``` ##### 2.3 查看可用字节数 ```java int available = bais.available(); // 返回剩余可读字节数 ``` ##### 2.4 跳过指定字节 ```java bais.skip(2); // 跳过前2个字节 ``` ##### 2.5 重置读取位置 ```java bais.reset(); // 回到流的起始位置 ``` --- #### 3. 完整示例代码 ```java import java.io.ByteArrayInputStream; import java.io.IOException; public class ByteArrayInputStreamDemo { public static void main(String[] args) throws IOException { // 创建字节数组 byte[] data = "Java I/O".getBytes(); // 创建流对象 try (ByteArrayInputStream bais = new ByteArrayInputStream(data)) { // 读取并转换为字符串 StringBuilder sb = new StringBuilder(); int ch; while ((ch = bais.read()) != -1) { sb.append((char) ch); } System.out.println("读取结果: " + sb); // 输出"Java I/O" } } } ``` --- #### 4. 注意事项 - **无需关闭流**:`close()`方法为空实现,调用后无实际效果[^3]。 - **线程安全**:多个线程读取同一个流时需自行同步。 - **内存效率**:适合处理内存中的小块数据,避免大文件直接加载到内存。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值