1 package org.apache.commons.net.io;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Commons" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.util.Enumeration;
58 import org.apache.commons.net.util.ListenerList;
59
60 /***
61 * The CopyStreamAdapter will relay CopyStreamEvents to a list of listeners
62 * when either of its bytesTransferred() methods are called. Its purpose
63 * is to facilitate the notification of the progress of a copy operation
64 * performed by one of the static copyStream() methods in
65 * org.apache.commons.io.Util to multiple listeners. The static
66 * copyStream() methods invoke the
67 * bytesTransfered(long, int) of a CopyStreamListener for performance
68 * reasons and also because multiple listeners cannot be registered given
69 * that the methods are static.
70 * <p>
71 * <p>
72 * @see CopyStreamEvent
73 * @see CopyStreamListener
74 * @see Util
75 * @author <a href="mailto:savarese@apache.org">Daniel F. Savarese</a>
76 * @version $Id: CopyStreamAdapter.java,v 1.6 2003/01/26 04:33:32 brekke Exp $
77 */
78 public class CopyStreamAdapter implements CopyStreamListener
79 {
80 private ListenerList internalListeners;
81
82 /***
83 * Creates a new copyStreamAdapter.
84 */
85 public CopyStreamAdapter()
86 {
87 internalListeners = new ListenerList();
88 }
89
90 /***
91 * This method is invoked by a CopyStreamEvent source after copying
92 * a block of bytes from a stream. The CopyStreamEvent will contain
93 * the total number of bytes transferred so far and the number of bytes
94 * transferred in the last write. The CopyStreamAdapater will relay
95 * the event to all of its registered listeners, listing itself as the
96 * source of the event.
97 * @param event The CopyStreamEvent fired by the copying of a block of
98 * bytes.
99 */
100 public void bytesTransferred(CopyStreamEvent event)
101 {
102 bytesTransferred(event.getTotalBytesTransferred(),
103 event.getBytesTransferred(),
104 event.getStreamSize());
105 }
106
107 /***
108 * This method is not part of the JavaBeans model and is used by the
109 * static methods in the org.apache.commons.io.Util class for efficiency.
110 * It is invoked after a block of bytes to inform the listener of the
111 * transfer. The CopyStreamAdapater will create a CopyStreamEvent
112 * from the arguments and relay the event to all of its registered
113 * listeners, listing itself as the source of the event.
114 * @param totalBytesTransferred The total number of bytes transferred
115 * so far by the copy operation.
116 * @param bytesTransferred The number of bytes copied by the most recent
117 * write.
118 * @param streamSize The number of bytes in the stream being copied.
119 * This may be equal to CopyStreamEvent.UNKNOWN_STREAM_SIZE if
120 * the size is unknown.
121 */
122 public void bytesTransferred(long totalBytesTransferred,
123 int bytesTransferred, long streamSize)
124 {
125 Enumeration listeners;
126 CopyStreamEvent event;
127
128 listeners = internalListeners.getListeners();
129
130 event = new CopyStreamEvent(this,
131 totalBytesTransferred,
132 bytesTransferred,
133 streamSize);
134
135 while (listeners.hasMoreElements())
136 {
137 ((CopyStreamListener) (listeners.nextElement())).
138 bytesTransferred(event);
139 }
140 }
141
142 /***
143 * Registers a CopyStreamListener to receive CopyStreamEvents.
144 * Although this method is not declared to be synchronized, it is
145 * implemented in a thread safe manner.
146 * @param listener The CopyStreamlistener to register.
147 */
148 public void addCopyStreamListener(CopyStreamListener listener)
149 {
150 internalListeners.addListener(listener);
151 }
152
153 /***
154 * Unregisters a CopyStreamListener. Although this method is not
155 * synchronized, it is implemented in a thread safe manner.
156 * @param listener The CopyStreamlistener to unregister.
157 */
158 public void removeCopyStreamListener(CopyStreamListener listener)
159 {
160 internalListeners.removeListener(listener);
161 }
162 }
This page was automatically generated by Maven