Skip to content

Commit e1cee44

Browse files
committed
clean up warnings and a couple small logic glitches, yikes
1 parent a9a589c commit e1cee44

File tree

1 file changed

+70
-79
lines changed

1 file changed

+70
-79
lines changed

app/src/processing/app/Util.java

Lines changed: 70 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ static public byte[] loadBytesRaw(File file) throws IOException {
6363
if (bytesRead == 0) break;
6464
}
6565
input.close(); // weren't properly being closed
66-
input = null;
6766
return buffer;
6867
}
6968

@@ -79,7 +78,7 @@ static public StringDict readSettings(File inputFile) {
7978
Messages.err(inputFile + " does not exist inside readSettings()");
8079
return null;
8180
}
82-
String lines[] = PApplet.loadStrings(inputFile);
81+
String[] lines = PApplet.loadStrings(inputFile);
8382
if (lines == null) {
8483
System.err.println("Could not read " + inputFile);
8584
return null;
@@ -138,11 +137,9 @@ static public void copyFile(File sourceFile,
138137
to.write(buffer, 0, bytesRead);
139138
}
140139
from.close();
141-
from = null;
142140

143141
to.flush();
144142
to.close();
145-
to = null;
146143

147144
targetFile.setLastModified(sourceFile.lastModified());
148145
targetFile.setExecutable(sourceFile.canExecute());
@@ -172,9 +169,8 @@ static public void saveFile(String text, File file) throws IOException {
172169
File temp = File.createTempFile(file.getName(), null, file.getParentFile());
173170
try {
174171
// fix from cjwant to prevent symlinks from being destroyed.
175-
File canon = file.getCanonicalFile();
176-
// assign the var as second step since previous line may throw exception
177-
file = canon;
172+
file = file.getCanonicalFile();
173+
178174
} catch (IOException e) {
179175
throw new IOException("Could not resolve canonical representation of " +
180176
file.getAbsolutePath());
@@ -242,27 +238,27 @@ static public void copyDir(File sourceDir,
242238
throw new IllegalArgumentException(urDum);
243239
}
244240
targetDir.mkdirs();
245-
String files[] = sourceDir.list();
246-
for (int i = 0; i < files.length; i++) {
241+
String[] filenames = sourceDir.list();
242+
for (String filename : filenames) {
247243
// Ignore dot files (.DS_Store), dot folders (.svn) while copying
248-
if (files[i].charAt(0) == '.') continue;
249-
//if (files[i].equals(".") || files[i].equals("..")) continue;
250-
File source = new File(sourceDir, files[i]);
251-
File target = new File(targetDir, files[i]);
252-
if (source.isDirectory()) {
253-
//target.mkdirs();
254-
copyDir(source, target);
255-
target.setLastModified(source.lastModified());
256-
} else {
257-
copyFile(source, target);
244+
if (filename.charAt(0) != '.') {
245+
File source = new File(sourceDir, filename);
246+
File target = new File(targetDir, filename);
247+
if (source.isDirectory()) {
248+
//target.mkdirs();
249+
copyDir(source, target);
250+
target.setLastModified(source.lastModified());
251+
} else {
252+
copyFile(source, target);
253+
}
258254
}
259255
}
260256
}
261257

262258

263259
static public void copyDirNative(File sourceDir,
264260
File targetDir) throws IOException {
265-
Process process = null;
261+
Process process;
266262
if (Platform.isMacOS() || Platform.isLinux()) {
267263
process = Runtime.getRuntime().exec(new String[] {
268264
"cp", "-a", sourceDir.getAbsolutePath(), targetDir.getAbsolutePath()
@@ -363,21 +359,25 @@ static public long calcSize(File file) {
363359
* Note that the function calls itself recursively.
364360
*/
365361
static public long calcFolderSize(File folder) {
366-
int size = 0;
362+
long size = 0;
367363

368-
String files[] = folder.list();
364+
String[] filenames = folder.list();
369365
// null if folder doesn't exist, happens when deleting sketch
370-
if (files == null) return -1;
371-
372-
for (int i = 0; i < files.length; i++) {
373-
if (files[i].equals(".") ||
374-
files[i].equals("..") ||
375-
files[i].equals(".DS_Store")) continue;
376-
File fella = new File(folder, files[i]);
377-
if (fella.isDirectory()) {
378-
size += calcFolderSize(fella);
379-
} else {
380-
size += (int) fella.length();
366+
if (filenames == null) return -1;
367+
368+
for (String file : filenames) {
369+
if (!file.equals(".") && !file.equals("..") && !file.equals(".DS_Store")) {
370+
File fella = new File(folder, file);
371+
if (fella.isDirectory()) {
372+
long subfolderSize = calcFolderSize(fella);
373+
if (subfolderSize == -1) {
374+
return -1;
375+
} else {
376+
size += subfolderSize;
377+
}
378+
} else {
379+
size += fella.length();
380+
}
381381
}
382382
}
383383
return size;
@@ -445,13 +445,10 @@ static void listFilesImpl(File folder, boolean relative,
445445
* @return a list of .jar and .zip files in that folder
446446
*/
447447
static public File[] listJarFiles(File folder) {
448-
return folder.listFiles(new FilenameFilter() {
449-
public boolean accept(File dir, String name) {
450-
return (!name.startsWith(".") &&
451-
(name.toLowerCase().endsWith(".jar") ||
452-
name.toLowerCase().endsWith(".zip")));
453-
}
454-
});
448+
return folder.listFiles((dir, name) ->
449+
(!name.startsWith(".") &&
450+
(name.toLowerCase().endsWith(".jar") ||
451+
name.toLowerCase().endsWith(".zip"))));
455452
}
456453

457454

@@ -485,17 +482,17 @@ static public String contentsToClassPath(File folder) {
485482
path += File.separator;
486483
}
487484

488-
String list[] = folder.list();
489-
for (int i = 0; i < list.length; i++) {
485+
String[] list = folder.list();
486+
for (String item : list) {
490487
// Skip . and ._ files. Prior to 0125p3, .jar files that had
491488
// OS X AppleDouble files associated would cause trouble.
492-
if (list[i].startsWith(".")) continue;
493-
494-
if (list[i].toLowerCase().endsWith(".jar") ||
495-
list[i].toLowerCase().endsWith(".zip")) {
496-
sb.append(sep);
497-
sb.append(path);
498-
sb.append(list[i]);
489+
if (!item.startsWith(".")) {
490+
if (item.toLowerCase().endsWith(".jar") ||
491+
item.toLowerCase().endsWith(".zip")) {
492+
sb.append(sep);
493+
sb.append(path);
494+
sb.append(item);
495+
}
499496
}
500497
}
501498
} catch (IOException e) {
@@ -516,25 +513,25 @@ static public String contentsToClassPath(File folder) {
516513
static public StringList packageListFromClassPath(String path) {
517514
// Map<String, Object> map = new HashMap<String, Object>();
518515
StringList list = new StringList();
519-
String pieces[] =
516+
String[] pieces =
520517
PApplet.split(path, File.pathSeparatorChar);
521518

522-
for (int i = 0; i < pieces.length; i++) {
519+
for (String piece : pieces) {
523520
//System.out.println("checking piece '" + pieces[i] + "'");
524-
if (pieces[i].length() == 0) continue;
525-
526-
if (pieces[i].toLowerCase().endsWith(".jar") ||
527-
pieces[i].toLowerCase().endsWith(".zip")) {
528-
//System.out.println("checking " + pieces[i]);
529-
packageListFromZip(pieces[i], list);
530-
531-
} else { // it's another type of file or directory
532-
File dir = new File(pieces[i]);
533-
if (dir.exists() && dir.isDirectory()) {
534-
packageListFromFolder(dir, null, list);
535-
//importCount = magicImportsRecursive(dir, null,
536-
// map);
537-
//imports, importCount);
521+
if (piece.length() != 0) {
522+
if (piece.toLowerCase().endsWith(".jar") ||
523+
piece.toLowerCase().endsWith(".zip")) {
524+
//System.out.println("checking " + pieces[i]);
525+
packageListFromZip(piece, list);
526+
527+
} else { // it's another type of file or directory
528+
File dir = new File(piece);
529+
if (dir.exists() && dir.isDirectory()) {
530+
packageListFromFolder(dir, null, list);
531+
//importCount = magicImportsRecursive(dir, null,
532+
// map);
533+
//imports, importCount);
534+
}
538535
}
539536
}
540537
}
@@ -593,24 +590,18 @@ static private void packageListFromFolder(File dir, String sofar,
593590
StringList list) {
594591
// Map<String, Object> map) {
595592
boolean foundClass = false;
596-
String files[] = dir.list();
593+
String[] filenames = dir.list();
597594

598-
for (int i = 0; i < files.length; i++) {
599-
if (files[i].equals(".") || files[i].equals("..")) continue;
595+
for (String filename : filenames) {
596+
if (filename.equals(".") || filename.equals("..")) continue;
600597

601-
File sub = new File(dir, files[i]);
598+
File sub = new File(dir, filename);
602599
if (sub.isDirectory()) {
603600
String nowfar =
604-
(sofar == null) ? files[i] : (sofar + "." + files[i]);
601+
(sofar == null) ? filename : (sofar + "." + filename);
605602
packageListFromFolder(sub, nowfar, list);
606-
//System.out.println(nowfar);
607-
//imports[importCount++] = nowfar;
608-
//importCount = magicImportsRecursive(sub, nowfar,
609-
// imports, importCount);
610603
} else if (!foundClass) { // if no classes found in this folder yet
611-
if (files[i].endsWith(".class")) {
612-
//System.out.println("unique class: " + files[i] + " for " + sofar);
613-
// map.put(sofar, new Object());
604+
if (filename.endsWith(".class")) {
614605
list.appendUnique(sofar);
615606
foundClass = true;
616607
}
@@ -628,7 +619,7 @@ static public void unzip(File zipFile, File dest) {
628619
FileInputStream fis = new FileInputStream(zipFile);
629620
CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32());
630621
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum));
631-
ZipEntry entry = null;
622+
ZipEntry entry;
632623
while ((entry = zis.getNextEntry()) != null) {
633624
final String name = entry.getName();
634625
if (!name.startsWith(("__MACOSX"))) {
@@ -655,7 +646,7 @@ static public void unzip(File zipFile, File dest) {
655646
static protected void unzipEntry(ZipInputStream zin, File f) throws IOException {
656647
FileOutputStream out = new FileOutputStream(f);
657648
byte[] b = new byte[512];
658-
int len = 0;
649+
int len;
659650
while ((len = zin.read(b)) != -1) {
660651
out.write(b, 0, len);
661652
}

0 commit comments

Comments
 (0)