@@ -118,7 +118,7 @@ public class PApplet implements PConstants {
118118 static {
119119 final String name = System .getProperty ("os.name" );
120120
121- if (name .indexOf ("Mac" ) != - 1 ) {
121+ if (name .contains ("Mac" )) {
122122 platform = MACOS ;
123123
124124 } else if (name .indexOf ("Windows" ) != -1 ) {
@@ -864,7 +864,7 @@ void handleSettings() {
864864 BufferedReader errReader = createReader (p .getErrorStream ());
865865 StringBuilder stdout = new StringBuilder ();
866866 StringBuilder stderr = new StringBuilder ();
867- String line = null ;
867+ String line ;
868868 try {
869869 while ((line = outReader .readLine ()) != null ) {
870870 stdout .append (line );
@@ -879,7 +879,7 @@ void handleSettings() {
879879 int resultCode = -1 ;
880880 try {
881881 resultCode = p .waitFor ();
882- } catch (InterruptedException e ) { }
882+ } catch (InterruptedException ignored ) { }
883883
884884 if (resultCode == 1 ) {
885885 System .err .println ("Could not check the status of “Displays have separate spaces.”" );
@@ -1252,6 +1252,7 @@ class RegisteredMethods {
12521252 Object [] emptyArgs = new Object [] { };
12531253
12541254
1255+ @ SuppressWarnings ("unused" )
12551256 void handle () {
12561257 handle (emptyArgs );
12571258 }
@@ -1404,7 +1405,7 @@ private void registerNoArgs(String name, Object o) {
14041405 }
14051406
14061407
1407- private void registerWithArgs (String name , Object o , Class <?> cargs [] ) {
1408+ private void registerWithArgs (String name , Object o , Class <?>[] cargs ) {
14081409 Class <?> c = o .getClass ();
14091410 try {
14101411 Method method = c .getMethod (name , cargs );
@@ -1438,8 +1439,6 @@ public void unregisterMethod(String name, Object target) {
14381439 die ("No registered methods with the name " + name + "() were found." );
14391440 }
14401441 try {
1441- // Method method = o.getClass().getMethod(name, new Class[] {});
1442- // meth.remove(o, method);
14431442 meth .remove (target );
14441443 } catch (Exception e ) {
14451444 die ("Could not unregister " + name + "() for " + target , e );
@@ -3707,13 +3706,7 @@ public void method(String name) {
37073706 * @see PApplet#noLoop()
37083707 */
37093708 public void thread (final String name ) {
3710- Thread later = new Thread () {
3711- @ Override
3712- public void run () {
3713- method (name );
3714- }
3715- };
3716- later .start ();
3709+ new Thread (() -> method (name )).start ();
37173710 }
37183711
37193712
@@ -5350,11 +5343,7 @@ public PImage requestImage(String filename, String extension) {
53505343
53515344 // if the image loading thread pool hasn't been created, create it
53525345 if (requestImagePool == null ) {
5353- ThreadFactory factory = new ThreadFactory () {
5354- public Thread newThread (Runnable r ) {
5355- return new Thread (r , REQUEST_IMAGE_THREAD_PREFIX );
5356- }
5357- };
5346+ ThreadFactory factory = r -> new Thread (r , REQUEST_IMAGE_THREAD_PREFIX );
53585347 requestImagePool = Executors .newFixedThreadPool (4 , factory );
53595348 }
53605349 requestImagePool .execute (() -> {
@@ -5434,13 +5423,7 @@ public XML loadXML(String filename, String options) {
54345423
54355424 // can't use catch-all exception, since it might catch the
54365425 // RuntimeException about the incorrect case sensitivity
5437- } catch (IOException e ) {
5438- throw new RuntimeException (e );
5439-
5440- } catch (ParserConfigurationException e ) {
5441- throw new RuntimeException (e );
5442-
5443- } catch (SAXException e ) {
5426+ } catch (IOException | ParserConfigurationException | SAXException e ) {
54445427 throw new RuntimeException (e );
54455428 }
54465429 }
@@ -5671,10 +5654,9 @@ public Table loadTable(String filename, String options) {
56715654 String optionStr = Table .extensionOptions (true , filename , options );
56725655 String [] optionList = trim (split (optionStr , ',' ));
56735656
5674- Table dictionary = null ;
56755657 for (String opt : optionList ) {
56765658 if (opt .startsWith ("dictionary=" )) {
5677- dictionary = loadTable (opt .substring (opt .indexOf ('=' ) + 1 ), "tsv" );
5659+ Table dictionary = loadTable (opt .substring (opt .indexOf ('=' ) + 1 ), "tsv" );
56785660 return dictionary .typedParse (createInput (filename ), optionStr );
56795661 }
56805662 }
@@ -6592,7 +6574,7 @@ public InputStream createInputRaw(String filename) {
65926574 }
65936575 }
65946576
6595- InputStream stream = null ;
6577+ InputStream stream ;
65966578
65976579 // Moved this earlier than the getResourceAsStream() checks, because
65986580 // calling getResourceAsStream() on a directory lists its contents.
@@ -6626,17 +6608,15 @@ public InputStream createInputRaw(String filename) {
66266608 filename + ". Rename the file " +
66276609 "or change your code." );
66286610 }
6629- } catch (IOException e ) { }
6611+ } catch (IOException ignored ) { }
66306612 }
66316613
66326614 // if this file is ok, may as well just load it
6633- stream = new FileInputStream (file );
6634- if (stream != null ) return stream ;
6615+ return new FileInputStream (file );
66356616
66366617 // have to break these out because a general Exception might
66376618 // catch the RuntimeException being thrown above
6638- } catch (IOException ioe ) {
6639- } catch (SecurityException se ) { }
6619+ } catch (IOException | SecurityException ignored ) { }
66406620
66416621 // Using getClassLoader() prevents java from converting dots
66426622 // to slashes or requiring a slash at the beginning.
@@ -6674,21 +6654,18 @@ public InputStream createInputRaw(String filename) {
66746654 // an application, or as a signed applet
66756655 try { // first try to catch any security exceptions
66766656 try {
6677- stream = new FileInputStream (dataPath (filename ));
6678- if (stream != null ) return stream ;
6679- } catch (IOException e2 ) { }
6657+ return new FileInputStream (dataPath (filename ));
6658+ } catch (IOException ignored ) { }
66806659
66816660 try {
6682- stream = new FileInputStream (sketchPath (filename ));
6683- if (stream != null ) return stream ;
6684- } catch (Exception e ) { } // ignored
6661+ return new FileInputStream (sketchPath (filename ));
6662+ } catch (Exception ignored ) { }
66856663
66866664 try {
6687- stream = new FileInputStream (filename );
6688- if (stream != null ) return stream ;
6689- } catch (IOException e1 ) { }
6665+ return new FileInputStream (filename );
6666+ } catch (IOException ignored ) { }
66906667
6691- } catch (SecurityException se ) { } // online, whups
6668+ } catch (SecurityException ignored ) { } // online, whups
66926669
66936670 } catch (Exception e ) {
66946671 printStackTrace (e );
@@ -6780,7 +6757,7 @@ public byte[] loadBytes(String filename) {
67806757 }
67816758
67826759 if (input != null ) {
6783- byte [] buffer = null ;
6760+ byte [] buffer ;
67846761 if (length != -1 ) {
67856762 buffer = new byte [length ];
67866763 int count ;
@@ -7003,22 +6980,17 @@ public String[] loadStrings(String filename) {
70036980 * @nowebref
70046981 */
70056982 static public String [] loadStrings (InputStream input ) {
7006- try {
7007- BufferedReader reader =
7008- new BufferedReader (new InputStreamReader (input , "UTF-8" ));
7009- return loadStrings (reader );
7010- } catch (IOException e ) {
7011- e .printStackTrace ();
7012- }
7013- return null ;
6983+ BufferedReader reader =
6984+ new BufferedReader (new InputStreamReader (input , StandardCharsets .UTF_8 ));
6985+ return loadStrings (reader );
70146986 }
70156987
70166988
70176989 static public String [] loadStrings (BufferedReader reader ) {
70186990 try {
70196991 String [] lines = new String [100 ];
70206992 int lineCount = 0 ;
7021- String line = null ;
6993+ String line ;
70226994 while ((line = reader .readLine ()) != null ) {
70236995 if (lineCount == lines .length ) {
70246996 String [] temp = new String [lineCount << 1 ];
@@ -7154,24 +7126,23 @@ static public boolean saveStream(File target, InputStream source) {
71547126
71557127 saveStream (targetStream , source );
71567128 targetStream .close ();
7157- targetStream = null ;
71587129
71597130 if (target .exists ()) {
71607131 if (!target .delete ()) {
7161- System .err .println ("Could not replace " +
7162- target .getAbsolutePath () + "." );
7132+ System .err .println ("Could not replace " + target );
71637133 }
71647134 }
71657135 if (!tempFile .renameTo (target )) {
7166- System .err .println ("Could not rename temporary file " +
7167- tempFile .getAbsolutePath ());
7136+ System .err .println ("Could not rename temporary file " + tempFile );
71687137 return false ;
71697138 }
71707139 return true ;
71717140
71727141 } catch (IOException e ) {
71737142 if (tempFile != null ) {
7174- tempFile .delete ();
7143+ if (!tempFile .delete ()) {
7144+ System .err .println ("Could not rename temporary file " + tempFile );
7145+ }
71757146 }
71767147 e .printStackTrace ();
71777148 return false ;
@@ -7235,7 +7206,9 @@ public void saveBytes(String filename, byte[] data) {
72357206 static private File createTempFile (File file ) throws IOException {
72367207 File parentDir = file .getParentFile ();
72377208 if (!parentDir .exists ()) {
7238- parentDir .mkdirs ();
7209+ if (!parentDir .mkdirs ()) {
7210+ throw new IOException ("Could not make directories for " + parentDir );
7211+ }
72397212 }
72407213 String name = file .getName ();
72417214 String prefix ;
@@ -7266,25 +7239,29 @@ static public void saveBytes(File file, byte[] data) {
72667239 tempFile = createTempFile (file );
72677240
72687241 OutputStream output = createOutput (tempFile );
7269- saveBytes (output , data );
7270- output .close ();
7271- output = null ;
7242+ if (output != null ) {
7243+ saveBytes (output , data );
7244+ output .close ();
7245+ } else {
7246+ System .err .println ("Could not write to " + tempFile );
7247+ }
72727248
72737249 if (file .exists ()) {
72747250 if (!file .delete ()) {
7275- System .err .println ("Could not replace " + file . getAbsolutePath () );
7251+ System .err .println ("Could not replace " + file );
72767252 }
72777253 }
72787254
72797255 if (!tempFile .renameTo (file )) {
7280- System .err .println ("Could not rename temporary file " +
7281- tempFile .getAbsolutePath ());
7256+ System .err .println ("Could not rename temporary file " + tempFile );
72827257 }
72837258
72847259 } catch (IOException e ) {
72857260 System .err .println ("error saving bytes to " + file );
72867261 if (tempFile != null ) {
7287- tempFile .delete ();
7262+ if (!tempFile .delete ()) {
7263+ System .err .println ("Could not delete temporary file " + tempFile );
7264+ }
72887265 }
72897266 e .printStackTrace ();
72907267 }
@@ -7624,11 +7601,7 @@ static public String urlEncode(String str) {
76247601 // using toURI() and URI.toURL()."
76257602 // https://docs.oracle.com/javase/8/docs/api/java/net/URL.html
76267603 static public String urlDecode (String str ) {
7627- try {
7628- return URLDecoder .decode (str , "UTF-8" );
7629- } catch (UnsupportedEncodingException e ) { // safe per the JDK source
7630- return null ;
7631- }
7604+ return URLDecoder .decode (str , StandardCharsets .UTF_8 );
76327605 }
76337606
76347607
@@ -8614,8 +8587,8 @@ static public String[] split(String value, char delim) {
86148587
86158588 char [] chars = value .toCharArray ();
86168589 int splitCount = 0 ; //1;
8617- for (int i = 0 ; i < chars . length ; i ++ ) {
8618- if (chars [ i ] == delim ) splitCount ++;
8590+ for (char ch : chars ) {
8591+ if (ch == delim ) splitCount ++;
86198592 }
86208593 // make sure that there is something in the input string
86218594 //if (chars.length > 0) {
0 commit comments