BufferedInputStream
BufferedInputStream is a buffered input stream. It inherits from FilterInputStream.
The purpose of BufferedInputStream is to add some features to another input stream, such as providing "buffering functionality" and supporting "mark() marks" and "reset() reset methods".
BufferedInputStream is essentially implemented through an internal buffer array. For example, after creating a BufferedInputStream corresponding to a certain input stream, when we read the data of the input stream through read(), the BufferedInputStream will fill in the data of the input stream into the buffer in batches. Whenever the data in the buffer is read, the input stream will fill the data buffer again; this will be repeated until we have finished reading the input stream data location.
BufferedInputStream function list:
BufferedInputStream(InputStream in)BufferedInputStream(InputStream in, int size)synchronized int available()void close()synchronized void mark(int readlimit)boolean markSupported()synchronized int read()synchronized int read(byte[] buffer, int offset, int byteCount)synchronized void reset()synchronized long skip(long byteCount)
Sample code:
For detailed usage of API in BufferedInputStream, refer to the example code (BufferedInputStreamTest.java):
import java.io.BufferedInputStream;import java.io.ByteArrayInputStream;import java.io.File;import java.io.InputStream;import java.io.FileInputStream;import java.io.IOException;import java.io.FileNotFoundException;import java.lang.SecurityException;/** * BufferedInputStream Test program* * @author skywang */public class BufferedInputStreamTest { private static final int LEN = 5; public static void main(String[] args) { testBufferedInputStream() ; } /** * API test function of BufferedInputStream*/ private static void testBufferedInputStream() { // Create a BufferedInputStream byte stream, the content is an ArrayLetters array try { File file = new File("bufferedinputstream.txt"); InputStream in = new BufferedInputStream( new FileInputStream(file), 512); // Read 5 bytes from the byte stream. "abcde", a corresponds to 0x61, b corresponds to 0x62, and so on... for (int i=0; i<LEN; i++) { // If you can continue to read the next byte, read the next byte if (in.available() >= 0) { // Read "next byte of byte stream" int tmp = in.read(); System.out.printf("%d : 0x%s/n", i, Integer.toHexString(tmp)); } } // If "this byte stream" does not support the marking function, exit directly if (!in.markSupported()) { System.out.println("make not supported!"); return ; } // Mark "current index position", that is, mark the element of the 6th position -- "f" // 1024 corresponds to marklimit in.mark(1024); // Skip 22 bytes. in.skip(22); // Read 5 bytes byte[] buf = new byte[LEN]; in.read(buf, 0, LEN); // Convert buf to a String string. String str1 = new String(buf); System.out.printf("str1=%s/n", str1); // Reset the "index of the input stream" to the position marked by mark(), that is, reset to "f". in.reset(); // Read 5 bytes into buf from "reset byte stream". That is, read "fghij" in.read(buf, 0, LEN); // Convert buf to a String string. String str2 = new String(buf); System.out.printf("str2=%s/n", str2); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }} The content of bufferedinputstream.txt read in the program is as follows:
abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
Running results:
0 : 0x611 : 0x622 : 0x633 : 0x644 : 0x65str1=01234str2=fghij
BufferedOutputStream
BufferedOutputStream is a buffered output stream. It inherits from FilterOutputStream.
The function of BufferedOutputStream is to provide "buffering" to another output stream.
BufferedOutputStream function list:
BufferedOutputStream(OutputStream out)BufferedOutputStream(OutputStream out, int size)synchronized void close()synchronized void flush()synchronized void write(byte[] buffer, int offset, int length)synchronized void write(int oneByte)
Sample code:
For detailed usage of API in BufferedOutputStream, refer to the example code (BufferedOutputStreamTest.java):
import java.io.BufferedOutputStream;import java.io.File;import java.io.OutputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.FileNotFoundException;import java.lang.SecurityException;import java.util.Scanner;/** * BufferedOutputStream Test program* * @author skywang */public class BufferedOutputStreamTest { private static final int LEN = 5; // Corresponding to English letters "abcddefghijklmnopqrssttuvwxyz" 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) { testBufferedOutputStream() ; } /** * API test function of BufferedOutputStream*/ private static void testBufferedOutputStream() { // Create the BufferedOutputStream corresponding to "File Output Stream" // Its corresponding buffer size is 16, that is, when the buffer data >=16, the content of the buffer will be automatically written to the output stream. try { File file = new File("out.txt"); OutputStream out = new BufferedOutputStream( new FileOutputStream(file), 16); // Write the first 10 bytes of the ArrayLetters array to the output stream out.write(ArrayLetters, 0, 10); // Write the "newline/n" to the output stream out.write('/n'); // TODO! // out.flush(); readUserInput() ; out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * Read user input*/ private static void readUserInput() { System.out.println("please input a text:"); Scanner reader=new Scanner(System.in); // Wait for an input String str = reader.next(); System.out.printf("the input is : %s/n", str); }} Running results:
Generate the file "out.txt", and the content of the file is "abcdefghij".
Step-by-step testing: Follow the following three steps to test the program to view the buffer size and the role of flush().
Type 1: Original program
(1) Run the program. When the program is waiting for user input, check the text content of "out.txt"; find: the content is empty.
(2) Run the program. After user input, check the text content of "out.txt"; find: the content is "abcdefghij".
From this, we find that the results of (01) and (02) are different; the reason why the out.txt content in (01) is empty is because the buffer size corresponding to out.txt is 16 bytes, and we only write 11 bytes, so it does not perform the clear buffer operation (i.e., write buffer data to the output stream).
The content corresponding to out.txt is "abcdefghij", because out.close() is executed, which closes the output stream; before closing the output stream, the buffer data will be written to the output stream.
Note: When retesting, first delete out.txt.
Type 2: Add the following statement before readUserInput()
out.flush();
The purpose of this sentence is to write the "buffer content" into the output stream.
(1) Run the program. When the program is waiting for user input, check the text content of "out.txt"; find: the content is "abcdefghij".
(2) Run the program. After user input, check the text content of "out.txt"; find: the content is "abcdefghij".
From this, we found that the same result as (01) and (02), the corresponding content of out.txt is "abcdefghij". This is because the flush() operation is performed, which is used to write the buffer data to the output stream.
Note: When retesting, delete out.txt first!
Type 3: Based on the first type,
out.write(ArrayLetters, 0, 10);
Modified to
out.write(ArrayLetters, 0, 20);
(1) Run the program. When the program is waiting for user input, check the text content of "out.txt"; find that the content is "abcdefghijklmnopqrst" (excluding Enter).
(02) Run the program. After user input, check the text content of "out.txt"; find that the content is "abcdefghijklmnopqrst" (including Enter).
From this, we found that (01) the result of the run is "abcdefghijklmnopqrst" (excluding carriage return). This is because the size of the buffer is 16, and we write 20 bytes through out.write(ArrayLetters, 0, 20), exceeding the size of the buffer; at this time, all inputs will be directly written into the output stream without passing through the buffer.
(3) The result of the operation is "abcdefghijklmnopqrst" (including carriage return), because when executing out.close(), the carriage return symbol '/n' is written into the output stream.
Note: When retesting, delete out.txt first!