IO stream - byte array stream [memory stream]

1. Overview of byte array streams

1.1 Node flow & packaging flow

 IO streams are classified according to different processing objects and can be divided into node streams and packaging streams.

1) Node stream: FileOutputStream, FileInputStream, FileWriter and FileReade

2) Packaging stream: Buffer stream, conversion stream, print stream, data stream and object stream all belong to it.

Node streams can be operated with package streams, for example:

Using byte streams to copy files is inefficient, so we can use buffered streams to improve efficiency.

Using byte streams to access data of any data type is cumbersome, so we can use object streams to simplify operations and so on.

1.2 Overview of byte array streams

 Byte array stream , which is also a node stream .

=====================The byte array stream is divided into? ================================

1) Input stream: ByteArrayInputStream

2) Output stream: ByteArrayOutputStream

=====================Tips? =======================================

When using a byte array stream, in order to improve efficiency and simplify operations, the byte array stream can be used together with the packaging stream .

=====================What is the difference between the rest of the node stream and the byte array stream? ====================

Among common node streams, for example: FileInputStream and FileReader both use "file" as the data source , while ByteArrayInputStream uses the "byte array" in memory as the data source .

=====================How is the byte array stream implemented? ===========================

A byte array stream is a stream related to an array in memory . It can write byte arrays to the output stream, or read byte arrays from the input stream , without involving the disk. The memory array output stream can be regarded as an automatically expanding byte array, into which bytes can be written.

=====================What is the use of byte array streams? =============================

Through byte array streaming, we can convert between all data types (basic data types, reference data types) and byte arrays , and then convert them into byte arrays that can be saved to files or transmitted to the network.

2. ByteArrayOutputStream class

ByteArrayOutputStream underlying logic

ByteArrayOutputStream The byte array output stream creates a byte array buffer in memory, and all data sent to the output stream is saved in the byte array buffer. When the buffer is initialized, the default size is 32 bytes, which will automatically grow as data is continuously written. However, the maximum capacity of the buffer is 2G. As long as the data does not exceed 2G, you can write into it.

After the data is written out, you can use the toByteArray() method or toString() method to obtain the data, thus realizing the conversion of any data type into a byte array .

For example, given a byte array, and then put various data into the array, such as integers, Boolean types, floating point types, strings and objects, etc., this requirement can be realized using ByteArrayOutputStream.

[Example] Case study of converting any data type into byte array

public class ArrayStreamTest {

    public static void main(String[] args) {

        try {

            // Byte array output stream (node ​​stream), which can convert any data type into byte array

            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            // Buffer stream (wrapper class), used to improve efficiency

            BufferedOutputStream bos = new BufferedOutputStream(baos);

            // Object stream (packaging stream) to write any data type

            ObjectOutputStream oos = new ObjectOutputStream(bos);

            //Use object stream to write data

            east.writeInt(123);

            oos.writeDouble(123.45);

            oos.writeChar('A');

            oos.writeBoolean(false);

            oos.writeUTF("node");

            oos.writeObject(new Date());

            // Refresh the stream. You must refresh the stream before getting the data because a wrapper stream is used.

            oos.flush();

            // retrieve data

            byte[] bs = baos.toByteArray();

            System.out.println(Arrays.toString(bs));

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

By looking at the underlying source code, we found that the close() method of the ByteArrayOutputStream class was not implemented, so the stream can still be used after calling the close() method to close it.

三、ByteArrayInputStream

The byte array input stream is to wrap a byte array byte[] so that it has the properties of a stream. It can be read sequentially and marked to jump back to continue reading. Its main function is to read the byte array. The data.

In the same way, closing the ByteArrayInputStream is invalid. Calling the close() method can still be called after closing the stream.

[Example] Read the character array obtained in the previous case

public class ArrayStreamTest {

    public static void main(String[] args) {

        try {

            // Get the byte array and return the byte array written through the byte array output stream in the previous case

            byte[] bs = outputStreamMethod();

            // 字节数组输入流(节点流),用于读取字节数组中的数据

            ByteArrayInputStream bios = new ByteArrayInputStream(bs);

            // 缓冲流(包装类),用于提高效率

            BufferedInputStream bis = new BufferedInputStream(bios);

            // 对象流(包装流),实现读取指定类型的数据

            ObjectInputStream ois = new ObjectInputStream(bis);

            // 读取数据

            System.out.println(ois.readInt());

            System.out.println(ois.readDouble());

            System.out.println(ois.readChar());

            System.out.println(ois.readBoolean());

            System.out.println(ois.readUTF());

            System.out.println(ois.readObject());

        } catch (IOException e) {

            e.printStackTrace();

        } catch (ClassNotFoundException e) {

            e.printStackTrace();

        }

    }

}

补充,ByteArrayInputStream和ByteArrayOutputStream是字节数组流,那么与之对应的字符数组流则是StringReader和StringWriter。

与字节数组流相比,字符数组流反而用得更少,因为StringBuilder和StringBuffer也能方便的用来存储动态长度的字符,而且大家更熟悉这些类。

 

 

Guess you like

Origin blog.csdn.net/shengshanlaolin_/article/details/127466119