TeeServletOutputStream.java
3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.ServletOutputStream
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.httpcache.utils;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class TeeServletOutputStream
extends ServletOutputStream {
private final Logger logger;
private final ServletOutputStream out;
private final String name;
private OutputStream output;
private boolean opened;
private boolean error;
private boolean closed;
public TeeServletOutputStream(ServletOutputStream out, String name) {
this.logger = LoggerFactory.getLogger(this.getClass());
this.out = out;
this.name = name;
}
public void write(byte[] b, int off, int len) throws IOException {
this.out.write(b, off, len);
try {
OutputStream output = this.getOutputStream();
if (output != null) {
output.write(b, off, len);
}
}
catch (IOException e) {
this.logger.warn(String.format("Unable to write to %s.", this.name), (Throwable)e);
this.error = true;
}
}
public void write(int c) throws IOException {
this.out.write(c);
try {
OutputStream output = this.getOutputStream();
if (output != null) {
output.write(c);
}
}
catch (IOException e) {
this.logger.warn(String.format("Unable to write to %s.", this.name), (Throwable)e);
this.error = true;
}
}
public void flush() throws IOException {
this.out.flush();
try {
OutputStream output = this.getOutputStream();
if (output != null) {
output.flush();
}
}
catch (IOException e) {
this.logger.warn(String.format("Unable to flush %s.", this.name), (Throwable)e);
this.error = true;
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void close() throws IOException {
if (this.opened && !this.closed) {
try {
this.output.close();
}
catch (IOException e) {
this.logger.warn(String.format("Unable to close %s.", this.name), (Throwable)e);
this.error = true;
}
finally {
this.closed = true;
}
}
this.out.close();
}
public boolean hasError() {
return this.error;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private OutputStream getOutputStream() throws IOException {
if (this.error || this.closed) {
return null;
}
if (this.output == null && !this.opened) {
try {
this.output = this.createOutputStream();
}
finally {
this.opened = true;
}
}
return this.output;
}
protected abstract OutputStream createOutputStream() throws IOException;
}