@Override public byte[] read() throws IOException { Closer closer = Closer.create(); try { FileInputStream in = closer.register(openStream()); return ByteStreams.toByteArray(in, in.getChannel().size()); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
private static byte[] getBytecode(Class<?> clazz) { try (InputStream stream = clazz.getClassLoader().getResourceAsStream(clazz.getName().replace('.', '/') + ".class")) { checkArgument(stream != null, "Could not obtain byte code for class %s", clazz.getName()); return ByteStreams.toByteArray(stream); } catch (IOException e) { throw new RuntimeException(format("Could not obtain byte code for class %s", clazz.getName()), e); } } }
private static String readBody(Response response) { try { return new String(toByteArray(response.getInputStream()), US_ASCII); } catch (IOException e) { return ""; } }
private static ClassReader readClass(String name, ClassLoader classLoader) { try (InputStream in = classLoader.getResourceAsStream(binaryName(name) + CLASS_FILE_SUFFIX)) { if (in == null) { throw new RuntimeException("Failed to read class: " + name); } return new ClassReader(toByteArray(in)); } catch (IOException e) { throw new UncheckedIOException(e); } }
/** * 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(); } }
private byte[] getCanonicalBytes() throws IOException { int resourceId = ResourceIds.raw.canonical; Resources resources = context.getResources(); InputStream is = resources.openRawResource(resourceId); return ByteStreams.toByteArray(is); } }
private byte[] loadVideoBytes() throws IOException { Resources resources = context.getResources(); InputStream is = resources.openRawResource(ResourceIds.raw.video); return ByteStreams.toByteArray(is); } }
public void testToByteArray() throws IOException { InputStream in = new ByteArrayInputStream(PRE_FILLED_100); byte[] b = ByteStreams.toByteArray(in); assertEquals(PRE_FILLED_100, b); }
public void testToByteArray_withSize_givenSmallerSize() throws IOException { InputStream in = new ByteArrayInputStream(PRE_FILLED_100); byte[] b = ByteStreams.toByteArray(in, 80); assertEquals(PRE_FILLED_100, b); }
public void testToByteArray_withSize_givenSizeZero() throws IOException { InputStream in = new ByteArrayInputStream(PRE_FILLED_100); byte[] b = ByteStreams.toByteArray(in, 0); assertEquals(PRE_FILLED_100, b); }
public void testToByteArray_withSize_givenLargerSize() throws IOException { InputStream in = new ByteArrayInputStream(PRE_FILLED_100); byte[] b = ByteStreams.toByteArray(in, 120); assertEquals(PRE_FILLED_100, b); }
public void testToByteArray_withSize_givenSizeOneSmallerThanActual() throws IOException { InputStream in = new ByteArrayInputStream(PRE_FILLED_100); // this results in toByteArrayInternal being called when the stream is actually exhausted byte[] b = ByteStreams.toByteArray(in, 99); assertEquals(PRE_FILLED_100, b); }
public void testToByteArray_withSize_givenCorrectSize() throws IOException { InputStream in = new ByteArrayInputStream(PRE_FILLED_100); byte[] b = ByteStreams.toByteArray(in, 100); assertEquals(PRE_FILLED_100, b); }
public void testToByteArray_withSize_givenSizeTwoSmallerThanActual() throws IOException { InputStream in = new ByteArrayInputStream(PRE_FILLED_100); byte[] b = ByteStreams.toByteArray(in, 98); assertEquals(PRE_FILLED_100, b); }
@Override public byte[] read() throws IOException { try (SeekableByteChannel channel = Files.newByteChannel(path, options)) { return ByteStreams.toByteArray(Channels.newInputStream(channel), channel.size()); } }
public void testOpenBufferedStream() throws IOException { InputStream in = source.openBufferedStream(); try { byte[] readBytes = ByteStreams.toByteArray(in); assertExpectedBytes(readBytes); } finally { in.close(); } }
public void testOpenStream() throws IOException { InputStream in = source.openStream(); try { byte[] readBytes = ByteStreams.toByteArray(in); assertExpectedBytes(readBytes); } finally { in.close(); } }
public void testToByteArray_largeStream() throws IOException { // well, large enough to require multiple buffers byte[] expected = newPreFilledByteArray(10000000); InputStream in = new ByteArrayInputStream(expected); byte[] b = ByteStreams.toByteArray(in); assertEquals(expected, b); }
public void testToByteArray_emptyStream() throws IOException { InputStream in = newTestStream(0); byte[] b = ByteStreams.toByteArray(in); assertEquals(new byte[0], b); }
public static Map.Entry<SchemaTableName, KafkaTopicDescription> loadTpchTopicDescription(JsonCodec<KafkaTopicDescription> topicDescriptionJsonCodec, String topicName, SchemaTableName schemaTableName) throws IOException { KafkaTopicDescription tpchTemplate = topicDescriptionJsonCodec.fromJson(ByteStreams.toByteArray(TestUtils.class.getResourceAsStream(format("/tpch/%s.json", schemaTableName.getTableName())))); return new AbstractMap.SimpleImmutableEntry<>( schemaTableName, new KafkaTopicDescription(schemaTableName.getTableName(), Optional.of(schemaTableName.getSchemaName()), topicName, tpchTemplate.getKey(), tpchTemplate.getMessage())); }