1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.net.io; 19 20 import java.util.EventListener; 21 22 import org.apache.commons.net.util.ListenerList; 23 24 /** 25 * The CopyStreamAdapter will relay CopyStreamEvents to a list of listeners 26 * when either of its bytesTransferred() methods are called. Its purpose 27 * is to facilitate the notification of the progress of a copy operation 28 * performed by one of the static copyStream() methods in 29 * org.apache.commons.io.Util to multiple listeners. The static 30 * copyStream() methods invoke the 31 * bytesTransfered(long, int) of a CopyStreamListener for performance 32 * reasons and also because multiple listeners cannot be registered given 33 * that the methods are static. 34 * <p> 35 * <p> 36 * @see CopyStreamEvent 37 * @see CopyStreamListener 38 * @see Util 39 * @version $Id: CopyStreamAdapter.java 1489361 2013-06-04 09:48:36Z sebb $ 40 */ 41 public class CopyStreamAdapter implements CopyStreamListener 42 { 43 private final ListenerList internalListeners; 44 45 /** 46 * Creates a new copyStreamAdapter. 47 */ 48 public CopyStreamAdapter() 49 { 50 internalListeners = new ListenerList(); 51 } 52 53 /** 54 * This method is invoked by a CopyStreamEvent source after copying 55 * a block of bytes from a stream. The CopyStreamEvent will contain 56 * the total number of bytes transferred so far and the number of bytes 57 * transferred in the last write. The CopyStreamAdapater will relay 58 * the event to all of its registered listeners, listing itself as the 59 * source of the event. 60 * @param event The CopyStreamEvent fired by the copying of a block of 61 * bytes. 62 */ 63 // @Override 64 public void bytesTransferred(CopyStreamEvent event) 65 { 66 for (EventListener listener : internalListeners) 67 { 68 ((CopyStreamListener) (listener)).bytesTransferred(event); 69 } 70 } 71 72 /** 73 * This method is not part of the JavaBeans model and is used by the 74 * static methods in the org.apache.commons.io.Util class for efficiency. 75 * It is invoked after a block of bytes to inform the listener of the 76 * transfer. The CopyStreamAdapater will create a CopyStreamEvent 77 * from the arguments and relay the event to all of its registered 78 * listeners, listing itself as the source of the event. 79 * @param totalBytesTransferred The total number of bytes transferred 80 * so far by the copy operation. 81 * @param bytesTransferred The number of bytes copied by the most recent 82 * write. 83 * @param streamSize The number of bytes in the stream being copied. 84 * This may be equal to CopyStreamEvent.UNKNOWN_STREAM_SIZE if 85 * the size is unknown. 86 */ 87 // @Override 88 public void bytesTransferred(long totalBytesTransferred, 89 int bytesTransferred, long streamSize) 90 { 91 for (EventListener listener : internalListeners) 92 { 93 ((CopyStreamListener) (listener)).bytesTransferred( 94 totalBytesTransferred, bytesTransferred, streamSize); 95 } 96 } 97 98 /** 99 * 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 }