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