Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use try-with-resources #432

Merged
merged 1 commit into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions buildSrc/src/main/java/com/uber/okbuck/core/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,12 @@ public static String getRelativePath(File root, File f) {

@SuppressWarnings("ResultOfMethodCallIgnored")
public static void copyResourceToProject(String resource, File destination) {
try {
InputStream inputStream = FileUtil.class.getResourceAsStream(resource);
destination.getParentFile().mkdirs();
OutputStream outputStream = new FileOutputStream(destination);
destination.getParentFile().mkdirs();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we move the directory creation inside the try?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, it can't. This was why this PR was originally failing on ci. The next line, new FileOutputStream(destination) depends on this file having been created.
Luckily, this method doesn't throw an IOException.

try (InputStream inputStream = FileUtil.class.getResourceAsStream(resource);
OutputStream outputStream = new FileOutputStream(destination)) {
IOUtils.copy(inputStream, outputStream);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
} catch (IOException e) {
throw new IllegalStateException(e);
throw new RuntimeException(e);
}
}

Expand Down
16 changes: 6 additions & 10 deletions buildSrc/src/main/java/com/uber/okbuck/core/util/ReplaceUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@ final class ReplaceUtil {

@SuppressWarnings("ResultOfMethodCallIgnored")
static void copyResourceToProject(String resource, File destination, Map<String, String> templates) {
try {
InputStream inputStream = FileUtil.class.getResourceAsStream(resource);
destination.getParentFile().mkdirs();
InputStreamReader reader = new InputStreamReader(inputStream);
TemplateReader replacingReader = new TemplateReader(reader, new TemplateMapResolver(templates));

OutputStream outputStream = new FileOutputStream(destination);
destination.getParentFile().mkdirs();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we move the directory creation inside the try?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same thing here new FileOutputStream(destination) depends on this file having been created.

try (InputStream inputStream = FileUtil.class.getResourceAsStream(resource);
InputStreamReader reader = new InputStreamReader(inputStream);
TemplateReader replacingReader = new TemplateReader(reader, new TemplateMapResolver(templates));
OutputStream outputStream = new FileOutputStream(destination)){
IOUtils.copy(replacingReader, outputStream);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
} catch (IOException e) {
throw new IllegalStateException(e);
throw new RuntimeException(e);
}
}

Expand Down