File tree 1 file changed +13
-11
lines changed
1 file changed +13
-11
lines changed Original file line number Diff line number Diff line change 1
1
/**
2
2
* The read4 API is defined in the parent class Reader4.
3
- * int read4(char[] buf );
3
+ * int read4(char[] buf4 );
4
4
*/
5
5
6
6
public class Solution extends Reader4 {
@@ -10,17 +10,19 @@ public class Solution extends Reader4 {
10
10
* @return The number of actual characters read
11
11
*/
12
12
public int read (char [] buf , int n ) {
13
- boolean endOfFile = false ;
14
- int totalLength = 0 ;
15
- char [] temp = new char [4 ];
16
- while (!endOfFile && totalLength < n ) {
17
- int count = read4 (temp );
18
- endOfFile = count < 4 ;
19
- count = Math .min (count , n - totalLength );
20
- for (int i = 0 ; i < count ; i ++) {
21
- buf [totalLength ++] = temp [i ];
13
+ int copied = 0 ;
14
+ int readLength = 4 ;
15
+ char [] buf4 = new char [4 ];
16
+ while (copied < n && readLength == 4 ) {
17
+ readLength = read4 (buf4 );
18
+ for (int i = 0 ; i < readLength ; i ++) {
19
+ if (copied == n ) {
20
+ return copied ;
21
+ }
22
+ buf [copied ] = buf4 [i ];
23
+ copied ++;
22
24
}
23
25
}
24
- return totalLength ;
26
+ return copied ;
25
27
}
26
28
}
You can’t perform that action at this time.
0 commit comments