/* 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
*/
Queue<Character> queue = new LinkedList<>();
public int read(char[] buf, int n) {
if (n < 1) {
return 0;
}
int readCount = 0;
while (!queue.isEmpty() && readCount < n) {
buf[readCount++] = queue.poll();
}
boolean eof = false;
while (!eof && readCount < n) {
char[] temp = new char[4];
int count = read4(temp);
if (count < 4) {
eof = true;
}
int finalCount = Math.min(count, n - readCount);
int i = 0;
for (; i < finalCount; i++) {
buf[readCount++] = temp[i];
}
if (finalCount < count) {
for (; i < count; i++) {
queue.offer(temp[i]);
}
}
}
return readCount;
}
}
Read N Characters Given Read4 II - Call multiple times
最新推荐文章于 2020-03-02 06:24:09 发布