Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -424,47 +424,23 @@ public void handle(String target, Request req, HttpServletRequest request, HttpS
if (HttpMethod.HEAD.is(req.getMethod())) {
return;
}
FileInputStream is = null;
try {
is = new FileInputStream(file);
try (FileInputStream is = new FileInputStream(file)) {
if (offset > 0L) {
long skipped = is.skip(offset);
while (skipped < offset && is.read() >= 0) {
skipped++;
}
}
IO.copy(is, response.getOutputStream());
is.close();
is = null;
} finally {
try {
if (is != null) {
is.close();
}
} catch (final IOException e) {
// Suppressed due to an exception already thrown in the try block.
}
}
} else if (HttpMethod.PUT.is(req.getMethod())) {
if (!webDav) {
file.getParentFile().mkdirs();
}
if (file.getParentFile().exists()) {
try {
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
try (FileOutputStream os = new FileOutputStream(file)) {
IO.copy(request.getInputStream(), os);
os.close();
os = null;
} finally {
try {
if (os != null) {
os.close();
}
} catch (final IOException e) {
// Suppressed due to an exception already thrown in the try block.
}
}
} catch (IOException e) {
file.delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,9 @@ public List<DependencyNode> parseMultiResource(String resource) throws IOExcepti
* Parse the graph definition read from the given URL.
*/
public DependencyNode parse(URL resource) throws IOException {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(resource.openStream(), StandardCharsets.UTF_8));
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(resource.openStream(), StandardCharsets.UTF_8))) {
return parse(reader);
} finally {
try {
if (reader != null) {
reader.close();
reader = null;
}
} catch (final IOException e) {
// Suppressed due to an exception already thrown in the try block.
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ private ArtifactDescription parse(Reader reader) throws IOException {

Map<State, List<String>> sections = new HashMap<>();

BufferedReader in = null;
try {
in = new BufferedReader(reader);
try (BufferedReader in = new BufferedReader(reader)) {
while ((line = in.readLine()) != null) {

line = cutComment(line);
Expand All @@ -123,17 +121,6 @@ private ArtifactDescription parse(Reader reader) throws IOException {
lines.add(line.trim());
}
}

in.close();
in = null;
} finally {
try {
if (in != null) {
in.close();
}
} catch (final IOException e) {
// Suppressed due to an exception already thrown in the try block.
}
}

Artifact relocation = relocation(sections.get(State.RELOCATION));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Copy link
Member

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?

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))) {
Copy link
Member

Choose a reason for hiding this comment

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

Are we sure new FileOutputStream is correctly close? Why not Files.newOuputStream what is the reasoning not using it?

Copy link
Contributor

Choose a reason for hiding this comment

The 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.
}
}
}

Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

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

what is the reasoning about not using Files.newInputStream and Files.newOutputStream?

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,11 @@ public static File createTempDir(String suffix) throws IOException {
public static long copyFile(File source, File target) throws IOException {
long total = 0;

FileInputStream fis = null;
OutputStream fos = null;
try {
fis = new FileInputStream(source);
try (FileInputStream fis = new FileInputStream(source);
OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))) {

mkdirs(target.getParentFile());

fos = new BufferedOutputStream(new FileOutputStream(target));

for (byte[] buffer = new byte[1024 * 32]; ; ) {
int bytes = fis.read(buffer);
if (bytes < 0) {
Expand All @@ -191,28 +187,6 @@ public static long copyFile(File source, File target) throws IOException {

total += bytes;
}

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;
Expand All @@ -228,45 +202,21 @@ public static long copyFile(File source, File target) throws IOException {
*/
@Deprecated
public static byte[] readBytes(File file) throws IOException {
RandomAccessFile in = null;
try {
in = new RandomAccessFile(file, "r");
try (RandomAccessFile in = new RandomAccessFile(file, "r")) {
byte[] actual = new byte[(int) in.length()];
in.readFully(actual);
in.close();
in = null;
return actual;
} finally {
try {
if (in != null) {
in.close();
}
} catch (final IOException e) {
// Suppressed due to an exception already thrown in the try block.
}
}
}

@Deprecated
public static void writeBytes(File file, byte[] pattern, int repeat) throws IOException {
file.deleteOnExit();
file.getParentFile().mkdirs();
OutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(file));
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
for (int i = 0; i < repeat; i++) {
out.write(pattern);
}
out.close();
out = null;
} finally {
try {
if (out != null) {
out.close();
}
} catch (final IOException e) {
// Suppressed due to an exception already thrown in the try block.
}
}
}

Expand All @@ -287,40 +237,16 @@ public static void writeString(File file, String content, long timestamp) throws
}

public static void readProps(File file, Properties props) throws IOException {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
try (FileInputStream fis = new FileInputStream(file)) {
props.load(fis);
fis.close();
fis = null;
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (final IOException e) {
// Suppressed due to an exception already thrown in the try block.
}
}
}

public static void writeProps(File file, Properties props) throws IOException {
file.getParentFile().mkdirs();

FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
try (FileOutputStream fos = new FileOutputStream(file)) {
props.store(fos, "aether-test");
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.
}
}
}
}
Loading