blob: 67afe2f897c406b1cfce989bf9328999ca7679a0 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.nio.cs;
27
28import java.nio.ByteBuffer;
29import java.nio.CharBuffer;
30import java.nio.charset.Charset;
31import java.nio.charset.CharsetDecoder;
32import java.nio.charset.CoderResult;
33import java.nio.charset.CharacterCodingException;
34import java.nio.charset.MalformedInputException;
35
36
37abstract class UnicodeDecoder extends CharsetDecoder {
38
39 protected static final char BYTE_ORDER_MARK = (char) 0xfeff;
40 protected static final char REVERSED_MARK = (char) 0xfffe;
41
42 protected static final int NONE = 0;
43 protected static final int BIG = 1;
44 protected static final int LITTLE = 2;
45
46 private final int expectedByteOrder;
47 private int currentByteOrder;
48 private int defaultByteOrder = BIG;
49
50 public UnicodeDecoder(Charset cs, int bo) {
51 super(cs, 0.5f, 1.0f);
52 expectedByteOrder = currentByteOrder = bo;
53 }
54
55 public UnicodeDecoder(Charset cs, int bo, int defaultBO) {
56 this(cs, bo);
57 defaultByteOrder = defaultBO;
58 }
59
60 private char decode(int b1, int b2) {
61 if (currentByteOrder == BIG)
62 return (char)((b1 << 8) | b2);
63 else
64 return (char)((b2 << 8) | b1);
65 }
66
67 protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
68 int mark = src.position();
69
70 try {
71 while (src.remaining() > 1) {
72 int b1 = src.get() & 0xff;
73 int b2 = src.get() & 0xff;
74
75 // Byte Order Mark interpretation
76 if (currentByteOrder == NONE) {
77 char c = (char)((b1 << 8) | b2);
78 if (c == BYTE_ORDER_MARK) {
79 currentByteOrder = BIG;
80 mark += 2;
81 continue;
82 } else if (c == REVERSED_MARK) {
83 currentByteOrder = LITTLE;
84 mark += 2;
85 continue;
86 } else {
87 currentByteOrder = defaultByteOrder;
88 // FALL THROUGH to process b1, b2 normally
89 }
90 }
91
92 char c = decode(b1, b2);
93
94 if (c == REVERSED_MARK) {
95 // A reversed BOM cannot occur within middle of stream
96 return CoderResult.malformedForLength(2);
97 }
98
99 // Surrogates
100 if (Surrogate.is(c)) {
101 if (Surrogate.isHigh(c)) {
102 if (src.remaining() < 2)
103 return CoderResult.UNDERFLOW;
104 char c2 = decode(src.get() & 0xff, src.get() & 0xff);
105 if (!Surrogate.isLow(c2))
106 return CoderResult.malformedForLength(4);
107 if (dst.remaining() < 2)
108 return CoderResult.OVERFLOW;
109 mark += 4;
110 dst.put(c);
111 dst.put(c2);
112 continue;
113 }
114 // Unpaired low surrogate
115 return CoderResult.malformedForLength(2);
116 }
117
118 if (!dst.hasRemaining())
119 return CoderResult.OVERFLOW;
120 mark += 2;
121 dst.put(c);
122
123 }
124 return CoderResult.UNDERFLOW;
125
126 } finally {
127 src.position(mark);
128 }
129 }
130
131 protected void implReset() {
132 currentByteOrder = expectedByteOrder;
133 }
134
135}