1 23 24 package org.apache.webdav.cmd; 25 26 import java.io.FileNotFoundException ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 32 38 public class Spool 39 { 40 private InputStream in = System.in; 41 private OutputStream out = System.out; 42 private OutputStream spool= null; 43 44 private InputStream wrappedInputStream; 45 private OutputStream wrappedOutputStream; 46 47 private boolean echo=false; 48 49 52 public Spool(InputStream in, OutputStream out) 53 { 54 this(); 55 this.in=in; 56 this.out=out; 57 } 58 59 62 public Spool() 63 { 64 wrappedInputStream = new InputStream () 65 { 66 public int read(byte b[]) throws IOException { 67 int bytesRead = in.read(b); 68 if (echo) 69 out.write(b,0,bytesRead); 70 if (spool!=null) 71 spool.write(b,0,bytesRead); 72 return bytesRead; 73 } 74 public int read(byte b[], int off, int len) throws IOException { 75 int bytesRead = in.read(b,off,len); 76 if (echo) 77 out.write(b,off,bytesRead); 78 if (spool!=null) 79 spool.write(b,off,bytesRead); 80 return bytesRead; 81 } 82 public int read() throws IOException { 83 int nextByte = in.read(); 84 if ((nextByte!=-1) && (echo)) 85 out.write(nextByte); 86 if ((nextByte!=-1) && (spool!=null)) 87 spool.write(nextByte); 88 return nextByte; 89 } 90 public long skip(long n) throws IOException { 91 return in.skip(n); 92 } 93 public int available() throws IOException { 94 return in.available(); 95 } 96 public void close() throws IOException { 97 in.close(); 98 } 99 public synchronized void mark(int readlimit) { 100 in.mark(readlimit); 101 } 102 public synchronized void reset() throws IOException { 103 in.reset(); 104 } 105 public boolean markSupported() { 106 return in.markSupported(); 107 } 108 }; 109 wrappedOutputStream = new OutputStream () 110 { 111 public void write(int b) throws IOException { 112 out.write(b); 113 if (spool!=null) 114 spool.write(b); 115 } 116 public void write(byte b[]) throws IOException { 117 out.write(b); 118 if (spool!=null) 119 spool.write(b); 120 } 121 public void write(byte b[], int off, int len) throws IOException { 122 out.write(b,off,len); 123 if (spool!=null) 124 spool.write(b,off,len); 125 } 126 public void flush() throws IOException { 127 out.flush(); 128 if (spool!=null) 129 spool.flush(); 130 } 131 public void close() throws IOException { 132 out.close(); 133 } 134 }; 135 } 136 137 140 public void setEcho(boolean isEnabled) 141 { 142 this.echo=isEnabled; 143 } 144 145 148 public void enable(String filename) throws FileNotFoundException 149 { 150 enable(new FileOutputStream (filename)); 151 } 152 153 156 public void enable(OutputStream spool) 157 { 158 if (isEnabled()) 159 disable(); 160 this.spool = spool; 161 } 162 163 166 public void disable() 167 { 168 try 169 { 170 if (spool!=null) 171 spool.close(); 172 } 173 catch (IOException ex) 174 { 175 } 176 spool=null; 177 } 178 179 182 public boolean isEnabled() 183 { 184 return (spool!=null); 185 } 186 187 191 public InputStream getInputStream() 192 { 193 return wrappedInputStream; 194 } 195 196 200 public OutputStream getOutputStream() 201 { 202 return wrappedOutputStream; 203 } 204 } 205 | Popular Tags |