java开源网络包org.apache.commons.net研究(二)

本文探讨了org.apache.commons.net.io.Util.copyStream方法,该方法用于从InputStream复制内容到OutputStream,并利用CopyStreamListener监听拷贝进度。文章详细分析了方法内部实现,包括缓冲区、监听器机制,以及如何处理IOException。最后提到了接下来将研究TelnetClient的connect方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       通过顺藤摸瓜的研究,现在代码的焦点集中在写线程中的下面这个方法:
               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相关的思路。
             都一点多了,睡啦!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值