KickJava   Java API By Example, From Geeks To Geeks.

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


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.Charset JavaDoc;
44 import java.nio.charset.CharsetDecoder JavaDoc;
45 import java.nio.charset.CoderResult JavaDoc;
46 import java.nio.charset.CodingErrorAction JavaDoc;
47
48 /**
49  * FilterInputStream that decodes a bytestream into a characterstream.
50  *
51  * @author Timo Stich <tstich@users.sourceforge.net>
52  */

53 public class CharsetDecoderInputStream extends FilterInputStream JavaDoc {
54
55     private Charset JavaDoc charset;
56     private CharsetDecoder JavaDoc decoder;
57
58     private ByteBuffer JavaDoc inBytes;
59     private CharBuffer JavaDoc outBytes;
60     
61     /**
62      * Constructs a CharsetDecoderInputStream.
63      *
64      * @param arg0 The raw bytestream
65      * @param charset The charset of the characterstream
66      */

67     public CharsetDecoderInputStream(InputStream JavaDoc arg0, Charset JavaDoc charset) {
68         super(arg0);
69         this.charset = charset;
70         decoder = charset.newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
71         
72         inBytes = ByteBuffer.allocate(5);
73         outBytes = CharBuffer.allocate(1);
74     }
75     
76     /**
77      * Read one byte (two if 16-bit charset) from the stream and
78      * decode it to the charset.
79      *
80      * @return the decoded char
81      * @throws IOException
82      */

83     private int decodeNextChar() throws IOException JavaDoc {
84         CoderResult JavaDoc result;
85         int read;
86
87         
88         inBytes.clear();
89         inBytes.limit(1);
90
91         read = in.read();
92         if( read == -1) return -1;
93         inBytes.put( 0, (byte) read );
94         
95         outBytes.clear();
96         result = decoder.decode(inBytes, outBytes, in.available() == 0);
97         
98                 
99         // Do we need to read a second byte?
100
while( outBytes.position() == 0 ) {
101             read = in.read();
102             if( read == -1) return -1;
103             inBytes.limit(inBytes.limit()+1);
104             inBytes.put(inBytes.limit()-1,(byte) read);
105
106             outBytes.clear();
107             result = decoder.decode(inBytes, outBytes, in.available() == 0);
108         }
109         
110         return outBytes.position();
111     }
112     
113     
114     /**
115      * @see java.io.InputStream#read()
116      */

117     public int read() throws IOException JavaDoc {
118         if( decodeNextChar() == -1) return -1;
119         
120         return outBytes.get(0);
121     }
122     
123     /**
124      * @see java.io.InputStream#read(byte[], int, int)
125      */

126     public int read(byte[] arg0, int arg1, int arg2) throws IOException JavaDoc {
127         int next;
128         for( int i=0; i<arg2; i++) {
129             next = read();
130             if( next == -1 ) {
131                 if( i == 0 ) {
132                     return -1;
133                 } else {
134                     return i;
135                 }
136             }
137             arg0[arg1+i] = (byte) next;
138         }
139         return arg2;
140     }
141     
142
143 }
144
Popular Tags