ConvertResult (byte @NotNull [] charsAsBytes, int startOffset, int endOffset) { // convert \r\n to \n, \r to \n, wide char to UNDEFINED_CHAR char prev = ' '; int crCount = 0; int lfCount = 0; int crlfCount = 0; StringBuilder result = new StringBuilder(endOffset - startOffset); for (int src = startOffset; src < endOffset; src++) { char c = (char)charsAsBytes[src]; if (c >= 128) { result.append(UNDEFINED_CHAR); continue; } switch (c) { case '\r': result.append('\n'); crCount++; break; case '\n': if (prev == '\r') { crCount--; crlfCount++; } else { result.append('\n'); lfCount++; } break; default: result.append(c); break; } prev = c; } return new ConvertResult(result, crCount, lfCount, crlfCount); }