propane 3.2.0-java → 3.3.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.mvn/extensions.xml +9 -0
- data/.mvn/wrapper/maven-wrapper.properties +3 -1
- data/CHANGELOG.md +3 -1
- data/README.md +4 -4
- data/lib/propane/app.rb +1 -1
- data/lib/propane/version.rb +1 -1
- data/pom.rb +79 -0
- data/pom.xml +32 -43
- data/propane.gemspec +2 -2
- data/src/main/java/japplemenubar/JAppleMenuBar.java +47 -41
- data/src/main/java/monkstone/vecmath/{AppRender.java → GfxRender.java} +12 -12
- data/src/main/java/processing/awt/PSurfaceAWT.java +3 -3
- data/src/main/java/processing/core/PApplet.java +14004 -14573
- data/src/main/java/processing/core/PGraphics.java +7563 -7213
- data/src/main/java/processing/core/PImage.java +2783 -3080
- data/src/main/java/processing/core/ThinkDifferent.java +120 -0
- data/src/main/java/processing/core/util/image/ImageLoadFacade.java +161 -0
- data/src/main/java/processing/core/util/image/ImageSaveFacade.java +169 -0
- data/src/main/java/processing/core/util/image/constants/TifConstants.java +45 -0
- data/src/main/java/processing/core/util/image/load/AwtImageLoadStrategy.java +80 -0
- data/src/main/java/processing/core/util/image/load/Base64StringImageLoadStrategy.java +73 -0
- data/src/main/java/processing/core/util/image/load/FallbackImageLoadStrategy.java +70 -0
- data/src/main/java/processing/core/util/image/load/ImageIoImageLoadStrategy.java +132 -0
- data/src/main/java/processing/core/util/image/load/ImageLoadStrategy.java +48 -0
- data/src/main/java/processing/core/util/image/load/ImageLoadUtil.java +45 -0
- data/src/main/java/processing/core/util/image/load/TgaImageLoadStrategy.java +255 -0
- data/src/main/java/processing/core/util/image/load/TiffImageLoadStrategy.java +98 -0
- data/src/main/java/processing/core/util/image/save/ImageSaveStrategy.java +49 -0
- data/src/main/java/processing/core/util/image/save/ImageSaveUtil.java +48 -0
- data/src/main/java/processing/core/util/image/save/ImageWriterImageSaveStrategy.java +179 -0
- data/src/main/java/processing/core/util/image/save/SaveImageException.java +41 -0
- data/src/main/java/processing/core/util/image/save/TgaImageSaveStrategy.java +198 -0
- data/src/main/java/processing/core/util/image/save/TiffImageSaveStrategy.java +91 -0
- data/src/main/java/processing/core/util/image/save/TiffNakedFilenameImageSaveStrategy.java +57 -0
- data/src/main/java/processing/core/util/io/InputFactory.java +285 -0
- data/src/main/java/processing/core/util/io/PathUtil.java +109 -0
- data/vendors/Rakefile +1 -1
- metadata +30 -7
- data/src/main/java/processing/core/DesktopHandler.java +0 -94
@@ -0,0 +1,91 @@
|
|
1
|
+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
2
|
+
|
3
|
+
/*
|
4
|
+
Part of the Processing project - http://processing.org
|
5
|
+
|
6
|
+
Copyright (c) 2012-18 The Processing Foundation
|
7
|
+
Copyright (c) 2004-12 Ben Fry and Casey Reas
|
8
|
+
Copyright (c) 2001-04 Massachusetts Institute of Technology
|
9
|
+
|
10
|
+
This library is free software; you can redistribute it and/or
|
11
|
+
modify it under the terms of the GNU Lesser General Public
|
12
|
+
License as published by the Free Software Foundation, version 2.1.
|
13
|
+
|
14
|
+
This library is distributed in the hope that it will be useful,
|
15
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
17
|
+
Lesser General Public License for more details.
|
18
|
+
|
19
|
+
You should have received a copy of the GNU Lesser General
|
20
|
+
Public License along with this library; if not, write to the
|
21
|
+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
22
|
+
Boston, MA 02111-1307 USA
|
23
|
+
*/
|
24
|
+
|
25
|
+
package processing.core.util.image.save;
|
26
|
+
|
27
|
+
import processing.core.util.image.constants.TifConstants;
|
28
|
+
|
29
|
+
import java.io.FileNotFoundException;
|
30
|
+
import java.io.IOException;
|
31
|
+
import java.io.OutputStream;
|
32
|
+
|
33
|
+
|
34
|
+
/**
|
35
|
+
* Strategy for saving tiff images.
|
36
|
+
*/
|
37
|
+
public class TiffImageSaveStrategy implements ImageSaveStrategy {
|
38
|
+
|
39
|
+
@Override
|
40
|
+
public boolean save(int[] pixels, int pixelWidth, int pixelHeight, int format,
|
41
|
+
String filename) throws FileNotFoundException {
|
42
|
+
|
43
|
+
OutputStream output = ImageSaveUtil.createForFile(filename);
|
44
|
+
|
45
|
+
// shutting off the warning, people can figure this out themselves
|
46
|
+
/*
|
47
|
+
if (format != RGB) {
|
48
|
+
System.err.println("Warning: only RGB information is saved with " +
|
49
|
+
".tif files. Use .tga or .png for ARGB images and others.");
|
50
|
+
}
|
51
|
+
*/
|
52
|
+
try {
|
53
|
+
byte tiff[] = new byte[768];
|
54
|
+
System.arraycopy(
|
55
|
+
TifConstants.TIFF_HEADER,
|
56
|
+
0,
|
57
|
+
tiff,
|
58
|
+
0,
|
59
|
+
TifConstants.TIFF_HEADER.length
|
60
|
+
);
|
61
|
+
|
62
|
+
tiff[30] = (byte) ((pixelWidth >> 8) & 0xff);
|
63
|
+
tiff[31] = (byte) ((pixelWidth) & 0xff);
|
64
|
+
tiff[42] = tiff[102] = (byte) ((pixelHeight >> 8) & 0xff);
|
65
|
+
tiff[43] = tiff[103] = (byte) ((pixelHeight) & 0xff);
|
66
|
+
|
67
|
+
int count = pixelWidth*pixelHeight*3;
|
68
|
+
tiff[114] = (byte) ((count >> 24) & 0xff);
|
69
|
+
tiff[115] = (byte) ((count >> 16) & 0xff);
|
70
|
+
tiff[116] = (byte) ((count >> 8) & 0xff);
|
71
|
+
tiff[117] = (byte) ((count) & 0xff);
|
72
|
+
|
73
|
+
// spew the header to the disk
|
74
|
+
output.write(tiff);
|
75
|
+
|
76
|
+
for (int i = 0; i < pixels.length; i++) {
|
77
|
+
output.write((pixels[i] >> 16) & 0xff);
|
78
|
+
output.write((pixels[i] >> 8) & 0xff);
|
79
|
+
output.write(pixels[i] & 0xff);
|
80
|
+
}
|
81
|
+
output.flush();
|
82
|
+
output.close();
|
83
|
+
return true;
|
84
|
+
|
85
|
+
} catch (IOException e) {
|
86
|
+
e.printStackTrace();
|
87
|
+
}
|
88
|
+
return false;
|
89
|
+
}
|
90
|
+
|
91
|
+
}
|
@@ -0,0 +1,57 @@
|
|
1
|
+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
2
|
+
|
3
|
+
/*
|
4
|
+
Part of the Processing project - http://processing.org
|
5
|
+
|
6
|
+
Copyright (c) 2012-18 The Processing Foundation
|
7
|
+
Copyright (c) 2004-12 Ben Fry and Casey Reas
|
8
|
+
Copyright (c) 2001-04 Massachusetts Institute of Technology
|
9
|
+
|
10
|
+
This library is free software; you can redistribute it and/or
|
11
|
+
modify it under the terms of the GNU Lesser General Public
|
12
|
+
License as published by the Free Software Foundation, version 2.1.
|
13
|
+
|
14
|
+
This library is distributed in the hope that it will be useful,
|
15
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
17
|
+
Lesser General Public License for more details.
|
18
|
+
|
19
|
+
You should have received a copy of the GNU Lesser General
|
20
|
+
Public License along with this library; if not, write to the
|
21
|
+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
22
|
+
Boston, MA 02111-1307 USA
|
23
|
+
*/
|
24
|
+
|
25
|
+
package processing.core.util.image.save;
|
26
|
+
|
27
|
+
import java.io.IOException;
|
28
|
+
|
29
|
+
|
30
|
+
/**
|
31
|
+
* Strategy for saving a Tiff and appending ".tif" to the filename.
|
32
|
+
*/
|
33
|
+
public class TiffNakedFilenameImageSaveStrategy implements ImageSaveStrategy {
|
34
|
+
|
35
|
+
private final ImageSaveStrategy tiffImageSaveStrategy;
|
36
|
+
|
37
|
+
/**
|
38
|
+
* Create a new Tiff save strategy where ".tif" is appended to the filename.
|
39
|
+
*/
|
40
|
+
public TiffNakedFilenameImageSaveStrategy() {
|
41
|
+
tiffImageSaveStrategy = new TiffImageSaveStrategy();
|
42
|
+
}
|
43
|
+
|
44
|
+
@Override
|
45
|
+
public boolean save(int[] pixels, int pixelWidth, int pixelHeight, int format,
|
46
|
+
String filename) throws IOException {
|
47
|
+
|
48
|
+
filename += ".tif";
|
49
|
+
return tiffImageSaveStrategy.save(
|
50
|
+
pixels,
|
51
|
+
pixelWidth,
|
52
|
+
pixelHeight,
|
53
|
+
format,
|
54
|
+
filename
|
55
|
+
);
|
56
|
+
}
|
57
|
+
}
|
@@ -0,0 +1,285 @@
|
|
1
|
+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
2
|
+
|
3
|
+
/*
|
4
|
+
Part of the Processing project - http://processing.org
|
5
|
+
|
6
|
+
Copyright (c) 2012-18 The Processing Foundation
|
7
|
+
Copyright (c) 2004-12 Ben Fry and Casey Reas
|
8
|
+
Copyright (c) 2001-04 Massachusetts Institute of Technology
|
9
|
+
|
10
|
+
This library is free software; you can redistribute it and/or
|
11
|
+
modify it under the terms of the GNU Lesser General Public
|
12
|
+
License as published by the Free Software Foundation, version 2.1.
|
13
|
+
|
14
|
+
This library is distributed in the hope that it will be useful,
|
15
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
17
|
+
Lesser General Public License for more details.
|
18
|
+
|
19
|
+
You should have received a copy of the GNU Lesser General
|
20
|
+
Public License along with this library; if not, write to the
|
21
|
+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
22
|
+
Boston, MA 02111-1307 USA
|
23
|
+
*/
|
24
|
+
|
25
|
+
package processing.core.util.io;
|
26
|
+
|
27
|
+
import processing.core.PApplet;
|
28
|
+
|
29
|
+
import java.io.*;
|
30
|
+
import java.net.*;
|
31
|
+
import java.util.zip.GZIPInputStream;
|
32
|
+
|
33
|
+
|
34
|
+
/**
|
35
|
+
* Factory producing InputStreams under various parameter sets.
|
36
|
+
*/
|
37
|
+
public class InputFactory {
|
38
|
+
|
39
|
+
/**
|
40
|
+
* Create a new input stream from a file.
|
41
|
+
*
|
42
|
+
* @param file The file for which an input stream should be created.
|
43
|
+
* @return InputStream for the input file.
|
44
|
+
*/
|
45
|
+
public static InputStream createInput(File file) {
|
46
|
+
if (file == null) {
|
47
|
+
throw new IllegalArgumentException("File passed to createInput() was null");
|
48
|
+
}
|
49
|
+
if (!file.exists()) {
|
50
|
+
System.err.println(file + " does not exist, createInput() will return null");
|
51
|
+
return null;
|
52
|
+
}
|
53
|
+
try {
|
54
|
+
InputStream input = new FileInputStream(file);
|
55
|
+
final String lower = file.getName().toLowerCase();
|
56
|
+
if (lower.endsWith(".gz") || lower.endsWith(".svgz")) {
|
57
|
+
return new BufferedInputStream(new GZIPInputStream(input));
|
58
|
+
}
|
59
|
+
return new BufferedInputStream(input);
|
60
|
+
|
61
|
+
} catch (IOException e) {
|
62
|
+
System.err.println("Could not createInput() for " + file);
|
63
|
+
e.printStackTrace();
|
64
|
+
return null;
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
/**
|
69
|
+
* Create an InputStream using path information from a PApplet.
|
70
|
+
*
|
71
|
+
* <p>
|
72
|
+
* Create an InputStream using path information from a PApplet with some pre-processing of file
|
73
|
+
* contents like unzipping a gzip compressed file. This is in contrast to createInputRaw which
|
74
|
+
* will not perform any pre-processing.
|
75
|
+
* </p>
|
76
|
+
*
|
77
|
+
* @param pApplet The PApplet whose sketch path informatino should be used.
|
78
|
+
* @param filename THe filename (url, absolute path, or relative path) to open.
|
79
|
+
* @return InputStream for the given filename or null if no input path could be created.
|
80
|
+
*/
|
81
|
+
public static InputStream createInput(PApplet pApplet, String filename) {
|
82
|
+
InputStream input = createInputRaw(pApplet, filename);
|
83
|
+
if (input != null) {
|
84
|
+
// if it's gzip-encoded, automatically decode
|
85
|
+
if (isGzipCompressed(filename)) {
|
86
|
+
try {
|
87
|
+
// buffered has to go *around* the GZ, otherwise 25x slower
|
88
|
+
return new BufferedInputStream(new GZIPInputStream(input));
|
89
|
+
|
90
|
+
} catch (IOException e) {
|
91
|
+
e.printStackTrace();
|
92
|
+
}
|
93
|
+
} else {
|
94
|
+
return new BufferedInputStream(input);
|
95
|
+
}
|
96
|
+
}
|
97
|
+
return null;
|
98
|
+
}
|
99
|
+
|
100
|
+
/**
|
101
|
+
* Create an InputStream without any pre-processing of file content before it is returned.
|
102
|
+
*
|
103
|
+
* <p>
|
104
|
+
* Create an InputStream using path information from a PApplet without any pre-processing of file
|
105
|
+
* contents like unzipping a gzip compressed file. This is in contrast to createInput which will
|
106
|
+
* perform some pre-processing.
|
107
|
+
* </p>
|
108
|
+
*
|
109
|
+
* @param pApplet The PApplet whose sketch path information should be used.
|
110
|
+
* @param filename THe filename (url, absolute path, or relative path) to open.
|
111
|
+
* @return InputStream for the given filename or null if no input path could be created.
|
112
|
+
*/
|
113
|
+
public static InputStream createInputRaw(PApplet pApplet, String filename) {
|
114
|
+
if (filename == null) return null;
|
115
|
+
|
116
|
+
String sketchPath = pApplet.sketchPath();
|
117
|
+
|
118
|
+
if (sketchPath == null) {
|
119
|
+
System.err.println("The sketch path is not set.");
|
120
|
+
throw new RuntimeException("Files must be loaded inside setup() or after it has been called.");
|
121
|
+
}
|
122
|
+
|
123
|
+
if (filename.length() == 0) {
|
124
|
+
// an error will be called by the parent function
|
125
|
+
//System.err.println("The filename passed to openStream() was empty.");
|
126
|
+
return null;
|
127
|
+
}
|
128
|
+
|
129
|
+
// First check whether this looks like a URL
|
130
|
+
if (filename.contains(":")) { // at least smells like URL
|
131
|
+
try {
|
132
|
+
URL url = new URL(filename);
|
133
|
+
URLConnection conn = url.openConnection();
|
134
|
+
|
135
|
+
if (conn instanceof HttpURLConnection) {
|
136
|
+
HttpURLConnection httpConn = (HttpURLConnection) conn;
|
137
|
+
// Will not handle a protocol change (see below)
|
138
|
+
httpConn.setInstanceFollowRedirects(true);
|
139
|
+
int response = httpConn.getResponseCode();
|
140
|
+
// Default won't follow HTTP -> HTTPS redirects for security reasons
|
141
|
+
// http://stackoverflow.com/a/1884427
|
142
|
+
if (response >= 300 && response < 400) {
|
143
|
+
String newLocation = httpConn.getHeaderField("Location");
|
144
|
+
return createInputRaw(pApplet, newLocation);
|
145
|
+
}
|
146
|
+
return conn.getInputStream();
|
147
|
+
} else if (conn instanceof JarURLConnection) {
|
148
|
+
return url.openStream();
|
149
|
+
}
|
150
|
+
} catch (MalformedURLException mfue) {
|
151
|
+
// not a url, that's fine
|
152
|
+
|
153
|
+
} catch (FileNotFoundException fnfe) {
|
154
|
+
// Added in 0119 b/c Java 1.5 throws FNFE when URL not available.
|
155
|
+
// http://dev.processing.org/bugs/show_bug.cgi?id=403
|
156
|
+
|
157
|
+
} catch (IOException e) {
|
158
|
+
// changed for 0117, shouldn't be throwing exception
|
159
|
+
e.printStackTrace();
|
160
|
+
//System.err.println("Error downloading from URL " + filename);
|
161
|
+
return null;
|
162
|
+
//throw new RuntimeException("Error downloading from URL " + filename);
|
163
|
+
}
|
164
|
+
}
|
165
|
+
|
166
|
+
InputStream stream = null;
|
167
|
+
|
168
|
+
// Moved this earlier than the getResourceAsStream() checks, because
|
169
|
+
// calling getResourceAsStream() on a directory lists its contents.
|
170
|
+
// http://dev.processing.org/bugs/show_bug.cgi?id=716
|
171
|
+
try {
|
172
|
+
// First see if it's in a data folder. This may fail by throwing
|
173
|
+
// a SecurityException. If so, this whole block will be skipped.
|
174
|
+
File file = new File(pApplet.dataPath(filename));
|
175
|
+
|
176
|
+
if (!file.exists()) {
|
177
|
+
// next see if it's just in the sketch folder
|
178
|
+
file = pApplet.sketchFile(filename);
|
179
|
+
}
|
180
|
+
|
181
|
+
if (file.isDirectory()) {
|
182
|
+
return null;
|
183
|
+
}
|
184
|
+
if (file.exists()) {
|
185
|
+
try {
|
186
|
+
// handle case sensitivity check
|
187
|
+
String filePath = file.getCanonicalPath();
|
188
|
+
String filenameActual = new File(filePath).getName();
|
189
|
+
// make sure there isn't a subfolder prepended to the name
|
190
|
+
String filenameShort = new File(filename).getName();
|
191
|
+
// if the actual filename is the same, but capitalized
|
192
|
+
// differently, warn the user.
|
193
|
+
//if (filenameActual.equalsIgnoreCase(filenameShort) &&
|
194
|
+
//!filenameActual.equals(filenameShort)) {
|
195
|
+
if (!filenameActual.equals(filenameShort)) {
|
196
|
+
throw new RuntimeException("This file is named " +
|
197
|
+
filenameActual + " not " +
|
198
|
+
filename + ". Rename the file " +
|
199
|
+
"or change your code.");
|
200
|
+
}
|
201
|
+
} catch (IOException e) { }
|
202
|
+
}
|
203
|
+
|
204
|
+
// if this file is ok, may as well just load it
|
205
|
+
stream = new FileInputStream(file);
|
206
|
+
if (stream != null) {
|
207
|
+
return stream;
|
208
|
+
}
|
209
|
+
|
210
|
+
// have to break these out because a general Exception might
|
211
|
+
// catch the RuntimeException being thrown above
|
212
|
+
} catch (IOException ioe) {
|
213
|
+
} catch (SecurityException se) { }
|
214
|
+
|
215
|
+
// Using getClassLoader() prevents java from converting dots
|
216
|
+
// to slashes or requiring a slash at the beginning.
|
217
|
+
// (a slash as a prefix means that it'll load from the root of
|
218
|
+
// the jar, rather than trying to dig into the package location)
|
219
|
+
ClassLoader cl = pApplet.getClass().getClassLoader();
|
220
|
+
|
221
|
+
// by default, data files are exported to the root path of the jar.
|
222
|
+
// (not the data folder) so check there first.
|
223
|
+
stream = cl.getResourceAsStream("data/" + filename);
|
224
|
+
if (stream != null) {
|
225
|
+
String cn = stream.getClass().getName();
|
226
|
+
// this is an irritation of sun's java plug-in, which will return
|
227
|
+
// a non-null stream for an object that doesn't exist. like all good
|
228
|
+
// things, this is probably introduced in java 1.5. awesome!
|
229
|
+
// http://dev.processing.org/bugs/show_bug.cgi?id=359
|
230
|
+
if (!cn.equals("sun.plugin.cache.EmptyInputStream")) {
|
231
|
+
return stream;
|
232
|
+
}
|
233
|
+
}
|
234
|
+
|
235
|
+
// When used with an online script, also need to check without the
|
236
|
+
// data folder, in case it's not in a subfolder called 'data'.
|
237
|
+
// http://dev.processing.org/bugs/show_bug.cgi?id=389
|
238
|
+
stream = cl.getResourceAsStream(filename);
|
239
|
+
if (stream != null) {
|
240
|
+
String cn = stream.getClass().getName();
|
241
|
+
if (!cn.equals("sun.plugin.cache.EmptyInputStream")) {
|
242
|
+
return stream;
|
243
|
+
}
|
244
|
+
}
|
245
|
+
|
246
|
+
try {
|
247
|
+
// attempt to load from a local file, used when running as
|
248
|
+
// an application, or as a signed applet
|
249
|
+
try { // first try to catch any security exceptions
|
250
|
+
try {
|
251
|
+
stream = new FileInputStream(pApplet.dataPath(filename));
|
252
|
+
if (stream != null) return stream;
|
253
|
+
} catch (IOException e2) { }
|
254
|
+
|
255
|
+
try {
|
256
|
+
stream = new FileInputStream(pApplet.sketchPath(filename));
|
257
|
+
if (stream != null) return stream;
|
258
|
+
} catch (Exception e) { } // ignored
|
259
|
+
|
260
|
+
try {
|
261
|
+
stream = new FileInputStream(filename);
|
262
|
+
if (stream != null) return stream;
|
263
|
+
} catch (IOException e1) { }
|
264
|
+
|
265
|
+
} catch (SecurityException se) { } // online, whups
|
266
|
+
|
267
|
+
} catch (Exception e) {
|
268
|
+
e.printStackTrace();
|
269
|
+
}
|
270
|
+
|
271
|
+
return null;
|
272
|
+
}
|
273
|
+
|
274
|
+
/**
|
275
|
+
* Determine if a file is gzip compressed.
|
276
|
+
*
|
277
|
+
* @param filename The name of the fiel to check.
|
278
|
+
* @return True if the file is a gzip compressed file and false otherwise.
|
279
|
+
*/
|
280
|
+
private static boolean isGzipCompressed(String filename) {
|
281
|
+
String extension = PathUtil.parseExtension(filename);
|
282
|
+
return extension.equalsIgnoreCase("gz") || extension.equalsIgnoreCase("svgz");
|
283
|
+
}
|
284
|
+
|
285
|
+
}
|
@@ -0,0 +1,109 @@
|
|
1
|
+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
|
2
|
+
|
3
|
+
/*
|
4
|
+
Part of the Processing project - http://processing.org
|
5
|
+
|
6
|
+
Copyright (c) 2012-18 The Processing Foundation
|
7
|
+
Copyright (c) 2004-12 Ben Fry and Casey Reas
|
8
|
+
Copyright (c) 2001-04 Massachusetts Institute of Technology
|
9
|
+
|
10
|
+
This library is free software; you can redistribute it and/or
|
11
|
+
modify it under the terms of the GNU Lesser General Public
|
12
|
+
License as published by the Free Software Foundation, version 2.1.
|
13
|
+
|
14
|
+
This library is distributed in the hope that it will be useful,
|
15
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
17
|
+
Lesser General Public License for more details.
|
18
|
+
|
19
|
+
You should have received a copy of the GNU Lesser General
|
20
|
+
Public License along with this library; if not, write to the
|
21
|
+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
22
|
+
Boston, MA 02111-1307 USA
|
23
|
+
*/
|
24
|
+
|
25
|
+
package processing.core.util.io;
|
26
|
+
|
27
|
+
import java.io.File;
|
28
|
+
|
29
|
+
|
30
|
+
/**
|
31
|
+
* Convenience functions for managing file paths.
|
32
|
+
*/
|
33
|
+
public class PathUtil {
|
34
|
+
private static final String UNKNOWN_FILE_EXTENSION = "unknown";
|
35
|
+
|
36
|
+
/**
|
37
|
+
* Determine the extension of a file given in a path.
|
38
|
+
*
|
39
|
+
* @param path The path like directory/subdirectory/image.png.
|
40
|
+
* @return The extension of the file at path like "png" or "unknown" if could not be determined.
|
41
|
+
*/
|
42
|
+
public static String parseExtension(String path) {
|
43
|
+
String lower = path.toLowerCase();
|
44
|
+
String extension;
|
45
|
+
|
46
|
+
int dot = path.lastIndexOf('.');
|
47
|
+
if (dot == -1) {
|
48
|
+
extension = UNKNOWN_FILE_EXTENSION; // no extension found
|
49
|
+
|
50
|
+
} else {
|
51
|
+
extension = lower.substring(dot + 1);
|
52
|
+
|
53
|
+
// check for, and strip any parameters on the url, i.e.
|
54
|
+
// filename.jpg?blah=blah&something=that
|
55
|
+
int question = extension.indexOf('?');
|
56
|
+
if (question != -1) {
|
57
|
+
extension = extension.substring(0, question);
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
return extension;
|
62
|
+
}
|
63
|
+
|
64
|
+
/**
|
65
|
+
* Clean up irregularities like leading period or capitalization from an extension string.
|
66
|
+
*
|
67
|
+
* @param extension The extension to clean up like ".PNG".
|
68
|
+
* @return The cleaned up extension like "png".
|
69
|
+
*/
|
70
|
+
public static String cleanExtension(String extension) {
|
71
|
+
extension = extension == null ? UNKNOWN_FILE_EXTENSION : extension.toLowerCase();
|
72
|
+
|
73
|
+
if (extension.startsWith(".")) {
|
74
|
+
extension = extension.substring(1);
|
75
|
+
}
|
76
|
+
|
77
|
+
return extension;
|
78
|
+
}
|
79
|
+
|
80
|
+
/**
|
81
|
+
* Create the directories so that files can be created at a path.
|
82
|
+
*
|
83
|
+
* @param path The path like folder1/folder2/image.png. In this example, folder1 and folder2 would
|
84
|
+
* both be created if not already existing.
|
85
|
+
*/
|
86
|
+
public static void createPath(String path) {
|
87
|
+
createPath(new File(path));
|
88
|
+
}
|
89
|
+
|
90
|
+
/**
|
91
|
+
* Create the directories so that files can be created at a path.
|
92
|
+
*
|
93
|
+
* @param file The file describing the path that should be created. A path of
|
94
|
+
* folder1/folder2/image.png would cause folder1 and folder2 to be created if not already
|
95
|
+
* existing.
|
96
|
+
*/
|
97
|
+
public static void createPath(File file) {
|
98
|
+
try {
|
99
|
+
String parent = file.getParent();
|
100
|
+
if (parent != null) {
|
101
|
+
File unit = new File(parent);
|
102
|
+
if (!unit.exists()) unit.mkdirs();
|
103
|
+
}
|
104
|
+
} catch (SecurityException se) {
|
105
|
+
System.err.println("You don't have permissions to create " +
|
106
|
+
file.getAbsolutePath());
|
107
|
+
}
|
108
|
+
}
|
109
|
+
}
|