View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/filestore/common/src/java/org/astrogrid/filestore/common/transfer/TransferUtil.java,v $</cvs:source>
3    * <cvs:author>$Author: clq2 $</cvs:author>
4    * <cvs:date>$Date: 2005/01/28 10:44:00 $</cvs:date>
5    * <cvs:version>$Revision: 1.3 $</cvs:version>
6    * <cvs:log>
7    *   $Log: TransferUtil.java,v $
8    *   Revision 1.3  2005/01/28 10:44:00  clq2
9    *   dave_dev_200501141257 (filemanager)
10   *
11   *   Revision 1.2.98.1  2005/01/20 07:18:05  dave
12   *   Tided up tabs to spaces ..
13   *
14   *   Revision 1.2  2004/07/21 18:11:55  dave
15   *   Merged development branch, dave-dev-200407201059, into HEAD
16   *
17   *   Revision 1.1.2.1  2004/07/21 16:28:16  dave
18   *   Added content properties and tests
19   *
20   * </cvs:log>
21   *
22   */
23  package org.astrogrid.filestore.common.transfer ;
24  
25  import java.io.InputStream  ;
26  import java.io.OutputStream ;
27  import java.io.IOException  ;
28  
29  /***
30   * An toolkit to transfer bytes from one source to another.
31   * Initially this runs in-line, later refactoring may extend this to run in a separate thread.
32   *
33   */
34  public class TransferUtil
35      {
36  
37      /***
38       * The size of our copy buffer (8k bytes).
39       *
40       */
41      private static final int BUFFER_SIZE = 8 * 1024 ;
42  
43      /***
44       * Public constructor.
45       *
46       */
47      public TransferUtil(InputStream in, OutputStream out)
48          {
49          this.in  = in  ;
50          this.out = out ;
51          }
52  
53      /***
54       * Our transfer buffer.
55       *
56       */
57      byte[] buffer = new byte[BUFFER_SIZE] ;
58  
59      /***
60       * Our input stream.
61       *
62       */
63      private InputStream in ;
64  
65      /***
66       * Our output stream.
67       *
68       */
69      private OutputStream out ;
70  
71      /***
72       * The number of bytes transferred so far.
73       *
74       */
75      private int total ;
76  
77      /***
78       * Access to our total.
79       *
80       */
81      public int getTotal()
82          {
83          return this.total ;
84          }
85  
86      /***
87       * Transfer data from one stream to another.
88       * @return The number fo bytes transferred.
89       *
90       */
91      public int transfer()
92          throws IOException
93          {
94          int count = 0;
95          try {
96              do {
97                  this.total += count ;
98                  this.out.write(this.buffer, 0, count);
99                  count = this.in.read(this.buffer, 0, this.buffer.length);
100                 }
101             while (count != -1);
102             }
103         finally {
104             if (null != out)
105                 {
106                 this.out.close() ;
107                 }
108             if (null != in)
109                 {
110                 this.in.close() ;
111                 }
112             }
113         return this.total ;
114         }
115     }
116