-
Notifications
You must be signed in to change notification settings - Fork 140
Refactor resource management to use try-with-resources for better readability (Issue: #1521) #1623
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,46 +63,18 @@ public boolean mkdirs(File directory) { | |
public void write(File file, String data) throws IOException { | ||
mkdirs(file.getParentFile()); | ||
|
||
FileOutputStream fos = null; | ||
try { | ||
fos = new FileOutputStream(file); | ||
|
||
try (FileOutputStream fos = new FileOutputStream(file)) { | ||
if (data != null) { | ||
fos.write(data.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
fos.close(); | ||
fos = null; | ||
} finally { | ||
try { | ||
if (fos != null) { | ||
fos.close(); | ||
} | ||
} catch (final IOException e) { | ||
// Suppressed due to an exception already thrown in the try block. | ||
} | ||
} | ||
} | ||
|
||
public void write(File target, InputStream source) throws IOException { | ||
mkdirs(target.getAbsoluteFile().getParentFile()); | ||
|
||
OutputStream fos = null; | ||
try { | ||
fos = new BufferedOutputStream(new FileOutputStream(target)); | ||
|
||
try (OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we sure There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, it will be closed. That's how chained streams work. |
||
copy(fos, source, null); | ||
|
||
fos.close(); | ||
fos = null; | ||
} finally { | ||
try { | ||
if (fos != null) { | ||
fos.close(); | ||
} | ||
} catch (final IOException e) { | ||
// Suppressed due to an exception already thrown in the try block. | ||
} | ||
} | ||
} | ||
|
||
|
@@ -113,38 +85,11 @@ public void copy(File source, File target) throws IOException { | |
public long copy(File source, File target, ProgressListener listener) throws IOException { | ||
long total = 0; | ||
|
||
InputStream fis = null; | ||
OutputStream fos = null; | ||
try { | ||
fis = new FileInputStream(source); | ||
try (InputStream fis = new FileInputStream(source); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the reasoning about not using |
||
OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))) { | ||
|
||
mkdirs(target.getAbsoluteFile().getParentFile()); | ||
|
||
fos = new BufferedOutputStream(new FileOutputStream(target)); | ||
|
||
total = copy(fos, fis, listener); | ||
|
||
fos.close(); | ||
fos = null; | ||
|
||
fis.close(); | ||
fis = null; | ||
} finally { | ||
try { | ||
if (fos != null) { | ||
fos.close(); | ||
} | ||
} catch (final IOException e) { | ||
// Suppressed due to an exception already thrown in the try block. | ||
} finally { | ||
try { | ||
if (fis != null) { | ||
fis.close(); | ||
} | ||
} catch (final IOException e) { | ||
// Suppressed due to an exception already thrown in the try block. | ||
} | ||
} | ||
} | ||
|
||
return total; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not
Files.newInputStream
?AI is not smart enough here?