View Javadoc

1   /*
2    * Copyright 2004-2007 the original author or authors.
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.kuali.maven.wagon;
17  
18  import java.io.File;
19  import java.io.FileNotFoundException;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  
23  /**
24   * An extension to the {@link FileOutputStream} that notifies
25   * a @{link TransferProgress} object as it is being written to.
26   *
27   * @author Ben Hale
28   * @since 1.1
29   */
30  class TransferProgressFileOutputStream extends FileOutputStream {
31  
32      private TransferProgress progress;
33  
34      public TransferProgressFileOutputStream(File file, TransferProgress progress) throws FileNotFoundException {
35          super(file);
36          this.progress = progress;
37      }
38  
39      public void write(int b) throws IOException {
40          super.write(b);
41          progress.notify(new byte[]{(byte) b}, 1);
42      }
43  
44      public void write(byte b[]) throws IOException {
45          super.write(b);
46          progress.notify(b, b.length);
47      }
48  
49      public void write(byte b[], int off, int len) throws IOException {
50          super.write(b, off, len);
51          if (off == 0) {
52              progress.notify(b, len);
53          } else {
54              byte[] bytes = new byte[len];
55              System.arraycopy(b, off, bytes, 0, len);
56              progress.notify(bytes, len);
57          }
58      }
59  }