    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements.  See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache License, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License.  You may obtain a copy of the License at
    008     *
    009     *      http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the License for the specific language governing permissions and
    015     * limitations under the License.
    016     */
    017
    018    package org.apache.commons.net.io;
    019
    020    import java.util.EventListener;
    021
    022    import org.apache.commons.net.util.ListenerList;
    023
    024    /**
    025     * The CopyStreamAdapter will relay CopyStreamEvents to a list of listeners
    026     * when either of its bytesTransferred() methods are called.  Its purpose
    027     * is to facilitate the notification of the progress of a copy operation
    028     * performed by one of the static copyStream() methods in
    029     * org.apache.commons.io.Util to multiple listeners.  The static
    030     * copyStream() methods invoke the
    031     * bytesTransfered(long, int) of a CopyStreamListener for performance
    032     * reasons and also because multiple listeners cannot be registered given
    033     * that the methods are static.
    034     * <p>
    035     * <p>
    036     * @see CopyStreamEvent
    037     * @see CopyStreamListener
    038     * @see Util
    039     * @version $Id: CopyStreamAdapter.java 1088720 2011-04-04 18:52:00Z dfs $
    040     */
    041    public class CopyStreamAdapter implements CopyStreamListener
    042    {
    043        private final ListenerList internalListeners;
    044
    045        /**
    046         * Creates a new copyStreamAdapter.
    047         */
    048        public CopyStreamAdapter()
    049        {
    050            internalListeners = new ListenerList();
    051        }
    052
    053        /**
    054         * This method is invoked by a CopyStreamEvent source after copying
    055         * a block of bytes from a stream.  The CopyStreamEvent will contain
    056         * the total number of bytes transferred so far and the number of bytes
    057         * transferred in the last write.  The CopyStreamAdapater will relay
    058         * the event to all of its registered listeners, listing itself as the
    059         * source of the event.
    060         * @param event The CopyStreamEvent fired by the copying of a block of
    061         *              bytes.
    062         */
    063        public void bytesTransferred(CopyStreamEvent event)
    064        {
    065            for (EventListener listener : internalListeners)
    066            {
    067                ((CopyStreamListener) (listener)).bytesTransferred(event);
    068            }
    069        }
    070
    071        /**
    072         * This method is not part of the JavaBeans model and is used by the
    073         * static methods in the org.apache.commons.io.Util class for efficiency.
    074         * It is invoked after a block of bytes to inform the listener of the
    075         * transfer.  The CopyStreamAdapater will create a CopyStreamEvent
    076         * from the arguments and relay the event to all of its registered
    077         * listeners, listing itself as the source of the event.
    078         * @param totalBytesTransferred  The total number of bytes transferred
    079         *         so far by the copy operation.
    080         * @param bytesTransferred  The number of bytes copied by the most recent
    081         *          write.
    082         * @param streamSize The number of bytes in the stream being copied.
    083         *        This may be equal to CopyStreamEvent.UNKNOWN_STREAM_SIZE if
    084         *        the size is unknown.
    085         */
    086        public void bytesTransferred(long totalBytesTransferred,
    087                                     int bytesTransferred, long streamSize)
    088        {
    089            for (EventListener listener : internalListeners)
    090            {
    091                ((CopyStreamListener) (listener)).bytesTransferred(
    092                        totalBytesTransferred, bytesTransferred, streamSize);
    093            }
    094        }
    095
    096        /**
    097         * Registers a CopyStreamListener to receive CopyStreamEvents.
    098         * Although this method is not declared to be synchronized, it is
    099         * implemented in a thread safe manner.
    100         * @param listener  The CopyStreamlistener to register.
    101         */
    102        public void addCopyStreamListener(CopyStreamListener listener)
    103        {
    104            internalListeners.addListener(listener);
    105        }
    106
    107        /**
    108         * Unregisters a CopyStreamListener.  Although this method is not
    109         * synchronized, it is implemented in a thread safe manner.
    110         * @param listener  The CopyStreamlistener to unregister.
    111         */
    112        public void removeCopyStreamListener(CopyStreamListener listener)
    113        {
    114            internalListeners.removeListener(listener);
    115        }
    116    }