001 /*
002 * Copyright 2001-2005 The Apache Software Foundation
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.apache.commons.net.io;
017
018 import java.util.Enumeration;
019 import org.apache.commons.net.util.ListenerList;
020
021 /**
022 * The CopyStreamAdapter will relay CopyStreamEvents to a list of listeners
023 * when either of its bytesTransferred() methods are called. Its purpose
024 * is to facilitate the notification of the progress of a copy operation
025 * performed by one of the static copyStream() methods in
026 * org.apache.commons.io.Util to multiple listeners. The static
027 * copyStream() methods invoke the
028 * bytesTransfered(long, int) of a CopyStreamListener for performance
029 * reasons and also because multiple listeners cannot be registered given
030 * that the methods are static.
031 * <p>
032 * <p>
033 * @see CopyStreamEvent
034 * @see CopyStreamListener
035 * @see Util
036 * @author <a href="mailto:savarese@apache.org">Daniel F. Savarese</a>
037 * @version $Id: CopyStreamAdapter.java 165675 2005-05-02 20:09:55Z rwinston $
038 */
039 public class CopyStreamAdapter implements CopyStreamListener
040 {
041 private ListenerList internalListeners;
042
043 /**
044 * Creates a new copyStreamAdapter.
045 */
046 public CopyStreamAdapter()
047 {
048 internalListeners = new ListenerList();
049 }
050
051 /**
052 * This method is invoked by a CopyStreamEvent source after copying
053 * a block of bytes from a stream. The CopyStreamEvent will contain
054 * the total number of bytes transferred so far and the number of bytes
055 * transferred in the last write. The CopyStreamAdapater will relay
056 * the event to all of its registered listeners, listing itself as the
057 * source of the event.
058 * @param event The CopyStreamEvent fired by the copying of a block of
059 * bytes.
060 */
061 public void bytesTransferred(CopyStreamEvent event)
062 {
063 bytesTransferred(event.getTotalBytesTransferred(),
064 event.getBytesTransferred(),
065 event.getStreamSize());
066 }
067
068 /**
069 * This method is not part of the JavaBeans model and is used by the
070 * static methods in the org.apache.commons.io.Util class for efficiency.
071 * It is invoked after a block of bytes to inform the listener of the
072 * transfer. The CopyStreamAdapater will create a CopyStreamEvent
073 * from the arguments and relay the event to all of its registered
074 * listeners, listing itself as the source of the event.
075 * @param totalBytesTransferred The total number of bytes transferred
076 * so far by the copy operation.
077 * @param bytesTransferred The number of bytes copied by the most recent
078 * write.
079 * @param streamSize The number of bytes in the stream being copied.
080 * This may be equal to CopyStreamEvent.UNKNOWN_STREAM_SIZE if
081 * the size is unknown.
082 */
083 public void bytesTransferred(long totalBytesTransferred,
084 int bytesTransferred, long streamSize)
085 {
086 Enumeration listeners;
087 CopyStreamEvent event;
088
089 listeners = internalListeners.getListeners();
090
091 event = new CopyStreamEvent(this,
092 totalBytesTransferred,
093 bytesTransferred,
094 streamSize);
095
096 while (listeners.hasMoreElements())
097 {
098 ((CopyStreamListener) (listeners.nextElement())).
099 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 }