BAEL-4856 Remove BinaryFileDownloadApplication.java and use correct indentation
This commit is contained in:
parent
9e369a8258
commit
91dfad8bec
|
@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -10,45 +10,45 @@ import java.io.IOException;
|
||||||
|
|
||||||
public class BinaryFileDownloader implements AutoCloseable {
|
public class BinaryFileDownloader implements AutoCloseable {
|
||||||
|
|
||||||
private final OkHttpClient client;
|
private final OkHttpClient client;
|
||||||
private final BinaryFileWriter writer;
|
private final BinaryFileWriter writer;
|
||||||
|
|
||||||
public BinaryFileDownloader(OkHttpClient client, BinaryFileWriter writer) {
|
public BinaryFileDownloader(OkHttpClient client, BinaryFileWriter writer) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.writer = writer;
|
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");
|
|
||||||
}
|
}
|
||||||
return responseBody;
|
|
||||||
}
|
|
||||||
|
|
||||||
private long write(ResponseBody responseBody) throws IOException {
|
public long download(String url) throws IOException {
|
||||||
return writer.write(responseBody.byteStream());
|
Request request = createRequest(url);
|
||||||
}
|
Response response = executeRequest(request);
|
||||||
|
ResponseBody responseBody = getResponseBodyOrFail(response);
|
||||||
|
return write(responseBody);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@NotNull
|
||||||
public void close() throws Exception {
|
private Request createRequest(String url) {
|
||||||
writer.close();
|
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 {
|
public class BinaryFileWriter implements AutoCloseable {
|
||||||
|
|
||||||
private static final int CHUNK_SIZE = 1024;
|
private static final int CHUNK_SIZE = 1024;
|
||||||
private final OutputStream outputStream;
|
private final OutputStream outputStream;
|
||||||
|
|
||||||
public BinaryFileWriter(OutputStream outputStream) {
|
public BinaryFileWriter(OutputStream outputStream) {
|
||||||
this.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;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
public long write(InputStream inputStream) throws IOException {
|
||||||
public void close() throws IOException {
|
try (BufferedInputStream input = new BufferedInputStream(inputStream)) {
|
||||||
outputStream.close();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,54 +27,48 @@ import static org.mockito.Mockito.when;
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class BinaryFileDownloaderUnitTest {
|
public class BinaryFileDownloaderUnitTest {
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private OkHttpClient client;
|
private OkHttpClient client;
|
||||||
@Mock
|
@Mock
|
||||||
private BinaryFileWriter writer;
|
private BinaryFileWriter writer;
|
||||||
@InjectMocks
|
@InjectMocks
|
||||||
private BinaryFileDownloader tested;
|
private BinaryFileDownloader tested;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrlAndResponse_whenDownload_thenExpectFileWritten() throws Exception {
|
public void givenUrlAndResponse_whenDownload_thenExpectFileWritten() throws Exception {
|
||||||
String url = "http://example.com/file";
|
String url = "http://example.com/file";
|
||||||
Call call = mock(Call.class);
|
Call call = mock(Call.class);
|
||||||
when(client.newCall(any(Request.class))).thenReturn(call);
|
when(client.newCall(any(Request.class))).thenReturn(call);
|
||||||
ResponseBody body = ResponseBody.create("BODY", MediaType.get("application/text"));
|
ResponseBody body = ResponseBody.create("BODY", MediaType.get("application/text"));
|
||||||
Response response = createResponse(url, body);
|
Response response = createResponse(url, body);
|
||||||
when(call.execute()).thenReturn(response);
|
when(call.execute()).thenReturn(response);
|
||||||
when(writer.write(any())).thenReturn(1L);
|
when(writer.write(any())).thenReturn(1L);
|
||||||
|
|
||||||
try (BinaryFileDownloader tested = new BinaryFileDownloader(client, writer)) {
|
try (BinaryFileDownloader tested = new BinaryFileDownloader(client, writer)) {
|
||||||
long size = tested.download(url);
|
long size = tested.download(url);
|
||||||
assertEquals(1L, size);
|
assertEquals(1L, size);
|
||||||
verify(writer).write(any(InputStream.class));
|
verify(writer).write(any(InputStream.class));
|
||||||
|
}
|
||||||
|
verify(writer).close();
|
||||||
}
|
}
|
||||||
verify(writer).close();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrlAndResponseWithNullBody_whenDownload_thenExpectIllegalStateException() throws Exception {
|
public void givenUrlAndResponseWithNullBody_whenDownload_thenExpectIllegalStateException() throws Exception {
|
||||||
String url = "http://example.com/file";
|
String url = "http://example.com/file";
|
||||||
Call call = mock(Call.class);
|
Call call = mock(Call.class);
|
||||||
when(client.newCall(any(Request.class))).thenReturn(call);
|
when(client.newCall(any(Request.class))).thenReturn(call);
|
||||||
Response response = createResponse(url, null);
|
Response response = createResponse(url, null);
|
||||||
when(call.execute()).thenReturn(response);
|
when(call.execute()).thenReturn(response);
|
||||||
|
|
||||||
assertThrows(IllegalStateException.class, () -> tested.download(url));
|
assertThrows(IllegalStateException.class, () -> tested.download(url));
|
||||||
|
|
||||||
verify(writer, times(0)).write(any(InputStream.class));
|
verify(writer, times(0)).write(any(InputStream.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private Response createResponse(String url, ResponseBody body) {
|
private Response createResponse(String url, ResponseBody body) {
|
||||||
Request request = new Request.Builder().url(url).build();
|
Request request = new Request.Builder().url(url).build();
|
||||||
return new Response.Builder()
|
return new Response.Builder().code(200).request(request).protocol(Protocol.HTTP_2).message("Message").body(body).build();
|
||||||
.code(200)
|
}
|
||||||
.request(request)
|
|
||||||
.protocol(Protocol.HTTP_2)
|
|
||||||
.message("Message")
|
|
||||||
.body(body)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -20,36 +20,36 @@ import static org.mockito.Mockito.when;
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class BinaryFileWriterUnitTest {
|
public class BinaryFileWriterUnitTest {
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private OutputStream outputStream;
|
private OutputStream outputStream;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInputStream_whenWrite_thenExpectWritten() throws Exception {
|
public void givenInputStream_whenWrite_thenExpectWritten() throws Exception {
|
||||||
InputStream inputStream = mock(InputStream.class);
|
InputStream inputStream = mock(InputStream.class);
|
||||||
when(inputStream.read(any(), anyInt(), anyInt())).thenReturn(10, -1);
|
when(inputStream.read(any(), anyInt(), anyInt())).thenReturn(10, -1);
|
||||||
|
|
||||||
try (BinaryFileWriter tested = new BinaryFileWriter(outputStream)) {
|
try (BinaryFileWriter tested = new BinaryFileWriter(outputStream)) {
|
||||||
long result = tested.write(inputStream);
|
long result = tested.write(inputStream);
|
||||||
|
|
||||||
assertEquals(10, result);
|
assertEquals(10, result);
|
||||||
verify(outputStream).write(any(), eq(0), eq(10));
|
verify(outputStream).write(any(), eq(0), eq(10));
|
||||||
verify(inputStream).close();
|
verify(inputStream).close();
|
||||||
|
}
|
||||||
|
verify(outputStream).close();
|
||||||
}
|
}
|
||||||
verify(outputStream).close();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInputStreamEmpty_whenWrite_thenExpectNotWritten() throws Exception {
|
public void givenInputStreamEmpty_whenWrite_thenExpectNotWritten() throws Exception {
|
||||||
InputStream inputStream = mock(InputStream.class);
|
InputStream inputStream = mock(InputStream.class);
|
||||||
|
|
||||||
try (BinaryFileWriter tested = new BinaryFileWriter(outputStream)) {
|
try (BinaryFileWriter tested = new BinaryFileWriter(outputStream)) {
|
||||||
long result = tested.write(inputStream);
|
long result = tested.write(inputStream);
|
||||||
|
|
||||||
assertEquals(0, result);
|
assertEquals(0, result);
|
||||||
verify(outputStream, times(0)).write(any(), anyInt(), anyInt());
|
verify(outputStream, times(0)).write(any(), anyInt(), anyInt());
|
||||||
verify(inputStream).close();
|
verify(inputStream).close();
|
||||||
|
}
|
||||||
|
verify(outputStream).close();
|
||||||
}
|
}
|
||||||
verify(outputStream).close();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue