/** * Obtains an instance by copying part of an array. * <p> * The input array is copied and not mutated. * * @param array the array to copy * @param fromIndex the offset from the start of the array * @return an array containing the specified values * @throws IndexOutOfBoundsException if the index is invalid */ public static ArrayByteSource copyOf(byte[] array, int fromIndex) { return copyOf(array, fromIndex, array.length); }
public void test_copyOf_fromTo_empty() { byte[] bytes = {1, 2, 3}; ArrayByteSource test = ArrayByteSource.copyOf(bytes, 1, 1); assertEquals(test.size(), 0); }
public void test_copyOf_fromTo_bad() { byte[] bytes = {1, 2, 3}; assertThrows(IndexOutOfBoundsException.class, () -> ArrayByteSource.copyOf(bytes, -1, 2)); assertThrows(IndexOutOfBoundsException.class, () -> ArrayByteSource.copyOf(bytes, 0, 4)); assertThrows(IndexOutOfBoundsException.class, () -> ArrayByteSource.copyOf(bytes, 4, 5)); }
public void test_copyOf_fromTo() { byte[] bytes = {1, 2, 3}; ArrayByteSource test = ArrayByteSource.copyOf(bytes, 1, 2); assertEquals(test.size(), 1); assertEquals(test.read()[0], 2); }
public void test_copyOf_from() { byte[] bytes = {1, 2, 3}; ArrayByteSource test = ArrayByteSource.copyOf(bytes, 1); assertEquals(test.size(), 2); assertEquals(test.read()[0], 2); assertEquals(test.read()[1], 3); }
public void test_from_ByteSource_alreadyArrayByteSource() { ArrayByteSource base = ArrayByteSource.copyOf(new byte[] {1, 2, 3}); ArrayByteSource test = ArrayByteSource.from(base); assertSame(test, base); }
public void test_copyOf() { byte[] bytes = {1, 2, 3}; ArrayByteSource test = ArrayByteSource.copyOf(bytes); assertEquals(test.size(), 3); assertEquals(test.read()[0], 1); assertEquals(test.read()[1], 2); assertEquals(test.read()[2], 3); bytes[0] = 4; assertEquals(test.read()[0], 1); }
public void test_read() { ArrayByteSource test = ArrayByteSource.copyOf(new byte[] {1, 2, 3}); assertEquals(test.size(), 3); byte[] safeArray = test.read(); safeArray[0] = 4; assertEquals(test.read()[0], 1); assertEquals(test.read()[1], 2); assertEquals(test.read()[2], 3); }
public void test_readUnsafe() { ArrayByteSource test = ArrayByteSource.copyOf(new byte[] {1, 2, 3}); assertEquals(test.size(), 3); byte[] unsafeArray = test.readUnsafe(); unsafeArray[0] = 4; // abusing the unsafe array assertEquals(test.read()[0], 4); assertEquals(test.read()[1], 2); assertEquals(test.read()[2], 3); }
public void test_methods() throws IOException { ArrayByteSource test = ArrayByteSource.copyOf(new byte[] {65, 66, 67}); assertEquals(test.size(), 3); assertEquals(test.isEmpty(), false); assertEquals(test.sizeIfKnown().isPresent(), true); assertEquals(test.sizeIfKnown().get(), (Long) 3L); assertEquals(test.readUtf8(), "ABC"); assertEquals(test.readUtf8UsingBom(), "ABC"); assertEquals(test.asCharSourceUtf8UsingBom().read(), "ABC"); assertEquals(test.toString(), "ArrayByteSource[3 bytes]"); }