1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.logging.log4j.io;
19
20 import java.io.FilterInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.nio.charset.Charset;
24
25 import org.apache.logging.log4j.Level;
26 import org.apache.logging.log4j.Marker;
27 import org.apache.logging.log4j.spi.ExtendedLogger;
28
29
30
31
32
33
34 public class LoggerInputStream extends FilterInputStream {
35 private static final String FQCN = LoggerInputStream.class.getName();
36
37 private final String fqcn;
38 private final ByteStreamLogger logger;
39
40 protected LoggerInputStream(final InputStream in, final Charset charset, final ExtendedLogger logger,
41 final String fqcn, final Level level, final Marker marker) {
42 super(in);
43 this.logger = new ByteStreamLogger(logger, level, marker, charset);
44 this.fqcn = fqcn == null ? FQCN : fqcn;
45 }
46
47 @Override
48 public void close() throws IOException {
49 this.logger.close(this.fqcn);
50 super.close();
51 }
52
53 @Override
54 public int read() throws IOException {
55 final int b = super.read();
56 this.logger.put(this.fqcn, b);
57 return b;
58 }
59
60 @Override
61 public int read(final byte[] b) throws IOException {
62 return read(b, 0, b.length);
63 }
64
65 @Override
66 public int read(final byte[] b, final int off, final int len) throws IOException {
67 final int bytesRead = super.read(b, off, len);
68 this.logger.put(this.fqcn, b, off, bytesRead);
69 return bytesRead;
70 }
71
72 @Override
73 public String toString() {
74 return LoggerInputStream.class.getSimpleName() + "{stream=" + this.in + '}';
75 }
76 }