/* 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 */ private int prevStart = 0; private int prevEnd = 0; private char[] buffered = new char[4]; public int read(char[] buf, int n) { int currentStart = 0; while (currentStart < n) { if (prevStart == 0) { prevEnd = read4(buffered); } if (prevEnd == 0) { break; } while (currentStart < n && prevStart < prevEnd) { buf[currentStart++] = buffered[prevStart++]; } if (prevStart >= prevEnd) { prevStart = 0; } } return currentStart; } }
时间: 2024-10-04 03:46:44