Skip to content

Commit 43bcf99

Browse files
committed
ID-1570 - code cleanup;
1 parent 19a4c6c commit 43bcf99

File tree

4 files changed

+22
-51
lines changed

4 files changed

+22
-51
lines changed

dice-where-downloader-lib/src/main/java/technology/dice/dicewhere/downloader/destination/local/LocalFileAcceptor.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
public class LocalFileAcceptor implements FileAcceptor<Void> {
2222

2323
private static final Logger LOG = LoggerFactory.getLogger(LocalFileAcceptor.class);
24-
private static final int BUFFER = 8192;
2524

2625
private final Path destination;
2726

@@ -39,7 +38,7 @@ public StreamConsumer<Void> getStreamConsumer(
3938
} catch (FileAlreadyExistsException e) {
4039
LOG.debug("Destination directory already exists");
4140
}
42-
Files.copy(stream.getInputStream(), destination, StandardCopyOption.REPLACE_EXISTING);
41+
Files.copy(stream.inputStream(), destination, StandardCopyOption.REPLACE_EXISTING);
4342
if ((!noMd5Check) && (!originalFileMd5.matches(stream.md5()))) {
4443
LOG.error("MD5 mismatch. Deleting destination file");
4544
Files.delete(destination);

dice-where-downloader-lib/src/main/java/technology/dice/dicewhere/downloader/destination/s3/S3FileAcceptor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public StreamConsumer<Void> getStreamConsumer(
5959
.contentLength(size)
6060
.storageClass(StorageClass.INTELLIGENT_TIERING)
6161
.build();
62-
client.putObject(putObjectRequest, RequestBody.fromInputStream(stream.getInputStream(), size));
62+
client.putObject(putObjectRequest, RequestBody.fromInputStream(stream.inputStream(), size));
6363
Latest latest = new Latest(clock.instant(), key);
6464
String latestContent = mapper.writeValueAsString(latest);
6565

dice-where-downloader-lib/src/main/java/technology/dice/dicewhere/downloader/stream/StreamWithMD5Decorator.java

+18-46
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@
1313

1414
public class StreamWithMD5Decorator extends InputStream {
1515

16-
private final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
16+
private static final int BUFFER_SIZE = 8192;
17+
private static final HexBinaryAdapter HEX_BINARY_ADAPTER = new HexBinaryAdapter();
18+
1719
private final MessageDigest md5;
18-
DigestInputStream inputStream;
20+
private final DigestInputStream originalStream;
21+
private final ByteArrayOutputStream duplicatedStream = new ByteArrayOutputStream();
22+
1923
private boolean consumed = false;
2024
private Optional<MD5Checksum> md5Checksum = Optional.empty();
2125

22-
private StreamWithMD5Decorator(DigestInputStream inputStream, MessageDigest md5)
26+
private StreamWithMD5Decorator(DigestInputStream originalStream, MessageDigest md5)
2327
throws IOException {
24-
this.inputStream = inputStream;
28+
this.originalStream = originalStream;
2529
this.md5 = md5;
2630
consumeStream();
2731
}
@@ -34,10 +38,10 @@ public static StreamWithMD5Decorator of(InputStream inputStream)
3438
}
3539

3640
private void consumeStream() throws IOException {
37-
byte[] data = new byte[8192];
41+
byte[] data = new byte[BUFFER_SIZE];
3842
int bytesRead;
39-
while ((bytesRead = inputStream.read(data)) != -1) {
40-
buffer.write(data, 0, bytesRead);
43+
while ((bytesRead = originalStream.read(data)) != -1) {
44+
duplicatedStream.write(data, 0, bytesRead);
4145
}
4246
consumed = true;
4347
}
@@ -48,68 +52,36 @@ public MD5Checksum md5() {
4852
}
4953
return md5Checksum.orElseGet(
5054
() -> {
51-
String hex = (new HexBinaryAdapter()).marshal(md5.digest());
55+
String hex = HEX_BINARY_ADAPTER.marshal(md5.digest());
5256
MD5Checksum checksum = MD5Checksum.of(hex);
5357
md5Checksum = Optional.of(checksum);
5458
return checksum;
5559
});
5660
}
5761

58-
public InputStream getInputStream() {
59-
return new ByteArrayInputStream(buffer.toByteArray());
62+
public InputStream inputStream() {
63+
return new ByteArrayInputStream(duplicatedStream.toByteArray());
6064
}
6165

6266
@Override
6367
public int read() throws IOException {
6468
if (!consumed) {
6569
throw new IllegalStateException("Stream not fully consumed yet.");
6670
}
67-
return getInputStream().read();
71+
return inputStream().read();
6872
}
6973

7074
@Override
7175
public int read(byte[] b, int off, int len) throws IOException {
7276
if (!consumed) {
7377
throw new IllegalStateException("Stream not fully consumed yet.");
7478
}
75-
return getInputStream().read(b, off, len);
79+
return inputStream().read(b, off, len);
7680
}
7781

7882
@Override
7983
public void close() throws IOException {
80-
getInputStream().close();
81-
inputStream.close();
82-
}
83-
/*
84-
public static String of1(InputStream inputStream) throws NoSuchAlgorithmException {
85-
return bytesToHex(checksum(inputStream));
86-
}
87-
88-
private static byte[] checksum(InputStream is) {
89-
90-
MessageDigest md;
91-
try {
92-
md = MessageDigest.getInstance("MD5");
93-
} catch (NoSuchAlgorithmException e) {
94-
throw new IllegalArgumentException(e);
95-
}
96-
97-
try (DigestInputStream dis = new DigestInputStream(is, md)) {
98-
while (dis.read() != -1)
99-
; // empty loop to clear the data
100-
md = dis.getMessageDigest();
101-
} catch (IOException e) {
102-
throw new IllegalArgumentException(e);
103-
}
104-
return md.digest();
105-
}
106-
107-
private static String bytesToHex(byte[] bytes) {
108-
StringBuilder sb = new StringBuilder();
109-
for (byte b : bytes) {
110-
sb.append(String.format("%02x", b));
111-
}
112-
return sb.toString();
84+
inputStream().close();
85+
originalStream.close();
11386
}
114-
*/
11587
}

dice-where-downloader-lib/src/test/java/technology/dice/dicewhere/downloader/stream/StreamWithMD5DecoratorTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void shouldSuccessfullyReadAndCalculateDigestOfStream()
4242

4343
String first = is.md5().stringFormat();
4444
// Read from the stream
45-
IOUtils.toString(is.getInputStream(), Charset.defaultCharset());
45+
IOUtils.toString(is.inputStream(), Charset.defaultCharset());
4646

4747
// Assert the Stream Hash before and after
4848
assertEquals(first, is.md5().stringFormat());
@@ -66,7 +66,7 @@ public void shouldSuccessfullyReadAndCalculateDigestOfStreamFromHttp()
6666

6767
String first = is.md5().stringFormat();
6868
// Read from the stream
69-
IOUtils.toString(is.getInputStream(), Charset.defaultCharset());
69+
IOUtils.toString(is.inputStream(), Charset.defaultCharset());
7070

7171
// Assert the Stream Hash before and after
7272
assertEquals(first, is.md5().stringFormat());

0 commit comments

Comments
 (0)