/* The read4 API is defined in the parent class Reader4.
int read4(char[] buf); */
public class Solution extends Reader4 {
/**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/
public int read(char[] buf, int n) {
char[] buffer = new char[4];
boolean flag = true;
int curSize = 0;
int i = 0;
while(i < n && (curSize > 0 || flag)) {
curSize = read4(buffer);
flag = false;
int j = 0;
while(j < curSize && i < n) {
buf[i++] = buffer[j++];
}
}
return i;
}
}
/* The read4 API is defined in the parent class Reader4.
int read4(char[] buf); */
public class Solution extends Reader4 {
/**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/
public int read(char[] buf, int n) {
char[] buf4 = new char[4];
int ptr = 0;
while (ptr < n) {
int remaining = read4(buf4);
if (remaining == 0) break;
for (int i = 0; i < remaining && ptr < n; i++) {
buf[ptr++] = buf4[i];
}
}
return ptr;
}
}