Showing the changes to java/pkg/sun/io/CharToByteUnicode.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,153 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1996, 2010, 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.io; | ||
| 27 | import java.io.*; | ||
| 28 | |||
| 29 | |||
| 30 | /** | ||
| 31 | * Convert arrays containing Unicode characters into arrays of bytes, using the | ||
| 32 | * platform-default byte order. | ||
| 33 | * | ||
| 34 | * @author Mark Reinhold | ||
| 35 | */ | ||
| 36 | |||
| 37 | public class CharToByteUnicode extends CharToByteConverter { | ||
| 38 | |||
| 39 | static final char BYTE_ORDER_MARK = (char) 0xfeff; | ||
| 40 | protected boolean usesMark = true; /* A mark should be written */ | ||
| 41 | private boolean markWritten = false; /* A mark has been written */ | ||
| 42 | |||
| 43 | static final int UNKNOWN = 0; | ||
| 44 | static final int BIG = 1; | ||
| 45 | static final int LITTLE = 2; | ||
| 46 | protected int byteOrder = UNKNOWN; | ||
| 47 | |||
| 48 | public CharToByteUnicode() { | ||
| 49 | String enc = java.security.AccessController.doPrivileged( | ||
| 50 | new sun.security.action.GetPropertyAction("sun.io.unicode.encoding", | ||
| 51 | "UnicodeBig")); | ||
| 52 | if (enc.equals("UnicodeBig")) | ||
| 53 | byteOrder = BIG; | ||
| 54 | else if (enc.equals("UnicodeLittle")) | ||
| 55 | byteOrder = LITTLE; | ||
| 56 | else | ||
| 57 | byteOrder = BIG; | ||
| 58 | } | ||
| 59 | |||
| 60 | public CharToByteUnicode(int byteOrder, boolean usesMark) { | ||
| 61 | this.byteOrder = byteOrder; | ||
| 62 | this.usesMark = usesMark; | ||
| 63 | } | ||
| 64 | |||
| 65 | public CharToByteUnicode(boolean usesMark) { | ||
| 66 | this(); | ||
| 67 | this.usesMark = usesMark; | ||
| 68 | } | ||
| 69 | |||
| 70 | public String getCharacterEncoding() { | ||
| 71 | switch (byteOrder) { | ||
| 72 | case BIG: | ||
| 73 | return usesMark ? "UnicodeBig" : "UnicodeBigUnmarked"; | ||
| 74 | case LITTLE: | ||
| 75 | return usesMark ? "UnicodeLittle" : "UnicodeLittleUnmarked"; | ||
| 76 | default: | ||
| 77 | return "UnicodeUnknown"; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | public int convert(char in[], int inOff, int inEnd, | ||
| 82 | byte out[], int outOff, int outEnd) | ||
| 83 | throws ConversionBufferFullException, MalformedInputException | ||
| 84 | { | ||
| 85 | charOff = inOff; | ||
| 86 | byteOff = outOff; | ||
| 87 | |||
| 88 | if (inOff >= inEnd) | ||
| 89 | return 0; | ||
| 90 | |||
| 91 | int inI = inOff, | ||
| 92 | outI = outOff, | ||
| 93 | outTop = outEnd - 2; | ||
| 94 | |||
| 95 | if (usesMark && !markWritten) { | ||
| 96 | if (outI > outTop) | ||
| 97 | throw new ConversionBufferFullException(); | ||
| 98 | if (byteOrder == BIG) { | ||
| 99 | out[outI++] = (byte) (BYTE_ORDER_MARK >> 8); | ||
| 100 | out[outI++] = (byte) (BYTE_ORDER_MARK & 0xff); | ||
| 101 | } | ||
| 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 | } | ||
| 121 | else { | ||
| 122 | while (inI < inEnd) { | ||
| 123 | if (outI > outTop) { | ||
| 124 | charOff = inI; | ||
| 125 | byteOff = outI; | ||
| 126 | throw new ConversionBufferFullException(); | ||
| 127 | } | ||
| 128 | char c = in[inI++]; | ||
| 129 | out[outI++] = (byte) (c & 0xff); | ||
| 130 | out[outI++] = (byte) (c >> 8); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | charOff = inI; | ||
| 135 | byteOff = outI; | ||
| 136 | return outI - outOff; | ||
| 137 | } | ||
| 138 | |||
| 139 | public int flush(byte in[], int inOff, int inEnd) { | ||
| 140 | byteOff = charOff = 0; | ||
| 141 | return 0; | ||
| 142 | } | ||
| 143 | |||
| 144 | public void reset () { | ||
| 145 | byteOff = charOff = 0; | ||
| 146 | markWritten = false; | ||
| 147 | } | ||
| 148 | |||
| 149 | public int getMaxBytesPerChar() { | ||
| 150 | return 4; /* To allow for writing the byte-order mark */ | ||
| 151 | } | ||
| 152 | |||
| 153 | } | ||