1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 }