|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 Square, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package okhttp3.recipes; |
| 17 | + |
| 18 | +import okhttp3.MediaType; |
| 19 | +import okhttp3.OkHttpClient; |
| 20 | +import okhttp3.Request; |
| 21 | +import okhttp3.RequestBody; |
| 22 | +import okhttp3.Response; |
| 23 | +import okio.Buffer; |
| 24 | +import okio.BufferedSink; |
| 25 | +import okio.ForwardingSink; |
| 26 | +import okio.Okio; |
| 27 | +import okio.Sink; |
| 28 | +import java.io.File; |
| 29 | +import java.io.IOException; |
| 30 | + |
| 31 | +public final class UploadProgress { |
| 32 | + private static final String IMGUR_CLIENT_ID = "9199fdef135c122"; |
| 33 | + private static final MediaType MEDIA_TYPE_PNG = MediaType.get("image/png"); |
| 34 | + |
| 35 | + private final OkHttpClient client = new OkHttpClient(); |
| 36 | + |
| 37 | + public void run() throws Exception { |
| 38 | + // Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image |
| 39 | + final ProgressListener progressListener = new ProgressListener() { |
| 40 | + boolean firstUpdate = true; |
| 41 | + |
| 42 | + @Override public void update(long bytesWritten, long contentLength, boolean done) { |
| 43 | + if (done) { |
| 44 | + System.out.println("completed"); |
| 45 | + } else { |
| 46 | + if (firstUpdate) { |
| 47 | + firstUpdate = false; |
| 48 | + if (contentLength == -1) { |
| 49 | + System.out.println("content-length: unknown"); |
| 50 | + } else { |
| 51 | + System.out.format("content-length: %d\n", contentLength); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + System.out.println(bytesWritten); |
| 56 | + |
| 57 | + if (contentLength != -1) { |
| 58 | + System.out.format("%d%% done\n", (100 * bytesWritten) / contentLength); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + RequestBody requestBody = RequestBody.create( |
| 65 | + new File("docs/images/logo-square.png"), |
| 66 | + MEDIA_TYPE_PNG); |
| 67 | + |
| 68 | + Request request = new Request.Builder() |
| 69 | + .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID) |
| 70 | + .url("https://api.imgur.com/3/image") |
| 71 | + .post(new ProgressRequestBody(requestBody, progressListener)) |
| 72 | + .build(); |
| 73 | + |
| 74 | + Response response = client.newCall(request).execute(); |
| 75 | + if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); |
| 76 | + |
| 77 | + System.out.println(response.body().string()); |
| 78 | + } |
| 79 | + |
| 80 | + public static void main(String... args) throws Exception { |
| 81 | + new UploadProgress().run(); |
| 82 | + } |
| 83 | + |
| 84 | + private static class ProgressRequestBody extends RequestBody { |
| 85 | + |
| 86 | + private final ProgressListener progressListener; |
| 87 | + private final RequestBody delegate; |
| 88 | + |
| 89 | + public ProgressRequestBody(RequestBody delegate, ProgressListener progressListener) { |
| 90 | + this.delegate = delegate; |
| 91 | + this.progressListener = progressListener; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public MediaType contentType() { |
| 96 | + return delegate.contentType(); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public long contentLength() throws IOException { |
| 101 | + return delegate.contentLength(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void writeTo(BufferedSink sink) throws IOException { |
| 106 | + BufferedSink bufferedSink = Okio.buffer(sink(sink)); |
| 107 | + delegate.writeTo(bufferedSink); |
| 108 | + bufferedSink.flush(); |
| 109 | + } |
| 110 | + |
| 111 | + public Sink sink(Sink sink) { |
| 112 | + return new ForwardingSink(sink) { |
| 113 | + private long totalBytesWritten = 0L; |
| 114 | + private boolean completed = false; |
| 115 | + |
| 116 | + @Override |
| 117 | + public void write(Buffer source, long byteCount) throws IOException { |
| 118 | + super.write(source, byteCount); |
| 119 | + totalBytesWritten += byteCount; |
| 120 | + progressListener.update(totalBytesWritten, contentLength(), completed); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void close() throws IOException { |
| 125 | + super.close(); |
| 126 | + if (!completed) { |
| 127 | + completed = true; |
| 128 | + progressListener.update(totalBytesWritten, contentLength(), completed); |
| 129 | + } |
| 130 | + } |
| 131 | + }; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + interface ProgressListener { |
| 136 | + void update(long bytesWritten, long contentLength, boolean done); |
| 137 | + } |
| 138 | +} |
0 commit comments