1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.net.io;
  17. import java.util.Enumeration;
  18. import org.apache.commons.net.util.ListenerList;
  19. /**
  20. * The CopyStreamAdapter will relay CopyStreamEvents to a list of listeners
  21. * when either of its bytesTransferred() methods are called. Its purpose
  22. * is to facilitate the notification of the progress of a copy operation
  23. * performed by one of the static copyStream() methods in
  24. * org.apache.commons.io.Util to multiple listeners. The static
  25. * copyStream() methods invoke the
  26. * bytesTransfered(long, int) of a CopyStreamListener for performance
  27. * reasons and also because multiple listeners cannot be registered given
  28. * that the methods are static.
  29. * <p>
  30. * <p>
  31. * @see CopyStreamEvent
  32. * @see CopyStreamListener
  33. * @see Util
  34. * @author <a href="mailto:savarese@apache.org">Daniel F. Savarese</a>
  35. * @version $Id: CopyStreamAdapter.java,v 1.9 2004/02/29 10:26:55 scolebourne Exp $
  36. */
  37. public class CopyStreamAdapter implements CopyStreamListener
  38. {
  39. private ListenerList internalListeners;
  40. /**
  41. * Creates a new copyStreamAdapter.
  42. */
  43. public CopyStreamAdapter()
  44. {
  45. internalListeners = new ListenerList();
  46. }
  47. /**
  48. * This method is invoked by a CopyStreamEvent source after copying
  49. * a block of bytes from a stream. The CopyStreamEvent will contain
  50. * the total number of bytes transferred so far and the number of bytes
  51. * transferred in the last write. The CopyStreamAdapater will relay
  52. * the event to all of its registered listeners, listing itself as the
  53. * source of the event.
  54. * @param event The CopyStreamEvent fired by the copying of a block of
  55. * bytes.
  56. */
  57. public void bytesTransferred(CopyStreamEvent event)
  58. {
  59. bytesTransferred(event.getTotalBytesTransferred(),
  60. event.getBytesTransferred(),
  61. event.getStreamSize());
  62. }
  63. /**
  64. * This method is not part of the JavaBeans model and is used by the
  65. * static methods in the org.apache.commons.io.Util class for efficiency.
  66. * It is invoked after a block of bytes to inform the listener of the
  67. * transfer. The CopyStreamAdapater will create a CopyStreamEvent
  68. * from the arguments and relay the event to all of its registered
  69. * listeners, listing itself as the source of the event.
  70. * @param totalBytesTransferred The total number of bytes transferred
  71. * so far by the copy operation.
  72. * @param bytesTransferred The number of bytes copied by the most recent
  73. * write.
  74. * @param streamSize The number of bytes in the stream being copied.
  75. * This may be equal to CopyStreamEvent.UNKNOWN_STREAM_SIZE if
  76. * the size is unknown.
  77. */
  78. public void bytesTransferred(long totalBytesTransferred,
  79. int bytesTransferred, long streamSize)
  80. {
  81. Enumeration listeners;
  82. CopyStreamEvent event;
  83. listeners = internalListeners.getListeners();
  84. event = new CopyStreamEvent(this,
  85. totalBytesTransferred,
  86. bytesTransferred,
  87. streamSize);
  88. while (listeners.hasMoreElements())
  89. {
  90. ((CopyStreamListener) (listeners.nextElement())).
  91. bytesTransferred(event);
  92. }
  93. }
  94. /**
  95. * Registers a CopyStreamListener to receive CopyStreamEvents.
  96. * Although this method is not declared to be synchronized, it is
  97. * implemented in a thread safe manner.
  98. * @param listener The CopyStreamlistener to register.
  99. */
  100. public void addCopyStreamListener(CopyStreamListener listener)
  101. {
  102. internalListeners.addListener(listener);
  103. }
  104. /**
  105. * Unregisters a CopyStreamListener. Although this method is not
  106. * synchronized, it is implemented in a thread safe manner.
  107. * @param listener The CopyStreamlistener to unregister.
  108. */
  109. public void removeCopyStreamListener(CopyStreamListener listener)
  110. {
  111. internalListeners.removeListener(listener);
  112. }
  113. }