View Javadoc

1   /*
2    * Copyright (c) 2004-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.danube.support.logging;
14  
15  import java.io.IOException;
16  import java.io.OutputStream;
17  
18  /**
19   * Output stream for logging. An given stream is duplicated to log stream as well.
20   *
21   * @author Daniel Sendula
22   */
23  public class LoggingOutputStream extends OutputStream {
24  
25      /** Original output stream */
26      protected OutputStream outputStream;
27  
28      /** Log output stream */
29      protected OutputStream logOutputStream;
30  
31      /** Is logging switched flag */
32      protected boolean logging = true;
33  
34      /**
35       * Constructor
36       * @param outputStream output stream
37       * @param logOutputStream log output stream
38       */
39      public LoggingOutputStream(OutputStream outputStream, OutputStream logOutputStream) {
40          this.outputStream = outputStream;
41          this.logOutputStream = logOutputStream;
42      }
43  
44      /**
45       * Should written bytes from output stream be copied to the output stream
46       * @param logging <code>true</code> if logging is on
47       */
48      public void setLogging(boolean logging) {
49          this.logging = logging;
50      }
51  
52      /**
53       * Returns <code>true</code> if logging is on
54       * @return <code>true</code> if logging is on
55       */
56      public boolean isLogging() {
57          return logging;
58      }
59  
60      @Override
61      public void close() throws IOException {
62           outputStream.close();
63      } // close
64  
65      @Override
66      public void flush() throws IOException {
67           outputStream.flush();
68      } // flush
69  
70      @Override
71      public void write(byte[] b) throws IOException {
72          outputStream.write(b);
73          if (logging) { logOutputStream.write(b); }
74      }
75  
76      @Override
77      public void write(byte[] b, int off, int len) throws IOException {
78          outputStream.write(b, off, len);
79          if (logging) { logOutputStream.write(b, off, len); }
80      }
81  
82      @Override
83      public void write(int b) throws IOException {
84          outputStream.write(b);
85          if (logging) { logOutputStream.write(b); }
86      }
87  }