class TailableFile implements Closeable {

    static int readLines(TailableFile file, ByteBuffer buffer, int maxLines, FileChangeListener listener) throws IOException {
        int lines = 0;
        while (buffer.hasRemaining() && lines < maxLines) {
            int startPos = buffer.position();
            boolean hasNewLine = skipUntil(buffer, NEW_LINE);
            int length = buffer.position() - startPos;
            if (hasNewLine) {
                length--;
                // for Windows-style \r\n line endings
                if (length > 0 && buffer.get(buffer.position() - 2) == '\r') {
                    length--;
                }
                lines++;
            }
            if (length > 0) {
                while (!listener.onLineAvailable(file, buffer.array(), startPos, length, hasNewLine)) ;
            }
        }
        return lines;
    }

    public  TailableFile(File file);

}