/** * Copies the contents of this byte source to the given {@code OutputStream}. Does not close * {@code output}. * * @return the number of bytes copied * @throws IOException if an I/O error occurs while reading from this source or writing to {@code * output} */ @CanIgnoreReturnValue public long copyTo(OutputStream output) throws IOException { checkNotNull(output); Closer closer = Closer.create(); try { InputStream in = closer.register(openStream()); return ByteStreams.copy(in, output); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
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()); }
/** * Copies all the bytes from one file to another. * * <p>Copying is not an atomic operation - in the case of an I/O error, power loss, process * termination, or other problems, {@code to} may not be a complete copy of {@code from}. If you * need to guard against those conditions, you should employ other file-level synchronization. * * <p><b>Warning:</b> If {@code to} represents an existing file, that file will be overwritten * with the contents of {@code from}. If {@code to} and {@code from} refer to the <i>same</i> * file, the contents of that file will be deleted. * * <p><b>{@link java.nio.file.Path} equivalent:</b> {@link * java.nio.file.Files#copy(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption...)}. * * @param from the source file * @param to the destination file * @throws IOException if an I/O error occurs * @throws IllegalArgumentException if {@code from.equals(to)} */ public static void copy(File from, File to) throws IOException { checkArgument(!from.equals(to), "Source %s and destination %s must be different", from, to); asByteSource(from).copyTo(asByteSink(to)); }
/** * Reads the full contents of this byte source as a byte array. * * @throws IOException if an I/O error occurs while reading from this source */ public byte[] read() throws IOException { Closer closer = Closer.create(); try { InputStream in = closer.register(openStream()); Optional<Long> size = sizeIfKnown(); return size.isPresent() ? ByteStreams.toByteArray(in, size.get()) : ByteStreams.toByteArray(in); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
@Override protected void _send(final byte[] bytes) throws Exception { List<Integer[]> lines = split(bytes); if (lines.size() == 1) { data = ByteSource.concat(data, DATA, ByteSource.wrap(bytes), NL); } else { for (Integer[] line : lines) { data = ByteSource.concat(data, DATA, ByteSource.wrap(bytes) .slice(line[0], line[1] - line[0]), NL); } } }
Optional<Long> sizeIfKnown = sizeIfKnown(); if (sizeIfKnown.isPresent()) { return sizeIfKnown.get(); Closer closer = Closer.create(); try { InputStream in = closer.register(openStream()); return countBySkipping(in); } catch (IOException e) { closer.close(); closer = Closer.create(); try { InputStream in = closer.register(openStream()); return ByteStreams.exhaust(in); } catch (Throwable e) { throw closer.rethrow(e);
public void test_ofUrl() throws Exception { File file = new File("src/test/resources/com/opengamma/strata/collect/io/TestFile.txt"); URL url = file.toURI().toURL(); ResourceLocator test = ResourceLocator.ofUrl(url); String locator = test.getLocator(); assertTrue(locator.startsWith("url:file:")); assertTrue(locator.endsWith("src/test/resources/com/opengamma/strata/collect/io/TestFile.txt")); assertEquals(test.getByteSource().read()[0], 'H'); assertEquals(test.getCharSource().readLines(), ImmutableList.of("HelloWorld")); assertEquals(test.getCharSource(StandardCharsets.UTF_8).readLines(), ImmutableList.of("HelloWorld")); assertEquals(test.toString(), locator); }
@Test public void testPrivateKeySpecPem() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException { RSAPrivateCrtKey key = (RSAPrivateCrtKey) KeyFactory.getInstance("RSA").generatePrivate( Pems.privateKeySpec(ByteSource.wrap(PRIVATE_KEY.getBytes(Charsets.UTF_8)))); String encoded = Pems.pem(key); assertEquals(encoded, PRIVATE_KEY.replaceAll("\n", ls)); }
public void test_ofClasspathUrl() throws Exception { URL url = Resources.getResource("com/opengamma/strata/collect/io/TestFile.txt"); ResourceLocator test = ResourceLocator.ofClasspathUrl(url); assertTrue(test.getLocator().startsWith("classpath")); assertTrue(test.getLocator().endsWith("com/opengamma/strata/collect/io/TestFile.txt")); assertEquals(test.getByteSource().read()[0], 'H'); assertEquals(test.getCharSource().readLines(), ImmutableList.of("HelloWorld")); assertEquals(test.getCharSource(StandardCharsets.UTF_8).readLines(), ImmutableList.of("HelloWorld")); assertTrue(test.toString().startsWith("classpath")); assertTrue(test.toString().endsWith("com/opengamma/strata/collect/io/TestFile.txt")); }
@Test(groups = { "integration", "live" }) public void testPutIncorrectContentMD5() throws InterruptedException, IOException { byte[] payload = createTestInput(1024).read(); HashCode contentMD5 = md5().hashBytes(new byte[0]); try { putBlobWithMd5(payload, contentMD5); fail(); } catch (HttpResponseException hre) { if (hre.getResponse().getStatusCode() != getIncorrectContentMD5StatusCode()) { throw hre; } } }
public void test_equalsHashCodeToString() { ByteSource source = ByteSource.wrap(SAMPLE.getBytes(StandardCharsets.UTF_8)); XmlFile test = XmlFile.of(source); XmlFile test2 = XmlFile.of(source); assertFalse(test.equals(null)); assertFalse(test.equals(ANOTHER_TYPE)); assertEquals(test, test); assertEquals(test, test2); assertEquals(test.hashCode(), test2.hashCode()); assertEquals(test.toString(), test2.toString()); }
/** * Checks that the contents of this byte source are equal to the contents of the given byte * source. * * @throws IOException if an I/O error occurs while reading from this source or {@code other} */ public boolean contentEquals(ByteSource other) throws IOException { checkNotNull(other); byte[] buf1 = createBuffer(); byte[] buf2 = createBuffer(); Closer closer = Closer.create(); try { InputStream in1 = closer.register(openStream()); InputStream in2 = closer.register(other.openStream()); while (true) { int read1 = ByteStreams.read(in1, buf1, 0, buf1.length); int read2 = ByteStreams.read(in2, buf2, 0, buf2.length); if (read1 != read2 || !Arrays.equals(buf1, buf2)) { return false; } else if (read1 != buf1.length) { return true; } } } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
/** * Reads the contents of this byte source using the given {@code processor} to process bytes as * they are read. Stops when all bytes have been read or the consumer returns {@code false}. * Returns the result produced by the processor. * * @throws IOException if an I/O error occurs while reading from this source or if {@code * processor} throws an {@code IOException} * @since 16.0 */ @Beta @CanIgnoreReturnValue // some processors won't return a useful result public <T> T read(ByteProcessor<T> processor) throws IOException { checkNotNull(processor); Closer closer = Closer.create(); try { InputStream in = closer.register(openStream()); return ByteStreams.readBytes(in, processor); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
public void test_from_Supplier() { ByteSource source = ByteSource.wrap(new byte[] {1, 2, 3}); ArrayByteSource test = ArrayByteSource.from(() -> source.openStream()); assertEquals(test.size(), 3); assertEquals(test.read()[0], 1); assertEquals(test.read()[1], 2); assertEquals(test.read()[2], 3); }
public void test_ofFile() throws Exception { File file = new File("src/test/resources/com/opengamma/strata/collect/io/TestFile.txt"); ResourceLocator test = ResourceLocator.ofFile(file); assertEquals(test.getLocator(), "file:src/test/resources/com/opengamma/strata/collect/io/TestFile.txt"); assertEquals(test.getByteSource().read()[0], 'H'); assertEquals(test.getCharSource().readLines(), ImmutableList.of("HelloWorld")); assertEquals(test.getCharSource(StandardCharsets.UTF_8).readLines(), ImmutableList.of("HelloWorld")); assertEquals(test.toString(), "file:src/test/resources/com/opengamma/strata/collect/io/TestFile.txt"); }
@BeforeTestWithContext public void setup() throws Exception { hdfsClient.createDirectory("/user/hive/warehouse/TestAvroSchemaUrl/schemas"); try (InputStream inputStream = Resources.asByteSource(Resources.getResource("avro/original_schema.avsc")).openStream()) { hdfsClient.saveFile("/user/hive/warehouse/TestAvroSchemaUrl/schemas/original_schema.avsc", inputStream); } }
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { URL dataUrl = Resources.getResource(TestExampleClient.class, request.getPathInfo()); Resources.asByteSource(dataUrl).copyTo(response.getOutputStream()); } }
public void testOpenStream() throws IOException { InputStream in = source.openStream(); try { byte[] readBytes = ByteStreams.toByteArray(in); assertExpectedBytes(readBytes); } finally { in.close(); } }
public void testOpenBufferedStream() throws IOException { InputStream in = source.openBufferedStream(); try { byte[] readBytes = ByteStreams.toByteArray(in); assertExpectedBytes(readBytes); } finally { in.close(); } }
/** * Computes the hash code of the {@code file} using {@code hashFunction}. * * @param file the file to read * @param hashFunction the hash function to use to hash the data * @return the {@link HashCode} of all of the bytes in the file * @throws IOException if an I/O error occurs * @since 12.0 * @deprecated Prefer {@code asByteSource(file).hash(hashFunction)}. This method is scheduled to * be removed in January 2019. */ @Deprecated public static HashCode hash(File file, HashFunction hashFunction) throws IOException { return asByteSource(file).hash(hashFunction); }