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    *      https://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      /**
40       * The number of bytes transferred during the write that triggered the CopyStreamEvent.
41       */
42      private final int bytesTransferred;
43  
44      /**
45       * The total number of bytes transferred so far during a copy operation.
46       */
47      private final long totalBytesTransferred;
48  
49      /**
50       * The number of bytes in the stream being copied. This may be set to {@code UNKNOWN_STREAM_SIZE} if the size is unknown.
51       */
52      private final long streamSize;
53  
54      /**
55       * Constructs a new instance.
56       *
57       * @param source                The source of the event.
58       * @param totalBytesTransferred The total number of bytes transferred so far during a copy operation.
59       * @param bytesTransferred      The number of bytes transferred during the write that triggered the CopyStreamEvent.
60       * @param streamSize            The number of bytes in the stream being copied. This may be set to {@code UNKNOWN_STREAM_SIZE} if the size is unknown.
61       */
62      public CopyStreamEvent(final Object source, final long totalBytesTransferred, final int bytesTransferred, final long streamSize) {
63          super(source);
64          this.bytesTransferred = bytesTransferred;
65          this.totalBytesTransferred = totalBytesTransferred;
66          this.streamSize = streamSize;
67      }
68  
69      /**
70       * Gets the number of bytes transferred by the write that triggered the event.
71       *
72       * @return The number of bytes transferred by the write that triggered the vent.
73       */
74      public int getBytesTransferred() {
75          return bytesTransferred;
76      }
77  
78      /**
79       * Gets the size of the stream being copied. This may be set to {@code UNKNOWN_STREAM_SIZE} if the size is unknown.
80       *
81       * @return The size of the stream being copied.
82       */
83      public long getStreamSize() {
84          return streamSize;
85      }
86  
87      /**
88       * Gets the total number of bytes transferred so far by the copy operation.
89       *
90       * @return The total number of bytes transferred so far by the copy operation.
91       */
92      public long getTotalBytesTransferred() {
93          return totalBytesTransferred;
94      }
95  
96      /**
97       * @since 3.0
98       */
99      @Override
100     public String toString() {
101         return getClass().getName() + "[source=" + source + ", total=" + totalBytesTransferred + ", bytes=" + bytesTransferred + ", size=" + streamSize + "]";
102     }
103 }