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