private static int findPageShiftFrom(long minProbStartPos,
                                       RandomAccessFile randomAccessFile,
                                       Charset charset,
                                       int maxPageBorderShift) throws IOException {

    int offsetToSymbolBeginning = findNextSymbolBeginningOffsetFrom(minProbStartPos, randomAccessFile, charset);

    if (offsetToSymbolBeginning >= maxPageBorderShift) {
      return offsetToSymbolBeginning;
    }

    int bufferLength = minProbStartPos + maxPageBorderShift >= randomAccessFile.length() ?
                       (int)(randomAccessFile.length() - minProbStartPos - offsetToSymbolBeginning) :
                       maxPageBorderShift - offsetToSymbolBeginning;
    byte[] buffer1 = new byte[bufferLength];

    randomAccessFile.seek(minProbStartPos + offsetToSymbolBeginning);
    randomAccessFile.readFully(buffer1);
    String text1 = CharsetToolkit.decodeString(buffer1, charset);


    int substringLength = -1;

    int indexOfSlashN = text1.indexOf('\n');
    if (indexOfSlashN != -1) {
      substringLength = indexOfSlashN + 1;  // for cases: "\n" and "\r\n"
    }
    else {
      int indexOfSlashR = text1.indexOf('\r');
      if (indexOfSlashR != -1) {
        substringLength = indexOfSlashR + 1;  // for case: "\r"
      }
    }

    if (substringLength != -1) {
      int substringLengthInBytes;
      if (charset.equals(StandardCharsets.UTF_16)) {
        substringLengthInBytes = text1.substring(0, substringLength).getBytes(StandardCharsets.UTF_16BE).length;
      }
      else {
        substringLengthInBytes = text1.substring(0, substringLength).getBytes(charset).length;
      }
      return offsetToSymbolBeginning + substringLengthInBytes;
    }
    else {
      return offsetToSymbolBeginning;
    }
  }
private static int findPageShiftFrom(long minProbStartPos,
                                       RandomAccessFile randomAccessFile,
                                       Charset charset,
                                       int maxPageBorderShift)