/* PipedOutputStream.cs
   Converted from java for bareFTP by Christian Eide
   Original Copyright follows
 */

/* PipedOutputStream.java -- Write portion of piped streams.
   Copyright (C) 1998, 2000, 2001, 2003 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
 
GNU Classpath 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
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


using System;
using System.IO;

namespace bareFTP.Protocol.Sftp.Streams
{
	
	
	public class PipedOutputStream : Stream
	{
		public PipedInputStream sink;
  
		/** Set to true if close() has been called on this OutputStream. */
		public bool closed;
		
		public PipedOutputStream()
		{
		}
		
		public PipedOutputStream(PipedInputStream sink)
		{
			sink.connect(this);
		}
		
		public void connect(PipedInputStream sink)
		{
			if (this.sink != null || sink.source != null)
				throw new IOException ("Already connected");
			sink.connect(this);
		}
			
		///////////////////////////////////////
		
		public override int Read(byte[] buffer, int offset, int count)
		{
			return 0;
		}

		public override int ReadByte()
		{
			return 0;
		}
		
		public override void WriteByte(byte value)
		{
			if (sink == null)
				throw new IOException ("Not connected");
			if (closed)
				throw new IOException ("Pipe closed");
      
			sink.receive (value);
		}

		public override void Write(byte[] buffer, int offset, int count)
		{
			if (sink == null)
				throw new IOException ("Not connected");
			if (closed)
				throw new IOException ("Pipe closed");
      
			sink.receive(buffer, offset, count);
		}
		public virtual void Write(byte[] buffer)
		{
			this.Write(buffer, 0, buffer.Length);
		}
		public override void Close()
		{
			base.Close ();
			// A close call on an unconnected PipedOutputStream has no effect.
			if (sink != null)
			{
				closed = true;
				// Notify any waiting readers that the stream is now closed.
				lock (sink)
				{	  
					System.Threading.Monitor.PulseAll(sink);
				}
			}
		}
		public override bool CanRead
		{
			get
			{
				return false;
			}
		}
		public override bool CanWrite
		{
			get
			{
				return true;
			}
		}
		public override bool CanSeek
		{
			get
			{
				return false;
			}
		}
		public override void Flush()
		{
			
		}
		public override long Length
		{
			get
			{
				return sink.Length;
			}
		}
		public override long Position
		{
			get
			{
				return sink.m_in;
			}
			set
			{
				throw new IOException("Setting the position of this stream is not supported");
			}
		}
		public override void SetLength(long value)
		{
			throw new IOException("Setting the length of this stream is not supported");
		}
		public override long Seek(long offset, SeekOrigin origin)
		{
			return 0;
		}

	}
}

