1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.encoder;
15
16 import java.io.IOException;
17 import java.io.OutputStream;
18 import java.io.UnsupportedEncodingException;
19 import java.nio.charset.Charset;
20
21 import ch.qos.logback.core.CoreConstants;
22 import ch.qos.logback.core.Layout;
23
24 public class LayoutWrappingEncoder<E> extends EncoderBase<E> {
25
26 protected Layout<E> layout;
27
28
29
30
31
32
33
34
35 private Charset charset;
36
37 private boolean immediateFlush = true;
38
39
40
41
42
43
44
45
46
47 public void setImmediateFlush(boolean immediateFlush) {
48 this.immediateFlush = immediateFlush;
49 }
50
51
52 public boolean isImmediateFlush() {
53 return immediateFlush;
54 }
55
56
57 public Layout<E> getLayout() {
58 return layout;
59 }
60
61 public void setLayout(Layout<E> layout) {
62 this.layout = layout;
63 }
64
65 public Charset getCharset() {
66 return charset;
67 }
68
69
70
71
72
73
74
75
76
77
78
79 public void setCharset(Charset charset) {
80 this.charset = charset;
81 }
82
83 public void init(OutputStream os) throws IOException {
84 super.init(os);
85 writeHeader();
86 }
87
88 void writeHeader() throws IOException {
89 if (layout != null && (outputStream != null)) {
90 StringBuilder sb = new StringBuilder();
91 appendIfNotNull(sb, layout.getFileHeader());
92 appendIfNotNull(sb, layout.getPresentationHeader());
93 if (sb.length() > 0) {
94 sb.append(CoreConstants.LINE_SEPARATOR);
95
96
97
98 outputStream.write(convertToBytes(sb.toString()));
99 outputStream.flush();
100 }
101 }
102 }
103
104 public void close() throws IOException {
105 writeFooter();
106 }
107
108 void writeFooter() throws IOException {
109 if (layout != null && outputStream != null) {
110 StringBuilder sb = new StringBuilder();
111 appendIfNotNull(sb, layout.getPresentationFooter());
112 appendIfNotNull(sb, layout.getFileFooter());
113 if (sb.length() > 0) {
114 outputStream.write(convertToBytes(sb.toString()));
115 outputStream.flush();
116 }
117 }
118 }
119
120 private byte[] convertToBytes(String s) {
121 if (charset == null) {
122 return s.getBytes();
123 } else {
124 try {
125 return s.getBytes(charset.name());
126 } catch (UnsupportedEncodingException e) {
127 throw new IllegalStateException(
128 "An existing charset cannot possibly be unsupported.");
129 }
130 }
131 }
132
133 public void doEncode(E event) throws IOException {
134 String txt = layout.doLayout(event);
135 outputStream.write(convertToBytes(txt));
136 if (immediateFlush)
137 outputStream.flush();
138 }
139
140 public boolean isStarted() {
141 return false;
142 }
143
144 public void start() {
145 started = true;
146 }
147
148 public void stop() {
149 started = false;
150 if(outputStream != null) {
151 try {
152 outputStream.flush();
153 } catch (IOException e) {
154 }
155 }
156 }
157
158 private void appendIfNotNull(StringBuilder sb, String s) {
159 if (s != null) {
160 sb.append(s);
161 }
162 }
163
164 }