package com.lovecoding.day22.io01;

import org.junit.Test;

import java.io.*;

public class TestBuffer {

    //流的关闭 输出
    @Test
    public void test07() throws IOException {
        FileWriter fw = new FileWriter("d:/1.txt");
        BufferedWriter bw = new BufferedWriter(fw);

        bw.write("hello");

        bw.close();//java.io.IOException: Stream closed
        fw.close();
    }

    @Test
    public void test06() throws IOException {
        FileReader fileReader = new FileReader("a.txt");
        BufferedReader bw = new BufferedReader(fileReader);

        String s = bw.readLine();
        System.out.println(s);

        bw.close();
        fileReader.close();

    }

    //BufferedReader
    @Test
    public void test05() throws IOException {

        BufferedReader bw = new BufferedReader(new FileReader("a.txt"));

        String str = null;

        while (  (str=bw.readLine()) != null ){
            System.out.println(str);
        }

        bw.close();
    }

    // 字节 byte  使用read() 方法 返回  int ？
    // 视频音频，2进制 ， 读取 有可能读取到  1000 0000  0111 1111 -128 - 127  值 1000 0001  等于 -1
    // read()  = -1 结束
    // 读取 1000 0001 = -1值 会造成 程序停止。读取没有结束，文件损坏。
    // 1000 0001换成 int  0000 0000 0000 0000 0000 0000 1000 0001 不会等于-1的。
    // 用int类型接收 不会造成停止。

    //8192 字节快
    @Test
    public void test04() throws Exception {
        long l = System.currentTimeMillis();
        //缓冲流字节流
        //字节流
        BufferedInputStream bif = new BufferedInputStream(new FileInputStream("a.mp4"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("c.mp4"));
        int len ;
        //循环读取
        while ( (len = bif.read())  != -1){
            bos.write(len);
        }
        bos.close();
        bif.close();
        long l1 = System.currentTimeMillis();

        System.out.println(l1-l);
    }
    @Test
    public void test03() throws Exception {
        long l = System.currentTimeMillis();
        //缓冲流字节流
        //字节流
        FileInputStream fis = new FileInputStream("a.mp4");
        FileOutputStream fos = new FileOutputStream("b.mp4");
        //数组
        byte[] bytes = new byte[8192];

        int len ;
        //循环读取
        while ( (len = fis.read(bytes))  != -1){
            fos.write(bytes,0,len);
        }
        fis.close();
        fos.close();

        long l1 = System.currentTimeMillis();

        System.out.println(l1-l);
    }

    //字节流用8192的数组和字节缓冲流  那个快？
    @Test
    public void test02() throws Exception {
        //字符流缓冲流
        BufferedReader br = new BufferedReader(null);
        String s = br.readLine();
        BufferedWriter bw = new BufferedWriter(null);
        bw.newLine();
    }
    @Test
    public void test01() throws Exception {
        //缓冲流字节流
        //字节流
        BufferedInputStream bif = new BufferedInputStream(new FileInputStream("a.txt"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("b.txt"));
        int len ;
        //循环读取
        while ( (len = bif.read())  != -1){
            bos.write(len);
        }
        bos.close();
        bif.close();
    }
}
