@Override public Reader openStream() throws IOException { return new InputStreamReader(ByteSource.this.openStream(), charset); }
/** * Opens a new buffered {@link InputStream} for reading from this source. The returned stream is * not required to be a {@link BufferedInputStream} in order to allow implementations to simply * delegate to {@link #openStream()} when the stream returned by that method does not benefit from * additional buffering (for example, a {@code ByteArrayInputStream}). This method returns a new, * independent stream each time it is called. * * <p>The caller is responsible for ensuring that the returned stream is closed. * * @throws IOException if an I/O error occurs while opening the stream * @since 15.0 (in 14.0 with return type {@link BufferedInputStream}) */ public InputStream openBufferedStream() throws IOException { InputStream in = openStream(); return (in instanceof BufferedInputStream) ? (BufferedInputStream) in : new BufferedInputStream(in); }
/** Closes the current input stream and opens the next one, if any. */ private void advance() throws IOException { close(); if (it.hasNext()) { in = it.next().openStream(); } }
@Override public InputStream openStream() throws IOException { return sliceStream(ByteSource.this.openStream()); }
@Override public InputStream openStream() throws IOException { return gzipInputStream(in.openStream()); } },
/** * 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(); } }
/** Closes the current input stream and opens the next one, if any. */ private void advance() throws IOException { close(); if (it.hasNext()) { in = it.next().openStream(); } }
@Override public InputStream openStream() throws IOException { return sliceStream(ByteSource.this.openStream()); }
@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]--; } }; } };
/** * 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(); } }
/** * Copies the contents of this byte source to the given {@code ByteSink}. * * @return the number of bytes copied * @throws IOException if an I/O error occurs while reading from this source or writing to {@code * sink} */ @CanIgnoreReturnValue public long copyTo(ByteSink sink) throws IOException { checkNotNull(sink); Closer closer = Closer.create(); try { InputStream in = closer.register(openStream()); OutputStream out = closer.register(sink.openStream()); return ByteStreams.copy(in, out); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
/** * Returns whether the source has zero bytes. The default implementation first checks {@link * #sizeIfKnown}, returning true if it's known to be zero and false if it's known to be non-zero. * If the size is not known, it falls back to opening a stream and checking for EOF. * * <p>Note that, in cases where {@code sizeIfKnown} returns zero, it is <i>possible</i> that bytes * are actually available for reading. (For example, some special files may return a size of 0 * despite actually having content when read.) This means that a source may return {@code true} * from {@code isEmpty()} despite having readable content. * * @throws IOException if an I/O error occurs * @since 15.0 */ public boolean isEmpty() throws IOException { Optional<Long> sizeIfKnown = sizeIfKnown(); if (sizeIfKnown.isPresent()) { return sizeIfKnown.get() == 0L; } Closer closer = Closer.create(); try { InputStream in = closer.register(openStream()); return in.read() == -1; } 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(); } }
/** * 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(); } }
public void testOpenStream() throws IOException { InputStream in = source.openStream(); try { byte[] readBytes = ByteStreams.toByteArray(in); assertExpectedBytes(readBytes); } finally { in.close(); } }
public ExampleRecordCursor(List<ExampleColumnHandle> columnHandles, ByteSource byteSource) { this.columnHandles = columnHandles; fieldToColumnIndex = new int[columnHandles.size()]; for (int i = 0; i < columnHandles.size(); i++) { ExampleColumnHandle columnHandle = columnHandles.get(i); fieldToColumnIndex[i] = columnHandle.getOrdinalPosition(); } try (CountingInputStream input = new CountingInputStream(byteSource.openStream())) { lines = byteSource.asCharSource(UTF_8).readLines().iterator(); totalBytes = input.getCount(); } catch (IOException e) { throw new UncheckedIOException(e); } }
@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); } }
private String readLog(TaskLogs taskLogs, String logFile, long offset) throws IOException { return StringUtils.fromUtf8(ByteStreams.toByteArray(taskLogs.streamTaskLog(logFile, offset).get().openStream())); } }
public void testReadSingleByte() throws Exception { ByteSource source = newByteSource(0, 10); ByteSource joined = ByteSource.concat(source, source); assertEquals(20, joined.size()); InputStream in = joined.openStream(); assertFalse(in.markSupported()); assertEquals(10, in.available()); int total = 0; while (in.read() != -1) { total++; } assertEquals(0, in.available()); assertEquals(20, total); }
/** * Tests that the default slice() behavior is correct when the source is sliced starting at an * offset that is greater than the current length of the source, a stream is then opened to that * source, and finally additional bytes are appended to the source before the stream is read. * * <p>Without special handling, it's possible to have reads of the open stream start <i>before</i> * the offset at which the slice is supposed to start. */ // TODO(cgdecker): Maybe add a test for this to ByteSourceTester public void testSlice_appendingAfterSlicing() throws IOException { // Source of length 5 AppendableByteSource source = new AppendableByteSource(newPreFilledByteArray(5)); // Slice it starting at offset 10. ByteSource slice = source.slice(10, 5); // Open a stream to the slice. InputStream in = slice.openStream(); // Append 10 more bytes to the source. source.append(newPreFilledByteArray(5, 10)); // The stream reports no bytes... importantly, it doesn't read the byte at index 5 when it // should be reading the byte at index 10. // We could use a custom InputStream instead to make the read start at index 10, but since this // is a racy situation anyway, this behavior seems reasonable. assertEquals(-1, in.read()); }