Skip to content

Commit

Permalink
Merge branch 'master' into multiplatform_android
Browse files Browse the repository at this point in the history
  • Loading branch information
yschimke authored Dec 24, 2024
2 parents 8d8d64d + 5db9a9c commit 1e18b6a
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 5 deletions.
10 changes: 5 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ org-bouncycastle = "1.76"
org-conscrypt = "2.5.2"
org-jetbrains-coroutines = "1.9.0"
org-jetbrains-kotlin = "2.1.0"
org-junit-jupiter = "5.11.3"
org-junit-jupiter = "5.11.4"
retrofit = "2.11.0"
testcontainers = "1.20.4"

Expand Down Expand Up @@ -43,7 +43,7 @@ graal-sdk = { module = "org.graalvm.sdk:graal-sdk", version.ref = "graalvm" }
gradlePlugin-android = "com.android.tools.build:gradle:8.7.3"
gradlePlugin-androidJunit5 = "de.mannodermaus.gradle.plugins:android-junit5:1.11.2.0"
gradlePlugin-animalsniffer = "ru.vyarus:gradle-animalsniffer-plugin:1.7.2"
gradlePlugin-binaryCompatibilityValidator = "org.jetbrains.kotlinx.binary-compatibility-validator:org.jetbrains.kotlinx.binary-compatibility-validator.gradle.plugin:0.16.3"
gradlePlugin-binaryCompatibilityValidator = "org.jetbrains.kotlinx.binary-compatibility-validator:org.jetbrains.kotlinx.binary-compatibility-validator.gradle.plugin:0.17.0"
gradlePlugin-bnd = { module = "biz.aQute.bnd:biz.aQute.bnd.gradle", version.ref = "biz-aQute-bnd" }
gradlePlugin-dokka = "org.jetbrains.dokka:dokka-gradle-plugin:1.9.20"
gradlePlugin-errorprone = "net.ltgt.gradle:gradle-errorprone-plugin:4.1.0"
Expand All @@ -54,7 +54,7 @@ gradlePlugin-mavenPublish = "com.vanniktech:gradle-maven-publish-plugin:0.30.0"
gradlePlugin-mavenSympathy = "io.github.usefulness.maven-sympathy:io.github.usefulness.maven-sympathy.gradle.plugin:0.3.0"
gradlePlugin-shadow = "gradle.plugin.com.github.johnrengelman:shadow:8.0.0"
gradlePlugin-spotless = "com.diffplug.spotless:spotless-plugin-gradle:6.25.0"
guava-jre = "com.google.guava:guava:33.3.1-jre"
guava-jre = "com.google.guava:guava:33.4.0-jre"
hamcrestLibrary = "org.hamcrest:hamcrest-library:3.0"
httpClient5 = "org.apache.httpcomponents.client5:httpclient5:5.3.1"
jettyClient = "org.eclipse.jetty:jetty-client:9.4.56.v20240826"
Expand All @@ -65,8 +65,8 @@ junit-ktx = "androidx.test.ext:junit-ktx:1.2.1"
junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "org-junit-jupiter" }
junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "org-junit-jupiter" }
junit-jupiter-params = { module = "org.junit.jupiter:junit-jupiter-params", version.ref = "org-junit-jupiter" }
junit-platform-console = "org.junit.platform:junit-platform-console:1.11.3"
junit-vintage-engine = "org.junit.vintage:junit-vintage-engine:5.11.3"
junit-platform-console = "org.junit.platform:junit-platform-console:1.11.4"
junit-vintage-engine = "org.junit.vintage:junit-vintage-engine:5.11.4"
junit-pioneer = "org.junit-pioneer:junit-pioneer:1.9.1"
junit5android-core = { module = "de.mannodermaus.junit5:android-test-core", version.ref = "de-mannodermaus-junit5" }
junit5android-runner = { module = "de.mannodermaus.junit5:android-test-runner", version.ref = "de-mannodermaus-junit5" }
Expand Down
138 changes: 138 additions & 0 deletions samples/guide/src/main/java/okhttp3/recipes/UploadProgress.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okhttp3.recipes;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Buffer;
import okio.BufferedSink;
import okio.ForwardingSink;
import okio.Okio;
import okio.Sink;
import java.io.File;
import java.io.IOException;

public final class UploadProgress {
private static final String IMGUR_CLIENT_ID = "9199fdef135c122";
private static final MediaType MEDIA_TYPE_PNG = MediaType.get("image/png");

private final OkHttpClient client = new OkHttpClient();

public void run() throws Exception {
// Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image
final ProgressListener progressListener = new ProgressListener() {
boolean firstUpdate = true;

@Override public void update(long bytesWritten, long contentLength, boolean done) {
if (done) {
System.out.println("completed");
} else {
if (firstUpdate) {
firstUpdate = false;
if (contentLength == -1) {
System.out.println("content-length: unknown");
} else {
System.out.format("content-length: %d\n", contentLength);
}
}

System.out.println(bytesWritten);

if (contentLength != -1) {
System.out.format("%d%% done\n", (100 * bytesWritten) / contentLength);
}
}
}
};

RequestBody requestBody = RequestBody.create(
new File("docs/images/logo-square.png"),
MEDIA_TYPE_PNG);

Request request = new Request.Builder()
.header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
.url("https://api.imgur.com/3/image")
.post(new ProgressRequestBody(requestBody, progressListener))
.build();

Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

System.out.println(response.body().string());
}

public static void main(String... args) throws Exception {
new UploadProgress().run();
}

private static class ProgressRequestBody extends RequestBody {

private final ProgressListener progressListener;
private final RequestBody delegate;

public ProgressRequestBody(RequestBody delegate, ProgressListener progressListener) {
this.delegate = delegate;
this.progressListener = progressListener;
}

@Override
public MediaType contentType() {
return delegate.contentType();
}

@Override
public long contentLength() throws IOException {
return delegate.contentLength();
}

@Override
public void writeTo(BufferedSink sink) throws IOException {
BufferedSink bufferedSink = Okio.buffer(sink(sink));
delegate.writeTo(bufferedSink);
bufferedSink.flush();
}

public Sink sink(Sink sink) {
return new ForwardingSink(sink) {
private long totalBytesWritten = 0L;
private boolean completed = false;

@Override
public void write(Buffer source, long byteCount) throws IOException {
super.write(source, byteCount);
totalBytesWritten += byteCount;
progressListener.update(totalBytesWritten, contentLength(), completed);
}

@Override
public void close() throws IOException {
super.close();
if (!completed) {
completed = true;
progressListener.update(totalBytesWritten, contentLength(), completed);
}
}
};
}
}

interface ProgressListener {
void update(long bytesWritten, long contentLength, boolean done);
}
}

0 comments on commit 1e18b6a

Please sign in to comment.