![]() |
Quadcap Embeddable Server |
00001 package com.quadcap.io; 00002 00003 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.InputStream; 00042 import java.io.IOException; 00043 00044 import java.util.Vector; 00045 00046 import com.quadcap.util.Debug; 00047 00048 /** 00049 * This class implements an input stream filter which keeps track of how 00050 * many bytes have been read. 00051 * 00052 * @author Stan Bailes 00053 */ 00054 public class CountedInputStream extends InputStream { 00055 InputStream in; 00056 int pos = 0; 00057 00058 /** 00059 * Construct a new CountedInputStream reading from the specified base 00060 * stream. 00061 * 00062 * @param in the input stream 00063 */ 00064 public CountedInputStream(InputStream in) { 00065 this.in = in; 00066 } 00067 00068 /** 00069 * Read the next byte from this input stream. 00070 * 00071 * @exception IOException if an I/O error occurs. 00072 */ 00073 public int read() throws IOException { 00074 pos++; 00075 return in.read(); 00076 } 00077 00078 /** 00079 * Read a block of bytes from this input stream. 00080 * 00081 * @exception IOException if an I/O error occcurs. 00082 */ 00083 public int read(byte[] buf, int offset, int cnt) throws IOException { 00084 int ret = in.read(buf, offset, cnt); 00085 pos += ret; 00086 return ret; 00087 } 00088 00089 /** 00090 * Read a block of bytes from this input stream. 00091 * 00092 * @exception IOException if an I/O error occcurs. 00093 */ 00094 public int read(byte[] buf) throws IOException { 00095 int ret = in.read(buf); 00096 pos += ret; 00097 return ret; 00098 } 00099 00100 /** 00101 * Return the number of bytes that can be read without blocking. 00102 */ 00103 public int available() throws IOException { 00104 return in.available(); 00105 } 00106 00107 /** 00108 * Close the stream. 00109 */ 00110 public void close() throws IOException { 00111 in.close(); 00112 } 00113 00114 /** 00115 * Skip ahead in the stream. 00116 */ 00117 public long skip(long n) throws IOException { 00118 return in.skip(n); 00119 } 00120 00121 /** 00122 * Mark the current position in the input stream. 00123 */ 00124 public void mark(int readLimit) { 00125 in.mark(readLimit); 00126 } 00127 00128 /** 00129 * Return to the previously marked position in the input stream. 00130 */ 00131 public void reset() throws IOException { 00132 in.reset(); 00133 } 00134 00135 /** 00136 * Return true if mark/reset supported (they are.) 00137 */ 00138 public boolean markSupported() { 00139 return in.markSupported(); 00140 } 00141 00142 /** 00143 * Return the number of bytes read so far from this input stream. 00144 */ 00145 public int getPosition() { 00146 return pos; 00147 } 00148 00149 /** 00150 * Set the current position 00151 */ 00152 public void setPosition(int pos) { 00153 this.pos = pos; 00154 } 00155 00156 }