rewrite loadBytes(File) for better performance

This commit is contained in:
Ben Fry
2017-04-24 19:29:16 -04:00
parent 49afbd205a
commit 9dba561fc3
2 changed files with 45 additions and 4 deletions
+40 -4
View File
@@ -7192,20 +7192,55 @@ public class PApplet implements PConstants {
return null;
}
/**
* @nowebref
*/
static public byte[] loadBytes(File file) {
InputStream is = createInput(file);
byte[] byteArr = loadBytes(is);
try {
is.close();
InputStream input;
int length;
if (file.getName().toLowerCase().endsWith(".gz")) {
RandomAccessFile raf = new RandomAccessFile(file, "r");
raf.seek(raf.length() - 4);
int b4 = raf.read();
int b3 = raf.read();
int b2 = raf.read();
int b1 = raf.read();
length = (b1 << 24) | (b2 << 16) + (b3 << 8) + b4;
raf.close();
// buffered has to go *around* the GZ, otherwise 25x slower
input = new BufferedInputStream(new GZIPInputStream(new FileInputStream(file)));
} else {
long len = file.length();
// http://stackoverflow.com/a/3039805
int maxArraySize = Integer.MAX_VALUE - 5;
if (len > maxArraySize) {
System.err.println("Cannot load files larger than " + maxArraySize);
return null;
}
length = (int) len;
input = new BufferedInputStream(new FileInputStream(file));
}
byte[] buffer = new byte[length];
int count;
int offset = 0;
while ((count = input.read(buffer, offset, length - offset)) != -1) {
offset += count;
}
input.close();
return buffer;
} catch (IOException e) {
e.printStackTrace();
return null;
}
return byteArr;
}
/**
* @nowebref
*/
@@ -7223,6 +7258,7 @@ public class PApplet implements PConstants {
return null;
}
/**
* ( begin auto-generated from loadStrings.xml )
*
+5
View File
@@ -1,9 +1,14 @@
0259 (3.3.2)
X add (far) more efficient file loading for loadBytes(File)
andres
X Assigning Pixels Vertically Flipped in P2D
X https://github.com/processing/processing/issues/5013
gohai
X improve loadBytes() performance
X https://github.com/processing/processing/pull/5027
high
_ implement sketch scaling into PApplet