class TrailFilePositionSetter {

  protected static String splitBytesByNewLines(byte[] buf,
                                               int numBytes,
                                               boolean spanFile,
                                               int endOffset,
                                               String prevLine,
                                               List<String> lines,
                                               List<Integer> lineEndPos)
  {
    String s = (prevLine != null) ? prevLine + new String(buf,0,numBytes) : new String(buf,0,numBytes);

    if ( s.isEmpty())
      return null;

    // endOffset should be updated with prepended string
    if ( prevLine != null)
      endOffset += prevLine.length();

    String[] strArr = s.split(NEW_LINE_PATTERN_STR); //Newline can be one \n, \r,\r\n

    // if buf contains only newlines, a zero sized array is returned. Massage it so it works with below logic
    if (strArr.length == 0)
    {
      strArr = new String[1];
      strArr[0] = "";
    }

    int pos = 0;
    for (int i = 0 ; i < strArr.length - 1; i++)
    {
      String l = strArr[i];

      // If this line spans file, split it
      if ( spanFile && (pos <= endOffset) && ((pos + l.length()) > endOffset))
      {
        int pivot = endOffset - pos;
        lines.add(l.substring(0,pivot));
        pos += pivot;
        l = l.substring(pivot);
        lineEndPos.add(-1);
        spanFile = false;
      }

      lines.add(l);
      pos += l.length();
      int newLineLen = 0;

      if (s.startsWith(DOUBLE_CHAR_NEWLINE, pos))
        newLineLen = 2;
      else
        newLineLen = 1;

      pos += newLineLen;
      lineEndPos.add(pos);
    }

    char c = s.charAt(s.length() -1);

    String lastLine = null;

    String l = strArr[strArr.length - 1];
    if ( spanFile && (pos <= endOffset) && ((pos + l.length()) > endOffset))
    {
      int pivot = endOffset - pos;
      lines.add(l.substring(0,pivot));
      pos += pivot;
      l = l.substring(pivot);
      lineEndPos.add(-1);
    }

    /**
     * If last character of the block read is '\r', then we need to read next block before deciding
     * if '\r' is newline or it is followed by '\n'
     */
    if ((c == '\r'))
    {
       lastLine = l + '\r';
    } else if (c != '\n') {
      lastLine = l;
    } else {
      lines.add(l);
      pos += l.length();
      if (s.startsWith(DOUBLE_CHAR_NEWLINE, pos))
        pos += 2;
      else
        pos += 1;
      lineEndPos.add(pos);
    }

    return lastLine;
  }

  public  TrailFilePositionSetter(String dir, String filePrefix);
  public  TrailFilePositionSetter(String dir, String filePrefix, String phySourceName);

}