java.io.OutputStreamokio.BufferedSink



Project square/okhttp in file ...ava.com.squareup.okhttp.internal.spdy.Variant.java (2014-02-23)
@@ -16,7 +16,7 @@
 package com.squareup.okhttp.internal.spdy;
 
 import com.squareup.okhttp.Protocol;
-import java.io.OutputStream;
+import okio.BufferedSink;
 import okio.BufferedSource;
 
 /** A version and dialect of the framed socket protocol. */
@@ -33,5 +33,5 @@ interface Variant {
   /**
    * @param client true if this is the HTTP client's writer, writing frames to a server.
    */
-  FrameWriter newWriter(OutputStream out, boolean client);
+  FrameWriter newWriter(BufferedSink sink, boolean client);
 }
Project square/okhttp in file ...up.okhttp.internal.http.RetryableOutputStream.java (2014-02-23)
@@ -19,8 +19,8 @@ package com.squareup.okhttp.internal.http;
 import com.squareup.okhttp.internal.AbstractOutputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.io.OutputStream;
 import java.net.ProtocolException;
+import okio.BufferedSink;
 
 import static com.squareup.okhttp.internal.Util.checkOffsetAndCount;
 
@@ -69,7 +69,7 @@ final class RetryableOutputStream extends AbstractOutputStream {
     return content.size();
   }
 
-  public void writeToSocket(OutputStream socketOut) throws IOException {
-    content.writeTo(socketOut);
+  public void writeToSocket(BufferedSink socketOut) throws IOException {
+    content.writeTo(socketOut.outputStream());
   }
 }
Project square/okhttp in file ...ttp.src.main.java.com.squareup.okhttp.Request.java (2014-02-23)
@@ -21,13 +21,13 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.List;
+import okio.BufferedSink;
 
 /**
  * An HTTP request. Instances of this class are immutable if their {@link #body}
@@ -158,7 +158,7 @@ public final class Request {
     }
 
     /** Writes the content of this request to {@code out}. */
-    public abstract void writeTo(OutputStream out) throws IOException;
+    public abstract void writeTo(BufferedSink sink) throws IOException;
 
     /**
      * Returns a new request body that transmits {@code content}. If {@code
@@ -190,8 +190,8 @@ public final class Request {
           return content.length;
         }
 
-        @Override public void writeTo(OutputStream out) throws IOException {
-          out.write(content);
+        @Override public void writeTo(BufferedSink sink) throws IOException {
+          sink.write(content);
         }
       };
     }
@@ -210,7 +210,7 @@ public final class Request {
           return file.length();
         }
 
-        @Override public void writeTo(OutputStream out) throws IOException {
+        @Override public void writeTo(BufferedSink sink) throws IOException {
           long length = contentLength();
           if (length == 0) return;
 
@@ -219,7 +219,7 @@ public final class Request {
             in = new FileInputStream(file);
             byte[] buffer = new byte[(int) Math.min(8192, length)];
             for (int c; (c = in.read(buffer)) != -1; ) {
-              out.write(buffer, 0, c);
+              sink.write(buffer, 0, c);
             }
           } finally {
             Util.closeQuietly(in);
Project square/okhttp in file ....squareup.okhttp.internal.spdy.SpdyConnection.java (2014-02-23)
@@ -21,7 +21,6 @@ import com.squareup.okhttp.internal.Util;
 import java.io.Closeable;
 import java.io.IOException;
 import java.io.InterruptedIOException;
-import java.io.OutputStream;
 import java.net.Socket;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -31,6 +30,7 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.SynchronousQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
+import okio.BufferedSink;
 import okio.BufferedSource;
 import okio.ByteString;
 import okio.Okio;
@@ -148,7 +148,7 @@ public final class SpdyConnection implements Closeable {
     bytesLeftInWriteWindow = peerSettings.getInitialWindowSize();
     bufferPool = new ByteArrayPool(INITIAL_WINDOW_SIZE * 8); // TODO: revisit size limit!
     frameReader = variant.newReader(builder.source, client);
-    frameWriter = variant.newWriter(builder.out, client);
+    frameWriter = variant.newWriter(builder.sink, client);
 
     readerRunnable = new Reader();
     new Thread(readerRunnable).start(); // Not a daemon thread.
@@ -232,6 +232,10 @@ public final class SpdyConnection implements Closeable {
           requestHeaders);
     }
 
+    if (!out) {
+      frameWriter.flush();
+    }
+
     return stream;
   }
 
@@ -458,25 +462,25 @@ public final class SpdyConnection implements Closeable {
   public static class Builder {
     private String hostName;
     private BufferedSource source;
-    private OutputStream out;
+    private BufferedSink sink;
     private IncomingStreamHandler handler = IncomingStreamHandler.REFUSE_INCOMING_STREAMS;
     private Protocol protocol = Protocol.SPDY_3;
     private boolean client;
 
     public Builder(boolean client, Socket socket) throws IOException {
       this("", client, Okio.buffer(Okio.source(socket.getInputStream())),
-          socket.getOutputStream());
+          Okio.buffer(Okio.sink(socket.getOutputStream())));
     }
 
     /**
      * @param client true if this peer initiated the connection; false if this
      *     peer accepted the connection.
      */
