    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
    018package org.apache.commons.net.io;
    019
    020import java.util.EventListener;
    021
    022import 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 *
    035 *
    036 * @see CopyStreamEvent
    037 * @see CopyStreamListener
    038 * @see Util
    039 */
    040public class CopyStreamAdapter implements CopyStreamListener
    041{
    042    private final ListenerList internalListeners;
    043
    044    /**
    045     * Creates a new copyStreamAdapter.
    046     */
    047    public CopyStreamAdapter()
    048    {
    049        internalListeners = new ListenerList();
    050    }
    051
    052    /**
    053     * This method is invoked by a CopyStreamEvent source after copying
    054     * a block of bytes from a stream.  The CopyStreamEvent will contain
    055     * the total number of bytes transferred so far and the number of bytes
    056     * transferred in the last write.  The CopyStreamAdapater will relay
    057     * the event to all of its registered listeners, listing itself as the
    058     * source of the event.
    059     * @param event The CopyStreamEvent fired by the copying of a block of
    060     *              bytes.
    061     */
    062    @Override
    063    public void bytesTransferred(final CopyStreamEvent event)
    064    {
    065        for (final 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    @Override
    087    public void bytesTransferred(final long totalBytesTransferred,
    088                                 final int bytesTransferred, final long streamSize)
    089    {
    090        for (final EventListener listener : internalListeners)
    091        {
    092            ((CopyStreamListener) listener).bytesTransferred(
    093                    totalBytesTransferred, bytesTransferred, streamSize);
    094        }
    095    }
    096
    097    /**
    098     * Registers a CopyStreamListener to receive CopyStreamEvents.
    099     * Although this method is not declared to be synchronized, it is
    100     * implemented in a thread safe manner.
    101     * @param listener  The CopyStreamlistener to register.
    102     */
    103    public void addCopyStreamListener(final CopyStreamListener listener)
    104    {
    105        internalListeners.addListener(listener);
    106    }
    107
    108    /**
    109     * Unregisters a CopyStreamListener.  Although this method is not
    110     * synchronized, it is implemented in a thread safe manner.
    111     * @param listener  The CopyStreamlistener to unregister.
    112     */
    113    public void removeCopyStreamListener(final CopyStreamListener listener)
    114    {
    115        internalListeners.removeListener(listener);
    116    }
    117}