Skip to content

Commit 27bcf6a

Browse files
Applying try() to more rsrcs
Ive also removed the test since its OS specific, and new code is supposed to be in kotlin.
1 parent 5a79e32 commit 27bcf6a

3 files changed

Lines changed: 39 additions & 128 deletions

File tree

app/build.gradle.kts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ sourceSets{
4141
}
4242
}
4343
test{
44-
java {
45-
srcDirs("test")
46-
}
4744
kotlin{
4845
srcDirs("test")
4946
}

app/src/processing/app/Util.java

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,17 @@ static public int countLines(String what) {
6060
*/
6161
static public byte[] loadBytesRaw(File file) throws IOException {
6262
int size = (int) file.length();
63-
FileInputStream input = new FileInputStream(file);
64-
byte[] buffer = new byte[size];
65-
int offset = 0;
66-
int bytesRead;
67-
while ((bytesRead = input.read(buffer, offset, size-offset)) != -1) {
68-
offset += bytesRead;
69-
if (bytesRead == 0) break;
70-
}
71-
input.close(); // weren't properly being closed
72-
return buffer;
63+
byte[] buffer;
64+
try (FileInputStream input = new FileInputStream(file)) {
65+
buffer = new byte[size];
66+
int offset = 0;
67+
int bytesRead;
68+
while ((bytesRead = input.read(buffer, offset, size - offset)) != -1) {
69+
offset += bytesRead;
70+
if (bytesRead == 0) break;
71+
}
72+
}
73+
return buffer;
7374
}
7475

7576

@@ -143,7 +144,7 @@ static public StringDict readSettings(String filename, String[] lines, boolean a
143144
line = line.substring(0, line.indexOf('#')).trim();
144145
}
145146

146-
if (line.length() != 0 && line.charAt(0) != '#') {
147+
if (!line.isEmpty() && line.charAt(0) != '#') {
147148
int equals = line.indexOf('=');
148149
if (equals == -1) {
149150
if (filename != null) {
@@ -161,26 +162,20 @@ static public StringDict readSettings(String filename, String[] lines, boolean a
161162
}
162163

163164

164-
static public void copyFile(File sourceFile,
165-
File targetFile) throws IOException {
166-
BufferedInputStream from =
167-
new BufferedInputStream(new FileInputStream(sourceFile));
168-
BufferedOutputStream to =
169-
new BufferedOutputStream(new FileOutputStream(targetFile));
165+
static public void copyFile(File sourceFile, File targetFile) throws IOException {
166+
try (
167+
BufferedInputStream from = new BufferedInputStream(new FileInputStream(sourceFile));
168+
BufferedOutputStream to = new BufferedOutputStream(new FileOutputStream(targetFile))) {
170169
byte[] buffer = new byte[16 * 1024];
171170
int bytesRead;
172171
while ((bytesRead = from.read(buffer)) != -1) {
173172
to.write(buffer, 0, bytesRead);
174173
}
175-
from.close();
176-
177-
to.flush();
178-
to.close();
179-
180174
//noinspection ResultOfMethodCallIgnored
181175
targetFile.setLastModified(sourceFile.lastModified());
182176
//noinspection ResultOfMethodCallIgnored
183177
targetFile.setExecutable(sourceFile.canExecute());
178+
}
184179
}
185180

186181

@@ -218,13 +213,15 @@ static public void saveFile(String text, File file) throws IOException {
218213
file.getAbsolutePath());
219214
}
220215
// Could use saveStrings(), but we wouldn't be able to checkError()
221-
PrintWriter writer = PApplet.createWriter(temp);
222-
for (String line : lines) {
223-
writer.println(line);
224-
}
225-
boolean error = writer.checkError(); // calls flush()
226-
writer.close(); // attempt to close regardless
227-
if (error) {
216+
boolean error;
217+
try (PrintWriter writer = PApplet.createWriter(temp)) {
218+
for (String line : lines) {
219+
writer.println(line);
220+
}
221+
// calls flush()
222+
error = writer.checkError();
223+
}
224+
if (error) {
228225
throw new IOException("Error while trying to save " + file);
229226
}
230227

@@ -589,7 +586,7 @@ static public StringList packageListFromClassPath(String path) {
589586

590587
for (String piece : pieces) {
591588
//System.out.println("checking piece '" + pieces[i] + "'");
592-
if (piece.length() != 0) {
589+
if (!piece.isEmpty()) {
593590
if (piece.toLowerCase().endsWith(".jar") ||
594591
piece.toLowerCase().endsWith(".zip")) {
595592
//System.out.println("checking " + pieces[i]);
@@ -623,8 +620,7 @@ static public StringList packageListFromClassPath(String path) {
623620

624621

625622
static private void packageListFromZip(String filename, StringList list) {
626-
try {
627-
ZipFile file = new ZipFile(filename);
623+
try (ZipFile file = new ZipFile(filename);) {
628624
Enumeration<?> entries = file.entries();
629625
while (entries.hasMoreElements()) {
630626
ZipEntry entry = (ZipEntry) entries.nextElement();
@@ -643,7 +639,6 @@ static private void packageListFromZip(String filename, StringList list) {
643639
}
644640
}
645641
}
646-
file.close();
647642
} catch (IOException e) {
648643
System.err.println("Ignoring " + filename + " (" + e.getMessage() + ")");
649644
//e.printStackTrace();
@@ -712,22 +707,22 @@ static public void unzip(File zipFile, File dest) throws IOException {
712707

713708

714709
static protected void unzipEntry(ZipInputStream zin, File f) throws IOException {
715-
FileOutputStream out = new FileOutputStream(f);
716-
byte[] b = new byte[512];
717-
int len;
718-
while ((len = zin.read(b)) != -1) {
719-
out.write(b, 0, len);
720-
}
721-
out.flush();
722-
out.close();
710+
try (FileOutputStream out = new FileOutputStream(f)) {
711+
byte[] b = new byte[512];
712+
int len;
713+
while ((len = zin.read(b)) != -1) {
714+
out.write(b, 0, len);
715+
}
716+
out.flush();
717+
}
723718
}
724719

725720

726721
static public byte[] gzipEncode(byte[] what) throws IOException {
727722
ByteArrayOutputStream baos = new ByteArrayOutputStream();
728-
GZIPOutputStream output = new GZIPOutputStream(baos);
729-
PApplet.saveStream(output, new ByteArrayInputStream(what));
730-
output.close();
723+
try (GZIPOutputStream output = new GZIPOutputStream(baos);) {
724+
PApplet.saveStream(output, new ByteArrayInputStream(what));
725+
}
731726
return baos.toByteArray();
732727
}
733728

app/test/processing/app/UtilTest.java

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)