Home | History | Annotate | Download | only in channel
      1 /*
      2  * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
      3  * Please refer to the LICENSE.txt for licensing details.
      4  */
      5 package ch.ethz.ssh2.channel;
      6 
      7 import java.io.IOException;
      8 import java.io.OutputStream;
      9 
     10 /**
     11  * ChannelOutputStream.
     12  *
     13  * @author Christian Plattner
     14  * @version 2.50, 03/15/10
     15  */
     16 public final class ChannelOutputStream extends OutputStream
     17 {
     18 	Channel c;
     19 
     20 	boolean isClosed = false;
     21 
     22 	ChannelOutputStream(Channel c)
     23 	{
     24 		this.c = c;
     25 	}
     26 
     27 	@Override
     28 	public void write(int b) throws IOException
     29 	{
     30 		byte[] buff = new byte[1];
     31 
     32 		buff[0] = (byte) b;
     33 
     34 		write(buff, 0, 1);
     35 	}
     36 
     37 	@Override
     38 	public void close() throws IOException
     39 	{
     40 		if (isClosed == false)
     41 		{
     42 			isClosed = true;
     43 			c.cm.sendEOF(c);
     44 		}
     45 	}
     46 
     47 	@Override
     48 	public void flush() throws IOException
     49 	{
     50 		if (isClosed)
     51 			throw new IOException("This OutputStream is closed.");
     52 
     53 		/* This is a no-op, since this stream is unbuffered */
     54 	}
     55 
     56 	@Override
     57 	public void write(byte[] b, int off, int len) throws IOException
     58 	{
     59 		if (isClosed)
     60 			throw new IOException("This OutputStream is closed.");
     61 
     62 		if (b == null)
     63 			throw new NullPointerException();
     64 
     65 		if ((off < 0) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0) || (off > b.length))
     66 			throw new IndexOutOfBoundsException();
     67 
     68 		if (len == 0)
     69 			return;
     70 
     71 		c.cm.sendData(c, b, off, len);
     72 	}
     73 
     74 	@Override
     75 	public void write(byte[] b) throws IOException
     76 	{
     77 		write(b, 0, b.length);
     78 	}
     79 }
     80