KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > net > io > CopyStreamAdapter


1 /*
2  * Copyright 2001-2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.net.io;
17
18 import java.util.Enumeration JavaDoc;
19 import org.apache.commons.net.util.ListenerList;
20
21 /**
22  * The CopyStreamAdapter will relay CopyStreamEvents to a list of listeners
23  * when either of its bytesTransferred() methods are called. Its purpose
24  * is to facilitate the notification of the progress of a copy operation
25  * performed by one of the static copyStream() methods in
26  * org.apache.commons.io.Util to multiple listeners. The static
27  * copyStream() methods invoke the
28  * bytesTransfered(long, int) of a CopyStreamListener for performance
29  * reasons and also because multiple listeners cannot be registered given
30  * that the methods are static.
31  * <p>
32  * <p>
33  * @see CopyStreamEvent
34  * @see CopyStreamListener
35  * @see Util
36  * @author <a HREF="mailto:savarese@apache.org">Daniel F. Savarese</a>
37  * @version $Id: CopyStreamAdapter.java 165675 2005-05-02 20:09:55Z rwinston $
38  */

39 public class CopyStreamAdapter implements CopyStreamListener
40 {
41     private ListenerList internalListeners;
42
43     /**
44      * Creates a new copyStreamAdapter.
45      */

46     public CopyStreamAdapter()
47     {
48         internalListeners = new ListenerList();
49     }
50
51     /**
52      * This method is invoked by a CopyStreamEvent source after copying
53      * a block of bytes from a stream. The CopyStreamEvent will contain
54      * the total number of bytes transferred so far and the number of bytes
55      * transferred in the last write. The CopyStreamAdapater will relay
56      * the event to all of its registered listeners, listing itself as the
57      * source of the event.
58      * @param event The CopyStreamEvent fired by the copying of a block of
59      * bytes.
60      */

61     public void bytesTransferred(CopyStreamEvent event)
62     {
63         bytesTransferred(event.getTotalBytesTransferred(),
64                          event.getBytesTransferred(),
65                          event.getStreamSize());
66     }
67
68     /**
69      * This method is not part of the JavaBeans model and is used by the
70      * static methods in the org.apache.commons.io.Util class for efficiency.
71      * It is invoked after a block of bytes to inform the listener of the
72      * transfer. The CopyStreamAdapater will create a CopyStreamEvent
73      * from the arguments and relay the event to all of its registered
74      * listeners, listing itself as the source of the event.
75      * @param totalBytesTransferred The total number of bytes transferred
76      * so far by the copy operation.
77      * @param bytesTransferred The number of bytes copied by the most recent
78      * write.
79      * @param streamSize The number of bytes in the stream being copied.
80      * This may be equal to CopyStreamEvent.UNKNOWN_STREAM_SIZE if
81      * the size is unknown.
82      */

83     public void bytesTransferred(long totalBytesTransferred,
84                                  int bytesTransferred, long streamSize)
85     {
86         Enumeration JavaDoc listeners;
87         CopyStreamEvent event;
88
89         listeners = internalListeners.getListeners();
90
91         event = new CopyStreamEvent(this,
92                                     totalBytesTransferred,
93                                     bytesTransferred,
94                                     streamSize);
95
96         while (listeners.hasMoreElements())
97         {
98             ((CopyStreamListener) (listeners.nextElement())).
99                 bytesTransferred(event);
100         }
101     }
102
103     /**
104      * Registers a CopyStreamListener to receive CopyStreamEvents.
105      * Although this method is not declared to be synchronized, it is
106      * implemented in a thread safe manner.
107      * @param listener The CopyStreamlistener to register.
108      */

109     public void addCopyStreamListener(CopyStreamListener listener)
110     {
111         internalListeners.addListener(listener);
112     }
113
114     /**
115      * Unregisters a CopyStreamListener. Although this method is not
116      * synchronized, it is implemented in a thread safe manner.
117      * @param listener The CopyStreamlistener to unregister.
118      */

119     public void removeCopyStreamListener(CopyStreamListener listener)
120     {
121         internalListeners.removeListener(listener);
122     }
123 }
124
Popular Tags