public static int computeTruncatedLength(Slice slice, int offset, int length, int maxCodePointCount, boolean isCharType) { if (isCharType) { // truncate the characters and then remove the trailing white spaces return byteCountWithoutTrailingSpace(slice, offset, length, maxCodePointCount); } if (maxCodePointCount >= 0 && length > maxCodePointCount) { return byteCount(slice, offset, length, maxCodePointCount); } return length; }
public static int byteCountWithoutTrailingSpace(Slice slice, int offset, int length, int codePointCount) { int truncatedLength = byteCount(slice, offset, length, codePointCount); return byteCountWithoutTrailingSpace(slice, offset, truncatedLength); }
public static Slice truncateToLength(Slice slice, int maxLength) { requireNonNull(slice, "slice is null"); if (maxLength < 0) { throw new IllegalArgumentException("Max length must be greater or equal than zero"); } if (maxLength == 0) { return Slices.EMPTY_SLICE; } return slice.slice(0, byteCount(slice, 0, slice.length(), maxLength)); }
private static void assertByteCountFailure(String string, int offset, int length, int codePointCount) { try { byteCount(utf8Slice(string), offset, length, codePointCount); fail("Expected exception"); } catch (IllegalArgumentException expected) { } }
private static void assertByteCount(byte[] actual, int offset, int length, int codePointCount, byte[] expected) { Slice slice = wrappedBuffer(actual); int truncatedLength = byteCount(slice, offset, length, codePointCount); byte[] bytes = slice.getBytes(offset, truncatedLength); assertEquals(bytes, expected); } }
public static int byteCountWithoutTrailingSpace(Slice slice, int offset, int length, int codePointCount) { int truncatedLength = byteCount(slice, offset, length, codePointCount); return byteCountWithoutTrailingSpace(slice, offset, truncatedLength); }
public static int computeTruncatedLength(Slice slice, int offset, int length, int maxCodePointCount, boolean isCharType) { if (isCharType) { // truncate the characters and then remove the trailing white spaces return byteCountWithoutTrailingSpace(slice, offset, length, maxCodePointCount); } if (maxCodePointCount >= 0 && length > maxCodePointCount) { return byteCount(slice, offset, length, maxCodePointCount); } return length; }
public static Slice truncateToLength(Slice slice, int maxLength) { requireNonNull(slice, "slice is null"); if (maxLength < 0) { throw new IllegalArgumentException("Max length must be greater or equal than zero"); } if (maxLength == 0) { return Slices.EMPTY_SLICE; } return slice.slice(0, byteCount(slice, 0, slice.length(), maxLength)); }