KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * QPInputStream.java
3  * Copyright (C) 2002 The Free Software Foundation
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  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package gnu.mail.util;
21
22 import java.io.*;
23
24 /**
25  * A Quoted-Printable decoder stream.
26  *
27  * @author <a HREF="mailto:dog@gnu.org">Chris Burdess</a>
28  */

29 public class QPInputStream
30   extends FilterInputStream
31 {
32
33   protected byte[] buf;
34
35   /**
36    * The number of times read() will return a space.
37    */

38   protected int spaceCount;
39
40   private static final int LF = 10;
41   private static final int CR = 13;
42   private static final int SPACE = 32;
43   private static final int EQ = 61;
44
45   /**
46    * Constructor.
47    * @param in the underlying input stream.
48    */

49   public QPInputStream(InputStream in)
50   {
51     super(new PushbackInputStream(in, 2));
52     buf = new byte[2];
53   }
54
55   /**
56    * Read a character from the stream.
57    */

58   public int read()
59     throws IOException
60   {
61     if (spaceCount>0)
62     {
63       spaceCount--;
64       return SPACE;
65     }
66     
67     int c = in.read();
68     if (c==SPACE)
69     {
70       while ((c = in.read())==SPACE)
71         spaceCount++;
72       if (c==LF || c==CR || c==-1)
73         spaceCount = 0;
74       else
75       {
76         ((PushbackInputStream)in).unread(c);
77         c = SPACE;
78       }
79       return c;
80     }
81     if (c==EQ)
82     {
83       int c2 = super.in.read();
84       if (c2==LF)
85         return read();
86       if (c2==CR)
87       {
88         int peek = in.read();
89         if (peek!=LF)
90           ((PushbackInputStream)in).unread(peek);
91         return read();
92       }
93       if (c2==-1)
94         return c2;
95       
96       buf[0] = (byte)c2;
97       buf[1] = (byte)in.read();
98       try
99       {
100         return Integer.parseInt(new String JavaDoc(buf, 0, 2), 16);
101       }
102       catch (NumberFormatException JavaDoc e)
103       {
104         ((PushbackInputStream)in).unread(buf);
105       }
106       return c;
107     }
108     else
109       return c;
110   }
111
112   /**
113    * Reads from the underlying stream into the specified byte array.
114    */

115   public int read(byte[] bytes, int off, int len)
116     throws IOException
117   {
118     int pos = 0;
119     try
120     {
121       while (pos<len)
122       {
123         int c = read();
124         if (c==-1)
125         {
126           if (pos==0)
127             pos = -1;
128           break;
129         }
130         bytes[off+pos] = (byte)c;
131         pos++;
132       }
133
134     }
135     catch (IOException e)
136     {
137       pos = -1;
138     }
139     return pos;
140   }
141
142   /**
143    * Mark is not supported.
144    */

145   public boolean markSupported()
146   {
147     return false;
148   }
149
150   /**
151    * Returns the number of bytes that can be read without blocking.
152    */

153   public int available()
154     throws IOException
155   {
156     return in.available();
157   }
158   
159 }
160
Popular Tags