@@ -0,0 +1,3190 @@
+/*%0A * Copyright (C) 2017 Square, Inc.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0Apackage okhttp3.recipes;%0A%0Aimport java.io.IOException;%0Aimport okhttp3.MediaType;%0Aimport okhttp3.OkHttpClient;%0Aimport okhttp3.Request;%0Aimport okhttp3.RequestBody;%0Aimport okhttp3.Response;%0Aimport okio.BufferedSink;%0Aimport okio.Okio;%0Aimport okio.Pipe;%0A%0Apublic final class PostStreamingWithPipe %7B%0A  public static final MediaType MEDIA_TYPE_MARKDOWN%0A      = MediaType.parse(%22text/x-markdown; charset=utf-8%22);%0A%0A  private final OkHttpClient client = new OkHttpClient();%0A%0A  public void run() throws Exception %7B%0A    final PipeBody pipeBody = new PipeBody();%0A%0A    Request request = new Request.Builder()%0A        .url(%22https://api.github.com/markdown/raw%22)%0A        .post(pipeBody)%0A        .build();%0A%0A    streamPrimesToSinkAsynchronously(pipeBody.sink());%0A%0A    try (Response response = client.newCall(request).execute()) %7B%0A      if (!response.isSuccessful()) throw new IOException(%22Unexpected code %22 + response);%0A%0A      System.out.println(response.body().string());%0A    %7D%0A  %7D%0A%0A  private void streamPrimesToSinkAsynchronously(final BufferedSink sink) %7B%0A    Thread thread = new Thread(%22writer%22) %7B%0A      @Override public void run() %7B%0A        try %7B%0A          sink.writeUtf8(%22Numbers%5Cn%22);%0A          sink.writeUtf8(%22-------%5Cn%22);%0A          for (int i = 2; i %3C= 997; i++) %7B%0A            System.out.println(i);%0A            Thread.sleep(10);%0A            sink.writeUtf8(String.format(%22 * %25s = %25s%5Cn%22, i, factor(i)));%0A          %7D%0A          sink.close();%0A        %7D catch (IOException %7C InterruptedException e) %7B%0A          e.printStackTrace();%0A        %7D%0A      %7D%0A%0A      private String factor(int n) %7B%0A        for (int i = 2; i %3C n; i++) %7B%0A          int x = n / i;%0A          if (x * i == n) return factor(x) + %22 %C3%97 %22 + i;%0A        %7D%0A        return Integer.toString(n);%0A      %7D%0A    %7D;%0A%0A    thread.start();%0A  %7D%0A%0A  /**%0A   * This request body makes it possible for another thread to stream data to the uploading request.%0A   * This is potentially useful for posting live event streams like video capture. Callers should%0A   * write to %7B@code sink()%7D and close it to complete the post.%0A   */%0A  static final class PipeBody extends RequestBody %7B%0A    private final Pipe pipe = new Pipe(8192);%0A    private final BufferedSink sink = Okio.buffer(pipe.sink());%0A%0A    public BufferedSink sink() %7B%0A      return sink;%0A    %7D%0A%0A    @Override public MediaType contentType() %7B%0A      return MEDIA_TYPE_MARKDOWN;%0A    %7D%0A%0A    @Override public void writeTo(BufferedSink sink) throws IOException %7B%0A      sink.writeAll(pipe.source());%0A    %7D%0A  %7D%0A%0A  public static void main(String... args) throws Exception %7B%0A    new PostStreamingWithPipe().run();%0A  %7D%0A%7D%0A

Add a sample where data is streamed via an Okio pipe.