|
30 | 30 | */ |
31 | 31 | publicclassSolutionextendsReader4 { |
32 | 32 |
|
33 | | -privatechar[]buffer =newchar[4]; |
| 33 | +// unread part from last read4 call |
| 34 | +privatechar[]cache =newchar[4]; |
34 | 35 | privateintoffset =0; |
35 | 36 | privateintbufsize =0; |
36 | 37 |
|
37 | 38 | /** |
38 | | - * Reads frombuffer first and then use read4 to read more content. |
| 39 | + * Reads fromcache first and then use read4 to read more content. |
39 | 40 | * |
40 | | - * @param buf Destinationbuffer |
| 41 | + * @param buf Destinationcache |
41 | 42 | * @param n Maximum number of characters to read |
42 | 43 | * @return The number of characters read |
43 | 44 | */ |
44 | 45 | publicintread(char[]buf,intn) { |
45 | 46 | intreadBytes =0; |
46 | 47 | booleaneof =false; |
47 | 48 | while (!eof &&readBytes <n) { |
48 | | -intsize = (bufsize >0) ?bufsize :read4(buffer); |
| 49 | +intsize = (bufsize >0) ?bufsize :read4(cache); |
49 | 50 | if (bufsize ==0 &&size <4) { |
50 | 51 | eof =true; |
51 | 52 | } |
52 | 53 | intbytes =Math.min(n -readBytes,size); |
53 | 54 | for (inti =0;i <bytes;i++) { |
54 | | -buf[readBytes +i] =buffer[offset +i]; |
| 55 | +buf[readBytes +i] =cache[offset +i]; |
55 | 56 | } |
56 | 57 | offset = (offset +bytes) %4; |
57 | 58 | bufsize =size -bytes; |
|