001/*
002$Source: v:/cvsroot/open/projects/WebARTS/ca/bc/webarts/widgets/StreamPump.java,v $
003$Name:  $
004
005$Revision: 567 $
006$Date: 2012-11-03 20:36:02 -0700 (Sat, 03 Nov 2012) $
007$Locker:  $
008*/
009
010/* StreamPump -- Pipes An InputStream Into an OutputStream and pumps
011 *               the data through it.
012 *
013 * Written by Tom Gutwin - WebARTS Design.
014 * Copyright (C) 2001 WebARTS Design, North Vancouver Canada
015 * http://www.webarts.bc.ca
016 *
017 * This program is free software; you can redistribute it and/or modify
018 * it under the terms of the GNU General Public License as published by
019 * the Free Software Foundation; either version 2 of the License, or
020 * (at your option) any later version.
021 *
022 * This program is distributed in the hope that it will be useful,
023 * but WITHOUT ANY WARRANTY; without even the implied warranty of
024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
025 * GNU General Public License for more details.
026 *
027 * You should have received a copy of the GNU General Public License
028 * along with this program; if not, write to the Free Software
029 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
030 */
031
032package ca.bc.webarts.widgets;
033
034import java.io.*;
035
036/**
037 * A class to pipe the data from the specified InputStream <code>in</code> to
038 * the specified OutputStream <code>out</code>.
039 * <P>
040 * It can be called in a blocking (default) or non-blocking fashion, and can
041 * optionally automatically close the output stream when it is done (default).
042 * Transfer block size is also configurable.
043 * <P>
044 * <B><U>Example Usage</U></B><BR>
045 * <PRE>
046 * InputStream myInput = new InputStream();
047 *
048 *
049 *
050 * </pre>
051 * @author    Tom Gutwin P.Eng
052 * @created   October 4, 2001
053 **/
054
055public class StreamPump implements Runnable
056{
057  /**
058   * A class copy of the InputStream to get data from.
059   */
060  private InputStream in;
061  /**
062   * The class copy of the OutputStream to pipe in's data to.
063   */
064  private OutputStream out;
065  /**
066   * A configurable field that flags the closing of the outputStream when done.
067   * default = true  which means out will be closed.
068   */
069  private boolean autoShutOff = true;
070  /**
071   * The block size of the chunk to dump. Compromise between
072   * BufferedInputStream's 2048 and BufferedOutputStream's 512?
073   */
074  private int blockSize = 1024;
075  /**
076   * A class holder for any exceptions that are thrown.
077   */
078  private Exception exception;
079
080
081  /**
082   * Constructor for the StreamPump object
083   */
084  public StreamPump() { }
085
086
087  /**
088   * Constructor for the StreamPump object
089   *
090   * @param input   Description of Parameter
091   * @param output  Description of Parameter
092   */
093  public StreamPump(InputStream input, OutputStream output)
094  {
095    in = input;
096    out = output;
097  }
098
099
100  /**
101   * Constructor for the StreamPump object
102   *
103   * @param input    Description of Parameter
104   * @param output   Description of Parameter
105   * @param shutoff  Description of Parameter
106   */
107  public StreamPump(InputStream input, OutputStream output, boolean shutoff)
108  {
109    in = input;
110    out = output;
111    autoShutOff = shutoff;
112  }
113
114
115  /**
116   * Sets the InputStream attribute of the StreamPump object
117   *
118   * @param i  The new Input value
119   */
120  public void setInputStream(InputStream i)
121  {
122    in = i;
123  }
124
125
126  /**
127   * Sets the OutputStream attribute of the StreamPump object
128   *
129   * @param o  The new Output value
130   */
131  public void setOutputStream(OutputStream o)
132  {
133    out = o;
134  }
135
136
137  /**
138   * Sets the BlockSize attribute of the StreamPump object
139   *
140   * @param i  The new BlockSize value
141   */
142  public void setBlockSize(int i)
143  {
144    blockSize = i;
145  }
146
147
148  /**
149   * Gets the InputStream attribute of the StreamPump object
150   *
151   * @return   The Input value
152   */
153  public InputStream getInputStream()
154  {
155    return in;
156  }
157
158
159  /**
160   * Gets the OutputStream attribute of the StreamPump object
161   *
162   * @return   The Output value
163   */
164  public OutputStream getOutputStream()
165  {
166    return out;
167  }
168
169
170  /**
171   * Gets the BlockSize attribute of the StreamPump object
172   *
173   * @return   The BlockSize value
174   */
175  public int getBlockSize()
176  {
177    return blockSize;
178  }
179
180
181  /**
182   * Gets the Exception attribute of the StreamPump object
183   *
184   * @return   The Exception value
185   */
186  public Exception getException()
187  {
188    return exception;
189  }
190
191
192  /**
193   * Starts pumping the data through the Pipe.
194   *
195   * @return   The newly created Thread that is running the StreamPump
196   */
197  public synchronized Thread start()
198  {
199    Thread t = new Thread(this);
200    t.start();
201    return t;
202  }
203
204
205  /**
206   * Main processing method for the StreamPump object. It reads from in and then
207   * writes it to out. Toooo Easy
208   */
209  public void run()
210  {
211    final BufferedInputStream bis;
212    final BufferedOutputStream bos;
213    final boolean shutoff;
214    final int blksz;
215    try
216    {
217      synchronized (this)
218      {
219        bis = new BufferedInputStream(in);
220        bos = new BufferedOutputStream(out);
221        shutoff = autoShutOff;
222        blksz = blockSize;
223      }
224
225      byte[] buf = new byte[blksz];
226      int bytesread = bis.read(buf);
227      while (bytesread != -1)
228      {
229        bos.write(buf, 0, bytesread);
230        bytesread = bis.read(buf);
231      }
232      bis.close();
233      bos.flush();
234      if (shutoff)
235      {
236        bos.close();
237      }
238    }
239    catch (IOException ioe)
240    {
241      synchronized (this)
242      {
243        exception = ioe;
244      }
245    }
246  }
247}
248
249