ConvertResult (@NotNull CharBuffer buffer) { int dst = 0; char prev = ' '; int crCount = 0; int lfCount = 0; int crlfCount = 0; int length = buffer.length(); char[] bufferArray = CharArrayUtil.fromSequenceWithoutCopying(buffer); for (int src = 0; src < length; src++) { char c = bufferArray != null ? bufferArray[src]:buffer.charAt(src); switch (c) { case '\r': if(bufferArray != null) bufferArray[dst++] = '\n'; else buffer.put(dst++, '\n'); crCount++; break; case '\n': if (prev == '\r') { crCount--; crlfCount++; } else { if(bufferArray != null) bufferArray[dst++] = '\n'; else buffer.put(dst++, '\n'); lfCount++; } break; default: if(bufferArray != null) bufferArray[dst++] = c; else buffer.put(dst++, c); break; } prev = c; } CharSequence result = buffer.length() == dst ? buffer : buffer.subSequence(0, dst); return new ConvertResult(result, crCount, lfCount, crlfCount); }