    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 *      https://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 org.apache.commons.net.util.ListenerList;
    021
    022/**
    023 * The CopyStreamAdapter will relay CopyStreamEvents to a list of listeners when either of its bytesTransferred() methods are called. Its purpose is to
    024 * facilitate the notification of the progress of a copy operation performed by one of the static copyStream() methods in org.apache.commons.io.Util to multiple
    025 * listeners. The static copyStream() methods invoke the bytesTransfered(long, int) of a CopyStreamListener for performance reasons and also because multiple
    026 * listeners cannot be registered given that the methods are static.
    027 *
    028 * @see CopyStreamEvent
    029 * @see CopyStreamListener
    030 * @see Util
    031 */
    032public class CopyStreamAdapter implements CopyStreamListener {
    033
    034    private final ListenerList<CopyStreamListener> listeners;
    035
    036    /**
    037     * Creates a new CopyStreamListener.
    038     */
    039    public CopyStreamAdapter() {
    040        listeners = new ListenerList<>();
    041    }
    042
    043    /**
    044     * Adds a CopyStreamListener to receive CopyStreamEvents. Although this method is not declared to be synchronized, it is implemented in a thread safe
    045     * manner.
    046     *
    047     * @param listener The CopyStreamlistener to register.
    048     */
    049    public void addCopyStreamListener(final CopyStreamListener listener) {
    050        listeners.addListener(listener);
    051    }
    052
    053    /**
    054     * This method is invoked by a CopyStreamEvent source after copying a block of bytes from a stream. The CopyStreamEvent will contain the total number of
    055     * bytes transferred so far and the number of bytes transferred in the last write. The CopyStreamAdapater will relay the event to all of its registered
    056     * listeners, listing itself as the source of the event.
    057     *
    058     * @param event The CopyStreamEvent fired by the copying of a block of bytes.
    059     */
    060    @Override
    061    public void bytesTransferred(final CopyStreamEvent event) {
    062        listeners.forEach(listener -> listener.bytesTransferred(event));
    063    }
    064
    065    /**
    066     * This method is not part of the JavaBeans model and is used by the static methods in the org.apache.commons.io.Util class for efficiency. It is invoked
    067     * after a block of bytes to inform the listener of the transfer. The CopyStreamAdapater will create a CopyStreamEvent from the arguments and relay the
    068     * event to all of its registered listeners, listing itself as the source of the event.
    069     *
    070     * @param totalBytesTransferred The total number of bytes transferred so far by the copy operation.
    071     * @param bytesTransferred      The number of bytes copied by the most recent write.
    072     * @param streamSize            The number of bytes in the stream being copied. This may be equal to CopyStreamEvent.UNKNOWN_STREAM_SIZE if the size is
    073     *                              unknown.
    074     */
    075    @Override
    076    public void bytesTransferred(final long totalBytesTransferred, final int bytesTransferred, final long streamSize) {
    077        listeners.forEach(listener -> listener.bytesTransferred(totalBytesTransferred, bytesTransferred, streamSize));
    078    }
    079
    080    /**
    081     * Removes a CopyStreamListener. Although this method is not synchronized, it is implemented in a thread safe manner.
    082     *
    083     * @param listener The CopyStreamlistener to unregister.
    084     */
    085    public void removeCopyStreamListener(final CopyStreamListener listener) {
    086        listeners.removeListener(listener);
    087    }
    088}