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 */
033public class CopyStreamEvent extends EventObject
034{
035    private static final long serialVersionUID = -964927635655051867L;
036
037    /**
038     * Constant used to indicate the stream size is unknown.
039     */
040    public static final long UNKNOWN_STREAM_SIZE = -1;
041
042    private final int bytesTransferred;
043    private final long totalBytesTransferred;
044    private final long streamSize;
045
046    /**
047     * Creates a new CopyStreamEvent instance.
048     * @param source  The source of the event.
049     * @param totalBytesTransferred The total number of bytes transferred so
050     *   far during a copy operation.
051     * @param bytesTransferred  The number of bytes transferred during the
052     *        write that triggered the CopyStreamEvent.
053     * @param streamSize  The number of bytes in the stream being copied.
054     *          This may be set to <code>UNKNOWN_STREAM_SIZE</code> if the
055     *          size is unknown.
056     */
057    public CopyStreamEvent(final Object source, final long totalBytesTransferred,
058                           final int bytesTransferred, final long streamSize)
059    {
060        super(source);
061        this.bytesTransferred = bytesTransferred;
062        this.totalBytesTransferred = totalBytesTransferred;
063        this.streamSize = streamSize;
064    }
065
066    /**
067     * Returns the number of bytes transferred by the write that triggered
068     * the event.
069     * @return The number of bytes transferred by the write that triggered
070     * the vent.
071     */
072    public int getBytesTransferred()
073    {
074        return bytesTransferred;
075    }
076
077    /**
078     * Returns the total number of bytes transferred so far by the copy
079     * operation.
080     * @return The total number of bytes transferred so far by the copy
081     * operation.
082     */
083    public long getTotalBytesTransferred()
084    {
085        return totalBytesTransferred;
086    }
087
088    /**
089     * Returns the size of the stream being copied.
090     * This may be set to <code>UNKNOWN_STREAM_SIZE</code> if the
091     * size is unknown.
092     * @return The size of the stream being copied.
093     */
094    public long getStreamSize()
095    {
096        return streamSize;
097    }
098
099    /**
100      * @since 3.0
101     */
102    @Override
103    public String toString(){
104        return getClass().getName() + "[source=" + source
105        + ", total=" + totalBytesTransferred
106        + ", bytes=" + bytesTransferred
107        + ", size=" + streamSize
108        + "]";
109    }
110}