File tree Expand file tree Collapse file tree 1 file changed +6
-5
lines changed
src/_158_ReadNCharactersGivenRead4II Expand file tree Collapse file tree 1 file changed +6
-5
lines changed Original file line number Diff line number Diff line change 30
30
*/
31
31
public class Solution extends Reader4 {
32
32
33
- private char [] buffer = new char [4 ];
33
+ // unread part from last read4 call
34
+ private char [] cache = new char [4 ];
34
35
private int offset = 0 ;
35
36
private int bufsize = 0 ;
36
37
37
38
/**
38
- * Reads from buffer first and then use read4 to read more content.
39
+ * Reads from cache first and then use read4 to read more content.
39
40
*
40
- * @param buf Destination buffer
41
+ * @param buf Destination cache
41
42
* @param n Maximum number of characters to read
42
43
* @return The number of characters read
43
44
*/
44
45
public int read (char [] buf , int n ) {
45
46
int readBytes = 0 ;
46
47
boolean eof = false ;
47
48
while (!eof && readBytes < n ) {
48
- int size = (bufsize > 0 ) ? bufsize : read4 (buffer );
49
+ int size = (bufsize > 0 ) ? bufsize : read4 (cache );
49
50
if (bufsize == 0 && size < 4 ) {
50
51
eof = true ;
51
52
}
52
53
int bytes = Math .min (n - readBytes , size );
53
54
for (int i = 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 ;
You can’t perform that action at this time.
0 commit comments