Skip to content

Commit

Permalink
Use more convenience methods from Okio 0.8.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Apr 25, 2014
1 parent f4c83e8 commit 7bfcaa6
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ private void writeResponse(SpdyStream stream, MockResponse response) throws IOEx
}
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) {
Expand Down
3 changes: 1 addition & 2 deletions okcurl/src/test/java/com/squareup/okhttp/curl/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ private static String bodyAsString(Request.Body body) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,6 @@ private static String gunzip(HttpEntity body) throws IOException {
while ((read = in.read(temp)) != -1) {
buffer.write(temp, 0, read);
}
return buffer.readUtf8(buffer.size());
return buffer.readUtf8();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();

Expand All @@ -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();

Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -634,20 +631,17 @@ private void checkReadThirdRequestWithoutHuffman() {
* 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();
Expand Down Expand Up @@ -928,6 +922,7 @@ private void checkEntry(Header entry, String name, String value, int size) {

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -106,7 +106,7 @@ public void headers(boolean outFinished, boolean inFinished, int streamId,
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);
Expand Down Expand Up @@ -149,7 +149,7 @@ public void headers(boolean outFinished, boolean inFinished, int streamId,
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);
Expand Down Expand Up @@ -191,7 +191,7 @@ public void headers(boolean outFinished, boolean inFinished, int streamId,
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);
Expand Down Expand Up @@ -557,7 +557,7 @@ public void pushPromise(int streamId, int promisedStreamId, List<Header> headerB
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);
Expand All @@ -574,7 +574,7 @@ public void pushPromise(int streamId, int promisedStreamId, List<Header> headerB
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());
Expand All @@ -589,7 +589,7 @@ public void pushPromise(int streamId, int promisedStreamId, List<Header> headerB
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());
Expand All @@ -608,7 +608,7 @@ public void pushPromise(int streamId, int promisedStreamId, List<Header> headerB
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);
Expand Down Expand Up @@ -636,7 +636,7 @@ public void pushPromise(int streamId, int promisedStreamId, List<Header> headerB
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);
Expand Down Expand Up @@ -676,7 +676,7 @@ public void pushPromise(int streamId, int promisedStreamId, List<Header> headerB
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);
}

Expand All @@ -703,7 +703,7 @@ public void pushPromise(int streamId, int promisedStreamId, List<Header> headerB
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);
Expand All @@ -728,7 +728,7 @@ public void pushPromise(int streamId, int promisedStreamId, List<Header> headerB
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);
Expand Down Expand Up @@ -757,7 +757,7 @@ public void pushPromise(int streamId, int promisedStreamId, List<Header> headerB
frame.writeByte(FLAG_PAD_HIGH);
frame.writeInt(expectedStreamId & 0x7fffffff);
frame.writeShort(paddingLength);
frame.write(headerBlock, headerBlock.size());
frame.writeAll(headerBlock);
frame.write(padding);
}

Expand Down Expand Up @@ -807,7 +807,7 @@ public void pushPromise(int streamId, int promisedStreamId, List<Header> headerB
} catch (IOException e) {
assertEquals("PROTOCOL_ERROR padding > 16383: 65535", e.getMessage());
}
}
}

@Test public void tooLargeDataFrame() throws IOException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private void readAndWriteFrames() throws IOException {
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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1547,10 +1547,7 @@ private SpdyConnection.Builder connectionBuilder(MockSpdyPeer peer, Variant vari
}

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);
}

Expand Down
18 changes: 4 additions & 14 deletions okhttp/src/main/java/com/squareup/okhttp/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
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;
Expand Down Expand Up @@ -199,26 +198,17 @@ public final byte[] bytes() throws IOException {
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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ static byte[] concatLengthPrefixed(List<Protocol> protocols) {
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ public long contentLength() throws IOException {

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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ static final class Writer implements FrameWriter {
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,
Expand All @@ -414,7 +414,7 @@ private void headers(boolean outFinished, int streamId, int priority,
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)
Expand Down
Loading

0 comments on commit 7bfcaa6

Please sign in to comment.