ws-mirae-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From i..@apache.org
Subject svn commit: r367177 - /webservices/mirae/trunk/src/mirae/jaxp/org/apache/mirae/j2me/xml/Utf16Reader.java
Date Mon, 09 Jan 2006 03:32:27 GMT
Author: ias
Date: Sun Jan  8 19:32:25 2006
New Revision: 367177

URL: http://svn.apache.org/viewcvs?rev=367177&view=rev
Log:
Initial drop to support UTF-16.

Added:
    webservices/mirae/trunk/src/mirae/jaxp/org/apache/mirae/j2me/xml/Utf16Reader.java

Added: webservices/mirae/trunk/src/mirae/jaxp/org/apache/mirae/j2me/xml/Utf16Reader.java
URL: http://svn.apache.org/viewcvs/webservices/mirae/trunk/src/mirae/jaxp/org/apache/mirae/j2me/xml/Utf16Reader.java?rev=367177&view=auto
==============================================================================
--- webservices/mirae/trunk/src/mirae/jaxp/org/apache/mirae/j2me/xml/Utf16Reader.java (added)
+++ webservices/mirae/trunk/src/mirae/jaxp/org/apache/mirae/j2me/xml/Utf16Reader.java Sun
Jan  8 19:32:25 2006
@@ -0,0 +1,114 @@
+/*
+ * Copyright 2001-2006 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mirae.j2me.xml;
+
+import java.io.Reader;
+import java.io.InputStream;
+import java.io.IOException;
+
+/**
+ * A Reader for UTF-16 encoded byte streams.
+ */
+public class Utf16Reader extends Reader {
+    private InputStream wrappedStream;
+
+    private boolean bigEndian;
+
+    /**
+     * Create a Utf16Reader that wraps an input stream and uses endian switch.
+     * 
+     * @param wrappedStream -
+     *            an input stream to wrap.
+     * @param bigEndian -
+     *            whether big endian or not.
+     */
+    public Utf16Reader(InputStream wrappedStream, boolean bigEndian) {
+        this.wrappedStream = wrappedStream;
+        this.bigEndian = bigEndian;
+    }
+
+    /**
+     * Read characters into a portion of an array.
+     * 
+     * @param b -
+     *            destination buffer.
+     * @param off -
+     *            the start offset of the characters.
+     * @param len -
+     *            the maximum number of characters read.
+     * 
+     * @exception IOException -
+     *                if an I/O error occurs.
+     */
+    public int read(char[] b, int off, int len) throws IOException {
+        int size = 0;
+        int readCharacter;
+        if (bigEndian) {
+            while (size < len) {
+                readCharacter = wrappedStream.read();
+                if (readCharacter < 0) {
+                    return (size == 0) ? -1 : size;
+                }
+                b[off++] = (char) ((readCharacter << 8) | (wrappedStream.read() &
0xff));
+                size++;
+            }
+        } else {
+            while (size < len) {
+                readCharacter = wrappedStream.read();
+                if (readCharacter < 0) {
+                    return (size == 0) ? -1 : size;
+                }
+                b[off++] = (char) ((wrappedStream.read() << 8) | (readCharacter &
0xff));
+                size++;
+            }
+        }
+        return size;
+    }
+
+    /**
+     * Read a single character.
+     * 
+     * @return the character read, or -1 if the end of the stream has been
+     *         reachend.
+     * 
+     * @exception IOException -
+     *                if an I/O error occurs.
+     */
+    public int read() throws IOException {
+        int readCharacter = wrappedStream.read();
+        if (readCharacter < 0) {
+            return -1;
+        }
+        if (bigEndian) {
+            readCharacter = (char) ((readCharacter << 8) | (wrappedStream
+                    .read() & 0xff));
+        } else {
+            readCharacter = (char) ((wrappedStream.read() << 8) | (readCharacter &
0xff));
+        }
+        return readCharacter;
+    }
+
+    /**
+     * Close the stream.
+     * 
+     * @exception IOException -
+     *                if an I/O error occurs.
+     */
+    public void close() throws IOException {
+        wrappedStream.close();
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: mirae-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: mirae-dev-help@ws.apache.org


Mime
View raw message