From 13c06879f4e98c86436bdca765046ef815e34838 Mon Sep 17 00:00:00 2001 From: "lingming.yb" Date: Sun, 22 Dec 2013 11:22:12 +0800 Subject: [PATCH 1/3] Buffered Stream wrapper should be taken place before SpdyConnection build. --- .../java/com/squareup/okhttp/Connection.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/okhttp/src/main/java/com/squareup/okhttp/Connection.java b/okhttp/src/main/java/com/squareup/okhttp/Connection.java index a6b798da9..95a644311 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/Connection.java +++ b/okhttp/src/main/java/com/squareup/okhttp/Connection.java @@ -106,13 +106,14 @@ public final class Connection implements Closeable { if (route.address.sslSocketFactory != null) { upgradeToTls(tunnelRequest); } - - // Use MTU-sized buffers to send fewer packets. - int mtu = Platform.get().getMtu(socket); - if (mtu < 1024) mtu = 1024; - if (mtu > 8192) mtu = 8192; - in = new BufferedInputStream(in, mtu); - out = new BufferedOutputStream(out, mtu); + else{ + // Use MTU-sized buffers to send fewer packets. + int mtu = Platform.get().getMtu(socket); + if (mtu < 1024) mtu = 1024; + if (mtu > 8192) mtu = 8192; + in = new BufferedInputStream(in, mtu); + out = new BufferedOutputStream(out, mtu); + } } /** @@ -152,6 +153,13 @@ public final class Connection implements Closeable { out = sslSocket.getOutputStream(); in = sslSocket.getInputStream(); + + // Use MTU-sized buffers to send fewer packets. + int mtu = Platform.get().getMtu(socket); + if (mtu < 1024) mtu = 1024; + if (mtu > 8192) mtu = 8192; + in = new BufferedInputStream(in, mtu); + out = new BufferedOutputStream(out, mtu); byte[] selectedProtocol; if (useNpn && (selectedProtocol = platform.getNpnSelectedProtocol(sslSocket)) != null) { From 35ead0f5d17b0c53610b0947aa9bd8fee6da50fa Mon Sep 17 00:00:00 2001 From: "lingming.yb" Date: Mon, 23 Dec 2013 17:02:52 +0800 Subject: [PATCH 2/3] extract a method --- .../java/com/squareup/okhttp/Connection.java | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/okhttp/src/main/java/com/squareup/okhttp/Connection.java b/okhttp/src/main/java/com/squareup/okhttp/Connection.java index 95a644311..f89930bb7 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/Connection.java +++ b/okhttp/src/main/java/com/squareup/okhttp/Connection.java @@ -107,12 +107,7 @@ public final class Connection implements Closeable { upgradeToTls(tunnelRequest); } else{ - // Use MTU-sized buffers to send fewer packets. - int mtu = Platform.get().getMtu(socket); - if (mtu < 1024) mtu = 1024; - if (mtu > 8192) mtu = 8192; - in = new BufferedInputStream(in, mtu); - out = new BufferedOutputStream(out, mtu); + streamWrapper(); } } @@ -153,13 +148,7 @@ public final class Connection implements Closeable { out = sslSocket.getOutputStream(); in = sslSocket.getInputStream(); - - // Use MTU-sized buffers to send fewer packets. - int mtu = Platform.get().getMtu(socket); - if (mtu < 1024) mtu = 1024; - if (mtu > 8192) mtu = 8192; - in = new BufferedInputStream(in, mtu); - out = new BufferedOutputStream(out, mtu); + streamWrapper(); byte[] selectedProtocol; if (useNpn && (selectedProtocol = platform.getNpnSelectedProtocol(sslSocket)) != null) { @@ -340,4 +329,13 @@ public final class Connection implements Closeable { } } } + + private void streamWrapper(){ + //Use MTU-sized buffers to send fewer packets. + int mtu = Platform.get().getMtu(socket); + if (mtu < 1024) mtu = 1024; + if (mtu > 8192) mtu = 8192; + in = new BufferedInputStream(in, mtu); + out = new BufferedOutputStream(out, mtu); + } } From b9c3ffb26afeff02ab978c8e42f9a0fd242cdd51 Mon Sep 17 00:00:00 2001 From: "lingming.yb" Date: Tue, 24 Dec 2013 10:43:38 +0800 Subject: [PATCH 3/3] format --- okhttp/src/main/java/com/squareup/okhttp/Connection.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/okhttp/src/main/java/com/squareup/okhttp/Connection.java b/okhttp/src/main/java/com/squareup/okhttp/Connection.java index f89930bb7..a30484515 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/Connection.java +++ b/okhttp/src/main/java/com/squareup/okhttp/Connection.java @@ -105,8 +105,7 @@ public final class Connection implements Closeable { if (route.address.sslSocketFactory != null) { upgradeToTls(tunnelRequest); - } - else{ + } else { streamWrapper(); } } @@ -329,8 +328,8 @@ public final class Connection implements Closeable { } } } - - private void streamWrapper(){ + + private void streamWrapper() throws IOException { //Use MTU-sized buffers to send fewer packets. int mtu = Platform.get().getMtu(socket); if (mtu < 1024) mtu = 1024;