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)
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
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
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
;
47 * Decoder for UTF-16, UTF-15LE, and UTF-16BE.
49 * @author Jesse Rosenstock
51 final class UTF_16Decoder
extends CharsetDecoder
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 ();
78 while (in
.remaining () >= 2)
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
;
93 else if (c
== REVERSED_BYTE_ORDER_MARK
)
95 byteOrder
= LITTLE_ENDIAN
;
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)
114 // make sure c is a high surrogate
116 return CoderResult
.malformedForLength (2);
117 if (in
.remaining () < 2)
118 return CoderResult
.UNDERFLOW
;
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);
132 if (!out
.hasRemaining ())
133 return CoderResult
.UNDERFLOW
;
139 return CoderResult
.UNDERFLOW
;
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));
160 out
.put ((byte) (c
& 0xFF));
161 out
.put ((byte) (c
>> 8));
165 protected void implReset ()
167 byteOrder
= originalByteOrder
;