KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mail > util > MessageInputStream


1 /*
2  * MessageInputStream.java
3  * Copyright (C) 2003 Doug Porter
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * You also have permission to link it with the Sun Microsystems, Inc.
11  * JavaMail(tm) extension and run that combination.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  */

22  
23 package gnu.mail.util;
24
25 import java.io.BufferedInputStream JavaDoc;
26 import java.io.FilterInputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 import javax.mail.Session JavaDoc;
31
32 /**
33  * An input stream for messages and message headers that terminates on
34  * a dot line as well as end of stream, and that includes a readLine method.
35  * <p>
36  * Be sure here to inherit, directly or indirectly, from classes that add
37  * functions, and to wrap the stream in the constructor with classes that
38  * are simply subclasses of FilterInputStream.
39  * <p>
40  * We may want to merge MessageInputStream and DotTerminatedInputStream.
41  *
42  * @author Doug Porter
43  */

44 public class MessageInputStream
45 extends LineInputStream
46 {
47     /**
48      * Constructs a message input stream from the specified input stream.
49      */

50     public MessageInputStream (InputStream JavaDoc in)
51     {
52         super (new DotTerminatedInputStream (in));
53     }
54     
55     /**
56      * Log the class and superclasses for the given stream, and for
57      * FilterInputStreams the class and superclasses of FilterInputStream.in.
58      * <p>
59      * We may want to put this in a different class.
60      *
61      * @param stream The input stream to log
62      */

63     public static void logClass (InputStream JavaDoc stream)
64     {
65         Class JavaDoc thisClass = stream.getClass ();
66         logClass (thisClass.toString() + " classes and superclasses:", stream);
67     }
68     
69     /**
70      * Log the class and superclasses for the given stream, and for
71      * FilterInputStreams the class and superclasses of FilterInputStream.in.
72      * <p>
73      * We may want to put this in a different class.
74      *
75      * @param stream The input stream to log
76      */

77     public static void logClass (String JavaDoc message,
78                                  InputStream JavaDoc stream)
79     {
80         Session.log (message);
81         
82         Class JavaDoc thisClass = stream.getClass ();
83         
84         if (stream instanceof FilterInputStream JavaDoc) {
85             
86             try {
87                 // find FilterInputStream
88
final String JavaDoc FilterInputClass = "class java.io.FilterInputStream";
89                 Class JavaDoc superClass = thisClass;
90                 while (! superClass.toString ().equals (FilterInputClass)) {
91                     
92                     superClass = superClass.getSuperclass ();
93                     Session.log (" " + superClass.toString());
94                 }
95                 
96                 // find FilterInputStream.in
97
java.lang.reflect.Field JavaDoc inField = superClass.getDeclaredField("in");
98                 InputStream JavaDoc innerStream = (java.io.InputStream JavaDoc) inField.get(stream);
99                 Session.log (thisClass.toString() + ".in follows");
100                 
101                 // recurse
102
logClass (innerStream);
103             }
104             catch (Exception JavaDoc e) {
105                 Session.log (thisClass.toString() + ": " + e);
106                 e.printStackTrace ();
107             }
108         }
109         else {
110             Session.logClass (stream);
111         }
112     }
113 }
114
Popular Tags