| 1 | |
package org.jtheque.utils.io; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
import java.io.IOException; |
| 20 | |
import java.io.InputStreamReader; |
| 21 | |
import java.io.InterruptedIOException; |
| 22 | |
import java.io.OutputStream; |
| 23 | |
import java.io.PrintStream; |
| 24 | |
import java.io.Reader; |
| 25 | |
import java.nio.CharBuffer; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
public final class SimpleApplicationConsumer { |
| 34 | 0 | private final StringBuilderOutputStream resultStream = new StringBuilderOutputStream(); |
| 35 | |
|
| 36 | 0 | private final PrintStream out = new PrintStream(resultStream, true); |
| 37 | 0 | private final PrintStream err = new PrintStream(resultStream, true); |
| 38 | |
|
| 39 | |
private static final int BUF_SIZE = 4196; |
| 40 | |
|
| 41 | |
private final ProcessBuilder builder; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public SimpleApplicationConsumer(String... args) { |
| 49 | 0 | super(); |
| 50 | |
|
| 51 | 0 | builder = new ProcessBuilder(args); |
| 52 | 0 | } |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public void consume() throws IOException { |
| 61 | 0 | final Process process = builder.start(); |
| 62 | |
|
| 63 | |
try { |
| 64 | 0 | process.getOutputStream().close(); |
| 65 | |
|
| 66 | 0 | dump(new InputStreamReader(process.getErrorStream()), err); |
| 67 | 0 | dump(new InputStreamReader(process.getInputStream()), out); |
| 68 | |
|
| 69 | |
try { |
| 70 | 0 | process.waitFor(); |
| 71 | 0 | } catch (InterruptedException e) { |
| 72 | 0 | IOException ioe = new InterruptedIOException(); |
| 73 | 0 | ioe.initCause(e); |
| 74 | 0 | throw ioe; |
| 75 | 0 | } |
| 76 | |
} finally { |
| 77 | 0 | process.destroy(); |
| 78 | 0 | } |
| 79 | 0 | } |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
private static void dump(Reader in, PrintStream out) throws IOException { |
| 89 | |
try { |
| 90 | |
try { |
| 91 | 0 | Thread current = Thread.currentThread(); |
| 92 | 0 | CharBuffer cb = CharBuffer.allocate(BUF_SIZE); |
| 93 | |
|
| 94 | |
|
| 95 | 0 | cb.clear(); |
| 96 | |
|
| 97 | 0 | int len = in.read(cb); |
| 98 | |
|
| 99 | 0 | while (len > 0 && !current.isInterrupted()) { |
| 100 | 0 | cb.position(0).limit(len); |
| 101 | 0 | out.append(cb); |
| 102 | 0 | cb.clear(); |
| 103 | 0 | out.flush(); |
| 104 | |
|
| 105 | 0 | if (current.isInterrupted()) { |
| 106 | 0 | break; |
| 107 | |
} |
| 108 | |
|
| 109 | 0 | len = in.read(cb); |
| 110 | |
} |
| 111 | |
} finally { |
| 112 | 0 | FileUtils.close(in); |
| 113 | 0 | } |
| 114 | |
} finally { |
| 115 | 0 | FileUtils.close(out); |
| 116 | 0 | } |
| 117 | 0 | } |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
public String getResult() { |
| 125 | 0 | return resultStream.getResult().toString(); |
| 126 | |
} |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | 0 | private static final class StringBuilderOutputStream extends OutputStream { |
| 134 | 0 | private final StringBuilder result = new StringBuilder(250); |
| 135 | |
|
| 136 | |
@Override |
| 137 | |
public void write(int b) throws IOException { |
| 138 | 0 | result.append(String.valueOf((char) b)); |
| 139 | 0 | } |
| 140 | |
|
| 141 | |
@Override |
| 142 | |
public void write(byte[] b, int off, int len) throws IOException { |
| 143 | 0 | result.append(new String(b, off, len)); |
| 144 | 0 | } |
| 145 | |
|
| 146 | |
@Override |
| 147 | |
public void write(byte[] b) throws IOException{ |
| 148 | 0 | write(b, 0, b.length); |
| 149 | 0 | } |
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
public StringBuilder getResult() { |
| 157 | 0 | return result; |
| 158 | |
} |
| 159 | |
} |
| 160 | |
} |