History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/sun/nio/cs/UnicodeDecoder.java (1 of 18352 files). Show all

Unified · Split

java/pkg/sun/nio/cs/UnicodeDecoder.java+135−0
@@ -0,0 +1,135 @@
1+/*
2+ * Copyright (c) 2000, 2006, Oracle and/or its affiliates. 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. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+ * or visit www.oracle.com if you need additional information or have any
23+ * questions.
24+ */
25+
26+package sun.nio.cs;
27+
28+import java.nio.ByteBuffer;
29+import java.nio.CharBuffer;
30+import java.nio.charset.Charset;
31+import java.nio.charset.CharsetDecoder;
32+import java.nio.charset.CoderResult;
33+import java.nio.charset.CharacterCodingException;
34+import java.nio.charset.MalformedInputException;
35+
36+
37+abstract 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 (Character.isSurrogate(c)) {
101+ if (Character.isHighSurrogate(c)) {
102+ if (src.remaining() < 2)
103+ return CoderResult.UNDERFLOW;
104+ char c2 = decode(src.get() & 0xff, src.get() & 0xff);
105+ if (!Character.isLowSurrogate(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+}