/** * 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); }
/** * Returns a {@code CharSource} for the same bytes, converted to UTF-8 using a Byte-Order Mark if available. * * @return the equivalent {@code CharSource} */ public CharSource asCharSourceUtf8UsingBom() { return CharSource.wrap(readUtf8UsingBom()); }
@Override public String toString() { return "ArrayByteSource[" + size() + " bytes]"; }
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]"); }
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_ofUnsafe() { byte[] bytes = {1, 2, 3}; ArrayByteSource test = ArrayByteSource.ofUnsafe(bytes); assertEquals(test.size(), 3); assertEquals(test.read()[0], 1); assertEquals(test.read()[1], 2); assertEquals(test.read()[2], 3); bytes[0] = 4; // abusing the unsafe factory assertEquals(test.read()[0], 4); }
/** * Creates an instance, not copying the array. * <p> * This method is inherently unsafe as it relies on good behavior by callers. * Callers must never make any changes to the passed in array after calling this method. * Doing so would violate the immutability of this class. * * @param array the array, not copied * @return the byte source */ public static ArrayByteSource ofUnsafe(byte[] array) { return new ArrayByteSource(array); }
public void test_from_SupplierExceptionOnCreate() { CheckedSupplier<InputStream> supplier = () -> { throw new IOException(); }; assertThrows(UncheckedIOException.class, () -> ArrayByteSource.from(supplier)); }
@Override public ByteArrayInputStream openBufferedStream() { return openStream(); }
/** * Creates an instance, copying the array. * * @param array the array, copied * @return the byte source */ public static ArrayByteSource copyOf(byte[] array) { return new ArrayByteSource(array.clone()); }
public void test_from_SupplierExceptionOnRead() { CheckedSupplier<InputStream> supplier = () -> { return new InputStream() { @Override public int read() throws IOException { throw new IOException(); } }; }; assertThrows(UncheckedIOException.class, () -> ArrayByteSource.from(supplier)); }
/** * Gets the size, which is always known. * * @return the size, which is always known */ @Override public Optional<Long> sizeIfKnown() { return Optional.of(size()); }