RandomAccessData { public RandomAccessData increment() throws IOException { RandomAccessData copy = copy(); for (int i = copy.size - 1; i >= 0; --i) { if (copy.buffer[i] != UnsignedBytes.MAX_VALUE) { copy.buffer[i] = UnsignedBytes.checkedCast(UnsignedBytes.toInt(copy.buffer[i]) + 1L); return copy; } } return POSITIVE_INFINITY; } RandomAccessData();  RandomAccessData(byte[] initialBuffer);  RandomAccessData(int initialBufferSize); RandomAccessData increment(); byte[] array(); int size(); void resetTo(int position); OutputStream asOutputStream(); InputStream asInputStream(final int offset, final int length); void writeTo(OutputStream out, int offset, int length); void readFrom(InputStream inStream, int offset, int length); RandomAccessData copy(); @Override boolean equals(@Nullable Object other); @Override int hashCode(); @Override String toString(); static final UnsignedLexicographicalComparator UNSIGNED_LEXICOGRAPHICAL_COMPARATOR; }
@Test public void testIncrement() throws Exception { assertEquals( new RandomAccessData(new byte[] {0x00, 0x01}), new RandomAccessData(new byte[] {0x00, 0x00}).increment()); assertEquals( new RandomAccessData(new byte[] {0x01, UnsignedBytes.MAX_VALUE}), new RandomAccessData(new byte[] {0x00, UnsignedBytes.MAX_VALUE}).increment()); assertSame(RandomAccessData.POSITIVE_INFINITY, new RandomAccessData(new byte[0]).increment()); assertSame( RandomAccessData.POSITIVE_INFINITY, new RandomAccessData(new byte[] {UnsignedBytes.MAX_VALUE}).increment()); assertSame(RandomAccessData.POSITIVE_INFINITY, RandomAccessData.POSITIVE_INFINITY.increment()); }