In Java, it is divided into a byte stream and a character stream depending on the unit of data being processed.
Character streams are composed of characters such as FileReader, FileWriter, BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter, and so on.
IT terminology, which does not contain continuous streams of boundary data.
A byte stream is composed of bytes, and a character stream is composed of characters. Characters in Java consist of two bytes. The byte stream is the most basic. All the subclasses of InputStream and OutputStream are mainly used to process binary data. It is processed in bytes, but in reality many of the data is text, and the concept of character stream is proposed. It is processed according to the virtual machine's encode, which is to convert the character set. When converting from byte stream to character stream, when byte[] is converted to String, public String(byte bytes, String charsetName) has a key parameter character set encoding. Usually we omit it, then the system uses Operating system default long
Streaming mainly refers to parsing multimedia files such as audio and video and 3D media into a compressed package by a specific compression method, and the video server transmits the data to the user computer sequentially or in real time. In a system using streaming, the user does not have to wait for the entire file to be downloaded as in the download mode, but only takes a few seconds or tens of seconds of startup delay to utilize the decompression device on the user's computer. The compressed A/V, 3D and other multimedia files are decompressed for playback and viewing. At this point, the rest of the multimedia file will continue to be downloaded in the background server.
The difference between character stream and byte stream1. What is the flow
The stream in Java is an abstraction of the sequence of bytes. We can imagine a water pipe, but now it is no longer water, but a sequence of bytes. Like water flow, a stream in Java also has a "flow direction". An object from which a sequence of bytes can usually be read is called an input stream; an object that can write a sequence of bytes to it is called an output stream. .
2. Byte stream
The most basic unit of byte stream processing in Java is a single byte, which is usually used to process binary data. The most basic two byte stream classes in Java are InputStream and OutputStream, which represent the basic input byte stream and output byte stream of the group, respectively. Both the InputStream class and the OutputStream class are abstract classes. In practice, we usually use a series of subclasses provided in the Java class library. Let's take the InputStream class as an example to introduce the byte stream in Java.
The InputStream class defines a basic method read for reading bytes from a byte stream. The definition of this method is as follows:
Public abstract int read() throws IOExcepTIon;
This is an abstract method, which means that any input byte stream class derived from InputStream needs to implement this method. The function of this method is to read a byte from the byte stream, and return -1 if it is at the end. , otherwise returns the byte read in. What we need to pay attention to about this method is that it will block until it knows to return a read byte or -1. In addition, the byte stream does not support caching by default, which means that every time the read method is called, the operating system is requested to read a byte, which is often accompanied by a disk IO, so the efficiency is lower. Some small partners may think that the overload method of read in the InputStream class with a byte array as a parameter can read multiple bytes at a time without frequent disk IO. So is this the case? Let's take a look at the source code for this method:
Public int read(byte b[]) throws IOExcepTIon {
Return read(b, 0, b.length);
}
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;
}
From the above code we can see that in fact the read(byte[]) method internally also reads the read() method in a loop to read a byte array "once", so essentially this method does not use memory. Buffer. To use memory buffers to improve read efficiency, we should use BufferedInputStream.
3. Character stream
The most basic unit of character stream processing in Java is the Unicode symbol (size 2 bytes), which is usually used to process text data. The so-called Unicode symbol, which is a Unicode code unit, ranges from 0x0000 to 0xFFFF. Each number in the above range corresponds to a character. The String type in Java encodes characters in Unicode rules and stores them in memory by default. However, unlike storage in memory, data stored on disk usually has a variety of encoding methods. Using different encoding methods, the same characters will have different binary representations. In fact the character stream works like this:
Output character stream: converts the sequence of characters to be written to the file (actually a Unicode symbol sequence) to a sequence of bytes in the specified encoding mode, and then writes to the file;
Input character stream: The byte sequence to be read is decoded into a corresponding character sequence (actually a Unicode symbol sequence) from the specified encoding mode so that it can exist in the memory.
We use a demo to deepen our understanding of this process. The sample code is as follows:
Import java.io.FileWriter;
Import java.io.IOException;
Public class FileWriterDemo {
Public static void main(String[] args) {
FileWriter fileWriter = null;
Try {
Try {
fileWriter = new FileWriter("demo.txt");
fileWriter.write("demo");
} finally {
fileWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the above code, we use FileWriter to write the four characters "demo" to demo.txt. We use the hex editor WinHex to view the contents of demo.txt:
As you can see from the above figure, the "demo" we wrote is encoded as "64 65 6D 6F", but we did not explicitly specify the encoding method in the above code. In fact, what we used when we did not specify The operating system's default character encoding is used to encode the characters we want to write.
Since the character stream is actually to complete the conversion of the Unicode symbol sequence to the byte sequence of the corresponding encoding mode before outputting, it uses the memory buffer to store the byte sequence obtained after the conversion, waiting for the conversion to be completed and then writing together Into the disk file.
4. The difference between character stream and byte stream
After the above description, we can know that the main difference between the byte stream and the character stream is reflected in the following aspects:
The basic unit of byte stream operation is byte; the basic unit of character stream operation is Unicode symbol.
The byte stream does not use buffers by default; character streams use buffers.
Byte streams are usually used to process binary data. In fact, it can handle any type of data, but it does not support direct writing or reading of Unicode symbols. Character streams usually process text data, which supports writing and reading Unicode symbols. .
Flat Wire Power Inductors,Flat Copper Wire Inductors,Flat Coil High Current Inductors,Flat Wire High Power Inductors
Shenzhen Sichuangge Magneto-electric Co. , Ltd , https://www.scginductor.com