Skip to content

[SHRINKWRAP-546] Using try-with-resources where appropriate - #178

Merged
petrberan merged 1 commit into
shrinkwrap:mainfrom
khermano:SHRINKWRAP-546_from_main
Sep 26, 2024
Merged

petrberan merged 1 commit into
shrinkwrap:mainfrom
khermano:SHRINKWRAP-546_from_main

Conversation

@khermano

@khermano khermano commented Sep 2, 2024

Copy link
Copy Markdown
Contributor

@khermano
khermano force-pushed the SHRINKWRAP-546_from_main branch 2 times, most recently from 8a4d67d to ec4e448 Compare September 2, 2024 13:24
@khermano
khermano force-pushed the SHRINKWRAP-546_from_main branch from ec4e448 to 35f3376 Compare September 25, 2024 07:51
}
};
Assertions.assertThrows(ArchiveExportException.class, () -> IOUtil.copyWithClose(in, sink));
try (// Export

@petrberan petrberan Sep 26, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this would look better as:

try (final InputStream in = this.exportAsInputStream(archive); // Export

*/
private static SerializableView serializeAndDeserialize(final SerializableView archive) throws IOException, ClassNotFoundException {
assert archive != null : "Archive must be specified";
//there are failing tests for JDK 8 with error message "ID not as expected after serialization" if we put this stream in try-with-resources

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
try (final ObjectOutputStream out = new ObjectOutputStream(byteOut)) {
    out.writeObject(archive);
}
try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(byteOut.toByteArray()))) {
    return (SerializableView) in.readObject();
}

@khermano khermano Sep 26, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what exactly changed since the last time I was testing this but now everything looks okay when I put both of the streams in try-with-resources statement and run it locally, so I will use the statement and remove the comments.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note here, but having the ByteArrayOutputStream inside the try clause is redundant. Documentation states that "Closing a ByteArrayOutputStream has no effect." and if you have a look inside the ByteArrayOutputStream you can see that the close method is empty.

But no harm in leaving it as is, just a fun fact

@petrberan

Copy link
Copy Markdown
Member

Just two small things to have a look at @khermano , otherwise this looks good

@khermano
khermano force-pushed the SHRINKWRAP-546_from_main branch from 35f3376 to 175ed29 Compare September 26, 2024 11:14

@petrberan petrberan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@petrberan
petrberan merged commit 20eddcc into shrinkwrap:main Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants