jdk/src/share/classes/sun/nio/cs/UTF_32Coder.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7542 514d58003c20
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7542
diff changeset
     2
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3714
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3714
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3714
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3714
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3714
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.nio.cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.charset.CoderResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.charset.CharsetDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.charset.CharsetEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
class UTF_32Coder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    protected static final int BOM_BIG = 0xFEFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    protected static final int BOM_LITTLE = 0xFFFE0000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    protected static final int NONE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    protected static final int BIG = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    protected static final int LITTLE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    protected static class Decoder extends CharsetDecoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        private int currentBO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        private int expectedBO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        protected Decoder(Charset cs, int bo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            super(cs, 0.25f, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            this.expectedBO = bo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            this.currentBO = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        private int getCP(ByteBuffer src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            return (currentBO==BIG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
              ?(((src.get() & 0xff) << 24) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                ((src.get() & 0xff) << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                ((src.get() & 0xff) <<  8) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                (src.get() & 0xff))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
              :((src.get() & 0xff) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                ((src.get() & 0xff) <<  8) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                ((src.get() & 0xff) << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                ((src.get() & 0xff) << 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if (src.remaining() < 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            int mark = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            int cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                if (currentBO == NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    cp = ((src.get() & 0xff) << 24) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                         ((src.get() & 0xff) << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                         ((src.get() & 0xff) <<  8) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                         (src.get() & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    if (cp == BOM_BIG && expectedBO != LITTLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                        currentBO = BIG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                        mark += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    } else if (cp == BOM_LITTLE && expectedBO != BIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                        currentBO = LITTLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                        mark += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                        if (expectedBO == NONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                            currentBO = BIG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                            currentBO = expectedBO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                        src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                }
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
    89
                while (src.remaining() >= 4) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    cp = getCP(src);
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
    91
                    if (Character.isBmpCodePoint(cp)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        if (!dst.hasRemaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                            return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        mark += 4;
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
    95
                        dst.put((char) cp);
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
    96
                    } else if (Character.isValidCodePoint(cp)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        if (dst.remaining() < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                            return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                        mark += 4;
5991
288afdbbca28 6933322: Add methods highSurrogate(), lowSurrogate() to class Character
martin
parents: 5986
diff changeset
   100
                        dst.put(Character.highSurrogate(cp));
288afdbbca28 6933322: Add methods highSurrogate(), lowSurrogate() to class Character
martin
parents: 5986
diff changeset
   101
                        dst.put(Character.lowSurrogate(cp));
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   102
                    } else {
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   103
                        return CoderResult.malformedForLength(4);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        protected void implReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            currentBO = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    protected static class Encoder extends CharsetEncoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        private boolean doBOM = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        private boolean doneBOM = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        private int byteOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        protected void put(int cp, ByteBuffer dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (byteOrder==BIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                dst.put((byte)(cp >> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                dst.put((byte)(cp >> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                dst.put((byte)(cp >> 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                dst.put((byte)cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                dst.put((byte)cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                dst.put((byte)(cp >>  8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                dst.put((byte)(cp >> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                dst.put((byte)(cp >> 24));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        protected Encoder(Charset cs, int byteOrder, boolean doBOM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            super(cs, 4.0f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                  doBOM?8.0f:4.0f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                  (byteOrder==BIG)?new byte[]{(byte)0, (byte)0, (byte)0xff, (byte)0xfd}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                  :new byte[]{(byte)0xfd, (byte)0xff, (byte)0, (byte)0});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            this.byteOrder = byteOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            this.doBOM = doBOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            this.doneBOM = !doBOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            int mark = src.position();
7542
514d58003c20 6415373: (cs) UnicodeEncoder emits BOM when there are no bytes to encode
sherman
parents: 5991
diff changeset
   147
            if (!doneBOM && src.hasRemaining()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                if (dst.remaining() < 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                put(BOM_BIG, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                doneBOM = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    char c = src.get();
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   156
                    if (!Character.isSurrogate(c)) {
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   157
                        if (dst.remaining() < 4)
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   158
                            return CoderResult.OVERFLOW;
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   159
                        mark++;
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   160
                        put(c, dst);
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   161
                    } else if (Character.isHighSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        if (!src.hasRemaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                            return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        char low = src.get();
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 2
diff changeset
   165
                        if (Character.isLowSurrogate(low)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                            if (dst.remaining() < 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                            mark += 2;
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   169
                            put(Character.toCodePoint(c, low), dst);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                            return CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        }
5986
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   173
                    } else {
04eb44085c00 6934265: Add public method Character.isBmpCodePoint
martin
parents: 5506
diff changeset
   174
                        // assert Character.isLowSurrogate(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        return CoderResult.malformedForLength(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        protected void implReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            doneBOM = !doBOM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
}