@Override public void write(Buffer source, long byteCount) throws IOException { long toRead = Math.min(remainingByteCount, byteCount); if (toRead > 0) { source.read(buffer, toRead); } long toSkip = byteCount - toRead; if (toSkip > 0) { source.skip(toSkip); } remainingByteCount -= toRead; receivedByteCount += byteCount; }
buf.read(bytes); result.add(InetAddress.getByAddress(bytes)); } else {
@Test public void moveAllRequestedBytesWithRead() throws Exception { Buffer sink = new Buffer(); sink.writeUtf8(repeat('a', 10)); Buffer source = new Buffer(); source.writeUtf8(repeat('b', 15)); assertEquals(10, source.read(sink, 10)); assertEquals(20, sink.size()); assertEquals(5, source.size()); assertEquals(repeat('a', 10) + repeat('b', 10), sink.readUtf8(20)); }
@Override public void readFully(byte[] sink) throws IOException { try { require(sink.length); } catch (EOFException e) { // The underlying source is exhausted. Copy the bytes we got before rethrowing. int offset = 0; while (buffer.size > 0) { int read = buffer.read(sink, offset, (int) buffer.size); if (read == -1) throw new AssertionError(); offset += read; } throw e; } buffer.readFully(sink); }
@Test public void moveFewerThanRequestedBytesWithRead() throws Exception { Buffer sink = new Buffer(); sink.writeUtf8(repeat('a', 10)); Buffer source = new Buffer(); source.writeUtf8(repeat('b', 20)); assertEquals(20, source.read(sink, 25)); assertEquals(30, sink.size()); assertEquals(0, source.size()); assertEquals(repeat('a', 10) + repeat('b', 20), sink.readUtf8(30)); }
@Override public long read(Buffer sink, long byteCount) throws IOException { synchronized (buffer) { if (sourceClosed) { throw new IllegalStateException("closed"); } while (buffer.size() == 0) { if (sinkClosed) { if (sinkClosedException == null) { return -1L; } throw new IOException(sinkClosedException); } timeout.waitUntilNotified(buffer); } final long result = buffer.read(sink, byteCount); buffer.notifyAll(); return result; } }
readBytesDelivered = readBuffer.read(sink, Math.min(byteCount, readBuffer.size())); unacknowledgedBytesRead += readBytesDelivered;
readBytesDelivered = readBuffer.read(sink, Math.min(byteCount, readBuffer.size())); unacknowledgedBytesRead += readBytesDelivered;
@Test public void retrieveSystemInfo_get_information_if_process_is_up() { Buffer response = new Buffer(); response.read(ProtobufSystemInfo.Section.newBuilder().build().toByteArray()); server.enqueue(new MockResponse().setBody(response)); // initialize registration of process setUpWithHttpUrl(ProcessId.COMPUTE_ENGINE); Optional<ProtobufSystemInfo.SystemInfo> info = underTest.retrieveSystemInfo(); assertThat(info.get().getSectionsCount()).isEqualTo(0); }
@Override public int read(byte[] sink, int offset, int byteCount) { return Buffer.this.read(sink, offset, byteCount); }
@Override public int read(byte[] sink) { return read(sink, 0, sink.length); }
@Override public void readFully(byte[] sink) throws EOFException { int offset = 0; while (offset < sink.length) { int read = read(sink, offset, sink.length - offset); if (read == -1) throw new EOFException(); offset += read; } }
@Override public void write(Buffer source, long byteCount) throws IOException { long toRead = Math.min(remainingByteCount, byteCount); if (toRead > 0) { source.read(buffer, toRead); } long toSkip = byteCount - toRead; if (toSkip > 0) { source.skip(toSkip); } remainingByteCount -= toRead; receivedByteCount += byteCount; }
@Override public void write(Buffer source, long byteCount) throws IOException { long toRead = Math.min(remainingByteCount, byteCount); if (toRead > 0) { source.read(buffer, toRead); } long toSkip = byteCount - toRead; if (toSkip > 0) { source.skip(toSkip); } remainingByteCount -= toRead; receivedByteCount += byteCount; }
@Override public long read(Buffer sink, long byteCount) throws IOException { if (sink == null) throw new IllegalArgumentException("sink == null"); if (byteCount < 0) throw new IllegalArgumentException("byteCount < 0: " + byteCount); if (closed) throw new IllegalStateException("closed"); if (buffer.size == 0) { long read = source.read(buffer, Segment.SIZE); if (read == -1) return -1; } long toRead = Math.min(byteCount, buffer.size); return buffer.read(sink, toRead); }
/** Write {@code byteCount} bytes from {@code source} to the file at {@code pos}. */ public void write(long pos, Buffer source, long byteCount) throws IOException { if (byteCount < 0 || byteCount > source.size()) throw new IndexOutOfBoundsException(); while (byteCount > 0L) { try { // Write bytes to the byte[], and tell the ByteBuffer wrapper about 'em. int toWrite = (int) Math.min(BUFFER_SIZE, byteCount); source.read(byteArray, 0, toWrite); byteBuffer.limit(toWrite); // Copy bytes from the ByteBuffer to the file. do { int bytesWritten = fileChannel.write(byteBuffer, pos); pos += bytesWritten; } while (byteBuffer.hasRemaining()); byteCount -= toWrite; } finally { byteBuffer.clear(); } } }
@Override public int read(byte[] sink, int offset, int byteCount) throws IOException { checkOffsetAndCount(sink.length, offset, byteCount); if (buffer.size == 0) { long read = source.read(buffer, Segment.SIZE); if (read == -1) return -1; } int toRead = (int) Math.min(byteCount, buffer.size); return buffer.read(sink, offset, toRead); }
@Override public long read(Buffer sink, long byteCount) throws IOException { if (closed) throw new IllegalStateException(); return data.read(sink, byteCount); }
@Override public int read(byte[] data, int offset, int byteCount) throws IOException { if (closed) throw new IOException("closed"); checkOffsetAndCount(data.length, offset, byteCount); if (buffer.size == 0) { long count = source.read(buffer, Segment.SIZE); if (count == -1) return -1; } return buffer.read(data, offset, byteCount); }
@Override public long read(Buffer sink, long byteCount) throws IOException { if (byteCount < 0) throw new IllegalArgumentException("byteCount < 0: " + byteCount); long read; synchronized (FramedStream.this) { waitUntilReadable(); checkNotClosed(); if (readBuffer.size() == 0) return -1; // This source is exhausted. // Move bytes from the read buffer into the caller's buffer. read = readBuffer.read(sink, Math.min(byteCount, readBuffer.size())); // Flow control: notify the peer that we're ready for more data! unacknowledgedBytesRead += read; if (unacknowledgedBytesRead >= connection.okHttpSettings.getInitialWindowSize(DEFAULT_INITIAL_WINDOW_SIZE) / 2) { connection.writeWindowUpdateLater(id, unacknowledgedBytesRead); unacknowledgedBytesRead = 0; } } // Update connection.unacknowledgedBytesRead outside the stream lock. synchronized (connection) { // Multiple application threads may hit this section. connection.unacknowledgedBytesRead += read; if (connection.unacknowledgedBytesRead >= connection.okHttpSettings.getInitialWindowSize(DEFAULT_INITIAL_WINDOW_SIZE) / 2) { connection.writeWindowUpdateLater(0, connection.unacknowledgedBytesRead); connection.unacknowledgedBytesRead = 0; } } return read; }