In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to look at the use of the decorator pattern from the Java IO source code". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to look at the usage of the decorator pattern from the Java IO source code".
Preface
Design pattern (design pattern) is a solution to all kinds of common (recurring) problems in software design.
When we are doing business development, we should abstract the business. Abstraction is to give a definition and a concept after understanding the business. So the concept is extrapolated from the actual scene, not the first concept. Since the design pattern is a summary of the problem, it naturally conforms to the first scene and then the concept.
In order to understand the design pattern, it is necessary to go to the specific scene, and it turns out that the examples cited by many books or articles circulating on the Internet are reluctant.
As we all know, the Java IO system uses the decorator pattern, if you do not understand this pattern, it is difficult to skillfully use io-related API, it is better to go directly to the most classic example, directly look at the Java io source design, not only understand the use of the pattern, but also familiar with the usage and system of Java IO!
JavaIO source code example public static void main (String [] args) throws IOException {File file = new File ("file.txt"); / / the file content is: abcdefgh InputStream in = new BufferedInputStream (new FileInputStream (file), 512); int firstByte = in.read (); System.out.println (firstByte); int secondByte = in.read () System.out.println (secondByte);} / / output result: 97998
This code means: use FileInputStream to read data from a file, use BufferedInputStream to wrap the data, and allocate a 512-byte buffer. Use in.read () to read one byte of data. When the first byte is read, because the buffer has no content, FileInputStream is called to read 512 bytes from the file into the memory buffer, and when the second byte is read, it is returned directly because the cache already has data. If you don't use BufferedInputStream, you have to read one byte into the file each time, which is inefficient, which is the meaning of using BufferedInputStream as a buffer.
Why output 97 here? Utf-8 encoding is used in the file, 1-4 bytes of variable length are used to represent a character, and only one byte is used for the characters in ascii, so the first byte in the file, that is, the utf-8 encoding of the character a, is also equal to ascii encoding, converted into decimal that is 97.
Here is how to construct BufferedInputStream:
Public BufferedInputStream (InputStream in, int size) {super (in); if (size = count) {fill (); if (pos > = count) return-1;} return getBufIfOpen () [pos++] & 0xff;}
Here is the fill method of BufferedInputStream, which deletes some unnecessary content, where buffer is the initialized buffer, which calls the getInIfOpen method to write data to the buffer.
Private void fill () throws IOException {byte [] buffer = getBufIfOpen (); count = pos; int n = getInIfOpen (). Read (buffer, pos, buffer.length-pos); if (n > 0) count = n + pos;}
The getInIfOpen method uses the object in, which is the FileInputStream passed in by the constructor, so it is equivalent to calling FileInputStream to read 512 bytes of data written to BufferedInputStream's buffer.
Private InputStream getInIfOpen () throws IOException {InputStream input = in; if (input = = null) throw new IOException ("Stream closed"); return input;}
You can see that BufferedInputStream provides an enhancement that provides buffers. So are there any other enhancements?
Public static void main (String [] args) throws IOException {File file = new File ("file.txt"); / / File content: abcde DataInputStream in = new DataInputStream (new BufferedInputStream (new FileInputStream (file), 512); char c = in.readChar (); System.out.println (c);} / / output result: slow
Here we continue to use DataInputStream to wrap BufferedInputStream,DataInputStream to read the original Java data type from the underlying input stream, for example, here we use the readChar method to read the first character, but why output the word slow? View the source code:
Public final char readChar () throws IOException {int ch2 = in.read (); / / 97 int ch3 = in.read (); / / 98 if ((ch2 | ch3) < 0) throw new EOFException (); return (char) ((ch2)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.