- Timestamp:
- 2017-04-06T19:15:55+12:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/gli/src/org/greenstone/gatherer/util/Utility.java
r30702 r31582 41 41 // Don't even think about adding import java.awt.* here! 42 42 // The functions in this class should not use any graphical classes. Put your function somewhere else buster! 43 import java.io.Closeable;44 43 import java.io.*; 45 44 import java.net.*; … … 50 49 // Don't even think about adding import org.greenstone.gatherer.Gatherer in here! 51 50 // The functions in this class should be independent of the Gatherer class. Put your function somewhere else buster! 52 51 import org.greenstone.gatherer.util.SafeProcess; // for the closeResource() static method 53 52 54 53 /** To provide a library of common methods, in a static context, for use in the Gatherer. … … 74 73 static final public String PERL_EXECUTABLE_WINDOWS = "Perl.exe"; 75 74 75 /** Platform independent NEWLINE character */ 76 public static final String NEWLINE; 77 78 // NEWLINE related code copied across from GS3 src code 79 // Before Java 7, no System.lineSeparator() or System.getProperty("line.separator") 80 // And on local linux, am compiling with JDK 6, so need this. 81 // http://stackoverflow.com/questions/207947/how-do-i-get-a-platform-dependent-new-line-character 82 // http://stackoverflow.com/questions/2591083/getting-java-version-at-runtime 83 // https://www.tutorialspoint.com/java/lang/package_getspecificationversion.htm 84 // https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html 85 // Can initialise static final vars on declaration or in static initialisation code block 86 // http://stackoverflow.com/questions/2339932/java-can-final-variables-be-initialized-in-static-initialization-block 87 // Initialise object member final vars on declaration or in constructors 88 static { 89 double java_version = Double.parseDouble(System.getProperty("java.specification.version")); 90 if(java_version >= 1.7) { 91 NEWLINE = System.getProperty("line.separator"); 92 } else { 93 NEWLINE = isWindows() ? "\r\n" : "\n"; 94 } 95 } 76 96 77 97 /** … … 93 113 System.err.println("*** Exception occurred: " + e.getMessage()); 94 114 } finally { 95 closeResource(fin);115 SafeProcess.closeResource(fin); 96 116 } 97 117 … … 575 595 System.err.println("Exception occurred: " + e.getMessage()); 576 596 } finally { 577 closeResource(fin);578 closeResource(fout);597 SafeProcess.closeResource(fin); 598 SafeProcess.closeResource(fout); 579 599 580 600 } 581 601 return oldValue; 582 602 } 583 584 // For safely closing streams/handles/resources.585 // For examples of use look in the Input- and OutputStreamGobbler classes.586 // http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html587 // http://stackoverflow.com/questions/481446/throws-exception-in-finally-blocks588 public static void closeResource(Closeable resourceHandle) {589 try {590 if(resourceHandle != null) {591 resourceHandle.close();592 resourceHandle = null;593 }594 } catch(Exception e) {595 System.err.println("Exception closing resource: " + e.getMessage());596 e.printStackTrace();597 resourceHandle = null;598 }599 }600 601 public static void closeProcess(Process prcs) {602 if( prcs != null ) {603 closeResource(prcs.getErrorStream());604 closeResource(prcs.getOutputStream());605 closeResource(prcs.getInputStream());606 prcs.destroy();607 }608 }609 603 }
Note:
See TracChangeset
for help on using the changeset viewer.