Saturday, January 17, 2015

Read N Characters Given Read4 II - Call multiple times @LeetCode


The API: int read4(char *buf) reads 4 characters at a time from a file.
The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file.
By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.
Note:
The read function may be called multiple times.

/*
* The read function may be called multiple times.
* The previous one we can't use it for multiple times since read4 actually read more and break continuity.
* This one will sovle that problem. read the buffer used last time.(through offset)
*/
public class Read4KII {
// reused variable.
private char[] buffer = new char[4];
private int bufsize = 0;
private int offset = 0;
public int read(char[] buf, int n) {
int total = 0;
boolean eof = true;
while (eof && total < n) {
if (bufsize == 0) {
bufsize = read4(buffer);
if (bufsize < 4) {
eof = false;
}
}
int bytes = Math.min(n - total, bufsize);
System.arraycopy(buffer, offset, buf, total, bytes);
offset = (offset + bytes) % 4;
bufsize -= bytes;
total += bytes;
}
return total;
}
// API funciton
public int read4(char[] buf) {
return 0;
}
view raw Read4KII.java hosted with ❤ by GitHub

No comments:

Post a Comment