jdk/src/share/classes/sun/io/CharToByteUnicode.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
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: 5506
diff changeset
     2
 * Copyright (c) 1996, 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: 4818
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: 4818
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: 4818
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4818
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4818
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.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Convert arrays containing Unicode characters into arrays of bytes, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * platform-default byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author      Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class CharToByteUnicode extends CharToByteConverter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static final char BYTE_ORDER_MARK = (char) 0xfeff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    protected boolean usesMark = true;      /* A mark should be written */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private boolean markWritten = false;  /* A mark has been written */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static final int UNKNOWN = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static final int BIG = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static final int LITTLE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    protected int byteOrder = UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public CharToByteUnicode() {
4818
fd477db6c4ee 6921740: Eliminate warnings from sun.io converters and allow compiling with JAVAC_MAX_WARNINGS=true
andrew
parents: 2
diff changeset
    49
        String enc = java.security.AccessController.doPrivileged(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
           new sun.security.action.GetPropertyAction("sun.io.unicode.encoding",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                                          "UnicodeBig"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (enc.equals("UnicodeBig"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            byteOrder = BIG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        else if (enc.equals("UnicodeLittle"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            byteOrder = LITTLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            byteOrder = BIG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public CharToByteUnicode(int byteOrder, boolean usesMark) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.byteOrder = byteOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.usesMark = usesMark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public CharToByteUnicode(boolean usesMark) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this.usesMark = usesMark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public String getCharacterEncoding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        switch (byteOrder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        case BIG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            return usesMark ? "UnicodeBig" : "UnicodeBigUnmarked";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        case LITTLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            return usesMark ? "UnicodeLittle" : "UnicodeLittleUnmarked";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            return "UnicodeUnknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public int convert(char in[], int inOff, int inEnd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                       byte out[], int outOff, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        throws ConversionBufferFullException, MalformedInputException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        charOff = inOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        byteOff = outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (inOff >= inEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        int inI = inOff,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            outI = outOff,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            outTop = outEnd - 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (usesMark && !markWritten) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (outI > outTop)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                throw new ConversionBufferFullException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (byteOrder == BIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                out[outI++] = (byte) (BYTE_ORDER_MARK >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                out[outI++] = (byte) (BYTE_ORDER_MARK & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                out[outI++] = (byte) (BYTE_ORDER_MARK & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                out[outI++] = (byte) (BYTE_ORDER_MARK >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            markWritten = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (byteOrder == BIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            while (inI < inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                if (outI > outTop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    charOff = inI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    byteOff = outI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    throw new ConversionBufferFullException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                char c = in[inI++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                out[outI++] = (byte) (c >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                out[outI++] = (byte) (c & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            while (inI < inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                if (outI > outTop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    charOff = inI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    byteOff = outI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    throw new ConversionBufferFullException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                char c = in[inI++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                out[outI++] = (byte) (c & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                out[outI++] = (byte) (c >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        charOff = inI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        byteOff = outI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return outI - outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public int flush(byte in[], int inOff, int inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        byteOff = charOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public void reset () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        byteOff = charOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        markWritten = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public int getMaxBytesPerChar() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return 4;               /* To allow for writing the byte-order mark */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
}