Read N Characters Given Read4

来源:互联网 发布:淘宝神笔有什么作用 编辑:程序博客网 时间:2024/06/02 18:42
/* 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     */         public int read(char[] buf, int n) {        char[] temp = new char[4];        int total = 0;        boolean end = false;        while (!end && total < n) {            int count = read4(temp);            if (count < 4) {                end = true;            }            for (int i = 0; i < count && total < n; i++) {                buf[total++] = temp[i];            }        }        return total;    }         // public int read(char[] buf, int n) {    //     if (buf.length <= n) {    //         return buf.length;    //     } else {    //         return n;    //     }    // }}

0 0
原创粉丝点击