0N/A/*
3261N/A * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.io;
0N/Aimport java.io.*;
0N/A
0N/A
0N/A/**
0N/A * Convert arrays containing Unicode characters into arrays of bytes, using the
0N/A * platform-default byte order.
0N/A *
0N/A * @author Mark Reinhold
0N/A */
0N/A
0N/Apublic class CharToByteUnicode extends CharToByteConverter {
0N/A
0N/A static final char BYTE_ORDER_MARK = (char) 0xfeff;
0N/A protected boolean usesMark = true; /* A mark should be written */
0N/A private boolean markWritten = false; /* A mark has been written */
0N/A
0N/A static final int UNKNOWN = 0;
0N/A static final int BIG = 1;
0N/A static final int LITTLE = 2;
0N/A protected int byteOrder = UNKNOWN;
0N/A
0N/A public CharToByteUnicode() {
2116N/A String enc = java.security.AccessController.doPrivileged(
0N/A new sun.security.action.GetPropertyAction("sun.io.unicode.encoding",
0N/A "UnicodeBig"));
0N/A if (enc.equals("UnicodeBig"))
0N/A byteOrder = BIG;
0N/A else if (enc.equals("UnicodeLittle"))
0N/A byteOrder = LITTLE;
0N/A else
0N/A byteOrder = BIG;
0N/A }
0N/A
0N/A public CharToByteUnicode(int byteOrder, boolean usesMark) {
0N/A this.byteOrder = byteOrder;
0N/A this.usesMark = usesMark;
0N/A }
0N/A
0N/A public CharToByteUnicode(boolean usesMark) {
0N/A this();
0N/A this.usesMark = usesMark;
0N/A }
0N/A
0N/A public String getCharacterEncoding() {
0N/A switch (byteOrder) {
0N/A case BIG:
0N/A return usesMark ? "UnicodeBig" : "UnicodeBigUnmarked";
0N/A case LITTLE:
0N/A return usesMark ? "UnicodeLittle" : "UnicodeLittleUnmarked";
0N/A default:
0N/A return "UnicodeUnknown";
0N/A }
0N/A }
0N/A
0N/A public int convert(char in[], int inOff, int inEnd,
0N/A byte out[], int outOff, int outEnd)
0N/A throws ConversionBufferFullException, MalformedInputException
0N/A {
0N/A charOff = inOff;
0N/A byteOff = outOff;
0N/A
0N/A if (inOff >= inEnd)
0N/A return 0;
0N/A
0N/A int inI = inOff,
0N/A outI = outOff,
0N/A outTop = outEnd - 2;
0N/A
0N/A if (usesMark && !markWritten) {
0N/A if (outI > outTop)
0N/A throw new ConversionBufferFullException();
0N/A if (byteOrder == BIG) {
0N/A out[outI++] = (byte) (BYTE_ORDER_MARK >> 8);
0N/A out[outI++] = (byte) (BYTE_ORDER_MARK & 0xff);
0N/A }
0N/A else {
0N/A out[outI++] = (byte) (BYTE_ORDER_MARK & 0xff);
0N/A out[outI++] = (byte) (BYTE_ORDER_MARK >> 8);
0N/A }
0N/A markWritten = true;
0N/A }
0N/A
0N/A if (byteOrder == BIG) {
0N/A while (inI < inEnd) {
0N/A if (outI > outTop) {
0N/A charOff = inI;
0N/A byteOff = outI;
0N/A throw new ConversionBufferFullException();
0N/A }
0N/A char c = in[inI++];
0N/A out[outI++] = (byte) (c >> 8);
0N/A out[outI++] = (byte) (c & 0xff);
0N/A }
0N/A }
0N/A else {
0N/A while (inI < inEnd) {
0N/A if (outI > outTop) {
0N/A charOff = inI;
0N/A byteOff = outI;
0N/A throw new ConversionBufferFullException();
0N/A }
0N/A char c = in[inI++];
0N/A out[outI++] = (byte) (c & 0xff);
0N/A out[outI++] = (byte) (c >> 8);
0N/A }
0N/A }
0N/A
0N/A charOff = inI;
0N/A byteOff = outI;
0N/A return outI - outOff;
0N/A }
0N/A
0N/A public int flush(byte in[], int inOff, int inEnd) {
0N/A byteOff = charOff = 0;
0N/A return 0;
0N/A }
0N/A
0N/A public void reset () {
0N/A byteOff = charOff = 0;
0N/A markWritten = false;
0N/A }
0N/A
0N/A public int getMaxBytesPerChar() {
0N/A return 4; /* To allow for writing the byte-order mark */
0N/A }
0N/A
0N/A}