TeeWriter.java
3.46 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
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.httpcache.utils;
import java.io.FilterWriter;
import java.io.IOException;
import java.io.Writer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class TeeWriter
extends FilterWriter {
private final Logger logger;
private final String name;
private Writer writer;
private boolean opened;
private boolean error;
private boolean closed;
public TeeWriter(Writer out, String name) {
super(out);
this.logger = LoggerFactory.getLogger(this.getClass());
this.name = name;
}
public void write(int c) throws IOException {
super.write(c);
try {
Writer writer = this.getWriter();
if (writer != null) {
writer.write(c);
}
}
catch (IOException e) {
this.logger.warn(String.format("Unable to write to %s.", this.name), (Throwable)e);
this.error = true;
}
}
public void write(char[] cbuf, int off, int len) throws IOException {
super.write(cbuf, off, len);
try {
Writer writer = this.getWriter();
if (writer != null) {
writer.write(cbuf, 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(String str, int off, int len) throws IOException {
super.write(str, off, len);
try {
Writer writer = this.getWriter();
if (writer != null) {
writer.write(str, off, len);
}
}
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 {
super.flush();
try {
Writer writer = this.getWriter();
if (writer != null) {
writer.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.writer.close();
}
catch (IOException e) {
this.logger.warn(String.format("Unable to close %s.", this.name), (Throwable)e);
this.error = true;
}
finally {
this.closed = true;
}
}
super.close();
}
public boolean hasError() {
return this.error;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private Writer getWriter() throws IOException {
if (this.error || this.closed) {
return null;
}
if (this.writer == null && !this.opened) {
try {
this.writer = this.createWriter();
}
finally {
this.opened = true;
}
}
return this.writer;
}
protected abstract Writer createWriter() throws IOException;
}