@Override public long size(File file) { Buffer buffer = files.get(file); return buffer != null ? buffer.size() : 0L; }
/** 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) { long bytesWritten = fileChannel.transferFrom(source, pos, byteCount); pos += bytesWritten; byteCount -= bytesWritten; } }
@Override public void flush() throws IOException { if (closed) throw new IOException("closed"); writeMessageFrame(formatOpcode, buffer.size(), isFirstFrame, false /* final */); isFirstFrame = false; }
@Override public void close() throws IOException { if (closed) throw new IOException("closed"); writeMessageFrame(formatOpcode, buffer.size(), isFirstFrame, true /* final */); closed = true; activeWriter = false; } }
/** * Now that we've buffered the entire request body, update the request headers and the body * itself. This happens late to enable HttpURLConnection users to complete the socket connection * before sending request body bytes. */ @Override public Request prepareToSendRequest(Request request) throws IOException { if (request.header("Content-Length") != null) return request; outputStream().close(); contentLength = buffer.size(); return request.newBuilder() .removeHeader("Transfer-Encoding") .header("Content-Length", Long.toString(buffer.size())) .build(); }
@Override public void write(Buffer source, long byteCount) throws IOException { assert (!Thread.holdsLock(Http2Stream.this)); sendBuffer.write(source, byteCount); while (sendBuffer.size() >= EMIT_BUFFER_SIZE) { emitFrame(false); } }
/** * Consumes and returns a non-empty token, terminating at special characters in {@link * #TOKEN_DELIMITERS}. Returns null if the buffer is empty or prefixed with a delimiter. */ private static String readToken(Buffer buffer) { try { long tokenSize = buffer.indexOfElement(TOKEN_DELIMITERS); if (tokenSize == -1L) tokenSize = buffer.size(); return tokenSize != 0L ? buffer.readUtf8(tokenSize) : null; } catch (EOFException e) { throw new AssertionError(); } }
public MockResponse setBody(Buffer body) { setHeader("Content-Length", body.size()); this.body = body.clone(); // Defensive copy. return this; }
@Override public void writeTo(BufferedSink sink) throws IOException { buffer.copyTo(sink.buffer(), 0, buffer.size()); } }
/** * Returns a new response body that transmits {@code content}. If {@code contentType} is non-null * and lacks a charset, this will use UTF-8. */ public static ResponseBody create(@Nullable MediaType contentType, String content) { Charset charset = UTF_8; if (contentType != null) { charset = contentType.charset(); if (charset == null) { charset = UTF_8; contentType = MediaType.parse(contentType + "; charset=utf-8"); } } Buffer buffer = new Buffer().writeString(content, charset); return create(contentType, buffer.size(), buffer); }
@Override public void writeTo(BufferedSink sink) throws IOException { Buffer buffer = new Buffer(); while (pipe.source().read(buffer, 8192) != -1L) { sink.write(buffer, buffer.size()); } } }
@Override public void flush() throws IOException { assert (!Thread.holdsLock(Http2Stream.this)); synchronized (Http2Stream.this) { checkOutNotClosed(); } while (sendBuffer.size() > 0) { emitFrame(false); connection.flush(); } }
@Override public void write(Buffer source, long byteCount) throws IOException { if (closed) throw new IllegalStateException("closed"); checkOffsetAndCount(source.size(), 0, byteCount); if (byteCount > bytesRemaining) { throw new ProtocolException("expected " + bytesRemaining + " bytes but received " + byteCount); } sink.write(source, byteCount); bytesRemaining -= byteCount; }
@Override public void write(Buffer source, long byteCount) throws IOException { if (closed) throw new IOException("closed"); buffer.write(source, byteCount); // Determine if this is a buffered write which we can defer until close() flushes. boolean deferWrite = isFirstFrame && contentLength != -1 && buffer.size() > contentLength - 8192 /* segment size */; long emitCount = buffer.completeSegmentByteCount(); if (emitCount > 0 && !deferWrite) { writeMessageFrame(formatOpcode, emitCount, isFirstFrame, false /* final */); isFirstFrame = false; } }
@Override public void close() throws IOException { long bytesDiscarded; synchronized (Http2Stream.this) { closed = true; bytesDiscarded = readBuffer.size(); readBuffer.clear(); Http2Stream.this.notifyAll(); // TODO(jwilson): Unnecessary? } if (bytesDiscarded > 0) { updateConnectionFlowControl(bytesDiscarded); } cancelStreamIfNecessary(); } }
private void writeHeader( ByteString prefix, long upstreamSize, long metadataSize) throws IOException { Buffer header = new Buffer(); header.write(prefix); header.writeLong(upstreamSize); header.writeLong(metadataSize); if (header.size() != FILE_HEADER_SIZE) throw new IllegalArgumentException(); FileOperator fileOperator = new FileOperator(file.getChannel()); fileOperator.write(0, header, FILE_HEADER_SIZE); }
public synchronized void headers( boolean outFinished, int streamId, List<Header> headerBlock) throws IOException { if (closed) throw new IOException("closed"); hpackWriter.writeHeaders(headerBlock); long byteCount = hpackBuffer.size(); int length = (int) Math.min(maxFrameSize, byteCount); byte type = TYPE_HEADERS; byte flags = byteCount == length ? FLAG_END_HEADERS : 0; if (outFinished) flags |= FLAG_END_STREAM; frameHeader(streamId, length, type, flags); sink.write(hpackBuffer, length); if (byteCount > length) writeContinuationFrames(streamId, byteCount - length); } }
private void completeEvent(String id, String type, Buffer data) throws IOException { skipCrAndOrLf(); if (data.size() != 0L) { lastId = id; data.skip(1L); // Leading newline. callback.onEvent(id, type, data.readUtf8()); } }
private void writeHttpResponse(Socket socket, BufferedSink sink, MockResponse response) throws IOException { sleepIfDelayed(response.getHeadersDelay(TimeUnit.MILLISECONDS)); sink.writeUtf8(response.getStatus()); sink.writeUtf8("\r\n"); writeHeaders(sink, response.getHeaders()); Buffer body = response.getBody(); if (body == null) return; sleepIfDelayed(response.getBodyDelay(TimeUnit.MILLISECONDS)); throttledTransfer(response, socket, body, sink, body.size(), false); if ("chunked".equalsIgnoreCase(response.getHeaders().get("Transfer-Encoding"))) { writeHeaders(sink, response.getTrailers()); } }
private MockResponse jsonPostRequest(RecordedRequest request, String location, String contentType) { if (request.getBodySize() > 0) { assertTrue("Invalid request content-length", Integer.parseInt(request.getHeader("Content-Length")) > 0); assertNotNull("No content-type", request.getHeader("Content-Type")); } return new MockResponse() .setHeader("Location", baseUrl + location) .setHeader("Content-Type", contentType) .setHeader("Content-Length", request.getBody().size()) .setBody(request.getBody()) .setResponseCode(201); }