001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.war.filter;
034:
035: import com.flexive.shared.FxSharedUtils;
036:
037: import javax.servlet.ServletOutputStream;
038: import java.io.ByteArrayOutputStream;
039: import java.io.IOException;
040:
041: /**
042: *
043: */
044: class FxOutputStream extends ServletOutputStream {
045:
046: ServletOutputStream sos = null;
047: long length = 0;
048: boolean catchContent = false;
049: ByteArrayOutputStream output = null;
050: boolean commited = false;
051: boolean clientWriteThrough = false;
052:
053: protected boolean isCommited() {
054: return commited;
055: }
056:
057: /**
058: * Constructor.
059: *
060: * @param sos the ServletOutputStream
061: * @param catchContent has to be true if getData() is called.
062: */
063: public FxOutputStream(ServletOutputStream sos,
064: boolean catchContent, boolean clientWriteThrough) {
065: super ();
066: this .sos = sos;
067: this .catchContent = catchContent;
068: this .clientWriteThrough = clientWriteThrough;
069: if (this .catchContent) {
070: output = new ByteArrayOutputStream(100000);
071: }
072: }
073:
074: /**
075: * Returns the data sent to the client if the catchContent option was true.
076: * <p/>
077: * if the catchContent option was false a empty array is returned.
078: *
079: * @return the data sent to the client.
080: */
081: public byte[] getData() {
082: if (output == null)
083: return new byte[0];
084: return output.toByteArray();
085: }
086:
087: /**
088: * Returns the byte count sent to the client.
089: *
090: * @return the byte count sent to the client.
091: */
092: public long getContentLength() {
093: return this .length;
094: }
095:
096: @Override
097: public void close() throws IOException {
098: super .close();
099: if (sos != null)
100: this .sos.close();
101: if (this .output != null)
102: this .output.close();
103: this .commited = true;
104: }
105:
106: @Override
107: public void flush() throws IOException {
108: super .flush();
109: if (sos != null)
110: this .sos.flush();
111: if (this .output != null)
112: this .output.flush();
113: }
114:
115: @Override
116: public void write(int b) throws IOException {
117: this .print((char) b);
118: }
119:
120: @Override
121: public void print(boolean b) throws IOException {
122: this .print(b ? "true" : "false");
123: }
124:
125: @Override
126: public void println(boolean b) throws IOException {
127: this .println(b ? "true" : "false");
128: }
129:
130: @Override
131: public void print(int i) throws IOException {
132: this .print("" + i);
133: }
134:
135: @Override
136: public void print(long l) throws IOException {
137: this .print("" + l);
138: }
139:
140: @Override
141: public void print(float v) throws IOException {
142: this .print("" + v);
143: }
144:
145: @Override
146: public void print(double v) throws IOException {
147: this .print("" + v);
148: }
149:
150: @Override
151: public void println(int i) throws IOException {
152: this .println("" + i);
153: }
154:
155: @Override
156: public void println(long l) throws IOException {
157: this .println("" + l);
158: }
159:
160: @Override
161: public void println(float v) throws IOException {
162: this .println("" + v);
163: }
164:
165: @Override
166: public void println(double v) throws IOException {
167: this .println("" + v);
168: }
169:
170: @Override
171: public void println() throws IOException {
172: this .println("");
173: }
174:
175: @Override
176: public void print(char c) throws IOException {
177: this .print("" + c);
178: }
179:
180: @Override
181: public void println(char c) throws IOException {
182: this .println("" + c);
183: }
184:
185: // ------------ Count operations after this point ------------
186:
187: @Override
188: public void print(String s) throws IOException {
189: if (sos != null)
190: sos.print(s);
191: length += s.length();
192: if (catchContent)
193: output.write(FxSharedUtils.getBytes(s));
194: }
195:
196: @Override
197: public void println(String s) throws IOException {
198: if (sos != null)
199: sos.println(s);
200: length += s.length() + 1;
201: if (catchContent)
202: output.write(FxSharedUtils.getBytes(s + "\n"));
203: }
204:
205: @Override
206: public void write(byte b[]) throws IOException {
207: if (sos != null)
208: sos.write(b);
209: length += b.length;
210: if (catchContent)
211: output.write(b);
212: }
213:
214: @Override
215: public void write(byte b[], int off, int len) throws IOException {
216: if (sos != null)
217: sos.write(b, off, len);
218: length += len;
219: if (catchContent)
220: output.write(b, off, len);
221: }
222:
223: }
|