View Javadoc
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  
18  package org.apache.commons.net.io;
19  
20  import java.util.EventObject;
21  
22  /**
23   * A CopyStreamEvent is triggered after every write performed by a stream copying operation. The event stores the number of bytes transferred by the write
24   * triggering the event as well as the total number of bytes transferred so far by the copy operation.
25   *
26   *
27   * @see CopyStreamListener
28   * @see CopyStreamAdapter
29   * @see Util
30   */
31  public class CopyStreamEvent extends EventObject {
32      private static final long serialVersionUID = -964927635655051867L;
33  
34      /**
35       * Constant used to indicate the stream size is unknown.
36       */
37      public static final long UNKNOWN_STREAM_SIZE = -1;
38  
39      private final int bytesTransferred;
40      private final long totalBytesTransferred;
41      private final long streamSize;
42  
43      /**
44       * Creates a new CopyStreamEvent instance.
45       *
46       * @param source                The source of the event.
47       * @param totalBytesTransferred The total number of bytes transferred so far during a copy operation.
48       * @param bytesTransferred      The number of bytes transferred during the write that triggered the CopyStreamEvent.
49       * @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.
50       */
51      public CopyStreamEvent(final Object source, final long totalBytesTransferred, final int bytesTransferred, final long streamSize) {
52          super(source);
53          this.bytesTransferred = bytesTransferred;
54          this.totalBytesTransferred = totalBytesTransferred;
55          this.streamSize = streamSize;
56      }
57  
58      /**
59       * Returns the number of bytes transferred by the write that triggered the event.
60       *
61       * @return The number of bytes transferred by the write that triggered the vent.
62       */
63      public int getBytesTransferred() {
64          return bytesTransferred;
65      }
66  
67      /**
68       * Returns the size of the stream being copied. This may be set to <code>UNKNOWN_STREAM_SIZE</code> if the size is unknown.
69       *
70       * @return The size of the stream being copied.
71       */
72      public long getStreamSize() {
73          return streamSize;
74      }
75  
76      /**
77       * Returns the total number of bytes transferred so far by the copy operation.
78       *
79       * @return The total number of bytes transferred so far by the copy operation.
80       */
81      public long getTotalBytesTransferred() {
82          return totalBytesTransferred;
83      }
84  
85      /**
86       * @since 3.0
87       */
88      @Override
89      public String toString() {
90          return getClass().getName() + "[source=" + source + ", total=" + totalBytesTransferred + ", bytes=" + bytesTransferred + ", size=" + streamSize + "]";
91      }
92  }