CopyStreamEvent.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.EventObject;

  19. /**
  20.  * A CopyStreamEvent is triggered after every write performed by a stream copying operation. The event stores the number of bytes transferred by the write
  21.  * triggering the event as well as the total number of bytes transferred so far by the copy operation.
  22.  *
  23.  *
  24.  * @see CopyStreamListener
  25.  * @see CopyStreamAdapter
  26.  * @see Util
  27.  */
  28. public class CopyStreamEvent extends EventObject {
  29.     private static final long serialVersionUID = -964927635655051867L;

  30.     /**
  31.      * Constant used to indicate the stream size is unknown.
  32.      */
  33.     public static final long UNKNOWN_STREAM_SIZE = -1;

  34.     private final int bytesTransferred;
  35.     private final long totalBytesTransferred;
  36.     private final long streamSize;

  37.     /**
  38.      * Creates a new CopyStreamEvent instance.
  39.      *
  40.      * @param source                The source of the event.
  41.      * @param totalBytesTransferred The total number of bytes transferred so far during a copy operation.
  42.      * @param bytesTransferred      The number of bytes transferred during the write that triggered the CopyStreamEvent.
  43.      * @param streamSize            The number of bytes in the stream being copied. This may be set to <code>UNKNOWN_STREAM_SIZE</code> if the size is unknown.
  44.      */
  45.     public CopyStreamEvent(final Object source, final long totalBytesTransferred, final int bytesTransferred, final long streamSize) {
  46.         super(source);
  47.         this.bytesTransferred = bytesTransferred;
  48.         this.totalBytesTransferred = totalBytesTransferred;
  49.         this.streamSize = streamSize;
  50.     }

  51.     /**
  52.      * Returns the number of bytes transferred by the write that triggered the event.
  53.      *
  54.      * @return The number of bytes transferred by the write that triggered the vent.
  55.      */
  56.     public int getBytesTransferred() {
  57.         return bytesTransferred;
  58.     }

  59.     /**
  60.      * Returns the size of the stream being copied. This may be set to <code>UNKNOWN_STREAM_SIZE</code> if the size is unknown.
  61.      *
  62.      * @return The size of the stream being copied.
  63.      */
  64.     public long getStreamSize() {
  65.         return streamSize;
  66.     }

  67.     /**
  68.      * Returns the total number of bytes transferred so far by the copy operation.
  69.      *
  70.      * @return The total number of bytes transferred so far by the copy operation.
  71.      */
  72.     public long getTotalBytesTransferred() {
  73.         return totalBytesTransferred;
  74.     }

  75.     /**
  76.      * @since 3.0
  77.      */
  78.     @Override
  79.     public String toString() {
  80.         return getClass().getName() + "[source=" + source + ", total=" + totalBytesTransferred + ", bytes=" + bytesTransferred + ", size=" + streamSize + "]";
  81.     }
  82. }