Skip to content

Commit

Permalink
Using try-with-resources for some minor cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Elopteryx committed Feb 28, 2015
1 parent 45be87d commit 508e801
Showing 1 changed file with 7 additions and 33 deletions.
40 changes: 7 additions & 33 deletions core/src/main/java/io/undertow/util/FileUtils.java
Expand Up @@ -20,7 +20,6 @@

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -61,26 +60,16 @@ public static String readFile(final File file) {
}

public static String readFile(InputStream file) {
BufferedInputStream stream = null;
try {
stream = new BufferedInputStream(file);
try (BufferedInputStream stream = new BufferedInputStream(file)) {
byte[] buff = new byte[1024];
StringBuilder builder = new StringBuilder();
int read = -1;
int read;
while ((read = stream.read(buff)) != -1) {
builder.append(new String(buff, 0, read));
}
return builder.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
//ignore
}
}
}
}

Expand Down Expand Up @@ -112,33 +101,18 @@ public static File getFileOrCheckParentsIfNotFound(String baseStr, String path)


public static void copyFile(final File src, final File dest) throws IOException {
final InputStream in = new BufferedInputStream(new FileInputStream(src));
try {
try (InputStream in = new BufferedInputStream(new FileInputStream(src))) {
copyFile(in, dest);
} finally {
close(in);
}
}

public static void copyFile(final InputStream in, final File dest) throws IOException {
dest.getParentFile().mkdirs();
final OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
try {
int i = in.read();
while (i != -1) {
out.write(i);
i = in.read();
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(dest))) {
int read;
while ((read = in.read()) != -1) {
out.write(read);
}
} finally {
close(out);
}
}


public static void close(Closeable closeable) {
try {
closeable.close();
} catch (IOException ignore) {
}
}

Expand Down

0 comments on commit 508e801

Please sign in to comment.