View Javadoc

1   /**
2    * Copyright 2010-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.FileInputStream;
20  import java.io.FileNotFoundException;
21  import java.io.IOException;
22  
23  import com.amazonaws.services.s3.internal.RepeatableFileInputStream;
24  
25  /**
26   * An extension to the {@link FileInputStream} that notifies a @{link TransferProgress} object as it is being read from
27   *
28   * @author Ben Hale
29   * @since 1.1
30   */
31  public class TransferProgressFileInputStream extends RepeatableFileInputStream {
32  
33      private TransferProgress progress;
34  
35      public TransferProgressFileInputStream(File file, TransferProgress progress) throws FileNotFoundException {
36          super(file);
37          this.progress = progress;
38      }
39  
40      @Override
41      public int read() throws IOException {
42          int b = super.read();
43          if (b != -1) {
44              progress.notify(new byte[] { (byte) b }, 1);
45          }
46          return b;
47      }
48  
49      @Override
50      public int read(byte b[]) throws IOException {
51          int length = super.read(b);
52          if (length != -1) {
53              progress.notify(b, length);
54          }
55          return length;
56      }
57  
58      @Override
59      public int read(byte b[], int off, int len) throws IOException {
60          int count = super.read(b, off, len);
61          if (count == -1) {
62              return count;
63          }
64          if (off == 0) {
65              progress.notify(b, count);
66          } else {
67              byte[] bytes = new byte[len];
68              System.arraycopy(b, off, bytes, 0, count);
69              progress.notify(bytes, count);
70          }
71          return count;
72      }
73  }