int (int offset) { if (offset < 0 || offset > getTextLength()) { throw new IndexOutOfBoundsException("Wrong offset: " + offset + ". Available text length: " + getTextLength()); } if (offset == 0) return 0; if (offset == getTextLength()) return getLineCount() - 1; int bsResult = Arrays.binarySearch(myLineEnds, offset); return bsResult >= 0 ? bsResult : -bsResult - 1; }