From 5ded2aecbdbdc86a5ed622c0d3bb727101a8efdc Mon Sep 17 00:00:00 2001 From: benfry Date: Sat, 18 Aug 2007 02:53:50 +0000 Subject: [PATCH] gunzip and saveStream work --- core/src/processing/core/PApplet.java | 43 +++++++++++++++++++++++++-- core/todo.txt | 32 ++++++++++++-------- 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index a47f4b56a..dc9a41a4f 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -4126,6 +4126,9 @@ public class PApplet extends Applet System.err.println(filename + " does not exist or could not be read"); return null; } + if (filename.endsWith(".gz")) { + is = new GZIPInputStream(is); + } return createReader(is); } catch (Exception e) { @@ -4144,7 +4147,11 @@ public class PApplet extends Applet */ static public BufferedReader createReader(File file) { try { - return createReader(new FileInputStream(file)); + InputStream is = new FileInputStream(file); + if (file.getName().endsWith(".gz")) { + is = new GZIPInputStream(is); + } + return createReader(is); } catch (Exception e) { if (file == null) { @@ -4557,7 +4564,7 @@ public class PApplet extends Applet * in a less confusing manner. */ public void saveStream(String filename, String stream) { - saveBytes(filename, loadBytes(stream)); + saveStream(new File(dataPath(filename)), stream); } @@ -4566,7 +4573,37 @@ public class PApplet extends Applet * object, for greater control over the file location. */ public void saveStream(File file, String stream) { - saveBytes(file, loadBytes(stream)); + //saveBytes(file, loadBytes(stream)); + + File tempFile = null; + try { + File parentDir = file.getParentFile(); + tempFile = File.createTempFile(file.getName(), null, parentDir); + + InputStream is = openStream(stream); + BufferedInputStream bis = new BufferedInputStream(is, 16384); + FileOutputStream fos = new FileOutputStream(tempFile); + BufferedOutputStream bos = new BufferedOutputStream(fos); + + byte[] buffer = new byte[8192]; + int bytesRead; + while ((bytesRead = bis.read(buffer)) != -1) { + bos.write(buffer, 0, bytesRead); + } + + bos.flush(); + bos.close(); + bos = null; + + if (!tempFile.renameTo(file)) { + System.err.println("Could not rename temporary file " + tempFile.getAbsolutePath()); + } + } catch (IOException e) { + if (tempFile != null) { + tempFile.delete(); + } + e.printStackTrace(); + } } diff --git a/core/todo.txt b/core/todo.txt index 7f8215d60..b00acf5f3 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -4,7 +4,26 @@ X http://dev.processing.org/bugs/show_bug.cgi?id=535 X bug in osx java 1.4 vs 1.5 X fix bug in str() methods that take arrays X method was not working properly, was using the object reference +X new version of saveStream() +X some auto-gunzip implemented +_ need to finish auto-gunzip +_ loadBytes() and saveStream() functions badly need optimization! +_ don't bother using a buffering stream, just handle internally. gah! +_ make version of loadBytes that checks length of the stream first +_ this might not be worth it +_ the number of cases where this works is small (half of url streams) +_ and who knows if the value returned will be correct +_ (i.e. will it be the uncompressed or compressed size of the data?) + +_ loadBytes() doesn't do auto gunzip? but loadStrings and createReader do? +X this might be a good solution for dealing with the differences +_ with loadBytes(), they can use gzipInput (or loadBytesGZ?) +_ although what does openStream do? (doesn't do auto?) +X auto-gunzip on createReader() +_ add auto-gunzip to loadStrings() +_ others for auto-gunzip? +_ do the same for createWriter() et al _ update the reference to cover parseXxxx() stuff _ also add notes about parseInt/Float(blah, otherwise) @@ -32,11 +51,6 @@ _ http://dev.processing.org/bugs/show_bug.cgi?id=561 _ draw() called twice in vista with java 1.6 _ http://dev.processing.org/bugs/show_bug.cgi?id=587 -_ loadBytes() doesn't do auto gunzip? but loadStrings and createReader do? -_ this might be a good solution for dealing with the differences -_ with loadBytes(), they can use gzipInput (or loadBytesGZ?) -_ although what does openStream do? (doesn't do auto?) - _ an image marked RGB but with 0s for the alpha won't draw in JAVA2D _ images with 0x00 in their high bits being drawn transparent _ also if transparency set but RGB is setting, still honors transparency @@ -249,14 +263,6 @@ _ http://processing.org/discourse/yabb_beta/YaBB.cgi?board=SoftwareBugs;action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . -_ loadBytes() and saveStream() functions badly need optimization! -_ don't bother using a buffering stream, just handle internally. gah! -_ make version of loadBytes that checks length of the stream first -_ this might not be worth it -_ the number of cases where this works is small (half of url streams) -_ and who knows if the value returned will be correct -_ (i.e. will it be the uncompressed or compressed size of the data?) - _ modelX/Y/Z still having trouble _ http://dev.processing.org/bugs/show_bug.cgi?id=486