Skip to content

Commit

Permalink
refactor: replace explicit close with try-with-resources
Browse files Browse the repository at this point in the history
  • Loading branch information
jdrueckert committed Oct 30, 2023
1 parent f585450 commit 01e8edd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,13 @@ private void setupTeraButtonsMapping() {
}

private void updateGamepadMappings() {
InputStream inputStream = LwjglControllerDevice.class.getResourceAsStream("/gamecontrollerdb.txt");
try {
try (InputStream inputStream = LwjglControllerDevice.class.getResourceAsStream("/gamecontrollerdb.txt")) {
byte[] bytes = ByteStreams.toByteArray(inputStream);
String gamecontrollerDBContent = new String(bytes, TerasologyConstants.CHARSET);
ByteBuffer gamecontrolleDB = MemoryUtil.memASCIISafe(gamecontrollerDBContent);
if (!GLFW.glfwUpdateGamepadMappings(gamecontrolleDB)) {
logger.error("Cannot update GLFW's gamepad mapping, gamepads may not work correctly");
}
inputStream.close();
} catch (IOException e) {
logger.error("Cannot read resource '/gamecontrollerdb.txt', gamepads may not work correctly", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ private void writeChunkStores() throws IOException {
try (BufferedOutputStream bos = new BufferedOutputStream(Files.newOutputStream(chunkPath))) {
bos.write(compressedChunk);
}
// FIXME: this only closes happy path but not if exceptions are thrown
zip.close();
}
// Copy existing, unmodified content into the zips and close them
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ public static Collection<FlowRenderable> parseHTMLLike(String text) {
if (text == null) {
return null;
}
StringReader reader = new StringReader(text);

int character;
try {
try (StringReader reader = new StringReader(text)) {
StringBuilder sb = new StringBuilder();
Font font = null;
Color color = null;
Expand Down Expand Up @@ -154,15 +153,11 @@ public static Collection<FlowRenderable> parseHTMLLike(String text) {
result.add(new TextFlowRenderable(sb.toString(), new DefaultTextRenderStyle(font, color), hyperlink));
}

reader.close();

return result;
} catch (IOException exp) {
// Ignore - can't happen
}

reader.close();

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,28 @@ public static float[][] readFile() throws IOException {

public static float[][] readValues(java.io.InputStream in, String delimiter) throws java.io.IOException, java.lang.NumberFormatException {
String thisLine;
java.io.BufferedInputStream s = new java.io.BufferedInputStream(in);
java.io.BufferedReader myInput = new java.io.BufferedReader(new java.io.InputStreamReader(s, Charsets.UTF_8));

int index = 0;
float min = 0;
float max = 0;
float[][] theMap = new float[512][512];

while ((thisLine = myInput.readLine()) != null) {

// scan it line by line
java.util.StringTokenizer st = new java.util.StringTokenizer(thisLine, delimiter);
float a = Float.valueOf(st.nextToken());
theMap[index / 512][index % 512] = a;
index++;
min = a < min ? a : min;
max = a > max ? a : max;
if (index / 512 > 511) {
break;
try (java.io.BufferedInputStream s = new java.io.BufferedInputStream(in);
java.io.BufferedReader myInput = new java.io.BufferedReader(new java.io.InputStreamReader(s, Charsets.UTF_8))) {
while ((thisLine = myInput.readLine()) != null) {

// scan it line by line
java.util.StringTokenizer st = new java.util.StringTokenizer(thisLine, delimiter);
float a = Float.valueOf(st.nextToken());
theMap[index / 512][index % 512] = a;
index++;
min = a < min ? a : min;
max = a > max ? a : max;
if (index / 512 > 511) {
break;
}
}
}

myInput.close();
s.close();

return (theMap);
}
}

0 comments on commit 01e8edd

Please sign in to comment.