@@ -5087,33 +5087,25 @@ public boolean saveStream(String targetFilename, InputStream sourceStream) {
50875087 }
50885088
50895089
5090- static public boolean saveStream (File targetFile , InputStream sourceStream ) {
5090+ static public boolean saveStream (File target , InputStream source ) {
50915091 File tempFile = null ;
50925092 try {
5093- File parentDir = targetFile .getParentFile ();
5094- createPath (targetFile );
5095- tempFile = File .createTempFile (targetFile .getName (), null , parentDir );
5096-
5097- BufferedInputStream bis = new BufferedInputStream (sourceStream , 16384 );
5098- FileOutputStream fos = new FileOutputStream (tempFile );
5099- BufferedOutputStream bos = new BufferedOutputStream (fos );
5100-
5101- byte [] buffer = new byte [8192 ];
5102- int bytesRead ;
5103- while ((bytesRead = bis .read (buffer )) != -1 ) {
5104- bos .write (buffer , 0 , bytesRead );
5105- }
5106-
5107- bos .flush ();
5108- bos .close ();
5109- bos = null ;
5110-
5111- if (targetFile .exists () && !targetFile .delete ()) {
5112- System .err .println ("Could not replace " +
5113- targetFile .getAbsolutePath () + "." );
5093+ // make sure that this path actually exists before writing
5094+ createPath (target );
5095+ tempFile = createTempFile (target );
5096+ FileOutputStream targetStream = new FileOutputStream (tempFile );
5097+
5098+ saveStream (targetStream , source );
5099+ targetStream .close ();
5100+ targetStream = null ;
5101+
5102+ if (target .exists ()) {
5103+ if (!target .delete ()) {
5104+ System .err .println ("Could not replace " +
5105+ target .getAbsolutePath () + "." );
5106+ }
51145107 }
5115-
5116- if (!tempFile .renameTo (targetFile )) {
5108+ if (!tempFile .renameTo (target )) {
51175109 System .err .println ("Could not rename temporary file " +
51185110 tempFile .getAbsolutePath ());
51195111 return false ;
@@ -5130,34 +5122,90 @@ static public boolean saveStream(File targetFile, InputStream sourceStream) {
51305122 }
51315123
51325124
5125+ static public void saveStream (OutputStream target ,
5126+ InputStream source ) throws IOException {
5127+ BufferedInputStream bis = new BufferedInputStream (source , 16384 );
5128+ BufferedOutputStream bos = new BufferedOutputStream (target );
5129+
5130+ byte [] buffer = new byte [8192 ];
5131+ int bytesRead ;
5132+ while ((bytesRead = bis .read (buffer )) != -1 ) {
5133+ bos .write (buffer , 0 , bytesRead );
5134+ }
5135+
5136+ bos .flush ();
5137+ }
5138+
5139+
51335140 /**
51345141 * Saves bytes to a file to inside the sketch folder.
51355142 * The filename can be a relative path, i.e. "poo/bytefun.txt"
51365143 * would save to a file named "bytefun.txt" to a subfolder
51375144 * called 'poo' inside the sketch folder. If the in-between
51385145 * subfolders don't exist, they'll be created.
51395146 */
5140- public void saveBytes (String filename , byte buffer []) {
5141- saveBytes (saveFile (filename ), buffer );
5147+ public void saveBytes (String filename , byte [] data ) {
5148+ saveBytes (saveFile (filename ), data );
5149+ }
5150+
5151+
5152+ /**
5153+ * Creates a temporary file based on the name/extension of another file
5154+ * and in the same parent directory. Ensures that the same extension is used
5155+ * (i.e. so that .gz files are gzip compressed on output) and that it's done
5156+ * from the same directory so that renaming the file later won't cross file
5157+ * system boundaries.
5158+ */
5159+ static private File createTempFile (File file ) throws IOException {
5160+ File parentDir = file .getParentFile ();
5161+ String name = file .getName ();
5162+ String prefix ;
5163+ String suffix = null ;
5164+ int dot = name .lastIndexOf ('.' );
5165+ if (dot == -1 ) {
5166+ prefix = name ;
5167+ } else {
5168+ // preserve the extension so that .gz works properly
5169+ prefix = name .substring (0 , dot );
5170+ suffix = name .substring (dot );
5171+ }
5172+ // Prefix must be three characters
5173+ if (prefix .length () < 3 ) {
5174+ prefix += "processing" ;
5175+ }
5176+ return File .createTempFile (prefix , suffix , parentDir );
51425177 }
51435178
51445179
51455180 /**
51465181 * Saves bytes to a specific File location specified by the user.
51475182 */
5148- static public void saveBytes (File file , byte buffer []) {
5183+ static public void saveBytes (File file , byte [] data ) {
5184+ File tempFile = null ;
51495185 try {
5150- String filename = file .getAbsolutePath ();
5151- createPath (filename );
5152- OutputStream output = new FileOutputStream (file );
5153- if (file .getName ().toLowerCase ().endsWith (".gz" )) {
5154- output = new GZIPOutputStream (output );
5155- }
5156- saveBytes (output , buffer );
5186+ tempFile = createTempFile (file );
5187+
5188+ OutputStream output = createOutput (tempFile );
5189+ saveBytes (output , data );
51575190 output .close ();
5191+ output = null ;
5192+
5193+ if (file .exists ()) {
5194+ if (!file .delete ()) {
5195+ System .err .println ("Could not replace " + file .getAbsolutePath ());
5196+ }
5197+ }
5198+
5199+ if (!tempFile .renameTo (file )) {
5200+ System .err .println ("Could not rename temporary file " +
5201+ tempFile .getAbsolutePath ());
5202+ }
51585203
51595204 } catch (IOException e ) {
51605205 System .err .println ("error saving bytes to " + file );
5206+ if (tempFile != null ) {
5207+ tempFile .delete ();
5208+ }
51615209 e .printStackTrace ();
51625210 }
51635211 }
@@ -5166,9 +5214,9 @@ static public void saveBytes(File file, byte buffer[]) {
51665214 /**
51675215 * Spews a buffer of bytes to an OutputStream.
51685216 */
5169- static public void saveBytes (OutputStream output , byte buffer [] ) {
5217+ static public void saveBytes (OutputStream output , byte [] data ) {
51705218 try {
5171- output .write (buffer );
5219+ output .write (data );
51725220 output .flush ();
51735221
51745222 } catch (IOException e ) {
0 commit comments