src/core/net/sf/basedb/util/ProgressInputStream.java

Code
Comments
Other
Rev Date Author Line
7168 08 Jun 16 nicklas 1 /**
7168 08 Jun 16 nicklas 2   $Id $
7168 08 Jun 16 nicklas 3
7168 08 Jun 16 nicklas 4   Copyright (C) 2016 Nicklas Nordborg
7168 08 Jun 16 nicklas 5
7168 08 Jun 16 nicklas 6   This file is part of BASE - BioArray Software Environment.
7168 08 Jun 16 nicklas 7   Available at http://base.thep.lu.se/
7168 08 Jun 16 nicklas 8
7168 08 Jun 16 nicklas 9   BASE is free software; you can redistribute it and/or
7168 08 Jun 16 nicklas 10   modify it under the terms of the GNU General Public License
7168 08 Jun 16 nicklas 11   as published by the Free Software Foundation; either version 3
7168 08 Jun 16 nicklas 12   of the License, or (at your option) any later version.
7168 08 Jun 16 nicklas 13
7168 08 Jun 16 nicklas 14   BASE is distributed in the hope that it will be useful,
7168 08 Jun 16 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
7168 08 Jun 16 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7168 08 Jun 16 nicklas 17   GNU General Public License for more details.
7168 08 Jun 16 nicklas 18
7168 08 Jun 16 nicklas 19   You should have received a copy of the GNU General Public License
7168 08 Jun 16 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
7168 08 Jun 16 nicklas 21 */
7168 08 Jun 16 nicklas 22 package net.sf.basedb.util;
7168 08 Jun 16 nicklas 23
7168 08 Jun 16 nicklas 24 import java.io.IOException;
7168 08 Jun 16 nicklas 25 import java.io.InputStream;
7168 08 Jun 16 nicklas 26
7168 08 Jun 16 nicklas 27 import net.sf.basedb.core.AbsoluteProgressReporter;
7168 08 Jun 16 nicklas 28
7168 08 Jun 16 nicklas 29 /**
7168 08 Jun 16 nicklas 30   Input stream wrapper that calls a progress reporter at regular
7168 08 Jun 16 nicklas 31   intervals. The default interval is 1MB of processed data. The
7168 08 Jun 16 nicklas 32   default progress message is simply to write the number of bytes
7168 08 Jun 16 nicklas 33   processed. If a different message is required the {@link #progressReport(String)}
7168 08 Jun 16 nicklas 34   method should be overridden.
7168 08 Jun 16 nicklas 35   
7168 08 Jun 16 nicklas 36   @author nicklas
7168 08 Jun 16 nicklas 37   @since 3.9
7168 08 Jun 16 nicklas 38 */
7168 08 Jun 16 nicklas 39 public class ProgressInputStream 
7168 08 Jun 16 nicklas 40   extends InputStreamTracker
7168 08 Jun 16 nicklas 41 {
7168 08 Jun 16 nicklas 42
7168 08 Jun 16 nicklas 43   protected final AbsoluteProgressReporter progress;
7168 08 Jun 16 nicklas 44   protected final long interval;
7168 08 Jun 16 nicklas 45   protected long nextProgress;
7168 08 Jun 16 nicklas 46   
7168 08 Jun 16 nicklas 47   /**
7168 08 Jun 16 nicklas 48     Create a new input stream using the default 1MB interval.
7168 08 Jun 16 nicklas 49   */
7168 08 Jun 16 nicklas 50   public ProgressInputStream(InputStream in, AbsoluteProgressReporter progress)
7168 08 Jun 16 nicklas 51   {
7168 08 Jun 16 nicklas 52     this(in, progress, Values.MB);
7168 08 Jun 16 nicklas 53   }
7168 08 Jun 16 nicklas 54
7168 08 Jun 16 nicklas 55   /**
7168 08 Jun 16 nicklas 56     Create a new input stream using the specified interval.
7168 08 Jun 16 nicklas 57   */
7168 08 Jun 16 nicklas 58   public ProgressInputStream(InputStream in, AbsoluteProgressReporter progress, long interval)
7168 08 Jun 16 nicklas 59   {
7168 08 Jun 16 nicklas 60     super(in);
7168 08 Jun 16 nicklas 61     this.progress = progress;
7168 08 Jun 16 nicklas 62     this.interval = interval;
7168 08 Jun 16 nicklas 63     this.nextProgress = interval;
7168 08 Jun 16 nicklas 64   }
7168 08 Jun 16 nicklas 65
7168 08 Jun 16 nicklas 66   
7168 08 Jun 16 nicklas 67   @Override
7168 08 Jun 16 nicklas 68   public int read(byte[] b, int off, int len) 
7168 08 Jun 16 nicklas 69     throws IOException 
7168 08 Jun 16 nicklas 70   {
7168 08 Jun 16 nicklas 71     int n = super.read(b, off, len);
7168 08 Jun 16 nicklas 72     if (getNumRead() > nextProgress)
7168 08 Jun 16 nicklas 73     {
7168 08 Jun 16 nicklas 74       long bytes = getNumRead();
7168 08 Jun 16 nicklas 75       progressReport(Values.formatBytes(bytes, bytes > Values.GB ? 1 : 0));
7168 08 Jun 16 nicklas 76       nextProgress = getNumRead()+interval;
7168 08 Jun 16 nicklas 77     }
7168 08 Jun 16 nicklas 78     return n;
7168 08 Jun 16 nicklas 79   }
7168 08 Jun 16 nicklas 80
7168 08 Jun 16 nicklas 81   /**
7168 08 Jun 16 nicklas 82     Send a progress report with the given message. This method is called
7461 14 Mar 18 nicklas 83     automatically at the given intervals. The default message is the number
7168 08 Jun 16 nicklas 84     of bytes read so far (formatted as a string with 
7168 08 Jun 16 nicklas 85     {@link Values#formatBytes(Long, int)}). Subclasses may override this
7168 08 Jun 16 nicklas 86     method to generate a different message.
7168 08 Jun 16 nicklas 87   */
7168 08 Jun 16 nicklas 88   protected void progressReport(String message)
7168 08 Jun 16 nicklas 89   {
7168 08 Jun 16 nicklas 90     progress.displayAbsolute(getNumRead(), message);
7168 08 Jun 16 nicklas 91   }
7168 08 Jun 16 nicklas 92   
7168 08 Jun 16 nicklas 93 }