001 /**
002 * Copyright 2010-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.maven.wagon;
017
018 import java.io.File;
019 import java.io.FileInputStream;
020 import java.io.FileNotFoundException;
021 import java.io.IOException;
022
023 import com.amazonaws.services.s3.internal.RepeatableFileInputStream;
024
025 /**
026 * An extension to the {@link FileInputStream} that notifies a @{link TransferProgress} object as it is being read from
027 *
028 * @author Ben Hale
029 * @since 1.1
030 */
031 public class TransferProgressFileInputStream extends RepeatableFileInputStream {
032
033 private TransferProgress progress;
034
035 public TransferProgressFileInputStream(File file, TransferProgress progress) throws FileNotFoundException {
036 super(file);
037 this.progress = progress;
038 }
039
040 @Override
041 public int read() throws IOException {
042 int b = super.read();
043 if (b != -1) {
044 progress.notify(new byte[] { (byte) b }, 1);
045 }
046 return b;
047 }
048
049 @Override
050 public int read(byte b[]) throws IOException {
051 int length = super.read(b);
052 if (length != -1) {
053 progress.notify(b, length);
054 }
055 return length;
056 }
057
058 @Override
059 public int read(byte b[], int off, int len) throws IOException {
060 int count = super.read(b, off, len);
061 if (count == -1) {
062 return count;
063 }
064 if (off == 0) {
065 progress.notify(b, count);
066 } else {
067 byte[] bytes = new byte[len];
068 System.arraycopy(b, off, bytes, 0, count);
069 progress.notify(bytes, count);
070 }
071 return count;
072 }
073 }