Buffer idea:
The Read method of FileInputStream is to read the data directly from the disk in the system, consuming resources more.
the function of buffer is to wrap FileInputStream object FIS through the wrapper class, store the byte array in the buffer through the Read method of the FIS, and then call the enhanced read method of the buffer to iterate through the byte array.
Explanation: Iterating through a byte array is much faster than traversing the data in the disk.
Source:
Import Java.io.ioexception;import Java.io.inputstream;public class Mybufferedinputstream {private InputStream input= Null;byte[] Buf=new byte[1024*4];int count=0,index=0; MYBUFFEREDINPUTSTREAM2 (InputStream input) {this.input=input;} public int Myread () throws Ioexception{if (count==0) {count=input.read (BUF); if (count<0) {return-1;} index=0;} if (count>0) {byte B=buf[index];count--;index++;return b*0xff;} return-1;}}
Problems encountered: 1,
about why the return is:
return b*0xff;
Because the data is composed of 1012-binary permutation combinations.
So it is possible to have 1111-1111 eight 1 cases, but reading one byte is equivalent to reading a eight 2 binary bit.
But since 1111-1111 of the decimal is-1.
<span style= "FONT-SIZE:14PX;" > Explanation: Its highest bit is 1, so it is negative. By the complement rule, the following equation is true: negative number = absolute value of negative number bitwise counter +1 negative number bitwise negation +1 = absolute value of negative number so 11111111 bitwise negation +1 equals 1. Therefore, corresponds to 1. </span>
This also explains why a byte array is used to return an int instead of a byte.
As the int is returned, byte is automatically lifted without &0xff, and the returned int is still-1.
Explain why you should add &0xff:
To avoid byte 1111-1111 to int to 1111-1111 1111-1111 1111-1111 1111-1111 result or-1.
Just "with" a bit.
1111-1111 1111-1111 1111-1111 1111-1111
& 0000-0000 0000-0000 0000-0000 1111-1111-->255-->0xff
To get the right value.
2.
an int promotion of byte causes 1 bytes to become 4 bytes.
Write out the words according to int, it should be 4 times times the original data.
But why isn't it true?
The Read method is lifted upward, it is in front of 0, guaranteed not-1 This situation occurs, and the Write method, it is actually doing a strong turn action.
The Write method actually writes the lowest 8 bits in. All the rest is cut off.
int is doing the promotion, write is doing a strong turn, so it can guarantee the source data does not change.
Buffer for Java custom byte stream