Showing the changes to java/pkg/sun/io/CharToByteDBCS_ASCII.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,195 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 1997, 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 | +package sun.io; | |
| 26 | + | |
| 27 | +import sun.nio.cs.ext.DoubleByte; | |
| 28 | +import static sun.nio.cs.CharsetMapping.*; | |
| 29 | + | |
| 30 | +public abstract class CharToByteDBCS_ASCII extends CharToByteConverter | |
| 31 | +{ | |
| 32 | + | |
| 33 | + private char highHalfZoneCode; | |
| 34 | + private byte[] outputByte = new byte[2]; | |
| 35 | + | |
| 36 | + private DoubleByte.Encoder enc; | |
| 37 | + | |
| 38 | + public CharToByteDBCS_ASCII(DoubleByte.Encoder enc) { | |
| 39 | + super(); | |
| 40 | + this.enc = enc; | |
| 41 | + } | |
| 42 | + | |
| 43 | + int encodeChar(char c) { | |
| 44 | + return enc.encodeChar(c); | |
| 45 | + } | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * flush out any residual data and reset the buffer state | |
| 49 | + */ | |
| 50 | + public int flush(byte [] output, int outStart, int outEnd) | |
| 51 | + throws MalformedInputException, ConversionBufferFullException | |
| 52 | + { | |
| 53 | + | |
| 54 | + if (highHalfZoneCode != 0) { | |
| 55 | + reset(); | |
| 56 | + badInputLength = 0; | |
| 57 | + throw new MalformedInputException(); | |
| 58 | + } | |
| 59 | + | |
| 60 | + reset(); | |
| 61 | + return 0; | |
| 62 | + } | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * Character conversion | |
| 66 | + */ | |
| 67 | + public int convert(char[] input, int inOff, int inEnd, | |
| 68 | + byte[] output, int outOff, int outEnd) | |
| 69 | + throws UnknownCharacterException, MalformedInputException, | |
| 70 | + ConversionBufferFullException | |
| 71 | + { | |
| 72 | + char inputChar; | |
| 73 | + int inputSize; | |
| 74 | + | |
| 75 | + byteOff = outOff; | |
| 76 | + charOff = inOff; | |
| 77 | + | |
| 78 | + while(charOff < inEnd) { | |
| 79 | + int index; | |
| 80 | + int theBytes; | |
| 81 | + int spaceNeeded; | |
| 82 | + | |
| 83 | + if (highHalfZoneCode == 0) { | |
| 84 | + inputChar = input[charOff]; | |
| 85 | + inputSize = 1; | |
| 86 | + } else { | |
| 87 | + inputChar = highHalfZoneCode; | |
| 88 | + inputSize = 0; | |
| 89 | + highHalfZoneCode = 0; | |
| 90 | + } | |
| 91 | + | |
| 92 | + // Is this a high surrogate? | |
| 93 | + if (Character.isHighSurrogate(inputChar)) { | |
| 94 | + // Is this the last character of the input? | |
| 95 | + if (charOff + inputSize >= inEnd) { | |
| 96 | + highHalfZoneCode = inputChar; | |
| 97 | + charOff += inputSize; | |
| 98 | + break; | |
| 99 | + } | |
| 100 | + | |
| 101 | + // Is there a low surrogate following? | |
| 102 | + inputChar = input[charOff + inputSize]; | |
| 103 | + if (Character.isLowSurrogate(inputChar)) { | |
| 104 | + // We have a valid surrogate pair. Too bad we don't do | |
| 105 | + // surrogates. Is substitution enabled? | |
| 106 | + if (subMode) { | |
| 107 | + if (subBytes.length == 1) { | |
| 108 | + outputByte[0] = 0x00; | |
| 109 | + outputByte[1] = subBytes[0]; | |
| 110 | + } | |
| 111 | + else { | |
| 112 | + outputByte[0] = subBytes[0]; | |
| 113 | + outputByte[1] = subBytes[1]; | |
| 114 | + } | |
| 115 | + inputSize++; | |
| 116 | + } else { | |
| 117 | + badInputLength = 2; | |
| 118 | + throw new UnknownCharacterException(); | |
| 119 | + } | |
| 120 | + } else { | |
| 121 | + // We have a malformed surrogate pair | |
| 122 | + badInputLength = 1; | |
| 123 | + throw new MalformedInputException(); | |
| 124 | + } | |
| 125 | + } | |
| 126 | + // Is this an unaccompanied low surrogate? | |
| 127 | + else if (Character.isLowSurrogate(inputChar)) { | |
| 128 | + badInputLength = 1; | |
| 129 | + throw new MalformedInputException(); | |
| 130 | + } else { | |
| 131 | + | |
| 132 | + // We have a valid character, get the bytes for it | |
| 133 | + theBytes = encodeChar(inputChar); | |
| 134 | + if (theBytes == UNMAPPABLE_ENCODING) { | |
| 135 | + // if there was no mapping - look for substitution characters | |
| 136 | + if (subMode) { | |
| 137 | + if (subBytes.length == 1) { | |
| 138 | + outputByte[0] = 0x00; | |
| 139 | + outputByte[1] = subBytes[0]; | |
| 140 | + } else { | |
| 141 | + outputByte[0] = subBytes[0]; | |
| 142 | + outputByte[1] = subBytes[1]; | |
| 143 | + } | |
| 144 | + } else { | |
| 145 | + badInputLength = 1; | |
| 146 | + throw new UnknownCharacterException(); | |
| 147 | + } | |
| 148 | + } else { | |
| 149 | + outputByte[0] = (byte)(theBytes >>8); | |
| 150 | + outputByte[1] = (byte)theBytes; | |
| 151 | + } | |
| 152 | + } | |
| 153 | + if (outputByte[0] == 0x00) | |
| 154 | + spaceNeeded = 1; | |
| 155 | + else | |
| 156 | + spaceNeeded = 2; | |
| 157 | + | |
| 158 | + if (byteOff + spaceNeeded > outEnd) | |
| 159 | + throw new ConversionBufferFullException(); | |
| 160 | + | |
| 161 | + if (spaceNeeded == 1) | |
| 162 | + output[byteOff++] = outputByte[1]; | |
| 163 | + else { | |
| 164 | + output[byteOff++] = outputByte[0]; | |
| 165 | + output[byteOff++] = outputByte[1]; | |
| 166 | + } | |
| 167 | + | |
| 168 | + charOff += inputSize; | |
| 169 | + } | |
| 170 | + return byteOff - outOff; | |
| 171 | + } | |
| 172 | + | |
| 173 | + /** | |
| 174 | + * Resets converter to its initial state. | |
| 175 | + */ | |
| 176 | + public void reset() { | |
| 177 | + charOff = byteOff = 0; | |
| 178 | + highHalfZoneCode = 0; | |
| 179 | + } | |
| 180 | + | |
| 181 | + /** | |
| 182 | + * Returns the maximum number of bytes needed to convert a char. | |
| 183 | + */ | |
| 184 | + public int getMaxBytesPerChar() { | |
| 185 | + return 2; | |
| 186 | + } | |
| 187 | + | |
| 188 | + /** | |
| 189 | + * Returns true if the given character can be converted to the | |
| 190 | + * target character encoding. | |
| 191 | + */ | |
| 192 | + public boolean canConvert(char c) { | |
| 193 | + return encodeChar(c) != UNMAPPABLE_ENCODING; | |
| 194 | + } | |
| 195 | +} | |