1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.access.servlet;
15
16 import java.io.ByteArrayOutputStream;
17 import java.io.IOException;
18
19 import javax.servlet.ServletOutputStream;
20 import javax.servlet.ServletResponse;
21
22 public class TeeServletOutputStream extends ServletOutputStream {
23
24 final ServletOutputStream underlyingStream;
25 final ByteArrayOutputStream baosCopy;
26
27 TeeServletOutputStream(ServletResponse httpServletResponse)
28 throws IOException {
29
30 this.underlyingStream = httpServletResponse.getOutputStream();
31 baosCopy = new ByteArrayOutputStream();
32 }
33
34 byte[] getOutputStreamAsByteArray() {
35 return baosCopy.toByteArray();
36 }
37
38 @Override
39 public void write(int val) throws IOException {
40 if (underlyingStream != null) {
41 underlyingStream.write(val);
42 baosCopy.write(val);
43 }
44 }
45
46 @Override
47 public void write(byte[] byteArray) throws IOException {
48 if (underlyingStream == null) {
49 return;
50 }
51
52 write(byteArray, 0, byteArray.length);
53 }
54
55 @Override
56 public void write(byte byteArray[], int offset, int length)
57 throws IOException {
58 if (underlyingStream == null) {
59 return;
60 }
61
62
63
64 underlyingStream.write(byteArray, offset, length);
65 baosCopy.write(byteArray, offset, length);
66 }
67
68 @Override
69 public void close() throws IOException {
70
71
72
73
74
75
76 }
77
78
79 @Override
80 public void flush() throws IOException {
81 if (underlyingStream == null) {
82 return;
83 }
84
85 underlyingStream.flush();
86 baosCopy.flush();
87 }
88 }