Read 4
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.
Create a 4-length char array buffer to keep read data.
Read data until get n size or buf is empty (size == 0), put the data which in tmp into buf.
public int read(char[] buf, int n) {
int idx = 0;
char [] tmp = new char[4];
while(idx < n){
int size = read4(tmp), i = 0;
if(size == 0) break;
while(idx < n && i < size){
buf[idx ++] = tmp[i ++];
}
}
return idx;
}