1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.access.common.servlet;
15
16 import java.io.ByteArrayOutputStream;
17 import java.io.IOException;
18
19 import jakarta.servlet.ServletOutputStream;
20 import jakarta.servlet.ServletResponse;
21 import jakarta.servlet.WriteListener;
22
23 public class TeeServletOutputStream extends ServletOutputStream {
24
25 final ServletOutputStream underlyingStream;
26 final ByteArrayOutputStream baosCopy;
27
28 TeeServletOutputStream(ServletResponse httpServletResponse) 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) throws IOException {
57 if (underlyingStream == null) {
58 return;
59 }
60
61
62
63 underlyingStream.write(byteArray, offset, length);
64 baosCopy.write(byteArray, offset, length);
65 }
66
67 @Override
68 public void close() throws IOException {
69
70
71
72
73
74
75 }
76
77 @Override
78 public void flush() throws IOException {
79 if (underlyingStream == null) {
80 return;
81 }
82
83 underlyingStream.flush();
84 baosCopy.flush();
85 }
86
87 @Override
88 public boolean isReady() {
89 throw new RuntimeException("Not yet implemented");
90 }
91
92 @Override
93 public void setWriteListener(WriteListener listener) {
94 throw new RuntimeException("Not yet implemented");
95 }
96 }