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