Skip to content

Commit

Permalink
Use try-with-resources (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
runningcode authored and kageiit committed May 2, 2017
1 parent be2d4fc commit b686859
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
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();
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();
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

0 comments on commit b686859

Please sign in to comment.