01: package com.mockrunner.mock.web;
02:
03: import java.io.ByteArrayOutputStream;
04: import java.io.IOException;
05:
06: import javax.servlet.ServletOutputStream;
07:
08: import com.mockrunner.base.NestedApplicationException;
09:
10: /**
11: * Mock implementation of <code>ServletOutputStream</code>.
12: */
13: public class MockServletOutputStream extends ServletOutputStream {
14: private ByteArrayOutputStream buffer;
15: private String encoding;
16:
17: public MockServletOutputStream() {
18: this ("ISO-8859-1");
19: }
20:
21: public MockServletOutputStream(String encoding) {
22: buffer = new ByteArrayOutputStream();
23: this .encoding = encoding;
24: }
25:
26: public void setEncoding(String encoding) {
27: this .encoding = encoding;
28: }
29:
30: public void write(int value) throws IOException {
31: buffer.write(value);
32: }
33:
34: public String getContent() {
35: try {
36: buffer.flush();
37: return buffer.toString(encoding);
38: } catch (IOException exc) {
39: throw new NestedApplicationException(exc);
40: }
41: }
42:
43: public byte[] getBinaryContent() {
44: try {
45: buffer.flush();
46: return buffer.toByteArray();
47: } catch (IOException exc) {
48: throw new NestedApplicationException(exc);
49: }
50: }
51:
52: public void clearContent() {
53: buffer = new ByteArrayOutputStream();
54: }
55: }
|