Skip to content

Commit

Permalink
[error-prone] close I/O resources properly
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Oct 23, 2024
1 parent 181e348 commit cec21d3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.xhtmlrenderer.extend.UserAgentCallback;
import org.xhtmlrenderer.resource.CSSResource;
import org.xhtmlrenderer.util.Configuration;
import org.xhtmlrenderer.util.IOUtil;
import org.xhtmlrenderer.util.XRLog;
import org.xml.sax.InputSource;

Expand All @@ -39,7 +38,6 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.logging.Level;

Expand Down Expand Up @@ -98,16 +96,12 @@ private Stylesheet parse(StylesheetInfo info) {
// since the null resource stream is wrapped in a BufferedInputStream
InputSource inputSource=cr.getResourceInputSource();
if (inputSource==null) return null;
InputStream is = inputSource.getByteStream();
if (is==null) return null;
try {
try (InputStream is = inputSource.getByteStream()) {
if (is == null) return null;
String charset = Configuration.valueFor("xr.stylesheets.charset-name", "UTF-8");
return parse(new InputStreamReader(is, charset), info);
} catch (UnsupportedEncodingException e) {
// Shouldn't happen
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
IOUtil.close(is);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.io.StringWriter;
import java.util.logging.Level;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.xhtmlrenderer.util.ImageUtil.withGraphics;

public class DocumentDiffTest {
Expand Down Expand Up @@ -98,45 +99,35 @@ private static String file_to_string(String filename) throws IOException {
}

private static String file_to_string(File file) throws IOException {
FileReader reader = null;
StringWriter writer = null;
String str;
try {
reader = new FileReader(file);
writer = new StringWriter();
char[] buf = new char[1000];
while (true) {
int n = reader.read(buf, 0, 1000);
if (n == -1) {
break;
try (FileReader reader = new FileReader(file, UTF_8)) {
try (StringWriter writer = new StringWriter()) {
char[] buf = new char[1000];
while (true) {
int n = reader.read(buf, 0, 1000);
if (n == -1) {
break;
}
writer.write(buf, 0, n);
}
writer.write(buf, 0, n);
}
str = writer.toString();
} finally {
if (reader != null) {
reader.close();
}
if (writer != null) {
writer.close();
return writer.toString();
}
}
return str;
}

public static void string_to_file(String text, File file)
throws IOException {
try (FileWriter writer = new FileWriter(file)) {
StringReader reader = new StringReader(text);
char[] buf = new char[1000];
while (true) {
int n = reader.read(buf, 0, 1000);
if (n == -1) {
break;
try (FileWriter writer = new FileWriter(file, UTF_8)) {
try (StringReader reader = new StringReader(text)) {
char[] buf = new char[1000];
while (true) {
int n = reader.read(buf, 0, 1000);
if (n == -1) {
break;
}
writer.write(buf, 0, n);
}
writer.write(buf, 0, n);
writer.flush();
}
writer.flush();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public static byte[] readBytes(InputStream is) throws IOException {
return result.toByteArray();
}

/**
* @deprecated Use try-with-resources idiom instead.
*/
@Deprecated
@SuppressWarnings("EmptyCatch")
public static void close(@Nullable Closeable in) {
if (in != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import static java.nio.charset.StandardCharsets.UTF_8;
Expand Down Expand Up @@ -58,17 +57,14 @@ private List<String> loadForKey(String lang) {

try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path)) {
if (is == null) return null;
BufferedReader r = new BufferedReader(new InputStreamReader(is, UTF_8));
List<String> result = new ArrayList<>();
String line;
while ((line = r.readLine()) != null) {
if (line.isEmpty() || line.startsWith("#")) continue;
result.add(line);
try (BufferedReader r = new BufferedReader(new InputStreamReader(is, UTF_8))) {
return r.lines()
.filter(line -> !line.isEmpty())
.filter(line -> !line.startsWith("#"))
.toList();
}
return result;
} catch (IOException e) {
throw new RuntimeException("Error while loading nbsp file from path " + path, e);
}
}

}

0 comments on commit cec21d3

Please sign in to comment.