View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.common.lib;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.io.OutputStream;
24  import org.slf4j.Logger;
25  
26  /**
27   * Delegates output stream writing onto an SLF4J logger.
28   * Inspired by {@code org.apache.commons.exec.LogOutputStream}
29   */
30  public class LogOutputStream extends OutputStream implements AutoCloseable {
31  
32      /** Initial buffer size. */
33      private static final int INTIAL_SIZE = 132;
34  
35      /** Carriage return. */
36      private static final int CR = 0x0d;
37  
38      /** Linefeed. */
39      private static final int LF = 0x0a;
40  
41      /** The internal buffer. */
42      private final ByteArrayOutputStream buffer = new ByteArrayOutputStream(INTIAL_SIZE);
43  
44      /**
45       * The delegate logger.
46       */
47      private final Logger logger;
48  
49      private boolean skip = false;
50  
51      public LogOutputStream(final Logger logger) {
52          this.logger = logger;
53      }
54  
55      /**
56       * Write the data to the buffer and flush the buffer, if a line separator is
57       * detected.
58       *
59       * @param cc data to log (byte).
60       */
61      @Override
62      public void write(final int cc) {
63          final byte c = (byte) cc;
64          if (c == '\n' || c == '\r') {
65              if (!skip) {
66                  processBuffer();
67              }
68          } else {
69              buffer.write(cc);
70          }
71          skip = c == '\r';
72      }
73  
74      /**
75       * Flush this log stream.
76       *
77       */
78      @Override
79      public void flush() {
80          if (buffer.size() > 0) {
81              processBuffer();
82          }
83      }
84  
85      /**
86       * Writes all remaining data from the buffer.
87       *
88       * @exception IOException if an I/O error occurs
89       * @see OutputStream#close()
90       */
91      @Override
92      public void close() throws IOException {
93          if (buffer.size() > 0) {
94              processBuffer();
95          }
96          super.close();
97      }
98  
99      /**
100      * Write a block of characters to the output stream
101      *
102      * @param b the array containing the data
103      * @param off the offset into the array where data starts
104      * @param len the length of block
105      */
106     @Override
107     public void write(final byte[] b, final int off, final int len) {
108         // find the line breaks and pass other chars through in blocks
109         int offset = off;
110         int blockStartOffset = offset;
111         int remaining = len;
112         while (remaining > 0) {
113             while (remaining > 0 && b[offset] != LF && b[offset] != CR) {
114                 offset++;
115                 remaining--;
116             }
117             // either end of buffer or a line separator char
118             final int blockLength = offset - blockStartOffset;
119             if (blockLength > 0) {
120                 buffer.write(b, blockStartOffset, blockLength);
121             }
122             while (remaining > 0 && (b[offset] == LF || b[offset] == CR)) {
123                 write(b[offset]);
124                 offset++;
125                 remaining--;
126             }
127             blockStartOffset = offset;
128         }
129     }
130 
131     /**
132      * Converts the buffer to a string and sends it to internal logger.
133      */
134     private void processBuffer() {
135         logger.debug(buffer.toString());
136         buffer.reset();
137     }
138 }