Changeset 29017 for main/trunk/gli


Ignore:
Timestamp:
2014-05-01T20:15:13+12:00 (10 years ago)
Author:
ak19
Message:

Moved the StreamGobbler classes and their closeResource help method out of FormatConversionDialog.java as these are more generally useful

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  
    77import org.greenstone.gatherer.cdm.CollectionConfigXMLReadWrite;
    88import org.greenstone.gatherer.util.Codec;
     9import org.greenstone.gatherer.util.InputStreamGobbler;
     10import org.greenstone.gatherer.util.OutputStreamGobbler;
    911import org.greenstone.gatherer.util.StaticStrings;
    1012import org.greenstone.gatherer.util.Utility;
     
    370372
    371373    return outputstr;
    372     }
    373 
    374 
    375     // http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
    376     // http://stackoverflow.com/questions/481446/throws-exception-in-finally-blocks
    377     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     }
    387374    }
    388375
     
    946933    }
    947934
    948     // http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2
    949     class InputStreamGobbler extends Thread
    950     {
    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 Thread
    995     {
    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 seems
    1018         // to already send EOF silently.
    1019 
    1020         /*if(Utility.isWindows()) {
    1021             osw.write("\032"); // octal for Ctrl-Z, EOF on Windows
    1022         } else { // EOF on Linux/Mac is Ctrl-D
    1023             osw.write("\004"); // octal for Ctrl-D, see http://www.unix-manuals.com/refs/misc/ascii-table.html
    1024         }
    1025         osw.flush();
    1026         */
    1027         } catch (IOException ioe) {
    1028         ioe.printStackTrace(); 
    1029         } finally {
    1030         closeResource(osw);
    1031         }
    1032     }   
    1033     }
    1034 
    1035935    private class UndoListener implements ActionListener
    1036936    {   
  • main/trunk/gli/src/org/greenstone/gatherer/util/Utility.java

    r23283 r29017  
    4141// Don't even think about adding import java.awt.* here!
    4242// The functions in this class should not use any graphical classes. Put your function somewhere else buster!
     43import java.io.Closeable;
    4344import java.io.*;
    4445import java.net.*;
     
    556557        return oldValue;
    557558    }
     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    }
    558575}
Note: See TracChangeset for help on using the changeset viewer.