- Timestamp:
- 2014-05-01T20:15:13+12:00 (10 years ago)
- Location:
- main/trunk/gli/src/org/greenstone/gatherer
- Files:
-
- 2 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/gli/src/org/greenstone/gatherer/gui/FormatConversionDialog.java
r29016 r29017 7 7 import org.greenstone.gatherer.cdm.CollectionConfigXMLReadWrite; 8 8 import org.greenstone.gatherer.util.Codec; 9 import org.greenstone.gatherer.util.InputStreamGobbler; 10 import org.greenstone.gatherer.util.OutputStreamGobbler; 9 11 import org.greenstone.gatherer.util.StaticStrings; 10 12 import org.greenstone.gatherer.util.Utility; … … 370 372 371 373 return outputstr; 372 }373 374 375 // http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html376 // http://stackoverflow.com/questions/481446/throws-exception-in-finally-blocks377 private void closeResource(Closeable resourceHandle) {378 try {379 if(resourceHandle != null) {380 resourceHandle.close();381 resourceHandle = null;382 }383 } catch(Exception e) {384 System.err.println("Exception closing resource: " + e.getMessage());385 e.printStackTrace();386 }387 374 } 388 375 … … 946 933 } 947 934 948 // http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2949 class InputStreamGobbler extends Thread950 {951 InputStream is = null;952 StringBuffer outputstr = new StringBuffer();953 boolean split_newlines = false;954 955 956 public InputStreamGobbler(InputStream is)957 {958 this.is = is;959 split_newlines = false;960 }961 962 public InputStreamGobbler(InputStream is, boolean split_newlines)963 {964 this.is = is;965 this.split_newlines = split_newlines;966 }967 968 public void run()969 {970 BufferedReader br = null;971 try {972 br = new BufferedReader(new InputStreamReader(is, "UTF-8"));973 String line=null;974 while ( (line = br.readLine()) != null) {975 //System.out.println("@@@ GOT LINE: " + line);976 outputstr.append(line);977 if(split_newlines) {978 outputstr.append("\n");979 }980 981 }982 } catch (IOException ioe) {983 ioe.printStackTrace();984 } finally {985 closeResource(br);986 }987 }988 989 public String getOutput() {990 return outputstr.toString(); // implicit toString() call anyway. //return outputstr;991 }992 }993 994 class OutputStreamGobbler extends Thread995 {996 OutputStream os = null;997 String inputstr = "";998 999 public OutputStreamGobbler(OutputStream os, String inputstr)1000 {1001 this.os = os;1002 this.inputstr = inputstr;1003 }1004 1005 public void run()1006 {1007 BufferedWriter osw = null;1008 try {1009 osw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));1010 //System.out.println("@@@ SENDING LINE: " + inputstr);1011 osw.write(inputstr, 0, inputstr.length());1012 osw.newLine();//osw.write("\n");1013 osw.flush();1014 1015 // Don't explicitly send EOF when using StreamGobblers as below,1016 // as the EOF char is echoed to output.1017 // Flushing the write handle and/or closing the resource seems1018 // to already send EOF silently.1019 1020 /*if(Utility.isWindows()) {1021 osw.write("\032"); // octal for Ctrl-Z, EOF on Windows1022 } else { // EOF on Linux/Mac is Ctrl-D1023 osw.write("\004"); // octal for Ctrl-D, see http://www.unix-manuals.com/refs/misc/ascii-table.html1024 }1025 osw.flush();1026 */1027 } catch (IOException ioe) {1028 ioe.printStackTrace();1029 } finally {1030 closeResource(osw);1031 }1032 }1033 }1034 1035 935 private class UndoListener implements ActionListener 1036 936 { -
main/trunk/gli/src/org/greenstone/gatherer/util/Utility.java
r23283 r29017 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; 43 44 import java.io.*; 44 45 import java.net.*; … … 556 557 return oldValue; 557 558 } 559 560 // For safely closing streams/handles/resources. 561 // For examples of use look in the Input- and OutputStreamGobbler classes. 562 // http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html 563 // http://stackoverflow.com/questions/481446/throws-exception-in-finally-blocks 564 public static void closeResource(Closeable resourceHandle) { 565 try { 566 if(resourceHandle != null) { 567 resourceHandle.close(); 568 resourceHandle = null; 569 } 570 } catch(Exception e) { 571 System.err.println("Exception closing resource: " + e.getMessage()); 572 e.printStackTrace(); 573 } 574 } 558 575 }
Note:
See TracChangeset
for help on using the changeset viewer.