View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.net.io;
19  
20  import java.util.EventListener;
21  
22  import org.apache.commons.net.util.ListenerList;
23  
24  /**
25   * The CopyStreamAdapter will relay CopyStreamEvents to a list of listeners
26   * when either of its bytesTransferred() methods are called.  Its purpose
27   * is to facilitate the notification of the progress of a copy operation
28   * performed by one of the static copyStream() methods in
29   * org.apache.commons.io.Util to multiple listeners.  The static
30   * copyStream() methods invoke the
31   * bytesTransfered(long, int) of a CopyStreamListener for performance
32   * reasons and also because multiple listeners cannot be registered given
33   * that the methods are static.
34   * <p>
35   * <p>
36   * @see CopyStreamEvent
37   * @see CopyStreamListener
38   * @see Util
39   * @author <a href="mailto:savarese@apache.org">Daniel F. Savarese</a>
40   * @version $Id: CopyStreamAdapter.java 489397 2006-12-21 16:28:51Z rwinston $
41   */
42  public class CopyStreamAdapter implements CopyStreamListener
43  {
44      private ListenerList internalListeners;
45  
46      /**
47       * Creates a new copyStreamAdapter.
48       */
49      public CopyStreamAdapter()
50      {
51          internalListeners = new ListenerList();
52      }
53  
54      /**
55       * This method is invoked by a CopyStreamEvent source after copying
56       * a block of bytes from a stream.  The CopyStreamEvent will contain
57       * the total number of bytes transferred so far and the number of bytes
58       * transferred in the last write.  The CopyStreamAdapater will relay
59       * the event to all of its registered listeners, listing itself as the
60       * source of the event.
61       * @param event The CopyStreamEvent fired by the copying of a block of
62       *              bytes.
63       */
64      public void bytesTransferred(CopyStreamEvent event)
65      {
66          bytesTransferred(event.getTotalBytesTransferred(),
67                           event.getBytesTransferred(),
68                           event.getStreamSize());
69      }
70  
71      /**
72       * This method is not part of the JavaBeans model and is used by the
73       * static methods in the org.apache.commons.io.Util class for efficiency.
74       * It is invoked after a block of bytes to inform the listener of the
75       * transfer.  The CopyStreamAdapater will create a CopyStreamEvent
76       * from the arguments and relay the event to all of its registered
77       * listeners, listing itself as the source of the event.
78       * @param totalBytesTransferred  The total number of bytes transferred
79       *         so far by the copy operation.
80       * @param bytesTransferred  The number of bytes copied by the most recent
81       *          write.
82       * @param streamSize The number of bytes in the stream being copied.
83       *        This may be equal to CopyStreamEvent.UNKNOWN_STREAM_SIZE if
84       *        the size is unknown.
85       */
86      public void bytesTransferred(long totalBytesTransferred,
87                                   int bytesTransferred, long streamSize)
88      {
89          CopyStreamEvent event;
90  
91          event = new CopyStreamEvent(this,
92                                      totalBytesTransferred,
93                                      bytesTransferred,
94                                      streamSize);
95  
96          for (EventListener listener : internalListeners)
97          {
98              ((CopyStreamListener) (listener)).bytesTransferred(event);
99          }
100     }
101 
102     /**
103      * Registers a CopyStreamListener to receive CopyStreamEvents.
104      * Although this method is not declared to be synchronized, it is
105      * implemented in a thread safe manner.
106      * @param listener  The CopyStreamlistener to register.
107      */
108     public void addCopyStreamListener(CopyStreamListener listener)
109     {
110         internalListeners.addListener(listener);
111     }
112 
113     /**
114      * Unregisters a CopyStreamListener.  Although this method is not
115      * synchronized, it is implemented in a thread safe manner.
116      * @param listener  The CopyStreamlistener to unregister.
117      */
118     public void removeCopyStreamListener(CopyStreamListener listener)
119     {
120         internalListeners.removeListener(listener);
121     }
122 }