KickJava   Java API By Example, From Geeks To Geeks.

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


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
35 /**
36  * Implements an encoding reader for UTF-16 using the reversed order.
37  */

38 public class UTF16_REVReader extends EncodingReader {
39   private InputStream JavaDoc is;
40
41   /**
42    * Null-arg constructor for instantiation by com.caucho.vfs.Encoding only.
43    */

44   public UTF16_REVReader()
45   {
46   }
47
48   /**
49    * Create a UTF-16 reader using the input stream to provide bytes.
50    */

51   private UTF16_REVReader(InputStream JavaDoc is)
52   {
53     this.is = is;
54   }
55
56   /**
57    * Create a UTF-16 reader based on the readStream.
58    *
59    * @param is the input stream providing the bytes.
60    * @param javaEncoding the JDK name for the encoding.
61    *
62    * @return the UTF-16 reader.
63    */

64   public Reader JavaDoc create(InputStream JavaDoc is, String JavaDoc javaEncoding)
65   {
66     return new UTF16_REVReader(is);
67   }
68
69   /**
70    * Reads into a character buffer using the correct encoding.
71    *
72    * @return the next character or -1 on end of file.
73    */

74   public int read()
75     throws IOException JavaDoc
76   {
77     int ch1 = is.read();
78     int ch2 = is.read();
79
80     if (ch2 < 0)
81       return -1;
82
83     return (ch2 << 8) + ch1;
84   }
85
86   /**
87    * Reads into a character buffer using the correct encoding.
88    *
89    * @param cbuf character buffer receiving the data.
90    * @param off starting offset into the buffer.
91    * @param len number of characters to read.
92    *
93    * @return the number of characters read or -1 on end of file.
94    */

95   public int read(char []cbuf, int off, int len)
96     throws IOException JavaDoc
97   {
98     int i = 0;
99
100     for (i = 0; i < len; i++) {
101       int ch1 = is.read();
102       int ch2 = is.read();
103
104       if (ch2 < 0)
105     return i == 0 ? -1 : i;
106
107       cbuf[off + i] = (char) ((ch2 << 8) + ch1);
108     }
109
110     return i;
111   }
112 }
113
Popular Tags