BAEL-4856 Remove BinaryFileDownloadApplication.java and use correct indentation
This commit is contained in:
-26
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.okhttp.download;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
public class BinaryFileDownloadApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String url = args[0];
|
||||
String file = args[1];
|
||||
try (BinaryFileDownloader downloader = new BinaryFileDownloader(new OkHttpClient(),
|
||||
new BinaryFileWriter(new FileOutputStream(file)))) {
|
||||
long downloadSize = downloader.download(url);
|
||||
double downloadSizeInMB = convertToMB(downloadSize);
|
||||
System.out.printf("Successfully downloaded file %s from %s. Total size %.2fMB%n", file, url, downloadSizeInMB);
|
||||
} catch (Exception ex) {
|
||||
System.err.printf("Could not download file %s from %s. %nError: %s%n", file, url, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static double convertToMB(double downloadSize) {
|
||||
return downloadSize / (1024.0 * 1024.0);
|
||||
}
|
||||
|
||||
}
|
||||
+37
-37
@@ -10,45 +10,45 @@ import java.io.IOException;
|
||||
|
||||
public class BinaryFileDownloader implements AutoCloseable {
|
||||
|
||||
private final OkHttpClient client;
|
||||
private final BinaryFileWriter writer;
|
||||
private final OkHttpClient client;
|
||||
private final BinaryFileWriter writer;
|
||||
|
||||
public BinaryFileDownloader(OkHttpClient client, BinaryFileWriter writer) {
|
||||
this.client = client;
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
public long download(String url) throws IOException {
|
||||
Request request = createRequest(url);
|
||||
Response response = executeRequest(request);
|
||||
ResponseBody responseBody = getResponseBodyOrFail(response);
|
||||
return write(responseBody);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Request createRequest(String url) {
|
||||
return new Request.Builder().url(url).build();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Response executeRequest(Request request) throws IOException {
|
||||
return client.newCall(request).execute();
|
||||
}
|
||||
|
||||
private ResponseBody getResponseBodyOrFail(Response response) {
|
||||
ResponseBody responseBody = response.body();
|
||||
if (responseBody == null) {
|
||||
throw new IllegalStateException("Response doesn't contain a file");
|
||||
public BinaryFileDownloader(OkHttpClient client, BinaryFileWriter writer) {
|
||||
this.client = client;
|
||||
this.writer = writer;
|
||||
}
|
||||
return responseBody;
|
||||
}
|
||||
|
||||
private long write(ResponseBody responseBody) throws IOException {
|
||||
return writer.write(responseBody.byteStream());
|
||||
}
|
||||
public long download(String url) throws IOException {
|
||||
Request request = createRequest(url);
|
||||
Response response = executeRequest(request);
|
||||
ResponseBody responseBody = getResponseBodyOrFail(response);
|
||||
return write(responseBody);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
writer.close();
|
||||
}
|
||||
@NotNull
|
||||
private Request createRequest(String url) {
|
||||
return new Request.Builder().url(url).build();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Response executeRequest(Request request) throws IOException {
|
||||
return client.newCall(request).execute();
|
||||
}
|
||||
|
||||
private ResponseBody getResponseBodyOrFail(Response response) {
|
||||
ResponseBody responseBody = response.body();
|
||||
if (responseBody == null) {
|
||||
throw new IllegalStateException("Response doesn't contain a file");
|
||||
}
|
||||
return responseBody;
|
||||
}
|
||||
|
||||
private long write(ResponseBody responseBody) throws IOException {
|
||||
return writer.write(responseBody.byteStream());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,28 +7,28 @@ import java.io.OutputStream;
|
||||
|
||||
public class BinaryFileWriter implements AutoCloseable {
|
||||
|
||||
private static final int CHUNK_SIZE = 1024;
|
||||
private final OutputStream outputStream;
|
||||
private static final int CHUNK_SIZE = 1024;
|
||||
private final OutputStream outputStream;
|
||||
|
||||
public BinaryFileWriter(OutputStream outputStream) {
|
||||
this.outputStream = outputStream;
|
||||
}
|
||||
|
||||
public long write(InputStream inputStream) throws IOException {
|
||||
try (BufferedInputStream input = new BufferedInputStream(inputStream)) {
|
||||
byte[] dataBuffer = new byte[CHUNK_SIZE];
|
||||
int readBytes;
|
||||
long totalBytes = 0;
|
||||
while ((readBytes = input.read(dataBuffer)) != -1) {
|
||||
totalBytes += readBytes;
|
||||
outputStream.write(dataBuffer, 0, readBytes);
|
||||
}
|
||||
return totalBytes;
|
||||
public BinaryFileWriter(OutputStream outputStream) {
|
||||
this.outputStream = outputStream;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
outputStream.close();
|
||||
}
|
||||
public long write(InputStream inputStream) throws IOException {
|
||||
try (BufferedInputStream input = new BufferedInputStream(inputStream)) {
|
||||
byte[] dataBuffer = new byte[CHUNK_SIZE];
|
||||
int readBytes;
|
||||
long totalBytes = 0;
|
||||
while ((readBytes = input.read(dataBuffer)) != -1) {
|
||||
totalBytes += readBytes;
|
||||
outputStream.write(dataBuffer, 0, readBytes);
|
||||
}
|
||||
return totalBytes;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user