2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / java / nio / charset / UTF_16Decoder.java
blobc8e474d5741de1a56a6f428fd852bf8e047fc55d
1 /* UTF_16Decoder.java --
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.java.nio.charset;
40 import java.nio.ByteBuffer;
41 import java.nio.CharBuffer;
42 import java.nio.charset.Charset;
43 import java.nio.charset.CharsetDecoder;
44 import java.nio.charset.CoderResult;
46 /**
47 * Decoder for UTF-16, UTF-15LE, and UTF-16BE.
49 * @author Jesse Rosenstock
51 final class UTF_16Decoder extends CharsetDecoder
53 // byte orders
54 static final int BIG_ENDIAN = 0;
55 static final int LITTLE_ENDIAN = 1;
56 static final int UNKNOWN_ENDIAN = 2;
58 private static final char BYTE_ORDER_MARK = '\uFEFF';
59 private static final char REVERSED_BYTE_ORDER_MARK = '\uFFFE';
61 private final int originalByteOrder;
62 private int byteOrder;
64 UTF_16Decoder (Charset cs, int byteOrder)
66 super (cs, 0.5f, 1.0f);
67 this.originalByteOrder = byteOrder;
68 this.byteOrder = byteOrder;
71 protected CoderResult decodeLoop (ByteBuffer in, CharBuffer out)
73 // TODO: Optimize this in the case in.hasArray() / out.hasArray()
75 int inPos = in.position ();
76 try
78 while (in.remaining () >= 2)
80 byte b1 = in.get ();
81 byte b2 = in.get ();
83 // handle byte order mark
84 if (byteOrder == UNKNOWN_ENDIAN)
86 char c = (char) ((b1 << 8) | b2);
87 if (c == BYTE_ORDER_MARK)
89 byteOrder = BIG_ENDIAN;
90 inPos += 2;
91 continue;
93 else if (c == REVERSED_BYTE_ORDER_MARK)
95 byteOrder = LITTLE_ENDIAN;
96 inPos += 2;
97 continue;
99 else
101 // assume big endian, do not consume bytes,
102 // continue with normal processing
103 byteOrder = BIG_ENDIAN;
107 char c = byteOrder == BIG_ENDIAN ? (char) ((b1 << 8) | b2)
108 : (char) ((b2 << 8) | b1);
110 if (0xD800 <= c && c <= 0xDFFF)
112 // c is a surrogate
114 // make sure c is a high surrogate
115 if (c > 0xDBFF)
116 return CoderResult.malformedForLength (2);
117 if (in.remaining () < 2)
118 return CoderResult.UNDERFLOW;
119 byte b3 = in.get ();
120 byte b4 = in.get ();
121 char d = byteOrder == BIG_ENDIAN ? (char) ((b3 << 8) | b4)
122 : (char) ((b4 << 8) | b3);
123 // make sure d is a low surrogate
124 if (d < 0xDC00 || d > 0xDFFF)
125 return CoderResult.malformedForLength (2);
126 out.put (c);
127 out.put (d);
128 inPos += 4;
130 else
132 if (!out.hasRemaining ())
133 return CoderResult.UNDERFLOW;
134 out.put (c);
135 inPos += 2;
139 return CoderResult.UNDERFLOW;
141 finally
143 in.position (inPos);
148 * Writes <code>c</code> to <code>out</code> in the byte order
149 * specified by <code>byteOrder</code>.
151 private void put (ByteBuffer out, char c)
153 if (byteOrder == BIG_ENDIAN)
155 out.put ((byte) (c >> 8));
156 out.put ((byte) (c & 0xFF));
158 else
160 out.put ((byte) (c & 0xFF));
161 out.put ((byte) (c >> 8));
165 protected void implReset ()
167 byteOrder = originalByteOrder;