dirkv       02/01/11 05:24:45

  Added:       src/webdav/client/src/org/apache/webdav/cmd Spool.java
  Log:
  helper class to spool in/out streams to a file
  
  Revision  Changes    Path
  1.1                  
jakarta-slide/src/webdav/client/src/org/apache/webdav/cmd/Spool.java
  
  Index: Spool.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/cmd/Spool.java,v 1.1 
2002/01/11 13:24:45 dirkv Exp $
   * $Revision: 1.1 $
   * $Date: 2002/01/11 13:24:45 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
   
  package org.apache.webdav.cmd;
  
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.FileNotFoundException;
  
  /**
   * This class provides wrappers around an InputStream and 
   * an OutputStream allowing you to spool both streams to a
   * shared OutputStream. 
   *
   * @author Dirk Verbeeck
   */
  public class Spool 
  {
        private InputStream  in   = System.in;
        private OutputStream out  = System.out;
        private OutputStream spool= null;
  
        private InputStream  wrappedInputStream;
        private OutputStream wrappedOutputStream;
        
        private boolean echo=false;
  
        /**
         * Create a spool for these two streams
         */
        public Spool(InputStream in, OutputStream out) 
        {
                this();
                this.in=in;
                this.out=out;
        }
  
        /**
         * Create a spool for System.in and System.out
         */
        public Spool() 
        {
                wrappedInputStream = new InputStream() 
                {
                    public int read(byte b[]) throws IOException {
                                int bytesRead = in.read(b);
                                if (echo)
                                        out.write(b,0,bytesRead);
                                if (spool!=null)
                                        spool.write(b,0,bytesRead);
                                return bytesRead;
                    }
                    public int read(byte b[], int off, int len) throws IOException {
                                int bytesRead = in.read(b,off,len);
                                if (echo)
                                        out.write(b,off,bytesRead);
                                if (spool!=null)
                                        spool.write(b,off,bytesRead);
                            return bytesRead;
                    }
                        public int read() throws IOException {
                                int nextByte = in.read();
                                if ((nextByte!=-1) && (echo))
                                        out.write(nextByte);
                                if ((nextByte!=-1) && (spool!=null))
                                        spool.write(nextByte);
                                return nextByte;
                        }
                    public long skip(long n) throws IOException {
                                return in.skip(n);
                    }
                    public int available() throws IOException {
                                return in.available();
                    }
                    public void close() throws IOException {
                                in.close();
                        }
                    public synchronized void mark(int readlimit) {
                                in.mark(readlimit);
                        }
                    public synchronized void reset() throws IOException {
                                in.reset();
                    }
                    public boolean markSupported() {
                                return in.markSupported();
                    }
                };
                wrappedOutputStream = new OutputStream() 
                {
                        public void write(int b) throws IOException {
                                out.write(b);
                                if (spool!=null)
                                        spool.write(b);
                        }
                        public void write(byte b[]) throws IOException {
                                out.write(b);
                                if (spool!=null)
                                        spool.write(b);
                        }
                        public void write(byte b[], int off, int len) throws 
IOException {
                                out.write(b,off,len);
                                if (spool!=null)
                                        spool.write(b,off,len);
                        }
                        public void flush() throws IOException {
                                out.flush();
                                if (spool!=null)
                                        spool.flush();
                        }
                        public void close() throws IOException {
                                out.close();
                        }
                };
        }
  
        /**
         * Echo the input stream to the output stream.
         */     
        public void setEcho(boolean isEnabled)
        {
                this.echo=isEnabled;
        }
        
        /**
         * Enable spooling and spool to the given filename.
         */
        public void enable(String filename) throws FileNotFoundException
        {
                enable(new FileOutputStream(filename));
        }
  
        /**
         * Enable Spooling and spool to the given OutputStream.
         */
        public void enable(OutputStream spool)
        {
                if (isEnabled())
                        disable();
                this.spool = spool;
        }
        
        /**
         * Disable spooling
         */
        public void disable() 
        {
                try 
                {
                        if (spool!=null) 
                                spool.close();
                }
                catch (IOException ex) 
                {
                }
                spool=null;     
        }
        
        /**
         * Returns if spooling is enabled.
         */
        public boolean isEnabled()
        {
                return (spool!=null);
        }
        
        /**
         * Returns an InputStream wrapped around the real InputStream.
         * Use this stream to read from.
         */
        public InputStream getInputStream() 
        {
                return wrappedInputStream;
        }
        
        /**
         * Returns an OutputStream wrapped around the real OutputStream.
         * Use this stream to write to.
         */
        public OutputStream getOutputStream() 
        {
                return wrappedOutputStream;
        }
  }
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to