01: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
02: * See license distributed with this file and
03: * available online at http://www.uportal.org/license.html
04: */
05:
06: package org.jasig.portal.utils;
07:
08: import java.io.IOException;
09: import java.io.OutputStreamWriter;
10:
11: import javax.servlet.ServletOutputStream;
12:
13: /**
14: * A filter presenting a <code>ServletOutputStream</code> that performs
15: * word substitution (search and replace) on the fly.
16: *
17: * 7/25/05 - UP-1180 - dmindler@rutgers.edu
18: * Modified to make use of optimized SubstitutionIntegerFilter
19: *
20: * @author Peter Kharchenko {@link <a href="mailto:pkharchenko@interactivebusiness.com"">pkharchenko@interactivebusiness.com"</a>}
21: * @version $Revision: 36690 $
22: */
23: public class SubstitutionServletOutputStream extends
24: ServletOutputStream {
25: SubstitutionIntegerFilter filter;
26:
27: /**
28: * Creates a new <code>SubstitutionServletOutputStream</code> instance.
29: *
30: * @param out a true <code>ServletOutputStream</code> value where processed stream should be directed
31: * @param target a <code>byte[]</code> value of a target to be replaced
32: * @param substitute a <code>byte[]</code> value with which the target will be replaced
33: */
34: public SubstitutionServletOutputStream(ServletOutputStream out,
35: byte[] target, byte[] substitute) {
36: filter = new SubstitutionIntegerFilter(new OutputStreamWriter(
37: out), getCharArrayFromByteArray(target),
38: getCharArrayFromByteArray(substitute));
39: }
40:
41: /**
42: * Creates a new <code>SubstitutionServletOutputStream</code> instance.
43: *
44: * @param out a true <code>ServletOutputStream</code> value where processed stream should be directed
45: * @param target a <code>byte[]</code> value of a target to be replaced
46: * @param substitute a <code>byte[]</code> value with which the target will be replaced
47: * @param bufferSize a buffer size
48: */
49: public SubstitutionServletOutputStream(ServletOutputStream out,
50: byte[] target, byte[] substitute, int bufferSize) {
51: filter = new SubstitutionIntegerFilter(new OutputStreamWriter(
52: out), getCharArrayFromByteArray(target),
53: getCharArrayFromByteArray(substitute), bufferSize);
54: }
55:
56: public void write(int i) throws IOException {
57: filter.write((char) i);
58: }
59:
60: public void flush() throws IOException {
61: filter.flush();
62: }
63:
64: public void close() throws IOException {
65: filter.close();
66: }
67:
68: /**
69: * A helper method to convert byte array to int array.
70: * I am sure there's a way to cast it correctly, but I don't want to take my chances :)
71: * @param c a <code>byte[]</code> value
72: * @return an <code>int[]</code> value
73: */
74: private static char[] getCharArrayFromByteArray(byte[] c) {
75: char[] ic = new char[c.length];
76: for (int i = 0; i < c.length; i++) {
77: ic[i] = (char) c[i];
78: }
79: return ic;
80: }
81:
82: }
|