Skip to content

Commit afbd568

Browse files
committed
mvn spotless:apply
Signed-off-by: vaidikcode <[email protected]>
1 parent ff1a75b commit afbd568

File tree

4 files changed

+42
-44
lines changed

4 files changed

+42
-44
lines changed

src/main/java/land/oras/Registry.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package land.oras;
22

3-
import java.io.InputStream;
4-
import java.io.IOException;
53
import java.io.BufferedInputStream;
64
import java.io.ByteArrayInputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
77
import java.io.OutputStream;
88
import java.net.URI;
99
import java.nio.file.Files;
@@ -223,13 +223,14 @@ public Manifest pushArtifact(
223223
public void pullArtifact(ContainerRef containerRef, Path path, boolean overwrite) {
224224
Manifest manifest = getManifest(containerRef);
225225
for (Layer layer : manifest.getLayers()) {
226-
Path targetPath = path.resolve(layer.getAnnotations()
227-
.getOrDefault(Const.ANNOTATION_TITLE, layer.getDigest()));
228-
226+
Path targetPath =
227+
path.resolve(layer.getAnnotations().getOrDefault(Const.ANNOTATION_TITLE, layer.getDigest()));
228+
229229
try (InputStream is = fetchBlob(containerRef.withDigest(layer.getDigest()))) {
230-
Files.copy(is, targetPath, overwrite ?
231-
StandardCopyOption.REPLACE_EXISTING :
232-
StandardCopyOption.ATOMIC_MOVE);
230+
Files.copy(
231+
is,
232+
targetPath,
233+
overwrite ? StandardCopyOption.REPLACE_EXISTING : StandardCopyOption.ATOMIC_MOVE);
233234
} catch (IOException e) {
234235
throw new OrasException("Failed to pull artifact", e);
235236
}
@@ -272,9 +273,10 @@ public Manifest pushArtifact(
272273
Layer layer = pushBlobStream(containerRef, is, size)
273274
.withMediaType(Const.DEFAULT_BLOB_DIR_MEDIA_TYPE) // Use tar+gzip for directories
274275
.withAnnotations(Map.of(
275-
Const.ANNOTATION_TITLE, path.getFileName().toString(),
276-
Const.ANNOTATION_ORAS_UNPACK, "true"
277-
));
276+
Const.ANNOTATION_TITLE,
277+
path.getFileName().toString(),
278+
Const.ANNOTATION_ORAS_UNPACK,
279+
"true"));
278280
layers.add(layer);
279281
LOG.info("Uploaded directory: {}", layer.getDigest());
280282
}
@@ -290,8 +292,8 @@ public Manifest pushArtifact(
290292
Layer layer = pushBlobStream(containerRef, is, size)
291293
.withMediaType(mediaType)
292294
.withAnnotations(Map.of(
293-
Const.ANNOTATION_TITLE, path.getFileName().toString()
294-
));
295+
Const.ANNOTATION_TITLE,
296+
path.getFileName().toString()));
295297
layers.add(layer);
296298
LOG.info("Uploaded: {}", layer.getDigest());
297299
}
@@ -669,8 +671,9 @@ public Layer pushBlobStream(ContainerRef containerRef, InputStream input, long s
669671
// Copy input stream to temp file while calculating digest
670672
String digest;
671673
try (InputStream bufferedInput = new BufferedInputStream(input);
672-
DigestInputStream digestInput = new DigestInputStream(bufferedInput, MessageDigest.getInstance("SHA-256"));
673-
OutputStream fileOutput = Files.newOutputStream(tempFile)) {
674+
DigestInputStream digestInput =
675+
new DigestInputStream(bufferedInput, MessageDigest.getInstance("SHA-256"));
676+
OutputStream fileOutput = Files.newOutputStream(tempFile)) {
674677

675678
digestInput.transferTo(fileOutput);
676679
byte[] digestBytes = digestInput.getMessageDigest().digest();
@@ -692,7 +695,10 @@ public Layer pushBlobStream(ContainerRef containerRef, InputStream input, long s
692695

693696
// Start with a POST request to initiate the upload
694697
OrasHttpClient.ResponseWrapper<String> initiateResponse = client.uploadStream(
695-
"POST", baseUri, emptyStream, 0,
698+
"POST",
699+
baseUri,
700+
emptyStream,
701+
0,
696702
Map.of(Const.CONTENT_TYPE_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE));
697703

698704
if (initiateResponse.statusCode() != 202) {
@@ -717,12 +723,15 @@ public Layer pushBlobStream(ContainerRef containerRef, InputStream input, long s
717723
// Upload the content from the temporary file
718724
try (InputStream uploadStream = Files.newInputStream(tempFile)) {
719725
OrasHttpClient.ResponseWrapper<String> uploadResponse = client.uploadStream(
720-
"PUT", finalizeUri, uploadStream, size,
726+
"PUT",
727+
finalizeUri,
728+
uploadStream,
729+
size,
721730
Map.of(Const.CONTENT_TYPE_HEADER, Const.APPLICATION_OCTET_STREAM_HEADER_VALUE));
722731

723732
if (uploadResponse.statusCode() != 201 && uploadResponse.statusCode() != 202) {
724-
throw new OrasException("Failed to upload blob: " + uploadResponse.statusCode() +
725-
" - Response: " + uploadResponse.response());
733+
throw new OrasException("Failed to upload blob: " + uploadResponse.statusCode() + " - Response: "
734+
+ uploadResponse.response());
726735
}
727736

728737
return Layer.fromDigest(digest, size);

src/main/java/land/oras/utils/DigestUtils.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public static String digest(String algorithm, byte[] bytes) {
9393
* @return The digest
9494
*/
9595
public static String sha256(InputStream input) {
96-
try{
96+
try {
9797
MessageDigest digest = MessageDigest.getInstance("SHA-256");
9898
byte[] buffer = new byte[8192];
9999
int bytesRead;
@@ -106,8 +106,7 @@ public static String sha256(InputStream input) {
106106
sb.append(String.format("%02x", b));
107107
}
108108
return "sha256:%s".formatted(sb.toString());
109-
}
110-
catch (Exception e) {
109+
} catch (Exception e) {
111110
throw new OrasException("Failed to calculate digest", e);
112111
}
113112
}

src/main/java/land/oras/utils/OrasHttpClient.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -277,19 +277,17 @@ public ResponseWrapper<String> put(URI uri, byte[] body, Map<String, String> hea
277277
* @param headers The headers
278278
* @return The response
279279
*/
280-
public ResponseWrapper<String> uploadStream(String method, URI uri,
281-
InputStream input, long size, Map<String, String> headers) {
280+
public ResponseWrapper<String> uploadStream(
281+
String method, URI uri, InputStream input, long size, Map<String, String> headers) {
282282
try {
283-
HttpRequest.BodyPublisher publisher = HttpRequest.BodyPublishers.ofInputStream(
284-
() -> input);
285-
286-
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
287-
.uri(uri)
288-
.method(method, publisher);
289-
283+
HttpRequest.BodyPublisher publisher = HttpRequest.BodyPublishers.ofInputStream(() -> input);
284+
285+
HttpRequest.Builder requestBuilder =
286+
HttpRequest.newBuilder().uri(uri).method(method, publisher);
287+
290288
// Add headers
291289
headers.forEach(requestBuilder::header);
292-
290+
293291
// Execute request
294292
HttpRequest request = requestBuilder.build();
295293
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
@@ -339,8 +337,7 @@ private <T> ResponseWrapper<T> toResponseWrapper(HttpResponse<T> response) {
339337
response.statusCode(),
340338
response.headers().map().entrySet().stream()
341339
.collect(Collectors.toMap(
342-
Map.Entry::getKey,
343-
e -> e.getValue().get(0))));
340+
Map.Entry::getKey, e -> e.getValue().get(0))));
344341
}
345342

346343
/**

src/test/java/land/oras/RegistryTest.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
import java.util.List;
1313
import java.util.Map;
1414
import java.util.Random;
15-
1615
import land.oras.utils.Const;
1716
import land.oras.utils.DigestUtils;
18-
import land.oras.utils.JsonUtils;
1917
import land.oras.utils.RegistryContainer;
2018
import org.junit.jupiter.api.Assertions;
2119
import org.junit.jupiter.api.BeforeEach;
@@ -249,8 +247,6 @@ void shouldPushAndGetBlobStream() throws IOException {
249247
assertEquals(fileSize, layer.getSize());
250248
}
251249

252-
253-
254250
// Test getBlobStream
255251
try (InputStream resultStream = registry.getBlobStream(containerRef.withDigest(layer.getDigest()))) {
256252
String result = new String(resultStream.readAllBytes());
@@ -317,9 +313,8 @@ public int read() throws IOException {
317313
};
318314

319315
// Verify exception is wrapped in OrasException
320-
OrasException exception = assertThrows(OrasException.class, () ->
321-
registry.pushBlobStream(containerRef, failingStream, 100)
322-
);
316+
OrasException exception =
317+
assertThrows(OrasException.class, () -> registry.pushBlobStream(containerRef, failingStream, 100));
323318
assertEquals("Failed to push blob stream", exception.getMessage());
324319
assertTrue(exception.getCause() instanceof IOException);
325320
}
@@ -337,9 +332,7 @@ void shouldHandleNonExistentBlobInGetStream() {
337332
String nonExistentDigest = "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
338333

339334
// Verify it throws OrasException
340-
assertThrows(OrasException.class, () ->
341-
registry.getBlobStream(containerRef.withDigest(nonExistentDigest))
342-
);
335+
assertThrows(OrasException.class, () -> registry.getBlobStream(containerRef.withDigest(nonExistentDigest)));
343336
}
344337

345338
@Test

0 commit comments

Comments
 (0)