KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > i18n > EncodingReader


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.vfs.i18n;
30
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.Reader JavaDoc;
34 import java.io.UnsupportedEncodingException JavaDoc;
35
36 /**
37  * Abstract class for a byte-to-character encoding reader and its
38  * associated factory.
39  *
40  * <p/>Implementations need to implement <code>create</code>
41  * and <code>read()</code> at minimum. Efficient implementations will
42  * also implement the <code>read</code> into a buffer.
43  *
44  * <p/>Implementations should not buffer the bytes.
45  */

46 abstract public class EncodingReader extends Reader JavaDoc {
47   private String JavaDoc javaEncoding;
48
49   public String JavaDoc getJavaEncoding()
50   {
51     return javaEncoding;
52   }
53
54   public void setJavaEncoding(String JavaDoc encoding)
55   {
56     this.javaEncoding = encoding;
57   }
58   /**
59    * Returns a new encoding reader for the given stream and javaEncoding.
60    *
61    * @param is the input stream providing the bytes.
62    * @param javaEncoding the JDK name for the encoding.
63    *
64    * @return an encoding reader or null for ISO-8859-1.
65    */

66   public abstract Reader JavaDoc create(InputStream JavaDoc is, String JavaDoc javaEncoding)
67     throws UnsupportedEncodingException JavaDoc;
68
69   /**
70    * Returns the next character using the correct encoding.
71    *
72    * @return the next character or -1 on end of file.
73    */

74   public abstract int read()
75     throws IOException JavaDoc;
76
77   /**
78    * Reads into a character buffer using the correct encoding.
79    *
80    * @param cbuf character buffer receiving the data.
81    * @param off starting offset into the buffer.
82    * @param len number of characters to read.
83    *
84    * @return the number of characters read or -1 on end of file.
85    */

86   public int read(char []cbuf, int off, int len)
87     throws IOException JavaDoc
88   {
89     for (int i = 0; i < len; i++) {
90       int ch = read();
91
92       if (ch < 0)
93         return len;
94
95       cbuf[off + i] = (char) ch;
96     }
97
98     return len;
99   }
100
101   /**
102    * Closes the reader, possibly returning it to a pool.
103    */

104   public void close()
105   {
106   }
107 }
108
Popular Tags