mirror of
https://github.com/square/okhttp.git
synced 2025-12-03 18:31:17 +03:00
Use more convenience methods from Okio 0.8.0.
This commit is contained in:
@@ -717,7 +717,7 @@ public final class MockWebServer {
|
||||
}
|
||||
BufferedSink sink = Okio.buffer(stream.getSink());
|
||||
if (response.getThrottleBytesPerPeriod() == Integer.MAX_VALUE) {
|
||||
sink.write(body, body.size());
|
||||
sink.writeAll(body);
|
||||
sink.flush();
|
||||
} else {
|
||||
while (body.size() > 0) {
|
||||
|
||||
@@ -87,8 +87,7 @@ public class MainTest {
|
||||
try {
|
||||
Buffer buffer = new Buffer();
|
||||
body.writeTo(buffer);
|
||||
return new String(buffer.readByteString(buffer.size()).toByteArray(),
|
||||
body.contentType().charset());
|
||||
return buffer.readString(body.contentType().charset());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
@@ -233,6 +233,6 @@ public class OkApacheClientTest {
|
||||
while ((read = in.read(temp)) != -1) {
|
||||
buffer.write(temp, 0, read);
|
||||
}
|
||||
return buffer.readUtf8(buffer.size());
|
||||
return buffer.readUtf8();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,8 +41,7 @@ public class RecordingCallback implements Response.Callback {
|
||||
Response.Body body = response.body();
|
||||
body.source().readAll(buffer);
|
||||
|
||||
responses.add(new RecordedResponse(
|
||||
response.request(), response, buffer.readUtf8(buffer.size()), null));
|
||||
responses.add(new RecordedResponse(response.request(), response, buffer.readUtf8(), null));
|
||||
notifyAll();
|
||||
}
|
||||
|
||||
|
||||
@@ -76,6 +76,6 @@ public final class RequestTest {
|
||||
private String bodyToHex(Request.Body body) throws IOException {
|
||||
Buffer buffer = new Buffer();
|
||||
body.writeTo(buffer);
|
||||
return buffer.readByteString(buffer.size()).hex();
|
||||
return buffer.readByteString().hex();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public class HpackDraft06Test {
|
||||
List<Header> headerBlock = headerEntries("cookie", new String(value));
|
||||
|
||||
hpackWriter.writeHeaders(headerBlock);
|
||||
bytesIn.write(bytesOut, bytesOut.size());
|
||||
bytesIn.writeAll(bytesOut);
|
||||
hpackReader.readHeaders();
|
||||
hpackReader.emitReferenceSet();
|
||||
|
||||
@@ -76,7 +76,7 @@ public class HpackDraft06Test {
|
||||
out.writeByte(0x0d); // Literal value (len = 13)
|
||||
out.writeUtf8("custom-header");
|
||||
|
||||
bytesIn.write(out, out.size());
|
||||
bytesIn.writeAll(out);
|
||||
hpackReader.maxHeaderTableByteCount(1);
|
||||
hpackReader.readHeaders();
|
||||
hpackReader.emitReferenceSet();
|
||||
@@ -111,7 +111,7 @@ public class HpackDraft06Test {
|
||||
out.writeByte(0x0d); // Literal value (len = 13)
|
||||
out.writeUtf8("custom-header");
|
||||
|
||||
bytesIn.write(out, out.size());
|
||||
bytesIn.writeAll(out);
|
||||
// Set to only support 110 bytes (enough for 2 headers).
|
||||
hpackReader.maxHeaderTableByteCount(110);
|
||||
hpackReader.readHeaders();
|
||||
@@ -150,7 +150,7 @@ public class HpackDraft06Test {
|
||||
out.writeUtf8("custom-header");
|
||||
}
|
||||
|
||||
bytesIn.write(out, out.size());
|
||||
bytesIn.writeAll(out);
|
||||
hpackReader.maxHeaderTableByteCount(16384); // Lots of headers need more room!
|
||||
hpackReader.readHeaders();
|
||||
hpackReader.emitReferenceSet();
|
||||
@@ -173,7 +173,7 @@ public class HpackDraft06Test {
|
||||
(byte) 0x25, (byte) 0xba, (byte) 0x7f};
|
||||
out.write(huffmanBytes, 0, huffmanBytes.length);
|
||||
|
||||
bytesIn.write(out, out.size());
|
||||
bytesIn.writeAll(out);
|
||||
hpackReader.readHeaders();
|
||||
hpackReader.emitReferenceSet();
|
||||
|
||||
@@ -198,7 +198,7 @@ public class HpackDraft06Test {
|
||||
out.writeByte(0x0d); // Literal value (len = 13)
|
||||
out.writeUtf8("custom-header");
|
||||
|
||||
bytesIn.write(out, out.size());
|
||||
bytesIn.writeAll(out);
|
||||
hpackReader.readHeaders();
|
||||
hpackReader.emitReferenceSet();
|
||||
|
||||
@@ -230,7 +230,7 @@ public class HpackDraft06Test {
|
||||
hpackWriter.writeHeaders(headerBlock);
|
||||
assertEquals(expectedBytes, bytesOut);
|
||||
|
||||
bytesIn.write(bytesOut, bytesOut.size());
|
||||
bytesIn.writeAll(bytesOut);
|
||||
hpackReader.readHeaders();
|
||||
hpackReader.emitReferenceSet();
|
||||
|
||||
@@ -254,7 +254,7 @@ public class HpackDraft06Test {
|
||||
hpackWriter.writeHeaders(headerBlock);
|
||||
assertEquals(expectedBytes, bytesOut);
|
||||
|
||||
bytesIn.write(bytesOut, bytesOut.size());
|
||||
bytesIn.writeAll(bytesOut);
|
||||
hpackReader.readHeaders();
|
||||
hpackReader.emitReferenceSet();
|
||||
|
||||
@@ -431,20 +431,17 @@ public class HpackDraft06Test {
|
||||
* http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-06#appendix-D.2
|
||||
*/
|
||||
@Test public void readRequestExamplesWithoutHuffman() throws IOException {
|
||||
Buffer out = firstRequestWithoutHuffman();
|
||||
bytesIn.write(out, out.size());
|
||||
bytesIn.writeAll(firstRequestWithoutHuffman());
|
||||
hpackReader.readHeaders();
|
||||
hpackReader.emitReferenceSet();
|
||||
checkReadFirstRequestWithoutHuffman();
|
||||
|
||||
out = secondRequestWithoutHuffman();
|
||||
bytesIn.write(out, out.size());
|
||||
bytesIn.writeAll(secondRequestWithoutHuffman());
|
||||
hpackReader.readHeaders();
|
||||
hpackReader.emitReferenceSet();
|
||||
checkReadSecondRequestWithoutHuffman();
|
||||
|
||||
out = thirdRequestWithoutHuffman();
|
||||
bytesIn.write(out, out.size());
|
||||
bytesIn.writeAll(thirdRequestWithoutHuffman());
|
||||
hpackReader.readHeaders();
|
||||
hpackReader.emitReferenceSet();
|
||||
checkReadThirdRequestWithoutHuffman();
|
||||
@@ -634,20 +631,17 @@ public class HpackDraft06Test {
|
||||
* http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-06#appendix-D.3
|
||||
*/
|
||||
@Test public void readRequestExamplesWithHuffman() throws IOException {
|
||||
Buffer out = firstRequestWithHuffman();
|
||||
bytesIn.write(out, out.size());
|
||||
bytesIn.writeAll(firstRequestWithHuffman());
|
||||
hpackReader.readHeaders();
|
||||
hpackReader.emitReferenceSet();
|
||||
checkReadFirstRequestWithHuffman();
|
||||
|
||||
out = secondRequestWithHuffman();
|
||||
bytesIn.write(out, out.size());
|
||||
bytesIn.writeAll(secondRequestWithHuffman());
|
||||
hpackReader.readHeaders();
|
||||
hpackReader.emitReferenceSet();
|
||||
checkReadSecondRequestWithHuffman();
|
||||
|
||||
out = thirdRequestWithHuffman();
|
||||
bytesIn.write(out, out.size());
|
||||
bytesIn.writeAll(thirdRequestWithHuffman());
|
||||
hpackReader.readHeaders();
|
||||
hpackReader.emitReferenceSet();
|
||||
checkReadThirdRequestWithHuffman();
|
||||
@@ -928,6 +922,7 @@ public class HpackDraft06Test {
|
||||
|
||||
private void assertBytes(int... bytes) {
|
||||
ByteString expected = intArrayToByteArray(bytes);
|
||||
// TODO change to bytesOut.readByteString() once Okio 0.8.1+ is available.
|
||||
ByteString actual = bytesOut.readByteString(bytesOut.size());
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ public class Http20Draft10Test {
|
||||
frame.writeByte(Http20Draft10.TYPE_HEADERS);
|
||||
frame.writeByte(FLAG_END_HEADERS | FLAG_END_STREAM);
|
||||
frame.writeInt(expectedStreamId & 0x7fffffff);
|
||||
frame.write(headerBytes, headerBytes.size());
|
||||
frame.writeAll(headerBytes);
|
||||
}
|
||||
|
||||
FrameReader fr = new Http20Draft10.Reader(frame, 4096, false);
|
||||
@@ -106,7 +106,7 @@ public class Http20Draft10Test {
|
||||
frame.writeByte(FLAG_END_HEADERS | FLAG_PRIORITY);
|
||||
frame.writeInt(expectedStreamId & 0x7fffffff);
|
||||
frame.writeInt(0); // Highest priority is 0.
|
||||
frame.write(headerBytes, headerBytes.size());
|
||||
frame.writeAll(headerBytes);
|
||||
}
|
||||
|
||||
FrameReader fr = new Http20Draft10.Reader(frame, 4096, false);
|
||||
@@ -149,7 +149,7 @@ public class Http20Draft10Test {
|
||||
frame.writeByte(Http20Draft10.TYPE_CONTINUATION);
|
||||
frame.writeByte(FLAG_END_HEADERS);
|
||||
frame.writeInt(expectedStreamId & 0x7fffffff);
|
||||
frame.write(headerBlock, headerBlock.size());
|
||||
frame.writeAll(headerBlock);
|
||||
}
|
||||
|
||||
FrameReader fr = new Http20Draft10.Reader(frame, 4096, false);
|
||||
@@ -191,7 +191,7 @@ public class Http20Draft10Test {
|
||||
frame.writeByte(Http20Draft10.FLAG_END_PUSH_PROMISE);
|
||||
frame.writeInt(expectedStreamId & 0x7fffffff);
|
||||
frame.writeInt(expectedPromisedStreamId & 0x7fffffff);
|
||||
frame.write(headerBytes, headerBytes.size());
|
||||
frame.writeAll(headerBytes);
|
||||
}
|
||||
|
||||
FrameReader fr = new Http20Draft10.Reader(frame, 4096, false);
|
||||
@@ -557,7 +557,7 @@ public class Http20Draft10Test {
|
||||
frame.writeByte(FLAG_END_HEADERS | FLAG_PAD_HIGH | FLAG_PAD_LOW);
|
||||
frame.writeInt(expectedStreamId & 0x7fffffff);
|
||||
frame.writeShort(paddingLength);
|
||||
frame.write(headerBlock, headerBlock.size());
|
||||
frame.writeAll(headerBlock);
|
||||
frame.write(padding);
|
||||
|
||||
FrameReader fr = new Http20Draft10.Reader(frame, 4096, false);
|
||||
@@ -574,7 +574,7 @@ public class Http20Draft10Test {
|
||||
frame.writeByte(FLAG_END_HEADERS | FLAG_PAD_HIGH | FLAG_PAD_LOW);
|
||||
frame.writeInt(expectedStreamId & 0x7fffffff);
|
||||
frame.writeShort(0);
|
||||
frame.write(headerBlock, headerBlock.size());
|
||||
frame.writeAll(headerBlock);
|
||||
|
||||
FrameReader fr = new Http20Draft10.Reader(frame, 4096, false);
|
||||
fr.nextFrame(assertHeaderBlock());
|
||||
@@ -589,7 +589,7 @@ public class Http20Draft10Test {
|
||||
frame.writeByte(FLAG_END_HEADERS | FLAG_PAD_LOW);
|
||||
frame.writeInt(expectedStreamId & 0x7fffffff);
|
||||
frame.writeByte(0);
|
||||
frame.write(headerBlock, headerBlock.size());
|
||||
frame.writeAll(headerBlock);
|
||||
|
||||
FrameReader fr = new Http20Draft10.Reader(frame, 4096, false);
|
||||
fr.nextFrame(assertHeaderBlock());
|
||||
@@ -608,7 +608,7 @@ public class Http20Draft10Test {
|
||||
frame.writeByte(FLAG_PAD_HIGH);
|
||||
frame.writeInt(expectedStreamId & 0x7fffffff);
|
||||
frame.writeShort(paddingLength);
|
||||
frame.write(headerBlock, headerBlock.size());
|
||||
frame.writeAll(headerBlock);
|
||||
frame.write(padding);
|
||||
|
||||
FrameReader fr = new Http20Draft10.Reader(frame, 4096, false);
|
||||
@@ -636,7 +636,7 @@ public class Http20Draft10Test {
|
||||
frame.writeByte(FLAG_PAD_HIGH | FLAG_PAD_LOW);
|
||||
frame.writeInt(expectedStreamId & 0x7fffffff);
|
||||
frame.writeShort(0xffff);
|
||||
frame.write(headerBlock, headerBlock.size());
|
||||
frame.writeAll(headerBlock);
|
||||
frame.write(padding);
|
||||
|
||||
FrameReader fr = new Http20Draft10.Reader(frame, 4096, false);
|
||||
@@ -676,7 +676,7 @@ public class Http20Draft10Test {
|
||||
frame.writeByte(FLAG_END_HEADERS | FLAG_PAD_HIGH | FLAG_PAD_LOW);
|
||||
frame.writeInt(expectedStreamId & 0x7fffffff);
|
||||
frame.writeShort(paddingLength);
|
||||
frame.write(headerBlock, headerBlock.size());
|
||||
frame.writeAll(headerBlock);
|
||||
frame.write(padding);
|
||||
}
|
||||
|
||||
@@ -703,7 +703,7 @@ public class Http20Draft10Test {
|
||||
frame.writeByte(FLAG_END_HEADERS | FLAG_PAD_HIGH | FLAG_PAD_LOW);
|
||||
frame.writeInt(expectedStreamId & 0x7fffffff);
|
||||
frame.writeShort(0);
|
||||
frame.write(headerBlock, headerBlock.size());
|
||||
frame.writeAll(headerBlock);
|
||||
}
|
||||
|
||||
FrameReader fr = new Http20Draft10.Reader(frame, 4096, false);
|
||||
@@ -728,7 +728,7 @@ public class Http20Draft10Test {
|
||||
frame.writeByte(FLAG_END_HEADERS | FLAG_PAD_LOW);
|
||||
frame.writeInt(expectedStreamId & 0x7fffffff);
|
||||
frame.writeByte(0);
|
||||
frame.write(headerBlock, headerBlock.size());
|
||||
frame.writeAll(headerBlock);
|
||||
}
|
||||
|
||||
FrameReader fr = new Http20Draft10.Reader(frame, 4096, false);
|
||||
@@ -757,7 +757,7 @@ public class Http20Draft10Test {
|
||||
frame.writeByte(FLAG_PAD_HIGH);
|
||||
frame.writeInt(expectedStreamId & 0x7fffffff);
|
||||
frame.writeShort(paddingLength);
|
||||
frame.write(headerBlock, headerBlock.size());
|
||||
frame.writeAll(headerBlock);
|
||||
frame.write(padding);
|
||||
}
|
||||
|
||||
@@ -807,7 +807,7 @@ public class Http20Draft10Test {
|
||||
} catch (IOException e) {
|
||||
assertEquals("PROTOCOL_ERROR padding > 16383: 65535", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test public void tooLargeDataFrame() throws IOException {
|
||||
try {
|
||||
|
||||
@@ -121,7 +121,7 @@ public final class MockSpdyPeer implements Closeable {
|
||||
FrameReader reader = variant.newReader(Okio.buffer(Okio.source(in)), client);
|
||||
|
||||
Iterator<OutFrame> outFramesIterator = outFrames.iterator();
|
||||
byte[] outBytes = bytesOut.readByteString(bytesOut.size()).toByteArray();
|
||||
byte[] outBytes = bytesOut.readByteArray();
|
||||
OutFrame nextOutFrame = null;
|
||||
|
||||
for (int i = 0; i < frameCount; i++) {
|
||||
|
||||
@@ -1547,10 +1547,7 @@ public final class SpdyConnectionTest {
|
||||
}
|
||||
|
||||
private void assertStreamData(String expected, Source source) throws IOException {
|
||||
Buffer buffer = new Buffer();
|
||||
while (source.read(buffer, Long.MAX_VALUE) != -1) {
|
||||
}
|
||||
String actual = buffer.readUtf8(buffer.size());
|
||||
String actual = Okio.buffer(source).readUtf8();
|
||||
assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,6 @@ import java.io.Reader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import okio.Buffer;
|
||||
import okio.BufferedSource;
|
||||
|
||||
import static com.squareup.okhttp.internal.Util.UTF_8;
|
||||
@@ -199,26 +198,17 @@ public final class Response {
|
||||
throw new IOException("Cannot buffer entire body for content length: " + contentLength);
|
||||
}
|
||||
|
||||
// TODO once https://github.com/square/okio/pull/41 is released.
|
||||
// byte[] bytes = source.readByteArray();
|
||||
// if (contentLength != -1 && contentLength != bytes.length) {
|
||||
// throw new IOException("Content-Length and stream length disagree");
|
||||
// }
|
||||
// return bytes;
|
||||
|
||||
BufferedSource source = source();
|
||||
Buffer buffer = new Buffer();
|
||||
long contentRead;
|
||||
byte[] bytes;
|
||||
try {
|
||||
contentRead = buffer.writeAll(source);
|
||||
bytes = source.readByteArray();
|
||||
} finally {
|
||||
Util.closeQuietly(source);
|
||||
}
|
||||
if (contentLength != -1 && contentLength != contentRead) {
|
||||
if (contentLength != -1 && contentLength != bytes.length) {
|
||||
throw new IOException("Content-Length and stream length disagree");
|
||||
}
|
||||
|
||||
return buffer.readByteArray(buffer.size());
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -367,6 +367,7 @@ public class Platform {
|
||||
result.writeByte(protocol.toString().length());
|
||||
result.writeUtf8(protocol.toString());
|
||||
}
|
||||
// TODO change to result.readByteArray() when Okio 0.8.1+ is available.
|
||||
return result.readByteArray(result.size());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,6 +74,6 @@ public final class RetryableSink implements Sink {
|
||||
|
||||
public void writeToSocket(BufferedSink socketOut) throws IOException {
|
||||
// Clone the content; otherwise we won't have data to retry.
|
||||
socketOut.write(content.clone(), content.size());
|
||||
socketOut.writeAll(content.clone());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -397,7 +397,7 @@ public final class Http20Draft10 implements Variant {
|
||||
byte flags = FLAG_END_HEADERS;
|
||||
frameHeader(streamId, length, type, flags); // TODO: CONTINUATION
|
||||
sink.writeInt(promisedStreamId & 0x7fffffff);
|
||||
sink.write(hpackBuffer, hpackBuffer.size());
|
||||
sink.writeAll(hpackBuffer);
|
||||
}
|
||||
|
||||
private void headers(boolean outFinished, int streamId, int priority,
|
||||
@@ -414,7 +414,7 @@ public final class Http20Draft10 implements Variant {
|
||||
if (priority != -1) length += 4;
|
||||
frameHeader(streamId, length, type, flags); // TODO: CONTINUATION
|
||||
if (priority != -1) sink.writeInt(priority & 0x7fffffff);
|
||||
sink.write(hpackBuffer, hpackBuffer.size());
|
||||
sink.writeAll(hpackBuffer);
|
||||
}
|
||||
|
||||
@Override public synchronized void rstStream(int streamId, ErrorCode errorCode)
|
||||
|
||||
@@ -341,7 +341,7 @@ public final class Spdy3 implements Variant {
|
||||
sink.writeInt(streamId & 0x7fffffff);
|
||||
sink.writeInt(associatedStreamId & 0x7fffffff);
|
||||
sink.writeShort((priority & 0x7) << 13 | (unused & 0x1f) << 8 | (slot & 0xff));
|
||||
sink.write(headerBlockBuffer, headerBlockBuffer.size());
|
||||
sink.writeAll(headerBlockBuffer);
|
||||
sink.flush();
|
||||
}
|
||||
|
||||
@@ -356,7 +356,7 @@ public final class Spdy3 implements Variant {
|
||||
sink.writeInt(0x80000000 | (VERSION & 0x7fff) << 16 | type & 0xffff);
|
||||
sink.writeInt((flags & 0xff) << 24 | length & 0xffffff);
|
||||
sink.writeInt(streamId & 0x7fffffff);
|
||||
sink.write(headerBlockBuffer, headerBlockBuffer.size());
|
||||
sink.writeAll(headerBlockBuffer);
|
||||
sink.flush();
|
||||
}
|
||||
|
||||
@@ -371,7 +371,7 @@ public final class Spdy3 implements Variant {
|
||||
sink.writeInt(0x80000000 | (VERSION & 0x7fff) << 16 | type & 0xffff);
|
||||
sink.writeInt((flags & 0xff) << 24 | length & 0xffffff);
|
||||
sink.writeInt(streamId & 0x7fffffff);
|
||||
sink.write(headerBlockBuffer, headerBlockBuffer.size());
|
||||
sink.writeAll(headerBlockBuffer);
|
||||
}
|
||||
|
||||
@Override public synchronized void rstStream(int streamId, ErrorCode errorCode)
|
||||
|
||||
@@ -454,7 +454,7 @@ public final class SpdyStream {
|
||||
// Move the received data to the read buffer to the reader can read it.
|
||||
synchronized (SpdyStream.this) {
|
||||
boolean wasEmpty = readBuffer.size() == 0;
|
||||
readBuffer.write(receiveBuffer, receiveBuffer.size());
|
||||
readBuffer.writeAll(receiveBuffer);
|
||||
if (wasEmpty) {
|
||||
SpdyStream.this.notifyAll();
|
||||
}
|
||||
|
||||
2
pom.xml
2
pom.xml
@@ -33,7 +33,7 @@
|
||||
|
||||
<!-- Compilation -->
|
||||
<java.version>1.6</java.version>
|
||||
<okio.version>0.7.0</okio.version>
|
||||
<okio.version>0.8.0</okio.version>
|
||||
<!-- Targetted to jdk7u60-b13; Oracle jdk7u55-b13. -->
|
||||
<npn.version>1.1.7.v20140316</npn.version>
|
||||
<bouncycastle.version>1.48</bouncycastle.version>
|
||||
|
||||
Reference in New Issue
Block a user