KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > log > LogInputStream


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.log;
37
38 import java.io.FilterInputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.InputStream JavaDoc;
41 import java.io.OutputStream JavaDoc;
42
43 /**
44  * InputStream that logs the data that flows through it.
45  *
46  * @author tstich
47  *
48  */

49 public class LogInputStream extends FilterInputStream JavaDoc {
50
51     private static final byte[] PREFIX_STRING = { 'S', ':', ' ' };
52
53     private static final int LINEEND = 0;
54     private static final int IN_LINE = 1;
55
56     private static final int MAX_LENGTH = 100 - PREFIX_STRING.length;
57
58     private int state;
59     private int line_length;
60
61     private OutputStream JavaDoc logOutputStream;
62
63     /**
64      * Constructs the LogInputStream.java.
65      *
66      * @param arg0
67      * @param log
68      * The LogOutputStream
69      */

70     public LogInputStream(InputStream JavaDoc arg0, OutputStream JavaDoc log) {
71         super(arg0);
72         this.logOutputStream = log;
73
74         state = LINEEND;
75
76         line_length = 0;
77     }
78
79     /**
80      * Constructs the LogInputStream.java. This constructor sets the
81      * LogOutputStream to System.out.
82      *
83      * @param arg0
84      */

85     public LogInputStream(InputStream JavaDoc arg0) {
86         this(arg0, System.out);
87     }
88
89     /**
90      * @see java.io.InputStream#read()
91      */

92     public int read() throws IOException JavaDoc {
93         int read = in.read();
94
95         if( read == -1 ) return -1;
96         
97         switch (state) {
98             case (LINEEND) :
99                 {
100                     line_length = 0;
101                     state = IN_LINE;
102
103                     // if something follows first print the prefix
104
logOutputStream.write(PREFIX_STRING);
105
106                     break;
107                 }
108
109             case (IN_LINE) :
110                 {
111                     line_length++;
112
113                     // check for line ends
114
if (read == '\n') {
115                         state = LINEEND;
116                     } else if (line_length == MAX_LENGTH) {
117                         // check for long lines
118
line_length = 0;
119                         logOutputStream.write('\\');
120                         logOutputStream.write('\n');
121                         logOutputStream.write(PREFIX_STRING);
122                     }
123
124                     break;
125                 }
126         }
127
128         logOutputStream.write(read);
129
130         return read;
131     }
132
133     /**
134      * @see java.io.InputStream#read(byte[], int, int)
135      */

136     public int read(byte[] arg0, int arg1, int arg2) throws IOException JavaDoc {
137         int next;
138         int i = 0;
139
140         for (; i < arg2; i++) {
141             next = read();
142             if (next == -1) {
143                 break;
144             }
145             arg0[arg1 + i] = (byte) next;
146         }
147         
148         if( i == 0) return -1;
149         else return i;
150     }
151
152     /**
153      * @return Returns the logOutputStream.
154      */

155     public OutputStream JavaDoc getLogOutputStream() {
156         return logOutputStream;
157     }
158
159     /**
160      * @param logOutputStream
161      * The logOutputStream to set.
162      */

163     public void setLogOutputStream(OutputStream JavaDoc logOutputStream) {
164         this.logOutputStream = logOutputStream;
165     }
166
167 }
168
Popular Tags