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