001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.net.io;
019
020import java.util.EventObject;
021
022/**
023 * A CopyStreamEvent is triggered after every write performed by a
024 * stream copying operation.  The event stores the number of bytes
025 * transferred by the write triggering the event as well as the total
026 * number of bytes transferred so far by the copy operation.
027 *
028 *
029 * @see CopyStreamListener
030 * @see CopyStreamAdapter
031 * @see Util
032 * @version $Id: CopyStreamEvent.java 1652801 2015-01-18 17:10:05Z sebb $
033 */
034public class CopyStreamEvent extends EventObject
035{
036    private static final long serialVersionUID = -964927635655051867L;
037
038    /**
039     * Constant used to indicate the stream size is unknown.
040     */
041    public static final long UNKNOWN_STREAM_SIZE = -1;
042
043    private final int bytesTransferred;
044    private final long totalBytesTransferred;
045    private final long streamSize;
046
047    /**
048     * Creates a new CopyStreamEvent instance.
049     * @param source  The source of the event.
050     * @param totalBytesTransferred The total number of bytes transferred so
051     *   far during a copy operation.
052     * @param bytesTransferred  The number of bytes transferred during the
053     *        write that triggered the CopyStreamEvent.
054     * @param streamSize  The number of bytes in the stream being copied.
055     *          This may be set to <code>UNKNOWN_STREAM_SIZE</code> if the
056     *          size is unknown.
057     */
058    public CopyStreamEvent(Object source, long totalBytesTransferred,
059                           int bytesTransferred, long streamSize)
060    {
061        super(source);
062        this.bytesTransferred = bytesTransferred;
063        this.totalBytesTransferred = totalBytesTransferred;
064        this.streamSize = streamSize;
065    }
066
067    /**
068     * Returns the number of bytes transferred by the write that triggered
069     * the event.
070     * @return The number of bytes transferred by the write that triggered
071     * the vent.
072     */
073    public int getBytesTransferred()
074    {
075        return bytesTransferred;
076    }
077
078    /**
079     * Returns the total number of bytes transferred so far by the copy
080     * operation.
081     * @return The total number of bytes transferred so far by the copy
082     * operation.
083     */
084    public long getTotalBytesTransferred()
085    {
086        return totalBytesTransferred;
087    }
088
089    /**
090     * Returns the size of the stream being copied.
091     * This may be set to <code>UNKNOWN_STREAM_SIZE</code> if the
092     * size is unknown.
093     * @return The size of the stream being copied.
094     */
095    public long getStreamSize()
096    {
097        return streamSize;
098    }
099
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}