LeetCode: Read N Characters Given Read4

LeetCode: Read N Characters Given Read4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

/**
* Description
*
* @author hzhou
*/
public class ReadNCharactersGivenReadFour 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];
int readByteCount = 0;
boolean eof = false;
while (readByteCount < n && !eof) {
int c = read4(buffer);
if (c < 4) {
eof = true;
}
int bytes = Math.min(n - readByteCount, c);
System.arraycopy(buffer, 0, buf, readByteCount, bytes);
readByteCount += bytes;
}
return readByteCount;
}
}