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
24   * stream copying operation.  The event stores the number of bytes
25   * transferred by the write triggering the event as well as the total
26   * number of bytes transferred so far by the copy operation.
27   * <p>
28   * <p>
29   * @see CopyStreamListener
30   * @see CopyStreamAdapter
31   * @see Util
32   * @version $Id: CopyStreamEvent.java 1299238 2012-03-10 17:12:28Z sebb $
33   */
34  public class CopyStreamEvent extends EventObject
35  {
36      private static final long serialVersionUID = -964927635655051867L;
37  
38      /**
39       * Constant used to indicate the stream size is unknown.
40       */
41      public static final long UNKNOWN_STREAM_SIZE = -1;
42  
43      private final int bytesTransferred;
44      private final long totalBytesTransferred;
45      private final long streamSize;
46  
47      /**
48       * Creates a new CopyStreamEvent instance.
49       * @param source  The source of the event.
50       * @param totalBytesTransferred The total number of bytes transferred so
51       *   far during a copy operation.
52       * @param bytesTransferred  The number of bytes transferred during the
53       *        write that triggered the CopyStreamEvent.
54       * @param streamSize  The number of bytes in the stream being copied.
55       *          This may be set to <code>UNKNOWN_STREAM_SIZE</code> if the
56       *          size is unknown.
57       */
58      public CopyStreamEvent(Object source, long totalBytesTransferred,
59                             int bytesTransferred, long streamSize)
60      {
61          super(source);
62          this.bytesTransferred = bytesTransferred;
63          this.totalBytesTransferred = totalBytesTransferred;
64          this.streamSize = streamSize;
65      }
66  
67      /**
68       * Returns the number of bytes transferred by the write that triggered
69       * the event.
70       * @return The number of bytes transferred by the write that triggered
71       * the vent.
72       */
73      public int getBytesTransferred()
74      {
75          return bytesTransferred;
76      }
77  
78      /**
79       * Returns the total number of bytes transferred so far by the copy
80       * operation.
81       * @return The total number of bytes transferred so far by the copy
82       * operation.
83       */
84      public long getTotalBytesTransferred()
85      {
86          return totalBytesTransferred;
87      }
88  
89      /**
90       * Returns the size of the stream being copied.
91       * This may be set to <code>UNKNOWN_STREAM_SIZE</code> if the
92       * size is unknown.
93       * @return The size of the stream being copied.
94       */
95      public long getStreamSize()
96      {
97          return streamSize;
98      }
99  
100     /**
101       * @since 3.0
102      */
103     @Override
104     public String toString(){
105         return getClass().getName() + "[source=" + source
106         + ", total=" + totalBytesTransferred
107         + ", bytes=" + bytesTransferred
108         + ", size=" + streamSize
109         + "]";
110     }
111 }