通过顺藤摸瓜的研究,现在代码的焦点集中在写线程中的下面这个方法:
org.apache.commons.net.io.Util.copyStream(remoteInput, localOutput);
再往里面跟踪,发现Util里面有几个copyStream的重载方法,归根结底,最终调用的是这个方法:
/***************************************************************************
* Copies the contents of an InputStream to an OutputStream using a copy
* buffer of a given size and notifies the provided CopyStreamListener of
* the progress of the copy operation by calling its bytesTransferred(long,
* int) method after each write to the destination. If you wish to notify
* more than one listener you should use a CopyStreamAdapter as the listener
* and register the additional listeners with the CopyStreamAdapter.
* <p>
* The contents of the InputStream are read until the end of the stream is
* reached, but neither the source nor the destination are closed. You must
* do this yourself outside of the method call. The number of bytes
* read/written is returned.
* <p>
*
* @param source
* The source InputStream.
* @param dest
* The destination OutputStream.
* @param bufferSize
* The number of bytes to buffer during the copy.
* @param streamSize
* The number of bytes in the stream being copied. Should be set
* to CopyStreamEvent.UNKNOWN_STREAM_SIZE if unknown.
* @param listener
* The CopyStreamListener to notify of progress. If this
* parameter is null, notification is not attempted.
* @param flush
* Whether to flush the output stream after every write. This is
* necessary for interactive sessions that rely on buffered
* streams. If you don't flush, the data will stay in the stream
* buffer.
* @exception CopyStreamException
* If an error occurs while reading from the source or
* writing to the destination. The CopyStreamException will
* contain the number of bytes confirmed to have been
* transferred before an IOException occurred, and it will
* also contain the IOException that caused the error. These
* values can be retrieved with the CopyStreamException
* getTotalBytesTransferred() and getIOException() methods.
**************************************************************************/
public static final long copyStream(InputStream source, OutputStream dest,
int bufferSize, long streamSize, CopyStreamListener listener,
boolean flush) throws CopyStreamException {
int bytes;
long total;
byte[] buffer;

buffer = new byte[bufferSize];
total = 0;

try {
while ((bytes = source.read(buffer)) != -1) {
// Technically, some read(byte[]) methods may return 0 and we
// cannot
// accept that as an indication of EOF.

if (bytes == 0) {
bytes = source.read();
if (bytes < 0)
break;
dest.write(bytes);
if (flush)
dest.flush();
++total;
if (listener != null)
listener.bytesTransferred(total, 1, streamSize);
continue;
}

dest.write(buffer, 0, bytes);
if(flush)
dest.flush();
total += bytes;
if(listener != null)
listener.bytesTransferred(total , bytes, streamSize);
}
} catch (IOException e) {
throw new CopyStreamException("IOException caught while copying.",
total, e);
}

return total;
}
该方法除了实现了把数据从输入流拷贝的输出流的功能之外,还有增强功能,那就是利用监听器(Listener)的机制,来监控拷贝数据时的状况。
java中的监听器,我还没用过,顺便研究一下。通过查看代码,我发现,监听机制需要这个几个组件:
1 Listener
2 Adapter
3 Event
4 ListenerList
具体到这个网络包,是这样表现的:
public interface CopyStreamListener extends EventListener
public class CopyStreamAdapter implements CopyStreamListener
public class ListenerList implements Serializable
public class CopyStreamEvent extends EventObject
研究了监听器,接下来研究什么呢?对了,把TelnetClient的connect方法给忘了。下一篇文章开始记录研究TelnetClient相关的思路。
都一点多了,睡啦!
org.apache.commons.net.io.Util.copyStream(remoteInput, localOutput);
再往里面跟踪,发现Util里面有几个copyStream的重载方法,归根结底,最终调用的是这个方法:



















































































java中的监听器,我还没用过,顺便研究一下。通过查看代码,我发现,监听机制需要这个几个组件:
1 Listener
2 Adapter
3 Event
4 ListenerList
具体到这个网络包,是这样表现的:
public interface CopyStreamListener extends EventListener
public class CopyStreamAdapter implements CopyStreamListener
public class ListenerList implements Serializable
public class CopyStreamEvent extends EventObject
研究了监听器,接下来研究什么呢?对了,把TelnetClient的connect方法给忘了。下一篇文章开始记录研究TelnetClient相关的思路。
都一点多了,睡啦!