273
|
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.
|
307
|
12 *
|
273
|
13 * @author Christian Plattner
|
|
14 * @version 2.50, 03/15/10
|
|
15 */
|
307
|
16 public final class ChannelOutputStream extends OutputStream {
|
|
17 Channel c;
|
273
|
18
|
307
|
19 boolean isClosed = false;
|
|
20
|
|
21 ChannelOutputStream(Channel c) {
|
|
22 this.c = c;
|
|
23 }
|
273
|
24
|
307
|
25 @Override
|
|
26 public void write(int b) throws IOException {
|
|
27 byte[] buff = new byte[1];
|
|
28 buff[0] = (byte) b;
|
|
29 write(buff, 0, 1);
|
|
30 }
|
273
|
31
|
307
|
32 @Override
|
|
33 public void close() throws IOException {
|
|
34 if (isClosed == false) {
|
|
35 isClosed = true;
|
|
36 c.cm.sendEOF(c);
|
|
37 }
|
|
38 }
|
273
|
39
|
307
|
40 @Override
|
|
41 public void flush() throws IOException {
|
|
42 if (isClosed)
|
|
43 throw new IOException("This OutputStream is closed.");
|
273
|
44
|
307
|
45 /* This is a no-op, since this stream is unbuffered */
|
|
46 }
|
|
47
|
|
48 @Override
|
|
49 public void write(byte[] b, int off, int len) throws IOException {
|
|
50 if (isClosed)
|
|
51 throw new IOException("This OutputStream is closed.");
|
273
|
52
|
307
|
53 if (b == null)
|
|
54 throw new NullPointerException();
|
273
|
55
|
307
|
56 if ((off < 0) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0) || (off > b.length))
|
|
57 throw new IndexOutOfBoundsException();
|
|
58
|
|
59 if (len == 0)
|
|
60 return;
|
273
|
61
|
307
|
62 c.cm.sendData(c, b, off, len);
|
|
63 }
|
273
|
64
|
307
|
65 @Override
|
|
66 public void write(byte[] b) throws IOException {
|
|
67 write(b, 0, b.length);
|
|
68 }
|
273
|
69 }
|