KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ReadFilterInputStream.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.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29
30 import javax.mail.Session JavaDoc;
31
32 /**
33  * A filter input stream that invokes read() from read(byte[]) and read(byte[],int,int).
34  * <p>
35  * This is different from FilterInputStream in that FilterInputStream invokes
36  * in.read(byte[]) from read(byte[]), and in.read(byte[],int,int) from read(byte[],int,int).
37  * In turn in.read(byte[]) calls in.read().
38  * This means that FilterInputStream never calls the read() method of subclasses.
39  * This class allows subclasses to override just the read() method and have other
40  * methods in superclasses eventually use that subclass's read() method.
41  *
42  * @author Doug Porter
43  */

44 public class ReadFilterInputStream
45 extends FilterInputStream JavaDoc
46 {
47     /**
48     * Constructor.
49     * @param in the inner input stream
50     */

51     public ReadFilterInputStream (InputStream JavaDoc in) {
52         super (in);
53     }
54     
55     /**
56      * Reads from this stream into the specified byte array.
57      * @param b byte array to read into
58      * @return The number of bytes read, or -1 on end of stream
59      */

60     public int read (byte[] b)
61     throws IOException JavaDoc
62     {
63         return read (b, 0, b.length);
64     }
65     
66     /**
67      * Reads from this stream into the specified byte array, at the offset and
68      * for the length specified.
69      * <p>
70      * On end of stream, returns -1.
71      *
72      * @param b byte array to read into
73      * @param off offset in byte array at which to begin reading
74      * @param len maximum number of bytes to read
75      * @return The number of bytes read, or -1 on end of stream
76      */

77     public int read (byte[] b,
78                      int off,
79                      int len)
80     throws IOException JavaDoc
81     {
82         final int EOS = -1;
83         int i = 0;
84         int n;
85         while (i < len &&
86                (n = read ()) >= 0) {
87                    
88             b [off + i] = (byte) n;
89             ++ i;
90             
91         }
92         
93         int result;
94         if (i > 0) {
95             
96             result = i;
97             
98         }
99         else {
100                 
101             result = EOS;
102             
103         }
104         
105         return result;
106     }
107 }
108
109
Popular Tags