CopyStreamAdapter.java

  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. package org.apache.commons.net.io;

  18. import java.util.EventListener;

  19. import org.apache.commons.net.util.ListenerList;

  20. /**
  21.  * The CopyStreamAdapter will relay CopyStreamEvents to a list of listeners when either of its bytesTransferred() methods are called. Its purpose is to
  22.  * 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
  23.  * listeners. The static copyStream() methods invoke the bytesTransfered(long, int) of a CopyStreamListener for performance reasons and also because multiple
  24.  * listeners cannot be registered given that the methods are static.
  25.  *
  26.  *
  27.  * @see CopyStreamEvent
  28.  * @see CopyStreamListener
  29.  * @see Util
  30.  */
  31. public class CopyStreamAdapter implements CopyStreamListener {
  32.     private final ListenerList internalListeners;

  33.     /**
  34.      * Creates a new copyStreamAdapter.
  35.      */
  36.     public CopyStreamAdapter() {
  37.         internalListeners = new ListenerList();
  38.     }

  39.     /**
  40.      * Registers a CopyStreamListener to receive CopyStreamEvents. Although this method is not declared to be synchronized, it is implemented in a thread safe
  41.      * manner.
  42.      *
  43.      * @param listener The CopyStreamlistener to register.
  44.      */
  45.     public void addCopyStreamListener(final CopyStreamListener listener) {
  46.         internalListeners.addListener(listener);
  47.     }

  48.     /**
  49.      * 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
  50.      * 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
  51.      * listeners, listing itself as the source of the event.
  52.      *
  53.      * @param event The CopyStreamEvent fired by the copying of a block of bytes.
  54.      */
  55.     @Override
  56.     public void bytesTransferred(final CopyStreamEvent event) {
  57.         for (final EventListener listener : internalListeners) {
  58.             ((CopyStreamListener) listener).bytesTransferred(event);
  59.         }
  60.     }

  61.     /**
  62.      * 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
  63.      * after a block of bytes to inform the listener of the transfer. The CopyStreamAdapater will create a CopyStreamEvent from the arguments and relay the
  64.      * event to all of its registered listeners, listing itself as the source of the event.
  65.      *
  66.      * @param totalBytesTransferred The total number of bytes transferred so far by the copy operation.
  67.      * @param bytesTransferred      The number of bytes copied by the most recent write.
  68.      * @param streamSize            The number of bytes in the stream being copied. This may be equal to CopyStreamEvent.UNKNOWN_STREAM_SIZE if the size is
  69.      *                              unknown.
  70.      */
  71.     @Override
  72.     public void bytesTransferred(final long totalBytesTransferred, final int bytesTransferred, final long streamSize) {
  73.         for (final EventListener listener : internalListeners) {
  74.             ((CopyStreamListener) listener).bytesTransferred(totalBytesTransferred, bytesTransferred, streamSize);
  75.         }
  76.     }

  77.     /**
  78.      * Unregisters a CopyStreamListener. Although this method is not synchronized, it is implemented in a thread safe manner.
  79.      *
  80.      * @param listener The CopyStreamlistener to unregister.
  81.      */
  82.     public void removeCopyStreamListener(final CopyStreamListener listener) {
  83.         internalListeners.removeListener(listener);
  84.     }
  85. }