View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.logging.log4j.io.internal;
19  
20  import java.io.FilterOutputStream;
21  import java.io.IOException;
22  import java.io.OutputStream;
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.io.ByteStreamLogger;
28  import org.apache.logging.log4j.spi.ExtendedLogger;
29  
30  /**
31   * Internal class that exists primiarly to allow location calculations to work.
32   *
33   * @since 2.12
34   */
35  public class InternalFilterOutputStream extends FilterOutputStream {
36  
37      private final ByteStreamLogger logger;
38      private final String fqcn;
39  
40      public InternalFilterOutputStream(final OutputStream out, final Charset charset, final ExtendedLogger logger,
41                                         final String fqcn, final Level level, final Marker marker) {
42          super(out);
43          this.logger = new ByteStreamLogger(logger, level, marker, charset);
44          this.fqcn = fqcn;
45      }
46  
47      @Override
48      public void close() throws IOException {
49          this.out.close();
50          this.logger.close(this.fqcn);
51      }
52  
53      @Override
54      public void flush() throws IOException {
55          this.out.flush();
56      }
57  
58      @Override
59      public String toString() {
60          return "{stream=" + this.out + '}';
61      }
62  
63      @Override
64      public void write(final byte[] b) throws IOException {
65          this.out.write(b);
66          this.logger.put(this.fqcn, b, 0, b.length);
67      }
68  
69      @Override
70      public void write(final byte[] b, final int off, final int len) throws IOException {
71          this.out.write(b, off, len);
72          this.logger.put(this.fqcn, b, off, len);
73      }
74  
75      @Override
76      public void write(final int b) throws IOException {
77          this.out.write(b);
78          this.logger.put(this.fqcn, (byte) (b & 0xFF));
79      }
80  }