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 018 package org.apache.commons.net.io; 019 020 import java.util.EventListener; 021 022 import 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 * <p> 035 * <p> 036 * @see CopyStreamEvent 037 * @see CopyStreamListener 038 * @see Util 039 * @author <a href="mailto:savarese@apache.org">Daniel F. Savarese</a> 040 * @version $Id: CopyStreamAdapter.java 929649 2010-03-31 18:12:07Z sebb $ 041 */ 042 public class CopyStreamAdapter implements CopyStreamListener 043 { 044 private final ListenerList internalListeners; 045 046 /** 047 * Creates a new copyStreamAdapter. 048 */ 049 public CopyStreamAdapter() 050 { 051 internalListeners = new ListenerList(); 052 } 053 054 /** 055 * This method is invoked by a CopyStreamEvent source after copying 056 * a block of bytes from a stream. The CopyStreamEvent will contain 057 * the total number of bytes transferred so far and the number of bytes 058 * transferred in the last write. The CopyStreamAdapater will relay 059 * the event to all of its registered listeners, listing itself as the 060 * source of the event. 061 * @param event The CopyStreamEvent fired by the copying of a block of 062 * bytes. 063 */ 064 public void bytesTransferred(CopyStreamEvent event) 065 { 066 bytesTransferred(event.getTotalBytesTransferred(), 067 event.getBytesTransferred(), 068 event.getStreamSize()); 069 } 070 071 /** 072 * This method is not part of the JavaBeans model and is used by the 073 * static methods in the org.apache.commons.io.Util class for efficiency. 074 * It is invoked after a block of bytes to inform the listener of the 075 * transfer. The CopyStreamAdapater will create a CopyStreamEvent 076 * from the arguments and relay the event to all of its registered 077 * listeners, listing itself as the source of the event. 078 * @param totalBytesTransferred The total number of bytes transferred 079 * so far by the copy operation. 080 * @param bytesTransferred The number of bytes copied by the most recent 081 * write. 082 * @param streamSize The number of bytes in the stream being copied. 083 * This may be equal to CopyStreamEvent.UNKNOWN_STREAM_SIZE if 084 * the size is unknown. 085 */ 086 public void bytesTransferred(long totalBytesTransferred, 087 int bytesTransferred, long streamSize) 088 { 089 CopyStreamEvent event; 090 091 event = new CopyStreamEvent(this, 092 totalBytesTransferred, 093 bytesTransferred, 094 streamSize); 095 096 for (EventListener listener : internalListeners) 097 { 098 ((CopyStreamListener) (listener)).bytesTransferred(event); 099 } 100 } 101 102 /** 103 * Registers a CopyStreamListener to receive CopyStreamEvents. 104 * Although this method is not declared to be synchronized, it is 105 * implemented in a thread safe manner. 106 * @param listener The CopyStreamlistener to register. 107 */ 108 public void addCopyStreamListener(CopyStreamListener listener) 109 { 110 internalListeners.addListener(listener); 111 } 112 113 /** 114 * Unregisters a CopyStreamListener. Although this method is not 115 * synchronized, it is implemented in a thread safe manner. 116 * @param listener The CopyStreamlistener to unregister. 117 */ 118 public void removeCopyStreamListener(CopyStreamListener listener) 119 { 120 internalListeners.removeListener(listener); 121 } 122 }