Skip to content

Commit c58def0

Browse files
authored
Try with resources safety (#1118)
1 parent a095a0c commit c58def0

2 files changed

Lines changed: 35 additions & 31 deletions

File tree

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/languageserver/ProjectConfigBuilder.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ private static String calculateProjectConfigHash(WurstProjectConfigData projectC
147147
.append(",").append(force.getFlags().getSharedControl())
148148
.append(",").append(force.getFlags().getSharedControlAdvanced())
149149
.append("players:");
150-
for (int id : force.getPlayerIds()) {
151-
sb.append(id).append(",");
152-
}
153-
sb.append("\n");
150+
for (int id : force.getPlayerIds()) {
151+
sb.append(id).append(",");
152+
}
153+
sb.append("\n");
154154
}
155155

156156
// Option flags
@@ -175,20 +175,22 @@ private static void applyBuildMapData(WurstProjectConfigData projectConfig, File
175175
prepareW3I(projectConfig, w3I);
176176
result.script = new File(buildDir, "war3mapj_with_config.j.txt");
177177

178-
FileInputStream inputStream = new FileInputStream(mapScript);
179-
StringWriter sw = new StringWriter();
180-
181-
if (w3data.getWc3PatchVersion().isPresent()) {
182-
w3I.injectConfigsInJassScript(inputStream, sw, w3data.getWc3PatchVersion().get());
183-
} else {
184-
GameVersion version = GameVersion.VERSION_1_32;
185-
WLogger.info(
186-
"Failed to determine installed game version. Falling back to " + version
187-
);
188-
w3I.injectConfigsInJassScript(inputStream, sw, version);
178+
try (FileInputStream inputStream = new FileInputStream(mapScript)) {
179+
StringWriter sw = new StringWriter();
180+
181+
if (w3data.getWc3PatchVersion().isPresent()) {
182+
w3I.injectConfigsInJassScript(inputStream, sw, w3data.getWc3PatchVersion().get());
183+
} else {
184+
GameVersion version = GameVersion.VERSION_1_32;
185+
WLogger.info(
186+
"Failed to determine installed game version. Falling back to " + version
187+
);
188+
w3I.injectConfigsInJassScript(inputStream, sw, version);
189+
}
190+
191+
byte[] scriptBytes = sw.toString().getBytes(StandardCharsets.UTF_8);
192+
Files.write(scriptBytes, result.script);
189193
}
190-
byte[] scriptBytes = sw.toString().getBytes(StandardCharsets.UTF_8);
191-
Files.write(scriptBytes, result.script);
192194
}
193195

194196

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/WLogger.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,24 @@ public static String getLog() {
8686
}
8787

8888
private static String getLast100Lines(File file) throws IOException {
89-
FileInputStream fileInputStream = new FileInputStream(file);
90-
FileChannel channel = fileInputStream.getChannel();
91-
ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
92-
buffer.position((int) channel.size());
93-
int count = 0;
94-
StringBuilder builder = new StringBuilder();
95-
for (long i = channel.size() - 1; i >= 0; i--) {
96-
char c = (char) buffer.get((int) i);
97-
builder.append(c);
98-
if (c == '\n') {
99-
if (count == 100) break;
100-
count++;
89+
FileChannel channel;
90+
try (FileInputStream fileInputStream = new FileInputStream(file)) {
91+
channel = fileInputStream.getChannel();
92+
ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
93+
buffer.position((int) channel.size());
94+
int count = 0;
95+
StringBuilder builder = new StringBuilder();
96+
for (long i = channel.size() - 1; i >= 0; i--) {
97+
char c = (char) buffer.get((int) i);
98+
builder.append(c);
99+
if (c == '\n') {
100+
if (count == 100) break;
101+
count++;
102+
}
101103
}
104+
return builder.toString();
102105
}
103-
channel.close();
104-
return builder.toString();
106+
105107
}
106108

107109

0 commit comments

Comments
 (0)