@Override
    public int read(char cbuf[], int off, int len) throws IOException {
        // System.out.println("read() off:" + off + " len:" + len);
        // Fetch new line if necessary
        if (curLine == null) {
            getNextLine();
        }

        // Return characters from current line
        if (curLine != null) {


            int num = Math.min(len, Math.min(cbuf.length - off,
                    curLine.length() - curLineIx));
            // Copy characters from curLine to cbuf
            for (int i = 0; i < num; i++) {
                cbuf[off++] = curLine.charAt(curLineIx++);
            }

            // No more characters in curLine
            if (curLineIx == curLine.length()) {
                curLine = null;

                // Is there room for the newline?
                if (num < len && off < cbuf.length) {
                    cbuf[off++] = '\n';
                    num++;
                }
            }

            // Return number of character read
            // System.out.println("read " + num);
            return num;
        } else if (len > 0) {
            // No more characters left in input reader
            return -1;
        } else {
            // Client did not ask for any characters
            return 0;
        }
    }