001: //httpByteCountinputStream.java
002: //-----------------------
003: //(C) by Michael Peter Christen; mc@anomic.de
004: //first published on http://www.anomic.de
005: //Frankfurt, Germany, 2004
006: //
007: // This file is contributed by Martin Thelian
008: //
009: // $LastChangedDate: 2008-01-28 18:21:08 +0000 (Mo, 28 Jan 2008) $
010: // $LastChangedRevision: 4411 $
011: // $LastChangedBy: orbiter $
012: //
013: //This program is free software; you can redistribute it and/or modify
014: //it under the terms of the GNU General Public License as published by
015: //the Free Software Foundation; either version 2 of the License, or
016: //(at your option) any later version.
017: //
018: //This program is distributed in the hope that it will be useful,
019: //but WITHOUT ANY WARRANTY; without even the implied warranty of
020: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
021: //GNU General Public License for more details.
022: //
023: //You should have received a copy of the GNU General Public License
024: //along with this program; if not, write to the Free Software
025: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
026: //
027: //Using this software in any meaning (reading, learning, copying, compiling,
028: //running) means that you agree that the Author(s) is (are) not responsible
029: //for cost, loss of data or any harm that may be caused directly or indirectly
030: //by usage of this softare or this documentation. The usage of this software
031: //is on your own risk. The installation and usage (starting/running) of this
032: //software may allow other people or application to access your computer and
033: //any attached devices and is highly dependent on the configuration of the
034: //software which must be done by the user of the software; the author(s) is
035: //(are) also not responsible for proper configuration and usage of the
036: //software, even if provoked by documentation provided together with
037: //the software.
038: //
039: //Any changes to this file according to the GPL as documented in the file
040: //gpl.txt aside this file in the shipment you received can be done to the
041: //lines that follows this copyright notice here, but changes must not be
042: //done inside the copyright notive above. A re-distribution must contain
043: //the intact and unchanged copyright notice.
044: //Contributions and changes to the program code must be marked as such.
045:
046: package de.anomic.http;
047:
048: import java.io.FilterInputStream;
049: import java.io.IOException;
050: import java.io.InputStream;
051: import java.util.HashMap;
052:
053: public class httpdByteCountInputStream extends FilterInputStream {
054:
055: private static final Object syncObject = new Object();
056: private static final HashMap<String, Long> byteCountInfo = new HashMap<String, Long>(
057: 2);
058: private static long globalByteCount = 0;
059:
060: private boolean finished = false;
061: protected long byteCount;
062: private String byteCountAccountName = null;
063:
064: protected httpdByteCountInputStream(InputStream inputStream) {
065: this (inputStream, null);
066: }
067:
068: /**
069: * Constructor of this class
070: * @param inputStream the {@link InputStream} to read from
071: */
072: public httpdByteCountInputStream(InputStream inputStream,
073: String accountName) {
074: this (inputStream, 0, accountName);
075: }
076:
077: /**
078: * Constructor of this class
079: * @param inputStream the {@link InputStream} to read from
080: * @param initByteCount to initialize the bytecount with a given value
081: */
082: public httpdByteCountInputStream(InputStream inputStream,
083: int initByteCount, String accountName) {
084: super (inputStream);
085: this .byteCount = initByteCount;
086: this .byteCountAccountName = accountName;
087: }
088:
089: public int read(byte[] b) throws IOException {
090: int readCount = super .read(b);
091: if (readCount > 0)
092: this .byteCount += readCount;
093: return readCount;
094: }
095:
096: public int read(byte[] b, int off, int len) throws IOException {
097: int readCount = super .read(b, off, len);
098: if (readCount > 0)
099: this .byteCount += readCount;
100: return readCount;
101: }
102:
103: public int read() throws IOException {
104: this .byteCount++;
105: return super .read();
106: }
107:
108: public long skip(long len) throws IOException {
109: long skipCount = super .skip(len);
110: if (skipCount > 0)
111: this .byteCount += skipCount;
112: return skipCount;
113: }
114:
115: public long getCount() {
116: return this .byteCount;
117: }
118:
119: public String getAccountName() {
120: return this .byteCountAccountName;
121: }
122:
123: public static long getGlobalCount() {
124: synchronized (syncObject) {
125: return globalByteCount;
126: }
127: }
128:
129: public static long getAccountCount(String accountName) {
130: synchronized (syncObject) {
131: if (byteCountInfo.containsKey(accountName)) {
132: return ((Long) byteCountInfo.get(accountName))
133: .longValue();
134: }
135: return 0;
136: }
137: }
138:
139: public void close() throws IOException {
140: super .close();
141: this .finish();
142: }
143:
144: public void finish() {
145: if (this .finished)
146: return;
147:
148: this .finished = true;
149: synchronized (syncObject) {
150: globalByteCount += this .byteCount;
151: if (this .byteCountAccountName != null) {
152: long lastByteCount = 0;
153: if (byteCountInfo
154: .containsKey(this .byteCountAccountName)) {
155: lastByteCount = byteCountInfo.get(
156: this .byteCountAccountName).longValue();
157: }
158: lastByteCount += this .byteCount;
159: byteCountInfo.put(this .byteCountAccountName, new Long(
160: lastByteCount));
161: }
162:
163: }
164: }
165:
166: public static void resetCount() {
167: synchronized (syncObject) {
168: globalByteCount = 0;
169: byteCountInfo.clear();
170: }
171: }
172:
173: protected void finalize() throws Throwable {
174: if (!this.finished)
175: finish();
176: super.finalize();
177: }
178: }
|