-    public Builder(String hostName, boolean client, BufferedSource source, OutputStream out) {
+    public Builder(String hostName, boolean client, BufferedSource source, BufferedSink sink) {
       this.hostName = hostName;
       this.client = client;
       this.source = source;
-      this.out = out;
+      this.sink = sink;
     }
 
     public Builder handler(IncomingStreamHandler handler) {
Project square/okhttp in file ....com.squareup.okhttp.internal.http.HttpEngine.java (2014-02-23)
@@ -30,7 +30,6 @@ import com.squareup.okhttp.TunnelRequest;
 import com.squareup.okhttp.internal.Dns;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
 import java.net.CacheRequest;
 import java.net.CookieHandler;
 import java.net.ProtocolException;
@@ -42,8 +41,10 @@ import java.util.Map;
 import javax.net.ssl.HostnameVerifier;
 import javax.net.ssl.SSLHandshakeException;
 import javax.net.ssl.SSLSocketFactory;
+import okio.BufferedSink;
 import okio.GzipSource;
 import okio.Okio;
+import okio.Sink;
 import okio.Source;
 
 import static com.squareup.okhttp.internal.Util.closeQuietly;
@@ -98,7 +99,8 @@ public class HttpEngine {
   public final boolean bufferRequestBody;
 
   private Request request;
-  private OutputStream requestBodyOut;
+  private Sink requestBodyOut;
+  private BufferedSink bufferedRequestBody;
 
   private ResponseSource responseSource;
 
@@ -131,7 +133,7 @@ public class HttpEngine {
    *     recover from a failure.
    */
   public HttpEngine(OkHttpClient client, Request request, boolean bufferRequestBody,
-      Connection connection, RouteSelector routeSelector, RetryableOutputStream requestBodyOut) {
+      Connection connection, RouteSelector routeSelector, RetryableSink requestBodyOut) {
     this.client = client;
     this.request = request;
     this.bufferRequestBody = bufferRequestBody;
@@ -257,11 +259,20 @@ public class HttpEngine {
   }
 
   /** Returns the request body or null if this request doesn't have a body. */
-  public final OutputStream getRequestBody() {
+  public final Sink getRequestBody() {
     if (responseSource == null) throw new IllegalStateException();
     return requestBodyOut;
   }
 
+  public final BufferedSink getBufferedRequestBody() {
+    BufferedSink result = bufferedRequestBody;
+    if (result != null) return result;
+    Sink requestBody = getRequestBody();
+    return requestBody != null
+        ? (bufferedRequestBody = Okio.buffer(requestBody))
+        : null;
+  }
+
   public final boolean hasResponse() {
     return response != null;
   }
@@ -307,8 +318,7 @@ public class HttpEngine {
       routeSelector.connectFailed(connection, e);
     }
 
-    boolean canRetryRequestBody = requestBodyOut == null
-        || requestBodyOut instanceof RetryableOutputStream;
+    boolean canRetryRequestBody = requestBodyOut == null || requestBodyOut instanceof RetryableSink;
     if (routeSelector == null && connection == null // No connection.
         || routeSelector != null && !routeSelector.hasNext() // No more routes to attempt.
         || !isRecoverable(e)
@@ -320,7 +330,7 @@ public class HttpEngine {
 
     // For failure recovery, use the same route selector with a new connection.
     return new HttpEngine(client, request, bufferRequestBody, connection, routeSelector,
-        (RetryableOutputStream) requestBodyOut);
+        (RetryableSink) requestBodyOut);
   }
 
   private boolean isRecoverable(IOException e) {
@@ -514,11 +524,15 @@ public class HttpEngine {
     if (responseSource == null) throw new IllegalStateException("call sendRequest() first!");
     if (!responseSource.requiresConnection()) return;
 
+    // Flush the response body if there's data outstanding.
+    if (bufferedRequestBody != null && bufferedRequestBody.buffer().byteCount() > 0) {
+      bufferedRequestBody.flush();
+    }
+
     if (sentRequestMillis == -1) {
-      if (OkHeaders.contentLength(request) == -1
-          && requestBodyOut instanceof RetryableOutputStream) {
+      if (OkHeaders.contentLength(request) == -1 && requestBodyOut instanceof RetryableSink) {
         // We might not learn the Content-Length until the request body has been buffered.
-        long contentLength = ((RetryableOutputStream) requestBodyOut).contentLength();
+        long contentLength = ((RetryableSink) requestBodyOut).contentLength();
         request = request.newBuilder()
             .header("Content-Length", Long.toString(contentLength))
             .build();
@@ -528,8 +542,8 @@ public class HttpEngine {
 
     if (requestBodyOut != null) {
       requestBodyOut.close();
-      if (requestBodyOut instanceof RetryableOutputStream) {
-        transport.writeRequestBody((RetryableOutputStream) requestBodyOut);
+      if (requestBodyOut instanceof RetryableSink) {
+        transport.writeRequestBody((RetryableSink) requestBodyOut);
       }
     }