@@ -0,0 +1,1404 @@
+/***%0A * The API: int read4(char *buf) reads 4 characters at a time from a file.%0A * %0A * The return value is the actual number of characters read. %0A * For example, it returns 3 if there is only 3 characters left in the file.%0A * %0A * By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.%0A * %0A * Note:%0A * The read function may be called multiple times.%0A */%0A%0A/*%0A *  The key to solve this problem is: %0A *%0A *    You have to buffer the extra data return from %60read4()%60 API, which is for next call for %60read()%60 .%0A */%0Apublic class Solution %7B%0A    private char%5B%5D buffer = new char%5B4%5D;%0A    int bufsize = 0, offset = 0;%0A    /**%0A     * @param buf Destination buffer%0A     * @param n Number of characters to read4%0A     * @return The number of characters read%0A     */%0A    public int read(char%5B%5D buf, int n) %7B%0A        int readBytes = 0;%0A        boolean eof = false;%0A        %0A        while (!eof && readBytes %3C n) %7B%0A            int sz = (bufsize %3E 0): bufsize : read4(buffer);%0A            if (bufsize == 0 && sz %3C 4) eof = true;%0A            int bytes = Math.min(n - readBytes, sz);%0A            System.arraycopy(buffer /* src */, offset /* srcPos */, buf /* dest */, readBytes /* destPos */, bytes /* length */);%0A            offset = (offset + bytes) %25 4;%0A            bufsize = sz - bytes;%0A            readBytes += bytes;%0A        %7D%0A%0A        return readBytes;%0A    %7D%0A%7D%0A

add ReadNCharactersGiveRead4II.java