/*
** Module   : DataSource.java
** Abstract : provides a read/write stream-oriented interface to the session
**
** Copyright (c) 2008-2009, Golden Code Development Corporation.
**
** -#- -I- --Date-- -----------------------Description------------------------
** 001 GES 20081218 Provides a read/write stream-oriented interface to the
**                  session.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.goldencode.harness.terminal;

import java.io.*;
import de.mud.jta.*;

/**
 * Provides a read/write stream-oriented interface to the session.
 * <p>
 * This plugin is not designed to "stack" properly in JTA terms. Rather it
 * is designed to match the JTA <code>FilterPlugin</code> interface and to
 * use an externally provided pair of input/output streams for its data
 * source/sink. 
 */
public class DataSource
implements FilterPlugin
{
   /** The data source from which to read. */
   private final InputStream is;
   
   /** The data sink to which to write. */
   private final OutputStream os;
   
   /**
    * Create an instance, setting the source and sink for the plugin to the
    * given streams.
    *
    * @param    is
    *           Data source.
    * @param    os
    *           Data sink.
    */
   public DataSource(InputStream is, OutputStream os)
   {
      this.is = is;
      this.os = os;
   }
   
   /**
    * Set the source plugin where we get our data from and where the data
    * sink (write) is. The actual data handling should be done in the
    * {@link #read} and {@link #write} methods.
    *
    * @param    source
    *           The data source.
    */
   public void setFilterSource(FilterPlugin source) 
   throws IllegalArgumentException
   {
      // nothing to do
   }
 
   /**
    * Return the configured source plugin.
    *
    * @return   Always <code>null</code>.
    */
   public FilterPlugin getFilterSource()
   {
      return null;
   }
 
   /**
    * Read a block of data from the back end.
    *
    * @param    b
    *           The buffer to read the data into.
    *
    * @return   The amount of bytes actually read.
    */
   public int read(byte[] b)
   throws IOException
   {
      return is.read(b);
   }
 
   /**
    * Write a block of data to the back end.
    *
    * @param    b
    *           The buffer to be sent.
    */
   public void write(byte[] b)
   throws IOException
   {
      os.write(b);
      os.flush();
   }
}
