2021-12-06_自定义字节流的缓冲区-read和write的特点

学习B站Java教程P241:自定义字节流的缓冲区-read和write的特点

import java.io.*;

class MyBufferedInputStream {

    private InputStream in;

    private byte[] buf = new byte[1024];

    private int pos = 0, count = 0;

    MyBufferedInputStream(InputStream in) {
        this.in = in;
    }

    //一次读一个字节,从缓冲区(字节数组)获取
    public int myRead() throws IOException {
        //通过in对象读取硬盘上数据,并存储buf中。
        if (count == 0) {
            count = in.read(buf);
            if (count < 0) {
                return -1;
            }
            pos = 0;
            byte b = buf[pos];

            count--;
            pos++;
            return b & 0xff;
        } else if (count > 0) {
            byte b = buf[pos];

            count--;
            pos++;
            return b & 0xff;
        }
        return -1;
    }

    public void myClose() throws IOException {
        in.close();
    }
}
/*
11111111-111111110000111010001011100010101001111010

byte:-1 ---> int:-1
11111111
提升为
11111111 11111111 11111111 11111111

改为前面补0:
00000000 00000000 00000000 11111111:255

11111111 -->提升为int类型 那不还是-1吗?是-1的原因是因为在8个1前面补的是1导致的。
那么我只要在前面补0,既可以保留原字节数据不变,又可以避免-1的出现。

  11111111 11111111 11111111 11111111
& 00000000 00000000 00000000 11111111
--------------------------------------
  00000000 00000000 00000000 11111111

备注:read方法,在向上提升,往前面补0,保证不是-1这种情况的发生,
而write方法,其实在做一个强转动作,只写最低的8位(即最低的一个字节)(其余的全砍掉),
即写出去的还是有效数据。从而保证了数据的有效性。


0000-0001:1
求-1:对1进行取反加1
 1111-1110
+0000-0001
=1111-1111:-1
*/

在如下的copy_2()方法中使用自定义字节流的缓冲区(备注:下面这些代码里,只有copy_2()方法是今天敲的,其余代码是好长时间之前敲的,不是今天新学的):

/*
演示MP3的复制,通过缓冲区。
BufferedOutputStream
BufferedInputStream
*/
import java.io.*;
class CopyMp3 {
    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        copy_1();
        long end = System.currentTimeMillis();

        System.out.println("copy_1:" + (end - start) + "毫秒"); // copy_1:420毫秒

        long start2 = System.currentTimeMillis();
        copy_2();
        long end2 = System.currentTimeMillis();
        
        System.out.println("copy_2:" + (end2 - start2) + "毫秒");
    }

    public static void copy_2() throws IOException {
        MyBufferedInputStream bufis = new MyBufferedInputStream(new FileInputStream("d:\\0.mp3"));
        BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("d:\\2.mp3"));

        int by = 0;

        while((by = bufis.myRead()) != -1) {
            bufos.write(by);
        }

        bufos.close();
        bufis.myClose();
    }

    // 通过字节流的缓冲区来完成复制
    public static void copy_1() throws IOException {
        BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("d:\\0.mp3"));
        BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("d:\\1.mp3"));

        int by = 0;

        while((by = bufis.read()) != -1){
            bufos.write(by);
        }

        bufos.close();
        bufis.close();
    }
}

 

posted on 2021-12-06 22:10  平凡力量  阅读(66)  评论(0编辑  收藏  举报