Skip to content

Commit

Permalink
refactor: use okhttp and okio
Browse files Browse the repository at this point in the history
1zun4 committed Jan 2, 2025
1 parent 73e8ae0 commit 28fa4f2
Showing 3 changed files with 31 additions and 34 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -61,6 +61,7 @@ dependencies {
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

implementation 'org.apache.commons:commons-exec:1.3'
implementation "com.squareup.okhttp3:okhttp:5.0.0-alpha.14"
}

sourceSets {
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ yarn_mappings=1.21.4+build.2
loader_version=0.16.5

archives_base_name=mcef
mod_version=1.3.1-1.21.4
mod_version=1.3.2-1.21.4
maven_group=CCBlueX

loom_version=1.9-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -21,16 +21,15 @@
package net.ccbluex.liquidbounce.mcef;

import net.ccbluex.liquidbounce.mcef.progress.MCEFProgressTracker;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okio.Buffer;
import okio.Okio;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.*;

@@ -232,39 +231,36 @@ private boolean compareChecksum(File checksumFile, File archiveFile) {
}

private void downloadFile(String urlString, File outputFile, MCEFProgressTracker percentCompleteConsumer) throws IOException {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet(urlString);
var client = new OkHttpClient();
var request = new Request.Builder()
.url(urlString)
.build();

try (var response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Unexpected response status: " + response.code());
}

httpClient.execute(httpGet, response -> {
int status = response.getStatusLine().getStatusCode();
if (status < 200 || status >= 300) {
throw new IOException("Unexpected response status: " + status);
}
var body = response.body();

HttpEntity entity = response.getEntity();
if (entity == null) {
throw new IOException("No content returned from " + urlString);
}
var contentLength = body.contentLength();
try (var source = body.source();
var sink = Okio.buffer(Okio.sink(outputFile))) {

long contentLength = entity.getContentLength();
try (InputStream inputStream = entity.getContent();
FileOutputStream outputStream = new FileOutputStream(outputFile)) {

byte[] buffer = new byte[8192];
int bytesRead;
long totalBytesRead = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
if (contentLength > 0) {
float percentComplete = (float) totalBytesRead / contentLength;
percentCompleteConsumer.setProgress(percentComplete);
}
var buffer = new Buffer();
var totalBytesRead = 0L;
long bytesRead;

while ((bytesRead = source.read(buffer, 8192)) != -1) {
sink.write(buffer, bytesRead);
totalBytesRead += bytesRead;

if (contentLength > 0) {
var percentComplete = (float) totalBytesRead / contentLength;
percentCompleteConsumer.setProgress(percentComplete);
}
}
EntityUtils.consume(entity);
return null;
});
}
}
}

0 comments on commit 28fa4f2

Please sign in to comment.