In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to analyze the BufferedOutputStream source code from the data loss of the download file, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
Today, I suddenly encountered a problem. The file downloaded through the download interface is 2kb smaller than the actual file, and the content in the file is much less than the actual content. With this problem, I track the code execution flow. Let's take a look at the core code: bis = new BufferedInputStream (new FileInputStream (targetFilePath)); bos = new BufferedOutputStream (response.getOutputStream ()); byte [] buff = new byte [2048] Int bytesRead; while (- 1! = (bytesRead = bis.read (buff, 0, buff.length)) {bos.write (buff, 0, bytesRead);}
The core code is very simple, that is, according to the target file, the target file stream is read through the FileInputStream stream and written to the output stream of response. BufferedInputStream and BufferedOutputStream buffered streams are used in the middle to improve performance. Based on the above code, I boldly guess that the possible cause of the problem is BufferedInputStream or BufferedOutputStream. Further analysis:
While (- 1! = (bytesRead = bis.read (buff, 0, buff.length)
This statement reads the contents of all read buffers in a loop, so it is not very likely that there will be a problem with the statement. It is most likely due to the write buffer problem. Let me analyze the source code of BufferedOutputStream to see if I can find out the cause of the problem:
BufferedOutputStream is located under the java.io package / * inherited from FilterOutputStream (FilterOutputStream has an attribute of OutputStream Is the target output out object stream) * / public class BufferedOutputStream extends FilterOutputStream {/ * buffer used to store data (default 8912 bytes) * / protected byte buf [] / * current bytes of stored data (personally understood as a pointer to the end of the stored data) * / protected int count / * Construction method 1: set the target output stream object with the default buff buffer size of 8912 bytes * / public BufferedOutputStream (OutputStream out) {this (out, 8192) } / * Construction method 2: set the output stream object, customize the buffer size, * / public BufferedOutputStream (OutputStream out, int size) {super (out) If (size 0) {out.write (buf, 0, count); count = 0 }} / * write a byte of data to the buffer * * / public synchronized void write (int b) throws IOException {/ / first determine whether the buffer is full, if so Clear the buffer if (count > = buf.length) {flushBuffer () } buf [count++] = (byte) b } / * write data of specified length to the buffer * * / public synchronized void write (byte b [], int off, int len) throws IOException {/ / determine whether the length of the written data exceeds the buffer size If it exceeds, write directly to the target object out stream and empty the buffer if (len > = buf.length) {flushBuffer () Out.write (b, off, len); return } / / if the length is greater than the remaining buffer space, clear the buffer and write data if (len > buf.length-count) {flushBuffer () } System.arraycopy (b, off, buf, count, len); count + = len } / * flush buffer * * / public synchronized void flush () throws IOException {flushBuffer (); out.flush ();}}
It can be found from the above source code that the buffer refresh is triggered when the write data size is greater than the available size of the buffer.
To solve this problem, put the core operation in try-catche-finally, manually close the BufferedInputStream and buffered OutputStream streams in finally (BufferedOutputStream does not have a close method, call the close method of the parent class FilterOutputStream), and force the buffer data to be flushed into the out write object stream before closing. The problem has been solved.
The answer to the question about how to analyze the BufferedOutputStream source code from the data loss of the download file is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.