Skip to content

Commit c0b05cf

Browse files
committed
don't wrap null InputStream in a BufferedInputStream, also more file checks for non-existence
1 parent 2b7a90b commit c0b05cf

2 files changed

Lines changed: 32 additions & 11 deletions

File tree

core/src/processing/core/PApplet.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6927,18 +6927,22 @@ static public PrintWriter createWriter(OutputStream output) {
69276927
*/
69286928
public InputStream createInput(String filename) {
69296929
InputStream input = createInputRaw(filename);
6930-
final String lower = filename.toLowerCase();
6931-
if ((input != null) &&
6932-
(lower.endsWith(".gz") || lower.endsWith(".svgz"))) {
6933-
try {
6934-
// buffered has to go *around* the GZ, otherwise 25x slower
6935-
return new BufferedInputStream(new GZIPInputStream(input));
6936-
} catch (IOException e) {
6937-
printStackTrace(e);
6938-
return null;
6930+
if (input != null) {
6931+
// if it's gzip-encoded, automatically decode
6932+
final String lower = filename.toLowerCase();
6933+
if (lower.endsWith(".gz") || lower.endsWith(".svgz")) {
6934+
try {
6935+
// buffered has to go *around* the GZ, otherwise 25x slower
6936+
return new BufferedInputStream(new GZIPInputStream(input));
6937+
6938+
} catch (IOException e) {
6939+
printStackTrace(e);
6940+
}
6941+
} else {
6942+
return new BufferedInputStream(input);
69396943
}
69406944
}
6941-
return new BufferedInputStream(input);
6945+
return null;
69426946
}
69436947

69446948

@@ -7109,9 +7113,14 @@ static public InputStream createInput(File file) {
71097113
if (file == null) {
71107114
throw new IllegalArgumentException("File passed to createInput() was null");
71117115
}
7116+
if (!file.exists()) {
7117+
System.err.println(file + " does not exist, createInput() will return null");
7118+
return null;
7119+
}
71127120
try {
71137121
InputStream input = new FileInputStream(file);
7114-
if (file.getName().toLowerCase().endsWith(".gz")) {
7122+
final String lower = file.getName().toLowerCase();
7123+
if (lower.endsWith(".gz") || lower.endsWith(".svgz")) {
71157124
return new BufferedInputStream(new GZIPInputStream(input));
71167125
}
71177126
return new BufferedInputStream(input);
@@ -7247,6 +7256,11 @@ static public byte[] loadBytes(InputStream input) {
72477256
* @nowebref
72487257
*/
72497258
static public byte[] loadBytes(File file) {
7259+
if (!file.exists()) {
7260+
System.err.println(file + " does not exist, loadBytes() will return null");
7261+
return null;
7262+
}
7263+
72507264
try {
72517265
InputStream input;
72527266
int length;
@@ -7295,6 +7309,11 @@ static public byte[] loadBytes(File file) {
72957309
* @nowebref
72967310
*/
72977311
static public String[] loadStrings(File file) {
7312+
if (!file.exists()) {
7313+
System.err.println(file + " does not exist, loadStrings() will return null");
7314+
return null;
7315+
}
7316+
72987317
InputStream is = createInput(file);
72997318
if (is != null) {
73007319
String[] outgoing = loadStrings(is);

core/todo.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ X add (far) more efficient file loading for loadBytes(File)
33
X add loadBytes(URL) variant that uses content length header for array size
44
X keyPressed is false if one key is released while multiple keys are pressed
55
X https://github.com/processing/processing/issues/4993
6+
X createInput() wasn't returning null for files that were not found
7+
X https://github.com/processing/processing/issues/5026
68

79
andres
810
X Assigning Pixels Vertically Flipped in P2D

0 commit comments

Comments
 (0)