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.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.io.Reader;
  21. import java.io.Writer;
  22. /***
  23. * The Util class cannot be instantiated and stores short static convenience
  24. * methods that are often quite useful.
  25. * <p>
  26. * <p>
  27. * @see CopyStreamException
  28. * @see CopyStreamListener
  29. * @see CopyStreamAdapter
  30. * @author Daniel F. Savarese
  31. ***/
  32. public final class Util
  33. {
  34. /***
  35. * The default buffer size used by <a href="#copyStream"> copyStream </a>
  36. * and <a href="#copyReader"> copyReader </a>. It's value is 1024.
  37. ***/
  38. public static final int DEFAULT_COPY_BUFFER_SIZE = 1024;
  39. // Cannot be instantiated
  40. private Util()
  41. { }
  42. /***
  43. * Copies the contents of an InputStream to an OutputStream using a
  44. * copy buffer of a given size and notifies the provided
  45. * CopyStreamListener of the progress of the copy operation by calling
  46. * its bytesTransferred(long, int) method after each write to the
  47. * destination. If you wish to notify more than one listener you should
  48. * use a CopyStreamAdapter as the listener and register the additional
  49. * listeners with the CopyStreamAdapter.
  50. * <p>
  51. * The contents of the InputStream are
  52. * read until the end of the stream is reached, but neither the
  53. * source nor the destination are closed. You must do this yourself
  54. * outside of the method call. The number of bytes read/written is
  55. * returned.
  56. * <p>
  57. * @param source The source InputStream.
  58. * @param dest The destination OutputStream.
  59. * @param bufferSize The number of bytes to buffer during the copy.
  60. * @param streamSize The number of bytes in the stream being copied.
  61. * Should be set to CopyStreamEvent.UNKNOWN_STREAM_SIZE if unknown.
  62. * @param listener The CopyStreamListener to notify of progress. If
  63. * this parameter is null, notification is not attempted.
  64. * @exception CopyStreamException If an error occurs while reading from the
  65. * source or writing to the destination. The CopyStreamException
  66. * will contain the number of bytes confirmed to have been
  67. * transferred before an
  68. * IOException occurred, and it will also contain the IOException
  69. * that caused the error. These values can be retrieved with
  70. * the CopyStreamException getTotalBytesTransferred() and
  71. * getIOException() methods.
  72. ***/
  73. public static final long copyStream(InputStream source, OutputStream dest,
  74. int bufferSize, long streamSize,
  75. CopyStreamListener listener)
  76. throws CopyStreamException
  77. {
  78. int bytes;
  79. long total;
  80. byte[] buffer;
  81. buffer = new byte[bufferSize];
  82. total = 0;
  83. try
  84. {
  85. while ((bytes = source.read(buffer)) != -1)
  86. {
  87. // Technically, some read(byte[]) methods may return 0 and we cannot
  88. // accept that as an indication of EOF.
  89. if (bytes == 0)
  90. {
  91. bytes = source.read();
  92. if (bytes < 0)
  93. break;
  94. dest.write(bytes);
  95. dest.flush();
  96. ++total;
  97. if (listener != null)
  98. listener.bytesTransferred(total, 1, streamSize);
  99. continue;
  100. }
  101. dest.write(buffer, 0, bytes);
  102. dest.flush();
  103. total += bytes;
  104. if (listener != null)
  105. listener.bytesTransferred(total, bytes, streamSize);
  106. }
  107. }
  108. catch (IOException e)
  109. {
  110. throw new CopyStreamException("IOException caught while copying.",
  111. total, e);
  112. }
  113. return total;
  114. }
  115. /***
  116. * Copies the contents of an InputStream to an OutputStream using a
  117. * copy buffer of a given size. The contents of the InputStream are
  118. * read until the end of the stream is reached, but neither the
  119. * source nor the destination are closed. You must do this yourself
  120. * outside of the method call. The number of bytes read/written is
  121. * returned.
  122. * <p>
  123. * @param source The source InputStream.
  124. * @param dest The destination OutputStream.
  125. * @return The number of bytes read/written in the copy operation.
  126. * @exception CopyStreamException If an error occurs while reading from the
  127. * source or writing to the destination. The CopyStreamException
  128. * will contain the number of bytes confirmed to have been
  129. * transferred before an
  130. * IOException occurred, and it will also contain the IOException
  131. * that caused the error. These values can be retrieved with
  132. * the CopyStreamException getTotalBytesTransferred() and
  133. * getIOException() methods.
  134. ***/
  135. public static final long copyStream(InputStream source, OutputStream dest,
  136. int bufferSize)
  137. throws CopyStreamException
  138. {
  139. return copyStream(source, dest, bufferSize,
  140. CopyStreamEvent.UNKNOWN_STREAM_SIZE, null);
  141. }
  142. /***
  143. * Same as <code> copyStream(source, dest, DEFAULT_COPY_BUFFER_SIZE); </code>
  144. ***/
  145. public static final long copyStream(InputStream source, OutputStream dest)
  146. throws CopyStreamException
  147. {
  148. return copyStream(source, dest, DEFAULT_COPY_BUFFER_SIZE);
  149. }
  150. /***
  151. * Copies the contents of a Reader to a Writer using a
  152. * copy buffer of a given size and notifies the provided
  153. * CopyStreamListener of the progress of the copy operation by calling
  154. * its bytesTransferred(long, int) method after each write to the
  155. * destination. If you wish to notify more than one listener you should
  156. * use a CopyStreamAdapter as the listener and register the additional
  157. * listeners with the CopyStreamAdapter.
  158. * <p>
  159. * The contents of the Reader are
  160. * read until its end is reached, but neither the source nor the
  161. * destination are closed. You must do this yourself outside of the
  162. * method call. The number of characters read/written is returned.
  163. * <p>
  164. * @param source The source Reader.
  165. * @param dest The destination writer.
  166. * @param bufferSize The number of characters to buffer during the copy.
  167. * @param streamSize The number of characters in the stream being copied.
  168. * Should be set to CopyStreamEvent.UNKNOWN_STREAM_SIZE if unknown.
  169. * @param listener The CopyStreamListener to notify of progress. If
  170. * this parameter is null, notification is not attempted.
  171. * @return The number of characters read/written in the copy operation.
  172. * @exception CopyStreamException If an error occurs while reading from the
  173. * source or writing to the destination. The CopyStreamException
  174. * will contain the number of bytes confirmed to have been
  175. * transferred before an
  176. * IOException occurred, and it will also contain the IOException
  177. * that caused the error. These values can be retrieved with
  178. * the CopyStreamException getTotalBytesTransferred() and
  179. * getIOException() methods.
  180. ***/
  181. public static final long copyReader(Reader source, Writer dest,
  182. int bufferSize, long streamSize,
  183. CopyStreamListener listener)
  184. throws CopyStreamException
  185. {
  186. int chars;
  187. long total;
  188. char[] buffer;
  189. buffer = new char[bufferSize];
  190. total = 0;
  191. try
  192. {
  193. while ((chars = source.read(buffer)) != -1)
  194. {
  195. // Technically, some read(char[]) methods may return 0 and we cannot
  196. // accept that as an indication of EOF.
  197. if (chars == 0)
  198. {
  199. chars = source.read();
  200. if (chars < 0)
  201. break;
  202. dest.write(chars);
  203. dest.flush();
  204. ++total;
  205. if (listener != null)
  206. listener.bytesTransferred(total, chars, streamSize);
  207. continue;
  208. }
  209. dest.write(buffer, 0, chars);
  210. dest.flush();
  211. total += chars;
  212. if (listener != null)
  213. listener.bytesTransferred(total, chars, streamSize);
  214. }
  215. }
  216. catch (IOException e)
  217. {
  218. throw new CopyStreamException("IOException caught while copying.",
  219. total, e);
  220. }
  221. return total;
  222. }
  223. /***
  224. * Copies the contents of a Reader to a Writer using a
  225. * copy buffer of a given size. The contents of the Reader are
  226. * read until its end is reached, but neither the source nor the
  227. * destination are closed. You must do this yourself outside of the
  228. * method call. The number of characters read/written is returned.
  229. * <p>
  230. * @param source The source Reader.
  231. * @param dest The destination writer.
  232. * @param bufferSize The number of characters to buffer during the copy.
  233. * @return The number of characters read/written in the copy operation.
  234. * @exception CopyStreamException If an error occurs while reading from the
  235. * source or writing to the destination. The CopyStreamException
  236. * will contain the number of bytes confirmed to have been
  237. * transferred before an
  238. * IOException occurred, and it will also contain the IOException
  239. * that caused the error. These values can be retrieved with
  240. * the CopyStreamException getTotalBytesTransferred() and
  241. * getIOException() methods.
  242. ***/
  243. public static final long copyReader(Reader source, Writer dest,
  244. int bufferSize)
  245. throws CopyStreamException
  246. {
  247. return copyReader(source, dest, bufferSize,
  248. CopyStreamEvent.UNKNOWN_STREAM_SIZE, null);
  249. }
  250. /***
  251. * Same as <code> copyReader(source, dest, DEFAULT_COPY_BUFFER_SIZE); </code>
  252. ***/
  253. public static final long copyReader(Reader source, Writer dest)
  254. throws CopyStreamException
  255. {
  256. return copyReader(source, dest, DEFAULT_COPY_BUFFER_SIZE);
  257. }
  258. }