@Override public String read() throws IOException { // Reading all the data as a byte array is more efficient than the default read() // implementation because: // 1. the string constructor can avoid an extra copy most of the time by correctly sizing the // internal char array (hard to avoid using StringBuilder) // 2. we avoid extra copies into temporary buffers altogether // The downside is that this will cause us to store the file bytes in memory twice for a short // amount of time. return new String(ByteSource.this.read(), charset); }
@Override public String read() throws IOException { // Reading all the data as a byte array is more efficient than the default read() // implementation because: // 1. the string constructor can avoid an extra copy most of the time by correctly sizing the // internal char array (hard to avoid using StringBuilder) // 2. we avoid extra copies into temporary buffers altogether // The downside is that this will cause us to store the file bytes in memory twice for a short // amount of time. return new String(ByteSource.this.read(), charset); }
/** * Reads all bytes from a URL into a byte array. * * @param url the URL to read from * @return a byte array containing all the bytes from the URL * @throws IOException if an I/O error occurs */ public static byte[] toByteArray(URL url) throws IOException { return asByteSource(url).read(); }
/** * Reads all bytes from a file into a byte array. * * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link java.nio.file.Files#readAllBytes}. * * @param file the file to read from * @return a byte array containing all the bytes from file * @throws IllegalArgumentException if the file is bigger than the largest possible byte array * (2^31 - 1) * @throws IOException if an I/O error occurs */ public static byte[] toByteArray(File file) throws IOException { return asByteSource(file).read(); }
@Override public String read() throws IOException { // Reading all the data as a byte array is more efficient than the default read() // implementation because: // 1. the string constructor can avoid an extra copy most of the time by correctly sizing the // internal char array (hard to avoid using StringBuilder) // 2. we avoid extra copies into temporary buffers altogether // The downside is that this will cause us to store the file bytes in memory twice for a short // amount of time. return new String(ByteSource.this.read(), charset); }
/** * Reads all bytes from a URL into a byte array. * * @param url the URL to read from * @return a byte array containing all the bytes from the URL * @throws IOException if an I/O error occurs */ public static byte[] toByteArray(URL url) throws IOException { return asByteSource(url).read(); }
/** * Reads all bytes from a file into a byte array. * * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link java.nio.file.Files#readAllBytes}. * * @param file the file to read from * @return a byte array containing all the bytes from file * @throws IllegalArgumentException if the file is bigger than the largest possible byte array * (2^31 - 1) * @throws IOException if an I/O error occurs */ public static byte[] toByteArray(File file) throws IOException { return asByteSource(file).read(); }
/** * Process the bytes of a file. * * <p>(If this seems too complicated, maybe you're looking for {@link #toByteArray}.) * * @param file the file to read * @param processor the object to which the bytes of the file are passed. * @return the result of the byte processor * @throws IOException if an I/O error occurs * @deprecated Prefer {@code asByteSource(file).read(processor)}. This method is scheduled to be * removed in January 2019. */ @Deprecated @CanIgnoreReturnValue // some processors won't return a useful result public static <T> T readBytes(File file, ByteProcessor<T> processor) throws IOException { return asByteSource(file).read(processor); }
/** * Reads all bytes from a URL into a byte array. * * @param url the URL to read from * @return a byte array containing all the bytes from the URL * @throws IOException if an I/O error occurs */ public static byte[] toByteArray(URL url) throws IOException { return asByteSource(url).read(); }
public void testRead() throws IOException { byte[] readBytes = source.read(); assertExpectedBytes(readBytes); }
public void testRead_usingByteProcessor() throws IOException { byte[] readBytes = source.read( new ByteProcessor<byte[]>() { final ByteArrayOutputStream out = new ByteArrayOutputStream(); @Override public boolean processBytes(byte[] buf, int off, int len) throws IOException { out.write(buf, off, len); return true; } @Override public byte[] getResult() { return out.toByteArray(); } }); assertExpectedBytes(readBytes); }
public void testSlice_constrainedRange() throws IOException { long size = source.read().length; if (size >= 2) { ByteSource sliced = source.slice(1, size - 2); assertEquals(size - 2, sliced.read().length); ByteSource resliced = sliced.slice(0, size - 1); assertTrue(sliced.contentEquals(resliced)); } }
public void testReset() throws Exception { byte[] data = newPreFilledByteArray(100); FileBackedOutputStream out = new FileBackedOutputStream(Integer.MAX_VALUE); ByteSource source = out.asByteSource(); out.write(data); assertTrue(Arrays.equals(data, source.read())); out.reset(); assertTrue(Arrays.equals(new byte[0], source.read())); out.write(data); assertTrue(Arrays.equals(data, source.read())); out.close(); } }
public void testToByteArray() throws IOException { File asciiFile = getTestFile("ascii.txt"); File i18nFile = getTestFile("i18n.txt"); assertTrue(Arrays.equals(ASCII.getBytes(Charsets.US_ASCII), Files.toByteArray(asciiFile))); assertTrue(Arrays.equals(I18N.getBytes(Charsets.UTF_8), Files.toByteArray(i18nFile))); assertTrue(Arrays.equals(I18N.getBytes(Charsets.UTF_8), Files.asByteSource(i18nFile).read())); }
/** * @param input the size of the input source * @param offset the first argument to {@link ByteSource#slice} * @param length the second argument to {@link ByteSource#slice} * @param expectRead the number of bytes we expect to read */ private static void assertCorrectSlice(int input, int offset, long length, int expectRead) throws IOException { checkArgument(expectRead == (int) Math.max(0, Math.min(input, offset + length) - offset)); byte[] expected = newPreFilledByteArray(offset, expectRead); ByteSource source = new TestByteSource(newPreFilledByteArray(input)); ByteSource slice = source.slice(offset, length); assertArrayEquals(expected, slice.read()); }
public void testConcat_infiniteIterable() throws IOException { ByteSource source = ByteSource.wrap(new byte[] {0, 1, 2, 3}); Iterable<ByteSource> cycle = Iterables.cycle(ImmutableList.of(source)); ByteSource concatenated = ByteSource.concat(cycle); byte[] expected = {0, 1, 2, 3, 0, 1, 2, 3}; assertArrayEquals(expected, concatenated.slice(0, 8).read()); }
public void testOnlyOneOpen() throws Exception { final ByteSource source = newByteSource(0, 50); final int[] counter = new int[1]; ByteSource checker = new ByteSource() { @Override public InputStream openStream() throws IOException { if (counter[0]++ != 0) { throw new IllegalStateException("More than one source open"); } return new FilterInputStream(source.openStream()) { @Override public void close() throws IOException { super.close(); counter[0]--; } }; } }; byte[] result = ByteSource.concat(checker, checker, checker).read(); assertEquals(150, result.length); }
public void testWriteErrorAfterClose() throws Exception { byte[] data = newPreFilledByteArray(100); FileBackedOutputStream out = new FileBackedOutputStream(50); ByteSource source = out.asByteSource(); out.write(data); assertTrue(Arrays.equals(data, source.read())); out.close(); try { out.write(42); fail("expected exception"); } catch (IOException expected) { } // Verify that write had no effect assertTrue(Arrays.equals(data, source.read())); out.reset(); }
public void testConcat() throws IOException { ByteSource b1 = ByteSource.wrap(new byte[] {0, 1, 2, 3}); ByteSource b2 = ByteSource.wrap(new byte[0]); ByteSource b3 = ByteSource.wrap(new byte[] {4, 5}); byte[] expected = {0, 1, 2, 3, 4, 5}; assertArrayEquals(expected, ByteSource.concat(ImmutableList.of(b1, b2, b3)).read()); assertArrayEquals(expected, ByteSource.concat(b1, b2, b3).read()); assertArrayEquals(expected, ByteSource.concat(ImmutableList.of(b1, b2, b3).iterator()).read()); assertEquals(expected.length, ByteSource.concat(b1, b2, b3).size()); assertFalse(ByteSource.concat(b1, b2, b3).isEmpty()); ByteSource emptyConcat = ByteSource.concat(ByteSource.empty(), ByteSource.empty()); assertTrue(emptyConcat.isEmpty()); assertEquals(0, emptyConcat.size()); }
assertTrue(Arrays.equals(data, source.read()));