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.EventListener; 021 022import org.apache.commons.net.util.ListenerList; 023 024/** 025 * The CopyStreamAdapter will relay CopyStreamEvents to a list of listeners 026 * when either of its bytesTransferred() methods are called. Its purpose 027 * is to facilitate the notification of the progress of a copy operation 028 * performed by one of the static copyStream() methods in 029 * org.apache.commons.io.Util to multiple listeners. The static 030 * copyStream() methods invoke the 031 * bytesTransfered(long, int) of a CopyStreamListener for performance 032 * reasons and also because multiple listeners cannot be registered given 033 * that the methods are static. 034 * 035 * 036 * @see CopyStreamEvent 037 * @see CopyStreamListener 038 * @see Util 039 * @version $Id: CopyStreamAdapter.java 1741829 2016-05-01 00:24:44Z sebb $ 040 */ 041public class CopyStreamAdapter implements CopyStreamListener 042{ 043 private final ListenerList internalListeners; 044 045 /** 046 * Creates a new copyStreamAdapter. 047 */ 048 public CopyStreamAdapter() 049 { 050 internalListeners = new ListenerList(); 051 } 052 053 /** 054 * This method is invoked by a CopyStreamEvent source after copying 055 * a block of bytes from a stream. The CopyStreamEvent will contain 056 * the total number of bytes transferred so far and the number of bytes 057 * transferred in the last write. The CopyStreamAdapater will relay 058 * the event to all of its registered listeners, listing itself as the 059 * source of the event. 060 * @param event The CopyStreamEvent fired by the copying of a block of 061 * bytes. 062 */ 063 @Override 064 public void bytesTransferred(CopyStreamEvent event) 065 { 066 for (EventListener listener : internalListeners) 067 { 068 ((CopyStreamListener) (listener)).bytesTransferred(event); 069 } 070 } 071 072 /** 073 * This method is not part of the JavaBeans model and is used by the 074 * static methods in the org.apache.commons.io.Util class for efficiency. 075 * It is invoked after a block of bytes to inform the listener of the 076 * transfer. The CopyStreamAdapater will create a CopyStreamEvent 077 * from the arguments and relay the event to all of its registered 078 * listeners, listing itself as the source of the event. 079 * @param totalBytesTransferred The total number of bytes transferred 080 * so far by the copy operation. 081 * @param bytesTransferred The number of bytes copied by the most recent 082 * write. 083 * @param streamSize The number of bytes in the stream being copied. 084 * This may be equal to CopyStreamEvent.UNKNOWN_STREAM_SIZE if 085 * the size is unknown. 086 */ 087 @Override 088 public void bytesTransferred(long totalBytesTransferred, 089 int bytesTransferred, long streamSize) 090 { 091 for (EventListener listener : internalListeners) 092 { 093 ((CopyStreamListener) (listener)).bytesTransferred( 094 totalBytesTransferred, bytesTransferred, streamSize); 095 } 096 } 097 098 /** 099 * Registers a CopyStreamListener to receive CopyStreamEvents. 100 * Although this method is not declared to be synchronized, it is 101 * implemented in a thread safe manner. 102 * @param listener The CopyStreamlistener to register. 103 */ 104 public void addCopyStreamListener(CopyStreamListener listener) 105 { 106 internalListeners.addListener(listener); 107 } 108 109 /** 110 * Unregisters a CopyStreamListener. Although this method is not 111 * synchronized, it is implemented in a thread safe manner. 112 * @param listener The CopyStreamlistener to unregister. 113 */ 114 public void removeCopyStreamListener(CopyStreamListener listener) 115 { 116 internalListeners.removeListener(listener); 117 } 118}