/**
   * Writes the tail portion of the file to the {@link OutputStream}.
   *
   * @param start The byte offset in the input file where the write operation starts.
   * @return if the file is still being written, this method writes the file until the last newline
   *     character and returns the offset to start the next write operation.
   */
  public long writeLogTo(long start, OutputStream out) throws IOException {
    CountingOutputStream os = new CountingOutputStream(out);

    Session f = source.open();
    f.skip(start);

    if (completed) {
      // write everything till EOF
      byte[] buf = new byte[1024];
      int sz;
      while ((sz = f.read(buf)) >= 0) os.write(buf, 0, sz);
    } else {
      ByteBuf buf = new ByteBuf(null, f);
      HeadMark head = new HeadMark(buf);
      TailMark tail = new TailMark(buf);
      buf = null;

      int readLines = 0;
      while (tail.moveToNextLine(f) && readLines++ < MAX_LINES_READ) {
        head.moveTo(tail, os);
      }
      head.finish(os);
    }

    f.close();
    os.flush();

    return os.getCount() + start;
  }
  /**
   * Writes the section of the file {@link OutputStream}.
   *
   * @param start The byte offset in the input file where the write operation starts.
   * @return if the file is still being written, this method writes the file until the last newline
   *     character and returns the offset to start the next write operation.
   */
  public long writeLogTo(long start, int size, OutputStream out) throws IOException {
    if (size <= 0) {
      return 0;
    }

    CountingOutputStream os = new CountingOutputStream(out);

    Session f = source.open();
    f.skip(start);

    long end = start + size;

    byte[] buf = new byte[size];
    int sz;
    if ((sz = f.read(buf)) >= 0) {
      os.write(buf, 0, sz);
    }
    /*
            if(completed) {
            } else {
                ByteBuf buf = new ByteBuf(null,f, size);
                HeadMark head = new HeadMark(buf);
                TailMark tail = new TailMark(buf);

                int readLines = 0;
                while(tail.moveToNextLine(f) && readLines++ < MAX_LINES_READ) {
                    head.moveTo(tail, os);
                    if (buf.isFull() || os.getCount() >= end) {
                        break;
                    }
                }
                head.finish(os);
            }
    */

    f.close();
    os.flush();

    return os.getCount() + start;
  }