001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.j2ssh.connection;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import java.io.IOException;
032: import java.io.OutputStream;
033:
034: /**
035: *
036: *
037: * @author $author$
038: * @version $Revision: 1.27 $
039: */
040: public class ChannelOutputStream extends OutputStream {
041: private static Log log = LogFactory
042: .getLog(ChannelOutputStream.class);
043: private Channel channel;
044: private boolean isClosed = false;
045: private Integer type = null;
046:
047: /**
048: * Creates a new ChannelOutputStream object.
049: *
050: * @param channel
051: * @param type
052: */
053: public ChannelOutputStream(Channel channel, Integer type) {
054: this .channel = channel;
055: this .type = type;
056: }
057:
058: /**
059: * Creates a new ChannelOutputStream object.
060: *
061: * @param channel
062: */
063: public ChannelOutputStream(Channel channel) {
064: this (channel, null);
065: }
066:
067: /**
068: *
069: *
070: * @return
071: */
072: public boolean isClosed() {
073: return isClosed;
074: }
075:
076: /**
077: *
078: *
079: * @throws IOException
080: */
081: public void close() throws IOException {
082: log.info("Closing ChannelOutputStream");
083: isClosed = true;
084:
085: // Send an EOF if the channel is not closed
086: if (!channel.isClosed()) {
087: channel.connection.sendChannelEOF(channel);
088: }
089: }
090:
091: /**
092: *
093: *
094: * @param b
095: * @param off
096: * @param len
097: *
098: * @throws IOException
099: */
100: public void write(byte[] b, int off, int len) throws IOException {
101: if (isClosed) {
102: throw new IOException("The ChannelOutputStream is closed!");
103: }
104:
105: byte[] data = null;
106:
107: if ((off > 0) || (len < b.length)) {
108: data = new byte[len];
109: System.arraycopy(b, off, data, 0, len);
110: } else {
111: data = b;
112: }
113:
114: sendChannelData(data);
115: }
116:
117: /**
118: *
119: *
120: * @param b
121: *
122: * @throws IOException
123: */
124: public void write(int b) throws IOException {
125: if (isClosed) {
126: throw new IOException("The ChannelOutputStream is closed!");
127: }
128:
129: byte[] data = new byte[1];
130: data[0] = (byte) b;
131: sendChannelData(data);
132: }
133:
134: private void sendChannelData(byte[] data) throws IOException {
135: channel.sendChannelData(data);
136:
137: /* int sent = 0;
138: int block;
139: int remaining;
140: long max;
141: byte[] buffer;
142: ChannelDataWindow window = channel.getRemoteWindow();
143: while (sent < data.length) {
144: remaining = data.length - sent;
145: max = ((window.getWindowSpace() < channel.getRemotePacketSize())
146: && window.getWindowSpace() > 0)
147: ? window.getWindowSpace() : channel.getRemotePacketSize();
148: block = (max < remaining) ? (int) max : remaining;
149: channel.remoteWindow.consumeWindowSpace(block);
150: buffer = new byte[block];
151: System.arraycopy(data, sent, buffer, 0, block);
152: if (type != null) {
153: channel.sendChannelExtData(type.intValue(), buffer);
154: } else {
155: channel.sendChannelData(buffer);
156: }
157: sent += block;
158: }*/
159: }
160: }
|