@@ -5087,33 +5087,25 @@ public boolean saveStream(String targetFilename, InputStream sourceStream) {
5087
5087
}
5088
5088
5089
5089
5090
- static public boolean saveStream (File targetFile , InputStream sourceStream ) {
5090
+ static public boolean saveStream (File target , InputStream source ) {
5091
5091
File tempFile = null ;
5092
5092
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
+ }
5114
5107
}
5115
-
5116
- if (!tempFile .renameTo (targetFile )) {
5108
+ if (!tempFile .renameTo (target )) {
5117
5109
System .err .println ("Could not rename temporary file " +
5118
5110
tempFile .getAbsolutePath ());
5119
5111
return false ;
@@ -5130,34 +5122,90 @@ static public boolean saveStream(File targetFile, InputStream sourceStream) {
5130
5122
}
5131
5123
5132
5124
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
+
5133
5140
/**
5134
5141
* Saves bytes to a file to inside the sketch folder.
5135
5142
* The filename can be a relative path, i.e. "poo/bytefun.txt"
5136
5143
* would save to a file named "bytefun.txt" to a subfolder
5137
5144
* called 'poo' inside the sketch folder. If the in-between
5138
5145
* subfolders don't exist, they'll be created.
5139
5146
*/
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 );
5142
5177
}
5143
5178
5144
5179
5145
5180
/**
5146
5181
* Saves bytes to a specific File location specified by the user.
5147
5182
*/
5148
- static public void saveBytes (File file , byte buffer []) {
5183
+ static public void saveBytes (File file , byte [] data ) {
5184
+ File tempFile = null ;
5149
5185
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 );
5157
5190
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
+ }
5158
5203
5159
5204
} catch (IOException e ) {
5160
5205
System .err .println ("error saving bytes to " + file );
5206
+ if (tempFile != null ) {
5207
+ tempFile .delete ();
5208
+ }
5161
5209
e .printStackTrace ();
5162
5210
}
5163
5211
}
@@ -5166,9 +5214,9 @@ static public void saveBytes(File file, byte buffer[]) {
5166
5214
/**
5167
5215
* Spews a buffer of bytes to an OutputStream.
5168
5216
*/
5169
- static public void saveBytes (OutputStream output , byte buffer [] ) {
5217
+ static public void saveBytes (OutputStream output , byte [] data ) {
5170
5218
try {
5171
- output .write (buffer );
5219
+ output .write (data );
5172
5220
output .flush ();
5173
5221
5174
5222
} catch (IOException e ) {
0 commit comments