1 23 24 package com.sun.enterprise.cli.framework; 25 26 import java.io.OutputStream ; 27 import java.io.InputStream ; 28 import java.io.FileOutputStream ; 29 import java.io.FileInputStream ; 30 import java.io.IOException ; 31 32 36 public class InputsAndOutputs 37 { 38 39 40 private static InputsAndOutputs sIO = null; 41 private IUserInput userInput = new UserInput( System.in ); 42 private IUserOutput userOutputImpl = new UserOutputImpl( System.out, false ); 43 private IErrorOutput errorOutputImpl = new ErrorOutputImpl( System.err, false ); 44 45 48 private InputsAndOutputs() 49 { 50 } 51 52 53 56 public static void setInstance(InputsAndOutputs iao) 57 { 58 if (iao == null) { 59 if (sIO!=null) { 60 try { 61 sIO.userOutputImpl.close(); 62 sIO.userInput.close(); 63 } 64 catch (Exception e){ 65 e.printStackTrace(); 66 } 67 } 68 sIO = new InputsAndOutputs(); 69 return; 70 } 71 if (iao != null && sIO != iao) 72 sIO = iao; 73 } 74 75 76 79 public static InputsAndOutputs getInstance() 80 { 81 if (sIO == null) 82 { 83 sIO = new InputsAndOutputs(); 84 } 85 return sIO; 86 } 87 88 89 92 public IUserOutput getUserOutput() 93 { 94 return this.userOutputImpl; 95 } 96 97 98 104 public void setUserOutput( OutputStream os ) 105 { 106 this.userOutputImpl.close(); 107 this.userOutputImpl = new UserOutputImpl( os, false ); 108 } 109 110 111 119 public void setUserOutputFile( String fileName ) throws IOException 120 { 121 FileOutputStream userOutputFile = new FileOutputStream ( fileName ); 122 this.userOutputImpl.close(); 123 this.userOutputImpl = new UserOutputImpl( userOutputFile, true ); 124 } 125 126 127 130 public IUserInput getUserInput() 131 { 132 return this.userInput; 133 } 134 135 136 141 public void setUserInput( InputStream is ) throws IOException 142 { 143 userInput.close(); 144 this.userInput = new UserInput(is); 145 } 146 147 148 154 public void setUserInput( InputStream is, String sEncoding ) 155 throws IOException 156 { 157 userInput.close(); 158 this.userInput = new UserInput(is); 159 this.userInput.setEncoding(sEncoding); 160 } 161 162 163 168 public void setUserInput( IUserInput userInput ) 169 throws IOException 170 { 171 this.userInput.close(); 172 this.userInput = userInput; 173 } 174 175 176 182 public void setUserInputFile( String fileName ) throws IOException 183 { 184 final FileInputStream userInputFile = new FileInputStream ( fileName ); 185 setUserInput(userInputFile); 186 } 187 188 189 195 public void setUserInputFile( String fileName, String sEncoding ) 196 throws IOException 197 { 198 final FileInputStream userInputFile = new FileInputStream ( fileName ); 199 setUserInput(userInputFile, sEncoding); 200 } 201 202 203 206 public IErrorOutput getErrorOutput() 207 { 208 return this.errorOutputImpl; 209 } 210 211 212 218 public void setErrorOutput( OutputStream os ) 219 { 220 this.errorOutputImpl.close(); 221 this.errorOutputImpl = new ErrorOutputImpl( os, false ); 222 } 223 224 225 233 public void setErrorOutputFile( String fileName ) throws IOException 234 { 235 FileOutputStream errorOutputFile = new FileOutputStream ( fileName ); 236 this.errorOutputImpl.close(); 237 this.errorOutputImpl = new ErrorOutputImpl( errorOutputFile, true ); 238 } 239 240 } 241 | Popular Tags |