@@ -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();
@@ -688,9 +683,7 @@ static private void packageListFromFolder(File dir, String sofar,
688683 * Ignores (does not extract) any __MACOSX files from macOS archives.
689684 */
690685 static public void unzip (File zipFile , File dest ) throws IOException {
691- FileInputStream fis = new FileInputStream (zipFile );
692- CheckedInputStream checksum = new CheckedInputStream (fis , new Adler32 ());
693- ZipInputStream zis = new ZipInputStream (new BufferedInputStream (checksum ));
686+ try (ZipInputStream zis = new ZipInputStream ( new BufferedInputStream ( new CheckedInputStream ( new FileInputStream (zipFile ), new Adler32 ())))) {
694687 ZipEntry entry ;
695688 while ((entry = zis .getNextEntry ()) != null ) {
696689 final String name = entry .getName ();
@@ -710,25 +703,26 @@ static public void unzip(File zipFile, File dest) throws IOException {
710703 }
711704 }
712705 }
706+ }
713707
714708
715709 static protected void unzipEntry (ZipInputStream zin , File f ) throws IOException {
716- FileOutputStream out = new FileOutputStream (f );
717- byte [] b = new byte [512 ];
718- int len ;
719- while ((len = zin .read (b )) != -1 ) {
720- out .write (b , 0 , len );
721- }
722- out .flush ();
723- 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+ }
724718 }
725719
726720
727721 static public byte [] gzipEncode (byte [] what ) throws IOException {
728722 ByteArrayOutputStream baos = new ByteArrayOutputStream ();
729- GZIPOutputStream output = new GZIPOutputStream (baos );
730- PApplet .saveStream (output , new ByteArrayInputStream (what ));
731- output . close ();
723+ try ( GZIPOutputStream output = new GZIPOutputStream (baos );) {
724+ PApplet .saveStream (output , new ByteArrayInputStream (what ));
725+ }
732726 return baos .toByteArray ();
733727 }
734728
0 commit comments