001package com.hfg.util.io; 002 003import java.io.FilterInputStream; 004import java.io.IOException; 005import java.io.InputStream; 006 007 008//------------------------------------------------------------------------------ 009/** 010 An InputStream variant for measuring the number of bytes that pass through. 011 <div> 012 @author J. Alex Taylor, hairyfatguy.com 013 </div> 014 */ 015//------------------------------------------------------------------------------ 016// com.hfg XML/HTML Coding Library 017// 018// This library is free software; you can redistribute it and/or 019// modify it under the terms of the GNU Lesser General Public 020// License as published by the Free Software Foundation; either 021// version 2.1 of the License, or (at your option) any later version. 022// 023// This library is distributed in the hope that it will be useful, 024// but WITHOUT ANY WARRANTY; without even the implied warranty of 025// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 026// Lesser General Public License for more details. 027// 028// You should have received a copy of the GNU Lesser General Public 029// License along with this library; if not, write to the Free Software 030// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 031// 032// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 033// jataylor@hairyfatguy.com 034//------------------------------------------------------------------------------ 035 036public class FlowMeterInputStream extends FilterInputStream 037{ 038 private long mNumBytes = 0; 039 040 //########################################################################## 041 // CONSTRUCTORS 042 //########################################################################## 043 044 //-------------------------------------------------------------------------- 045 046 /** 047 * An InputStream that measures the number of bytes that pass through. 048 */ 049 public FlowMeterInputStream(InputStream in) 050 { 051 super(in); 052 } 053 054 055 //########################################################################## 056 // PUBLIC METHODS 057 //########################################################################## 058 059 //-------------------------------------------------------------------------- 060 @Override 061 public int read() 062 throws IOException 063 { 064 int theByte = super.read(); 065 if (theByte > 0) 066 { 067 mNumBytes++; 068 } 069 070 return theByte; 071 } 072 073 //-------------------------------------------------------------------------- 074 @Override 075 public int read(byte[] inBytes, int inOffset, int inLength) 076 throws IOException 077 { 078 int numBytesRead = super.read(inBytes, inOffset, inLength); 079 if (numBytesRead > 0) 080 { 081 mNumBytes += numBytesRead; 082 } 083 084 return numBytesRead; 085 } 086 087 //-------------------------------------------------------------------------- 088 public void reset() 089 { 090 mNumBytes = 0; 091 } 092 093 //-------------------------------------------------------------------------- 094 public long getNumBytes() 095 { 096 return mNumBytes; 097 } 098 099 //-------------------------------------------------------------------------- 100 @Override 101 public long skip(long inBytesToSkip) 102 throws IOException 103 { 104 long totalBytesSkipped = 0; 105 while (totalBytesSkipped < inBytesToSkip) 106 { 107 long bytesSkipped = super.skip(inBytesToSkip - totalBytesSkipped); 108 mNumBytes += bytesSkipped; 109 totalBytesSkipped += bytesSkipped; 110 111 if (0 == bytesSkipped) 112 { 113 break; 114 } 115 } 116 117 return totalBytesSkipped; 118 } 119 120 //-------------------------------------------------------------------------- 121 public long skipTo(long inBytesOffset) 122 throws IOException 123 { 124 return skip(inBytesOffset - getNumBytes()); 125 } 126}