public final InputStream byteStream() { return source().inputStream(); }
@Override public int read(char[] cbuf, int off, int len) throws IOException { if (closed) throw new IOException("Stream closed"); Reader delegate = this.delegate; if (delegate == null) { Charset charset = Util.bomAwareCharset(source, this.charset); delegate = this.delegate = new InputStreamReader(source.inputStream(), charset); } return delegate.read(cbuf, off, len); }
public final InputStream byteStream() { return source().inputStream(); }
@Override public int read(char[] cbuf, int off, int len) throws IOException { if (closed) throw new IOException("Stream closed"); Reader delegate = this.delegate; if (delegate == null) { Charset charset = Util.bomAwareCharset(source, this.charset); delegate = this.delegate = new InputStreamReader(source.inputStream(), charset); } return delegate.read(cbuf, off, len); }
} else { if (calculateSize) { BitmapFactory.decodeStream(bufferedSource.peek().inputStream(), null, options); calculateInSampleSize(request.targetWidth, request.targetHeight, checkNotNull(options, "options == null"), request); bitmap = BitmapFactory.decodeStream(bufferedSource.inputStream(), null, options);
@Test public void inputStreamCloses() throws Exception { BufferedSource source = Okio.buffer((Source) new Buffer()); InputStream in = source.inputStream(); in.close(); try { source.require(1); fail(); } catch (IllegalStateException e) { assertEquals("closed", e.getMessage()); } }
public static FilterInputStream getFromCache(Context context, String url) throws Exception { // File cacheDirectory = new File("/storage/emulated/0/Android/data/com.name.demo .dev/cache/HttpCache"); File cacheDirectory = context.getExternalCacheDir(); DiskLruCache cache = DiskLruCache.create(FileSystem.SYSTEM, cacheDirectory, 201105, 2, 1024 * 1024 * 30); cache.flush(); String key = Util.md5Hex(url); final DiskLruCache.Snapshot snapshot; try { snapshot = cache.get(key); if (snapshot == null) { return null; } } catch (IOException e) { return null; } okio.Source source = snapshot.getSource(1); BufferedSource metadata = Okio.buffer(source); FilterInputStream bodyIn = new FilterInputStream(metadata.inputStream()) { @Override public void close() throws IOException { snapshot.close(); super.close(); } }; return bodyIn; }
@Test public void inputStreamCharByChar() throws Exception { sink.writeUtf8("abc"); sink.emit(); InputStream in = source.inputStream(); assertEquals('a', in.read()); assertEquals('b', in.read()); assertEquals('c', in.read()); assertEquals(-1, in.read()); }
@Test public void getInputStream_executed_getRightInputStream() throws IOException { final Call call = mock(Call.class); when(client.newCall(any(Request.class))).thenReturn(call); final ResponseBody body = mock(ResponseBody.class); final Response response = createResponseBuilder() .body(body).build(); when(call.execute()).thenReturn(response); final BufferedSource source = mock(BufferedSource.class); when(body.source()).thenReturn(source); final InputStream expectedInputStream = mock(InputStream.class); when(source.inputStream()).thenReturn(expectedInputStream); connection.execute(); final InputStream resultInputStream = connection.getInputStream(); assertThat(resultInputStream).isEqualTo(expectedInputStream); }
@Test public void inputStreamSkip() throws Exception { sink.writeUtf8("abcde"); sink.emit(); InputStream in = source.inputStream(); assertEquals(4, in.skip(4)); assertEquals('e', in.read()); sink.writeUtf8("abcde"); sink.emit(); assertEquals(5, in.skip(10)); // Try to skip too much. assertEquals(0, in.skip(1)); // Try to skip when exhausted. }
@Test public void inputStreamBounds() throws IOException { sink.writeUtf8(repeat('a', 100)); sink.emit(); InputStream in = source.inputStream(); try { in.read(new byte[100], 50, 51); fail(); } catch (ArrayIndexOutOfBoundsException expected) { } }
@Test public void inputStreamOffsetCount() throws Exception { sink.writeUtf8("abcde"); sink.emit(); InputStream in = source.inputStream(); byte[] bytes = { 'z', 'z', 'z', 'z', 'z' }; int read = in.read(bytes, 1, 3); if (factory.isOneByteAtATime()) { assertEquals(1, read); assertByteArrayEquals("zazzz", bytes); } else { assertEquals(3, read); assertByteArrayEquals("zabcz", bytes); } }
@Test public void inputStream() throws Exception { sink.writeUtf8("abc"); sink.emit(); InputStream in = source.inputStream(); byte[] bytes = { 'z', 'z', 'z' }; int read = in.read(bytes); if (factory.isOneByteAtATime()) { assertEquals(1, read); assertByteArrayEquals("azz", bytes); read = in.read(bytes); assertEquals(1, read); assertByteArrayEquals("bzz", bytes); read = in.read(bytes); assertEquals(1, read); assertByteArrayEquals("czz", bytes); } else { assertEquals(3, read); assertByteArrayEquals("abc", bytes); } assertEquals(-1, in.read()); }
@Test public void gzipSink() throws Exception { Pipe pipe = new Pipe(1024 * 1024); GzipSink gzipSink = new GzipSink(pipe.sink()); // Disable compression to speed up a slow test. Improved from 141s to 35s on one machine. gzipSink.deflater().setLevel(Deflater.NO_COMPRESSION); Future<Long> future = readAllAndCloseAsync(randomSource(FOUR_GIB_PLUS_ONE), gzipSink); HashingSink hashingSink = HashingSink.sha256(Okio.blackhole()); GZIPInputStream gzipIn = new GZIPInputStream(Okio.buffer(pipe.source()).inputStream()); readAllAndClose(Okio.source(gzipIn), hashingSink); assertEquals(FOUR_GIB_PLUS_ONE, (long) future.get()); assertEquals(SHA256_RANDOM_FOUR_GIB_PLUS_1, hashingSink.hash()); }
InputStream is = bufferedSource.inputStream(); try { is.read();
@Test public void inputStreamTracksSegments() throws Exception { Buffer source = new Buffer(); source.writeUtf8("a"); source.writeUtf8(repeat('b', SEGMENT_SIZE)); source.writeUtf8("c"); InputStream in = Okio.buffer((Source) source).inputStream(); assertEquals(0, in.available()); assertEquals(SEGMENT_SIZE + 2, source.size()); // Reading one byte buffers a full segment. assertEquals('a', in.read()); assertEquals(SEGMENT_SIZE - 1, in.available()); assertEquals(2, source.size()); // Reading as much as possible reads the rest of that buffered segment. byte[] data = new byte[SEGMENT_SIZE * 2]; assertEquals(SEGMENT_SIZE - 1, in.read(data, 0, data.length)); assertEquals(repeat('b', SEGMENT_SIZE - 1), new String(data, 0, SEGMENT_SIZE - 1, UTF_8)); assertEquals(2, source.size()); // Continuing to read buffers the next segment. assertEquals('b', in.read()); assertEquals(1, in.available()); assertEquals(0, source.size()); // Continuing to read reads from the buffer. assertEquals('c', in.read()); assertEquals(0, in.available()); assertEquals(0, source.size()); // Once we've exhausted the source, we're done. assertEquals(-1, in.read()); assertEquals(0, source.size()); }
@Override public Parsed call(@Nonnull BufferedSource source) { try (InputStreamReader reader = new InputStreamReader(source.inputStream(), Charset.forName("UTF-8"))) { return gson.fromJson(reader, type); } catch (IOException e) { throw new RuntimeException(e); } } }
@Override public int read(char[] cbuf, int off, int len) throws IOException { if (closed) throw new IOException("Stream closed"); Reader delegate = this.delegate; if (delegate == null) { Charset charset = Util.bomAwareCharset(source, this.charset); delegate = this.delegate = new InputStreamReader(source.inputStream(), charset); } return delegate.read(cbuf, off, len); }
@Override public int read(char[] cbuf, int off, int len) throws IOException { if (closed) throw new IOException("Stream closed"); Reader delegate = this.delegate; if (delegate == null) { Charset charset = Util.bomAwareCharset(source, this.charset); delegate = this.delegate = new InputStreamReader(source.inputStream(), charset); } return delegate.read(cbuf, off, len); }
@Override public Parsed apply(@NonNull BufferedSource bufferedSource) throws ParserException { try (InputStreamReader reader = new InputStreamReader(bufferedSource.inputStream(), Charset.forName("UTF-8"))) { return gson.fromJson(reader, type); } catch (IOException e) { throw new ParserException(e.getMessage(), e); } } }