Subversion Repository Public Repository

litesoft

Diff Revisions 314 vs 315 for /trunk/Java/ScarPlus/src/com/esotericsoftware/scar/Utils.java

Diff revisions: vs.
  @@ -2,14 +2,13 @@
2 2
3 3 import java.io.*;
4 4 import java.net.*;
5 - import java.nio.channels.*;
6 5 import java.util.*;
7 6 import java.util.regex.*;
8 7 import javax.tools.*;
9 8
10 9 import com.esotericsoftware.utils.*;
11 10
12 - public class Utils extends Util
11 + public class Utils extends FileUtil
13 12 {
14 13 /**
15 14 * The Scar installation directory. The value comes from the SCAR_HOME environment variable, if it exists. Alternatively, the
  @@ -117,107 +116,6 @@
117 116 }
118 117
119 118 /**
120 - * Reads to the end of the input stream and writes the bytes to the output stream.
121 - */
122 - static public void copyStream( InputStream input, OutputStream output )
123 - throws IOException
124 - {
125 - assertNotNull( "input", input );
126 - assertNotNull( "output", output );
127 - Closeable zCloseable = output;
128 - try
129 - {
130 - byte[] buffer = new byte[4096];
131 - while ( true )
132 - {
133 - int length = input.read( buffer );
134 - if ( length == -1 )
135 - {
136 - break;
137 - }
138 - output.write( buffer, 0, length );
139 - }
140 - zCloseable = null;
141 - output.close();
142 - }
143 - finally
144 - {
145 - dispose( zCloseable );
146 - dispose( input );
147 - }
148 - }
149 -
150 - /**
151 - * Copies a file, overwriting any existing file at the destination.
152 - */
153 - static public String copyFile( String in, String out )
154 - throws IOException
155 - {
156 - assertNotNull( "in", in );
157 - assertNotNull( "out", out );
158 - LOGGER.trace.log( "Copying file: ", in, " -> ", out );
159 -
160 - FileChannel sourceChannel = null;
161 - FileChannel destinationChannel = null;
162 - Closeable zCloseable = null;
163 - try
164 - {
165 - sourceChannel = new FileInputStream( in ).getChannel();
166 - zCloseable = (destinationChannel = new FileOutputStream( out ).getChannel());
167 - sourceChannel.transferTo( 0, sourceChannel.size(), destinationChannel );
168 - zCloseable = null;
169 - destinationChannel.close();
170 - }
171 - finally
172 - {
173 - dispose( zCloseable );
174 - dispose( sourceChannel );
175 - }
176 - return out;
177 - }
178 -
179 - /**
180 - * Moves a file, overwriting any existing file at the destination.
181 - */
182 - static public String moveFile( String in, String out )
183 - throws IOException
184 - {
185 - assertNotNull( "in", in );
186 - assertNotNull( "out", out );
187 - copyFile( in, out );
188 - delete( in );
189 - return out;
190 - }
191 -
192 - /**
193 - * Returns the textual contents of the specified file.
194 - */
195 - static public String fileContents( String path )
196 - throws IOException
197 - {
198 - StringBuilder stringBuffer = new StringBuilder( 4096 );
199 - FileReader reader = new FileReader( path );
200 - try
201 - {
202 - char[] buffer = new char[2048];
203 - while ( true )
204 - {
205 - int length = reader.read( buffer );
206 - if ( length == -1 )
207 - {
208 - break;
209 - }
210 - stringBuffer.append( buffer, 0, length );
211 - }
212 - }
213 - finally
214 - {
215 - dispose( reader );
216 - }
217 - return stringBuffer.toString();
218 - }
219 -
220 - /**
221 119 * Returns only the filename portion of the specified path.
222 120 */
223 121 static public String fileName( String path )
  @@ -282,7 +180,6 @@
282 180 * Splits the specified command at spaces that are not surrounded by quotes and passes the result to {@link #shell(String...)}.
283 181 */
284 182 static public void shell( String command )
285 - throws IOException
286 183 {
287 184 List<String> matchList = new ArrayList<String>();
288 185 Pattern regex = Pattern.compile( "[^\\s\"']+|\"([^\"]*)\"|'([^']*)'" );
  @@ -310,7 +207,6 @@
310 207 * Windows the same filename with an "exe" extension is also tried.
311 208 */
312 209 static public void shell( String... command )
313 - throws IOException
314 210 {
315 211 assertNotEmpty( "command", command );
316 212
  @@ -336,41 +232,36 @@
336 232 LOGGER.trace.log( "Executing command: ", buffer );
337 233 }
338 234
339 - Process process = new ProcessBuilder( command ).start();
340 - // try {
341 - // process.waitFor();
342 - // } catch (InterruptedException ignored) {
343 - // }
344 - BufferedReader reader = new BufferedReader( new InputStreamReader( process.getInputStream() ) );
345 - while ( true )
235 + try
346 236 {
347 - String line = reader.readLine();
348 - if ( line == null )
237 + Process process = new ProcessBuilder( command ).start();
238 + Thread zOut = new CopyOutputThread( process.getInputStream(), System.out );
239 + Thread zErr = new CopyOutputThread( process.getErrorStream(), System.err );
240 +
241 + zOut.join();
242 + zErr.join();
243 + // try {
244 + // process.waitFor();
245 + // } catch (InterruptedException ignored) {
246 + // }
247 + if ( process.exitValue() != 0 )
349 248 {
350 - break;
249 + StringBuilder buffer = new StringBuilder( 256 );
250 + for ( String text : command )
251 + {
252 + buffer.append( text );
253 + buffer.append( ' ' );
254 + }
255 + throw new RuntimeException( "Error (" + process.exitValue() + ") executing command: " + buffer );
351 256 }
352 - System.out.println( line );
353 257 }
354 - reader.close();
355 - reader = new BufferedReader( new InputStreamReader( process.getErrorStream() ) );
356 - while ( true )
258 + catch ( IOException e )
357 259 {
358 - String line = reader.readLine();
359 - if ( line == null )
360 - {
361 - break;
362 - }
363 - System.out.println( line );
260 + throw new WrappedIOException( e );
364 261 }
365 - if ( process.exitValue() != 0 )
262 + catch ( InterruptedException e )
366 263 {
367 - StringBuilder buffer = new StringBuilder( 256 );
368 - for ( String text : command )
369 - {
370 - buffer.append( text );
371 - buffer.append( ' ' );
372 - }
373 - throw new RuntimeException( "Error executing command: " + buffer );
264 + throw new RuntimeException( e );
374 265 }
375 266 }
376 267
  @@ -380,7 +271,6 @@
380 271 * @return The path to the keystore file.
381 272 */
382 273 static public String keystore( String keystoreFile, String alias, String password, String company, String title )
383 - throws IOException
384 274 {
385 275 if ( fileExists( keystoreFile = assertNotEmpty( "keystoreFile", keystoreFile ) ) )
386 276 {
  @@ -397,22 +287,30 @@
397 287
398 288 File file = new File( keystoreFile );
399 289 file.delete();
400 - Process process = Runtime.getRuntime().exec( new String[]{resolvePath( "keytool" ), "-genkey", "-keystore", keystoreFile, "-alias", alias} );
401 - OutputStreamWriter writer = new OutputStreamWriter( process.getOutputStream() );
402 - writer.write( password + "\n" ); // Enter keystore password:
403 - writer.write( password + "\n" ); // Re-enter new password:
404 - writer.write( company + "\n" ); // What is your first and last name?
405 - writer.write( title + "\n" ); // What is the name of your organizational unit?
406 - writer.write( title + "\n" ); // What is the name of your organization?
407 - writer.write( "\n" ); // What is the name of your City or Locality? [Unknown]
408 - writer.write( "\n" ); // What is the name of your State or Province? [Unknown]
409 - writer.write( "\n" ); // What is the two-letter country code for this unit? [Unknown]
410 - writer.write( "yes\n" ); // Correct?
411 - writer.write( "\n" ); // Return if same alias key password as keystore.
412 - writer.flush();
413 - process.getOutputStream().close();
414 - process.getInputStream().close();
415 - process.getErrorStream().close();
290 + Process process = null;
291 + try
292 + {
293 + process = Runtime.getRuntime().exec( new String[]{resolvePath( "keytool" ), "-genkey", "-keystore", keystoreFile, "-alias", alias} );
294 + OutputStreamWriter writer = new OutputStreamWriter( process.getOutputStream() );
295 + writer.write( password + "\n" ); // Enter keystore password:
296 + writer.write( password + "\n" ); // Re-enter new password:
297 + writer.write( company + "\n" ); // What is your first and last name?
298 + writer.write( title + "\n" ); // What is the name of your organizational unit?
299 + writer.write( title + "\n" ); // What is the name of your organization?
300 + writer.write( "\n" ); // What is the name of your City or Locality? [Unknown]
301 + writer.write( "\n" ); // What is the name of your State or Province? [Unknown]
302 + writer.write( "\n" ); // What is the two-letter country code for this unit? [Unknown]
303 + writer.write( "yes\n" ); // Correct?
304 + writer.write( "\n" ); // Return if same alias key password as keystore.
305 + writer.flush();
306 + process.getOutputStream().close();
307 + process.getInputStream().close();
308 + process.getErrorStream().close();
309 + }
310 + catch ( IOException e )
311 + {
312 + throw new WrappedIOException( e );
313 + }
416 314 try
417 315 {
418 316 process.waitFor();