View Javadoc

1   /*
2    * ------------------------------------------------------------------------------
3    * Hermes FTP Server
4    * Copyright (c) 2005-2007 Lars Behnke
5    * ------------------------------------------------------------------------------
6    * 
7    * This file is part of Hermes FTP Server.
8    * 
9    * Hermes FTP Server is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as published by
11   * the Free Software Foundation; either version 2 of the License, or
12   * (at your option) any later version.
13   * 
14   * Hermes FTP Server is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public License
20   * along with Hermes FTP Server; if not, write to the Free Software
21   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22   * ------------------------------------------------------------------------------
23   */
24  
25  package net.sf.hermesftp.utils;
26  
27  import java.io.BufferedReader;
28  import java.io.IOException;
29  import java.io.Reader;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  /***
35   * A writer that adds logging capability to a passed writer. This mechanism corresponds the
36   * decorator pattern.
37   *
38   * @author Lars Behnke
39   *
40   */
41  public class LoggingReader
42      extends BufferedReader {
43  
44      private static Log log = LogFactory.getLog(LoggingReader.class);
45  
46      private static final int LOG_LINE_LENGTH = 80;
47  
48      /***
49       * Constructor.
50       *
51       * @param in A reader.
52       */
53      public LoggingReader(Reader in) {
54          super(in);
55      }
56  
57      /***
58       * Constructor.
59       *
60       * @param in A reader.
61       * @param sz Input buffer size.
62       */
63      public LoggingReader(Reader in, int sz) {
64          super(in, sz);
65      }
66  
67      /***
68       * {@inheritDoc}
69       */
70      public String readLine() throws IOException {
71          String result = super.readLine();
72          if (log.isDebugEnabled()) {
73              String x;
74              if (result != null && result.length() >= LOG_LINE_LENGTH) {
75                  x = result.substring(0, LOG_LINE_LENGTH) + " ["
76                      + (result.length() - LOG_LINE_LENGTH) + " more chars]";
77              } else {
78                  x = result;
79              }
80              log.debug("<--: " + x);
81          }
82          return result;
83      }
84  
85  }