KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > coder > FallbackCharsetDecoderInputStream


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.coder;
37
38 import java.io.FilterInputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.InputStream JavaDoc;
41 import java.nio.ByteBuffer JavaDoc;
42 import java.nio.CharBuffer JavaDoc;
43 import java.nio.charset.CharacterCodingException JavaDoc;
44 import java.nio.charset.Charset JavaDoc;
45 import java.nio.charset.CharsetDecoder JavaDoc;
46 import java.nio.charset.CharsetEncoder JavaDoc;
47 import java.nio.charset.CoderResult JavaDoc;
48 import java.nio.charset.CodingErrorAction JavaDoc;
49 import java.util.Hashtable JavaDoc;
50
51 /**
52  * FilterInputStream that decodes a bytestream into a characterstream.
53  *
54  * @author Timo Stich <tstich@users.sourceforge.net>
55  */

56 public class FallbackCharsetDecoderInputStream extends FilterInputStream JavaDoc {
57
58     private Charset JavaDoc charset;
59     private CharsetDecoder JavaDoc decoder;
60     private ByteBuffer JavaDoc inBytes;
61     private CharBuffer JavaDoc outChars;
62
63     private static Hashtable JavaDoc fallbackTable = initFallBackTable();
64     
65     /**
66      * Constructs a CharsetDecoderInputStream.
67      *
68      * @param arg0 The raw bytestream
69      * @param charset The charset of the characterstream
70      */

71     public FallbackCharsetDecoderInputStream(InputStream JavaDoc arg0, Charset JavaDoc charset) {
72         super(arg0);
73         this.charset = charset;
74         initDecoder();
75         
76         inBytes = ByteBuffer.allocate(5);
77         outChars = CharBuffer.allocate(1);
78     }
79     
80     private static Hashtable JavaDoc initFallBackTable() {
81         Hashtable JavaDoc table = new Hashtable JavaDoc();
82         
83         table.put(Charset.forName("iso-8859-15"), Charset.forName("windows-1252"));
84         table.put(Charset.forName("iso-8859-1"), Charset.forName("windows-1252"));
85         table.put(Charset.forName("us-ascii"), Charset.forName("windows-1252"));
86         
87         return table;
88     }
89     
90     /**
91      * Add a fallback for this charset.
92      *
93      * @param original
94      * @param fallback
95      */

96     public static void addFallback(Charset JavaDoc original, Charset JavaDoc fallback) {
97         fallbackTable.put(original, fallback);
98     }
99     
100     /**
101      * Read one byte (two if 16-bit charset) from the stream and
102      * decode it to the charset.
103      *
104      * @return the decoded char
105      * @throws IOException
106      */

107     private int decodeNextChar() throws IOException JavaDoc {
108         CoderResult JavaDoc result;
109         int read;
110
111         
112         inBytes.clear();
113         inBytes.limit(1);
114
115         read = in.read();
116         if( read == -1) return -1;
117         inBytes.put( 0, (byte) read );
118         
119         outChars.clear();
120         result = decoder.decode(inBytes, outChars, in.available() == 0);
121
122                 
123         // Do we need to read a second byte?
124
if( outChars.position() == 0 ) {
125             read = in.read();
126             if( read == -1) return -1;
127             inBytes.limit(inBytes.limit()+1);
128             inBytes.put(inBytes.limit()-1,(byte) read);
129
130             outChars.clear();
131             result = decoder.decode(inBytes, outChars, in.available() == 0);
132         }
133         
134         ByteBuffer JavaDoc test = charset.encode(outChars);
135         while( test.capacity() == 0 && fallback()) {
136             inBytes.rewind();
137             outChars.clear();
138             result = decoder.decode(inBytes, outChars, in.available() == 0);
139             test = charset.encode(outChars);
140         }
141         
142         return outChars.position();
143     }
144     
145     
146     private boolean fallback() {
147         if( fallbackTable.containsKey(charset)) {
148             this.charset = (Charset JavaDoc)fallbackTable.get(charset);
149             initDecoder();
150             
151             return true;
152         }
153         
154         return false;
155     }
156     
157     private void initDecoder() {
158         decoder = charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
159     }
160     
161
162     /**
163      * @see java.io.InputStream#read()
164      */

165     public int read() throws IOException JavaDoc {
166         if( decodeNextChar() == -1) return -1;
167         
168         return outChars.get(0);
169     }
170     
171     /**
172      * @see java.io.InputStream#read(byte[], int, int)
173      */

174     public int read(byte[] arg0, int arg1, int arg2) throws IOException JavaDoc {
175         int next;
176         for( int i=0; i<arg2; i++) {
177             next = read();
178             if( next == -1 ) {
179                 if( i == 0 ) {
180                     return -1;
181                 } else {
182                     return i;
183                 }
184             }
185             arg0[arg1+i] = (byte) next;
186         }
187         return arg2;
188     }
189     
190
191 }
192
Popular Tags