本文整理了Java中okio.Buffer.size()
方法的一些代码示例,展示了Buffer.size()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Buffer.size()
方法的具体详情如下:
包路径:okio.Buffer
类名称:Buffer
方法名:size
[英]Returns the number of bytes currently in this buffer.
[中]返回当前在此缓冲区中的字节数。
代码示例来源:origin: square/okhttp
@Override public long size(File file) {
Buffer buffer = files.get(file);
return buffer != null ? buffer.size() : 0L;
}
代码示例来源:origin: square/okhttp
/** 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;
}
}
代码示例来源:origin: square/okhttp
@Override public void flush() throws IOException {
if (closed) throw new IOException("closed");
writeMessageFrame(formatOpcode, buffer.size(), isFirstFrame, false /* final */);
isFirstFrame = false;
}
代码示例来源:origin: square/okhttp
@Override public void close() throws IOException {
if (closed) throw new IOException("closed");
writeMessageFrame(formatOpcode, buffer.size(), isFirstFrame, true /* final */);
closed = true;
activeWriter = false;
}
}
代码示例来源:origin: square/okhttp
/**
* 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();
}
代码示例来源:origin: square/okhttp
@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);
}
}
代码示例来源:origin: square/okhttp
/**
* 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();
}
}
代码示例来源:origin: square/okhttp
public MockResponse setBody(Buffer body) {
setHeader("Content-Length", body.size());
this.body = body.clone(); // Defensive copy.
return this;
}
代码示例来源:origin: square/okhttp
@Override public void writeTo(BufferedSink sink) throws IOException {
buffer.copyTo(sink.buffer(), 0, buffer.size());
}
}
代码示例来源:origin: square/okhttp
/**
* 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);
}
代码示例来源:origin: square/okhttp
@Override public void writeTo(BufferedSink sink) throws IOException {
Buffer buffer = new Buffer();
while (pipe.source().read(buffer, 8192) != -1L) {
sink.write(buffer, buffer.size());
}
}
}
代码示例来源:origin: square/okhttp
@Override public void flush() throws IOException {
assert (!Thread.holdsLock(Http2Stream.this));
synchronized (Http2Stream.this) {
checkOutNotClosed();
}
while (sendBuffer.size() > 0) {
emitFrame(false);
connection.flush();
}
}
代码示例来源:origin: square/okhttp
@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;
}
代码示例来源:origin: square/okhttp
@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;
}
}
代码示例来源:origin: square/okhttp
@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();
}
}
代码示例来源:origin: square/okhttp
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);
}
代码示例来源:origin: square/okhttp
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);
}
}
代码示例来源:origin: square/okhttp
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());
}
}
代码示例来源:origin: square/okhttp
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());
}
}
代码示例来源:origin: spring-projects/spring-framework
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);
}
内容来源于网络,如有侵权,请联系作者删除!