1 28 29 package com.idaremedia.antx.ownhelpers.io; 30 31 import java.io.OutputStream ; 32 import java.io.PrintStream ; 33 34 import com.idaremedia.antx.AntX; 35 import com.idaremedia.antx.apis.BuildError; 36 37 56 57 public final class ErrOutHandle 58 { 59 private static final String IAM_ = AntX.utilities+"ErrOutHandle"; 60 61 62 63 66 public ErrOutHandle() 67 { 68 } 69 70 71 72 79 public ErrOutHandle(OutputStream out, OutputStream err) 80 { 81 install(new PrintStream (out),new PrintStream (err)); 82 m_newOut0 = out; 83 m_newErr0 = err; 84 } 85 86 87 88 96 public void install(PrintStream out, PrintStream err) 97 { 98 AntX.require_(out!=null && err!=null,IAM_,"install- nonzro streams"); 99 try { 100 PrintStream os; 101 102 os = System.out; 103 System.setOut(out); 104 m_oldOut = os; 105 m_newOut = out; 106 107 os = System.err; 108 System.setErr(err); 109 m_oldErr = os; 110 m_newErr = err; 111 } 112 catch(RuntimeException secX) { 113 if (m_oldErr!=null) { 115 System.setErr(m_oldErr); 116 } 117 if (m_oldOut!=null) { 118 System.setOut(m_oldOut); 119 } 120 clear(); 121 throw secX; 122 } 123 } 124 125 126 127 136 public final void install() 137 { 138 install(new NullPrintStream(), new NullPrintStream()); 139 } 140 141 142 143 155 public void uninstall(boolean flush, boolean check) 156 { 157 AntX.verify_(wasInstalled(),IAM_,"uninstall- installed"); 158 if (flush) { 159 try { 160 System.out.flush(); 161 System.err.flush(); 162 } catch(Exception iox) {} 163 } 164 if (check) { 165 if (System.out!=m_newOut || System.err!=m_newErr) { 166 String error = AntX.uistrs().get("fixture.stdio.outof.sync"); 167 throw new BuildError(error); 168 } 169 } 170 System.setErr(m_oldErr); 171 System.setOut(m_oldOut); 172 clear(); 173 } 174 175 176 177 182 public final void uninstall() 183 { 184 uninstall(true,true); 185 } 186 187 188 189 192 public final boolean wasInstalled() 193 { 194 return m_oldOut!=null && m_oldErr!=null; 195 } 196 197 198 199 200 204 public final OutputStream getOurSystemOut() 205 { 206 return m_newOut0; 207 } 208 209 210 214 public final OutputStream getOurSystemErr() 215 { 216 return m_newErr0; 217 } 218 219 220 221 225 public final PrintStream getReplacedSystemOut() 226 { 227 return m_oldOut; 228 } 229 230 231 235 public final PrintStream getReplacedSystemErr() 236 { 237 return m_oldErr; 238 } 239 240 241 242 243 246 private void clear() 247 { 248 m_newOut = m_newErr = null; 249 m_oldOut = m_oldErr = null; 250 m_newOut0 = m_newErr0 = null; 251 } 252 253 254 private PrintStream m_newOut, m_newErr; 255 private OutputStream m_newOut0, m_newErr0; private PrintStream m_oldOut, m_oldErr; 257 } 258 259 260 | Popular Tags |