public static int byteCount(Slice slice, int offset, int length, int codePointCount)
    {
        requireNonNull(slice, "slice is null");
        if (length < 0) {
            throw new IllegalArgumentException("length must be greater than or equal to zero");
        }
        if (offset < 0 || offset + length > slice.length()) {
            throw new IllegalArgumentException("invalid offset/length");
        }
        if (codePointCount < 0) {
            throw new IllegalArgumentException("codePointsCount must be greater than or equal to zero");
        }
        if (codePointCount == 0) {
            return 0;
        }

        // min codepoint size is 1 byte.
        // if size in bytes is less than the max length
        // we don't need to decode codepoints
        if (codePointCount > length) {
            return length;
        }

        // get the end index with respect to code point count
        int endIndex = offsetOfCodePoint(slice, offset, codePointCount);
        if (endIndex < 0) {
            // end index runs over the slice's length
            return length;
        }
        if (offset > endIndex) {
            throw new AssertionError("offset cannot be smaller than or equal to endIndex");
        }

        // end index could run over length because of large code points (e.g., 4-byte code points)
        // or within length because of small code points (e.g., 1-byte code points)
        return min(endIndex - offset, length);
    }
@Test
    public void testByteCount()
    {
        // Single byte code points
        assertByteCount("abc", 0, 0, 1, "");
        assertByteCount("abc", 0, 1, 0, "");
        assertByteCount("abc", 1, 1, 1, "b");
        assertByteCount("abc", 1, 1, 2, "b");
        assertByteCount("abc", 1, 2, 1, "b");
        assertByteCount("abc", 1, 2, 2, "bc");
        assertByteCount("abc", 1, 2, 3, "bc");
        assertByteCount("abc", 0, 3, 1, "a");
        assertByteCount("abc", 0, 3, 5, "abc");
        assertByteCountFailure("abc", 4, 5, 1);
        assertByteCountFailure("abc", 5, 0, 1);
        assertByteCountFailure("abc", -1, 1, 1);
        assertByteCountFailure("abc", 1, -1, 1);
        assertByteCountFailure("abc", 1, 1, -1);

        // 2 bytes code points
        assertByteCount("абв", 0, 0, 1, "");
        assertByteCount("абв", 0, 1, 0, "");
        assertByteCount("абв", 0, 2, 1, "а");
        assertByteCount("абв", 0, 4, 1, "а");
        assertByteCount("абв", 0, 1, 1, utf8Slice("а").getBytes(0, 1));
        assertByteCount("абв", 2, 2, 2, "б");
        assertByteCount("абв", 2, 2, 0, "");
        assertByteCount("абв", 0, 3, 5, utf8Slice("аб").getBytes(0, 3));
        assertByteCountFailure("абв", 8, 5, 1);
        // we do not check if the offset is in the middle of a code point
        assertByteCount("абв", 1, 1, 5, utf8Slice("а").getBytes(1, 1));
        assertByteCount("абв", 2, 1, 5, utf8Slice("б").getBytes(0, 1));

        // 3 bytes code points
        assertByteCount("\u6000\u6001\u6002\u6003", 0, 0, 2, "");
        assertByteCount("\u6000\u6001\u6002\u6003", 0, 1, 1, utf8Slice("\u6000").getBytes(0, 1));
        assertByteCount("\u6000\u6001\u6002\u6003", 0, 2, 1, utf8Slice("\u6000").getBytes(0, 2));
        assertByteCount("\u6000\u6001\u6002\u6003", 0, 3, 1, "\u6000");
        assertByteCount("\u6000\u6001\u6002\u6003", 0, 6, 1, "\u6000");
        assertByteCount("\u6000\u6001\u6002\u6003", 6, 2, 4, utf8Slice("\u6002").getBytes(0, 2));
        assertByteCount("\u6000\u6001\u6002\u6003", 0, 12, 6, "\u6000\u6001\u6002\u6003");
        // we do not check if the offset is in the middle of a code point
        assertByteCount("\u6000\u6001\u6002\u6003", 1, 6, 2, utf8Slice("\u6000\u6001\u6002").getBytes(1, 6));
        assertByteCount("\u6000\u6001\u6002\u6003", 2, 6, 2, utf8Slice("\u6000\u6001\u6002").getBytes(2, 6));
        assertByteCount("\u6000\u6001\u6002\u6003", 3, 6, 2, utf8Slice("\u6000\u6001\u6002").getBytes(3, 6));
        assertByteCountFailure("\u6000\u6001\u6002\u6003", 21, 0, 1);

        // invalid code points; always return the original lengths unless code point count is 0
        assertByteCount(new byte[] {(byte) 0x81, (byte) 0x81, (byte) 0x81}, 0, 2, 0, new byte[] {});
        assertByteCount(new byte[] {(byte) 0x81, (byte) 0x81, (byte) 0x81}, 0, 2, 1, new byte[] {(byte) 0x81, (byte) 0x81});
        assertByteCount(new byte[] {(byte) 0x81, (byte) 0x81, (byte) 0x81}, 0, 2, 3, new byte[] {(byte) 0x81, (byte) 0x81});
    }