001: /*
002: * @(#)CharToByteUnicode.java 1.19 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.io;
029:
030: import java.io.*;
031:
032: /**
033: * Convert arrays containing Unicode characters into arrays of bytes, using the
034: * platform-default byte order.
035: *
036: * @version 1.13, 00/02/02
037: * @author Mark Reinhold
038: */
039:
040: public class CharToByteUnicode extends CharToByteConverter {
041:
042: static final char BYTE_ORDER_MARK = (char) 0xfeff;
043: protected boolean usesMark = true; /* A mark should be written */
044: private boolean markWritten = false; /* A mark has been written */
045:
046: static final int UNKNOWN = 0;
047: static final int BIG = 1;
048: static final int LITTLE = 2;
049: protected int byteOrder = UNKNOWN;
050:
051: public CharToByteUnicode() {
052: String enc = (String) java.security.AccessController
053: .doPrivileged(new sun.security.action.GetPropertyAction(
054: "sun.io.unicode.encoding", "UnicodeBig"));
055: if (enc.equals("UnicodeBig"))
056: byteOrder = BIG;
057: else if (enc.equals("UnicodeLittle"))
058: byteOrder = LITTLE;
059: else
060: byteOrder = BIG;
061: }
062:
063: public CharToByteUnicode(int byteOrder, boolean usesMark) {
064: this .byteOrder = byteOrder;
065: this .usesMark = usesMark;
066: }
067:
068: public CharToByteUnicode(boolean usesMark) {
069: this ();
070: this .usesMark = usesMark;
071: }
072:
073: public String getCharacterEncoding() {
074: switch (byteOrder) {
075: case BIG:
076: return usesMark ? "UnicodeBig" : "UnicodeBigUnmarked";
077: case LITTLE:
078: return usesMark ? "UnicodeLittle" : "UnicodeLittleUnmarked";
079: default:
080: return "UnicodeUnknown";
081: }
082: }
083:
084: public int convert(char in[], int inOff, int inEnd, byte out[],
085: int outOff, int outEnd)
086: throws ConversionBufferFullException,
087: MalformedInputException {
088: charOff = inOff;
089: byteOff = outOff;
090:
091: if (inOff >= inEnd)
092: return 0;
093:
094: int inI = inOff, outI = outOff, outTop = outEnd - 2;
095:
096: if (usesMark && !markWritten) {
097: if (outI > outTop)
098: throw new ConversionBufferFullException();
099: if (byteOrder == BIG) {
100: out[outI++] = (byte) (BYTE_ORDER_MARK >> 8);
101: out[outI++] = (byte) (BYTE_ORDER_MARK & 0xff);
102: } else {
103: out[outI++] = (byte) (BYTE_ORDER_MARK & 0xff);
104: out[outI++] = (byte) (BYTE_ORDER_MARK >> 8);
105: }
106: markWritten = true;
107: }
108:
109: if (byteOrder == BIG) {
110: while (inI < inEnd) {
111: if (outI > outTop) {
112: charOff = inI;
113: byteOff = outI;
114: throw new ConversionBufferFullException();
115: }
116: char c = in[inI++];
117: out[outI++] = (byte) (c >> 8);
118: out[outI++] = (byte) (c & 0xff);
119: }
120: } else {
121: while (inI < inEnd) {
122: if (outI > outTop) {
123: charOff = inI;
124: byteOff = outI;
125: throw new ConversionBufferFullException();
126: }
127: char c = in[inI++];
128: out[outI++] = (byte) (c & 0xff);
129: out[outI++] = (byte) (c >> 8);
130: }
131: }
132:
133: charOff = inI;
134: byteOff = outI;
135: return outI - outOff;
136: }
137:
138: public int flush(byte in[], int inOff, int inEnd) {
139: byteOff = charOff = 0;
140: return 0;
141: }
142:
143: public void reset() {
144: byteOff = charOff = 0;
145: markWritten = false;
146: }
147:
148: public int getMaxBytesPerChar() {
149: return 4; /* To allow for writing the byte-order mark */
150: }
151:
152: }
|