001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: StreamRelay.java,v $
023   Revision 1.5  2004/05/05 21:36:35  markl
024   comment block updates
025
026   Revision 1.4  2003/01/19 09:37:12  markl
027   Javadoc & comment header updates.
028
029   Revision 1.3  2001/03/12 01:58:42  markl
030   Source code cleanup.
031
032   Revision 1.2  1999/01/10 03:34:42  markl
033   added GPL header & RCS tag
034   ----------------------------------------------------------------------------
035*/
036
037package kiwi.io;
038
039import java.io.*;
040
041import kiwi.util.KiwiUtils;
042
043/** This class represents a stream relay; it reads data from one stream and
044  * writes it to another, in a dedicated thread.
045  *
046  * @author Mark Lindner
047  */
048
049public class StreamRelay implements Runnable
050  {
051  private DataInputStream din;
052  private DataOutputStream dout;
053  private byte buf[];
054
055  /** Construct a new <code>StreamRelay</code>.
056    *
057    * @param source The data source.
058    * @param sink The data sink.
059    */
060
061  public StreamRelay(DataInputStream source, DataOutputStream sink)
062    {
063    din = source;
064    dout = sink;
065
066    buf = new byte[KiwiUtils.blockSize];
067    }
068
069  /** Thread body. Activate via a call to superclass <code>start()</code>
070    * method.
071    */
072
073  public void run()
074    {
075    int r;
076
077    out:
078
079    for(;;)
080      {
081      try
082        {
083        if((r = din.read(buf)) < 0) break;
084        dout.write(buf, 0, r);
085        }
086      catch(IOException ex)
087        {
088        break out;
089        }
090      }
091
092    try
093      {
094      din.close();
095      dout.close();
096      }
097    catch(IOException ex)
098      {
099      }
100    }
101  
102  }
103
104/* end of source file */