refactor: Convert ProgressRequestBody to Kotlin (#78)

This commit is contained in:
Nik Clayton 2023-09-19 22:18:29 +02:00 committed by GitHub
parent 9de829995b
commit b157791058
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 77 deletions

View File

@ -1,77 +0,0 @@
/* Copyright 2017 Andrew Dawson
*
* This file is a part of Pachli.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Pachli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package app.pachli.network;
import androidx.annotation.NonNull;
import java.io.IOException;
import java.io.InputStream;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.BufferedSink;
public final class ProgressRequestBody extends RequestBody {
private final InputStream content;
private final long contentLength;
private final UploadCallback uploadListener;
private final MediaType mediaType;
private static final int DEFAULT_BUFFER_SIZE = 2048;
public interface UploadCallback {
void onProgressUpdate(int percentage);
}
public ProgressRequestBody(@NonNull final InputStream content, long contentLength, @NonNull final MediaType mediaType, @NonNull final UploadCallback listener) {
this.content = content;
this.contentLength = contentLength;
this.mediaType = mediaType;
this.uploadListener = listener;
}
@NonNull
@Override
public MediaType contentType() {
return mediaType;
}
@Override
public long contentLength() {
return contentLength;
}
@Override
public void writeTo(@NonNull BufferedSink sink) throws IOException {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
long uploaded = 0;
try {
int read;
while ((read = content.read(buffer)) != -1) {
uploadListener.onProgressUpdate((int)(100 * uploaded / contentLength));
uploaded += read;
sink.write(buffer, 0, read);
}
uploadListener.onProgressUpdate((int)(100 * uploaded / contentLength));
} finally {
content.close();
}
}
}

View File

@ -0,0 +1,56 @@
/*
* Copyright 2023 Pachli Association
*
* This file is a part of Pachli.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Pachli is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Pachli; if not,
* see <http://www.gnu.org/licenses>.
*/
package app.pachli.network
import okhttp3.MediaType
import okhttp3.RequestBody
import okio.BufferedSink
import java.io.IOException
import java.io.InputStream
class ProgressRequestBody(
private val content: InputStream,
private val contentLength: Long,
private val mediaType: MediaType,
private val uploadListener: (Int) -> Unit,
) : RequestBody() {
override fun contentType(): MediaType {
return mediaType
}
@Throws(IOException::class)
override fun writeTo(sink: BufferedSink) {
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var uploaded: Long = 0
try {
var read: Int
while (content.read(buffer).also { read = it } != -1) {
uploadListener((100 * uploaded / contentLength).toInt())
uploaded += read.toLong()
sink.write(buffer, 0, read)
}
uploadListener((100 * uploaded / contentLength).toInt())
} finally {
content.close()
}
}
companion object {
private const val DEFAULT_BUFFER_SIZE = 2048
}
}