@Override public long read(Buffer sink, long byteCount) throws IOException { try { long read = source.read(sink, byteCount); if (read > 0) { bytesRead += read; } return read; } catch (IOException e) { endOfInput(false, e); throw e; } }
@Test public void gunzipExhaustsSource() throws Exception { Buffer gzippedSource = new Buffer() .write(ByteString.decodeHex("1f8b08000000000000004b4c4a0600c241243503000000")); // 'abc' ExhaustableSource exhaustableSource = new ExhaustableSource(gzippedSource); BufferedSource gunzippedSource = Okio.buffer(new GzipSource(exhaustableSource)); assertEquals('a', gunzippedSource.readByte()); assertEquals('b', gunzippedSource.readByte()); assertEquals('c', gunzippedSource.readByte()); assertFalse(exhaustableSource.exhausted); assertEquals(-1, gunzippedSource.read(new Buffer(), 1)); assertTrue(exhaustableSource.exhausted); }
@Override public long read(Buffer sink, long byteCount) throws IOException { while (left == 0) { source.skip(padding); padding = 0; if ((flags & FLAG_END_HEADERS) != 0) return -1; readContinuationHeader(); // TODO: test case for empty continuation header? } long read = source.read(sink, Math.min(byteCount, left)); if (read == -1) return -1; left -= read; return read; }
@Override public long read(Buffer sink, long byteCount) throws IOException { try { long read = source.read(sink, byteCount); if (read > 0) { bytesRead += read; } return read; } catch (IOException e) { endOfInput(false, e); throw e; } }
@Override public long read(Buffer sink, long byteCount) throws IOException { while (left == 0) { source.skip(padding); padding = 0; if ((flags & FLAG_END_HEADERS) != 0) return -1; readContinuationHeader(); // TODO: test case for empty continuation header? } long read = source.read(sink, Math.min(byteCount, left)); if (read == -1) return -1; left -= read; return read; }
/** * Eagerly reads {@code byteCount} bytes from the source before launching a background task to * process the data. This avoids corrupting the stream. */ void pushDataLater(final int streamId, final BufferedSource source, final int byteCount, final boolean inFinished) throws IOException { final Buffer buffer = new Buffer(); source.require(byteCount); // Eagerly read the frame before firing client thread. source.read(buffer, byteCount); if (buffer.size() != byteCount) throw new IOException(buffer.size() + " != " + byteCount); pushExecutorExecute(new NamedRunnable("OkHttp %s Push Data[%s]", connectionName, streamId) { @Override public void execute() { try { boolean cancel = pushObserver.onData(streamId, buffer, byteCount, inFinished); if (cancel) writer.rstStream(streamId, ErrorCode.CANCEL); if (cancel || inFinished) { synchronized (Http2Connection.this) { currentPushRequests.remove(streamId); } } } catch (IOException ignored) { } } }); }
static void copyResponseBody(Response response, Sink sink) throws IOException { final int bufferSize = 8 * 1024; BufferedSource responseBodySource = response.body().source(); BufferedSink cacheResponseBody = Okio.buffer(sink); while (responseBodySource.read(cacheResponseBody.buffer(), bufferSize) > 0) { cacheResponseBody.emit(); } closeQuietly(responseBodySource); }
long read = source.read(buffer, toRead); if (read == -1) return;
long read = in.read(receiveBuffer, byteCount); if (read == -1) throw new EOFException(); byteCount -= read;
@Override public long read(Buffer sink, long byteCount) throws IOException { long bytesRead; try { bytesRead = source.read(sink, byteCount); } catch (IOException e) { if (!cacheRequestClosed) { cacheRequestClosed = true; cacheRequest.abort(); // Failed to write a complete cache response. } throw e; } if (bytesRead == -1) { if (!cacheRequestClosed) { cacheRequestClosed = true; cacheBody.close(); // The cache response is complete! } return -1; } sink.copyTo(cacheBody.buffer(), sink.size() - bytesRead, bytesRead); cacheBody.emitCompleteSegments(); return bytesRead; }
@Test public void readNioBuffer() throws Exception { String expected = factory.isOneByteAtATime() ? "a" : "abcdefg"; sink.writeUtf8("abcdefg"); sink.emit(); ByteBuffer nioByteBuffer = ByteBuffer.allocate(1024); int byteCount = source.read(nioByteBuffer); assertEquals(expected.length(), byteCount); assertEquals(expected.length(), nioByteBuffer.position()); assertEquals(nioByteBuffer.capacity(), nioByteBuffer.limit()); nioByteBuffer.flip(); byte[] data = new byte[expected.length()]; nioByteBuffer.get(data); assertEquals(expected, new String(data)); }
@Test public void readLargeNioBufferOnlyReadsOneSegment() throws Exception { String expected = factory.isOneByteAtATime() ? "a" : TestUtil.repeat('a', SEGMENT_SIZE); sink.writeUtf8(TestUtil.repeat('a', SEGMENT_SIZE * 4)); sink.emit(); ByteBuffer nioByteBuffer = ByteBuffer.allocate(SEGMENT_SIZE * 3); int byteCount = source.read(nioByteBuffer); assertEquals(expected.length(), byteCount); assertEquals(expected.length(), nioByteBuffer.position()); assertEquals(nioByteBuffer.capacity(), nioByteBuffer.limit()); nioByteBuffer.flip(); byte[] data = new byte[expected.length()]; nioByteBuffer.get(data); assertEquals(expected, new String(data)); }
public static void getOut(final String url) { try { mOkHttpClient.setConnectTimeout(1000, TimeUnit.MINUTES); mOkHttpClient.setReadTimeout(1000, TimeUnit.MINUTES); Request request = new Request.Builder() .url(url) .build(); Response response = mOkHttpClient.newCall(request).execute(); if (response.isSuccessful()) { FileOutputStream fo = new FileOutputStream("/storage/emulated/0/" + "gedangein" + ".json"); byte[] c = new byte[1024]; while (response.body().source().read(c) != -1) { fo.write(c); } } } catch (Exception e) { e.printStackTrace(); } }
@Override public void run() { try { mOkHttpClient.setConnectTimeout(1000, TimeUnit.MINUTES); mOkHttpClient.setReadTimeout(1000, TimeUnit.MINUTES); Request request = new Request.Builder() .url(url) .build(); Response response = mOkHttpClient.newCall(request).execute(); if (response.isSuccessful()) { FileOutputStream fo = new FileOutputStream("/storage/emulated/0/" + name + ".mp3"); byte[] c = new byte[1024]; while (response.body().source().read(c) != -1) { fo.write(c); } } } catch (Exception e) { e.printStackTrace(); } } }).start();
@Override public long read(Buffer sink, long byteCount) throws IOException { long bytesRead; try { bytesRead = source.read(sink, byteCount); } catch (IOException e) { if (!cacheRequestClosed) { cacheRequestClosed = true; cacheRequest.abort(); // Failed to write a complete cache response. } throw e; } if (bytesRead == -1) { if (!cacheRequestClosed) { cacheRequestClosed = true; cacheBody.close(); // The cache response is complete! } return -1; } sink.copyTo(cacheBody.buffer(), sink.size() - bytesRead, bytesRead); cacheBody.emitCompleteSegments(); return bytesRead; }
@Test public void readExhaustedSource() throws Exception { Buffer sink = new Buffer(); sink.writeUtf8(repeat('a', 10)); assertEquals(-1, source.read(sink, 10)); assertEquals(10, sink.size()); assertTrue(source.exhausted()); }
@Test public void readZeroBytesFromSource() throws Exception { Buffer sink = new Buffer(); sink.writeUtf8(repeat('a', 10)); // Either 0 or -1 is reasonable here. For consistency with Android's // ByteArrayInputStream we return 0. assertEquals(-1, source.read(sink, 0)); assertEquals(10, sink.size()); assertTrue(source.exhausted()); }
@Test public void readIntoByteArrayNotEnough() throws IOException { sink.writeUtf8("abcd"); sink.emit(); byte[] sink = new byte[5]; int read = source.read(sink); if (factory.isOneByteAtATime()) { assertEquals(1, read); byte[] expected = { 'a', 0, 0, 0, 0 }; assertByteArraysEquals(expected, sink); } else { assertEquals(4, read); byte[] expected = { 'a', 'b', 'c', 'd', 0 }; assertByteArraysEquals(expected, sink); } }
@Test public void readIntoByteArray() throws IOException { sink.writeUtf8("abcd"); sink.emit(); byte[] sink = new byte[3]; int read = source.read(sink); if (factory.isOneByteAtATime()) { assertEquals(1, read); byte[] expected = { 'a', 0, 0 }; assertByteArraysEquals(expected, sink); } else { assertEquals(3, read); byte[] expected = { 'a', 'b', 'c' }; assertByteArraysEquals(expected, sink); } }
@Test public void readIntoByteArrayOffsetAndCount() throws IOException { sink.writeUtf8("abcd"); sink.emit(); byte[] sink = new byte[7]; int read = source.read(sink, 2, 3); if (factory.isOneByteAtATime()) { assertEquals(1, read); byte[] expected = { 0, 0, 'a', 0, 0, 0, 0 }; assertByteArraysEquals(expected, sink); } else { assertEquals(3, read); byte[] expected = { 0, 0, 'a', 'b', 'c', 0, 0 }; assertByteArraysEquals(expected, sink); } }