package sun.io;
import sun.nio.cs.ext.IBM933;
public class CharToByteCp933 extends CharToByteConverter
{
private static final char SBase = '\uAC00';
private static final char LBase = '\u1100';
private static final char VBase = '\u1161';
private static final char TBase = '\u11A7';
private static final int VCount = 21;
private static final int TCount = 28;
private static final byte G0 = 0;
private static final byte G1 = 1;
private static final byte G2 = 2;
private static final byte G3 = 3;
private byte charState = G0;
private char l, v, t;
private int byteState;
private byte[] outputByte;
private static final int SBCS = 0;
private static final int DBCS = 1;
private static final byte SO = 0x0e;
private static final byte SI = 0x0f;
private char highHalfZoneCode;
private short[] index1;
private String index2;
private String index2a;
private int mask1;
private int mask2;
private int shift;
private final static IBM933 nioCoder = new IBM933();
public CharToByteCp933() {
super();
byteState = doSBCS()?SBCS:DBCS;
highHalfZoneCode = 0;
outputByte = new byte[2];
mask1 = 0xFFF8;
mask2 = 0x0007;
shift = 3;
index1 = nioCoder.getEncoderIndex1();
index2 = nioCoder.getEncoderIndex2();
index2a = nioCoder.getEncoderIndex2a();
subBytes = new byte[1];
subBytes[0] = 0x6f;
}
public int flush(byte[] output, int outStart, int outEnd)
throws MalformedInputException,
ConversionBufferFullException
{
int bytesOut;
byteOff = outStart;
if (highHalfZoneCode != 0) {
reset();
badInputLength = 0;
throw new MalformedInputException();
}
if (charState != G0) {
try {
unicodeToBuffer(composeHangul() ,output, outEnd);
}
catch(UnknownCharacterException e) {
reset();
badInputLength = 0;
throw new MalformedInputException();
}
charState = G0;
}
if (byteState == DBCS && doSBCS()) {
if (byteOff >= outEnd)
throw new ConversionBufferFullException();
output[byteOff++] = SI;
byteState = SBCS;
}
bytesOut = byteOff - outStart;
reset();
return bytesOut;
}
public void reset() {
byteState = doSBCS()?SBCS:DBCS;
highHalfZoneCode = 0;
charState = G0;
charOff = byteOff = 0;
}
public boolean canConvert(char ch) {
return encodeHangul(ch) != -1;
}
public void setSubstitutionBytes( byte[] newSubBytes )
throws IllegalArgumentException
{
if( newSubBytes.length > 2 || newSubBytes.length == 0) {
throw new IllegalArgumentException();
}
subBytes = new byte[ newSubBytes.length ];
System.arraycopy( newSubBytes, 0, subBytes, 0, newSubBytes.length );
}
public int convert(char[] input, int inOff, int inEnd,
byte[] output, int outOff, int outEnd)
throws UnknownCharacterException, MalformedInputException,
ConversionBufferFullException
{
char inputChar;
int inputSize;
charOff = inOff;
byteOff = outOff;
while (charOff < inEnd) {
if (highHalfZoneCode == 0) {
inputChar = input[charOff];
inputSize = 1;
} else {
inputChar = highHalfZoneCode;
inputSize = 0;
highHalfZoneCode = 0;
}
switch (charState) {
case G0:
l = LBase;
v = VBase;
t = TBase;
if ( isLeadingC(inputChar) ) {
l = inputChar;
charState = G1;
break;
}
if ( isVowel(inputChar) ) {
v = inputChar;
charState = G2;
break;
}
if ( isTrailingC(inputChar) ) {
t = inputChar;
charState = G3;
break;
}
break;
case G1:
if ( isLeadingC(inputChar) ) {
l = composeLL(l, inputChar);
break;
}
if ( isVowel(inputChar) ) {
v = inputChar;
charState = G2;
break;
}
if ( isTrailingC(inputChar) ) {
t = inputChar;
charState = G3;
break;
}
unicodeToBuffer(composeHangul(), output, outEnd);
charState = G0;
break;
case G2:
if ( isLeadingC(inputChar) ) {
unicodeToBuffer(composeHangul(), output, outEnd);
l = inputChar;
v = VBase;
t = TBase;
charState = G1;
break;
}
if ( isVowel(inputChar) ) {
v = composeVV(l, inputChar);
charState = G2;
break;
}
if ( isTrailingC(inputChar) ) {
t = inputChar;
charState = G3;
break;
}
unicodeToBuffer(composeHangul(), output, outEnd);
charState = G0;
break;
case G3:
if ( isTrailingC(inputChar) ) {
t = composeTT(t, inputChar);
charState = G3;
break;
}
unicodeToBuffer(composeHangul(), output, outEnd);
charState = G0;
break;
}
if (charState != G0)
charOff++;
else {
if(inputChar >= '\ud800' && inputChar <= '\udbff') {
if (charOff + inputSize >= inEnd) {
highHalfZoneCode = inputChar;
charOff += inputSize;
break;
}
inputChar = input[charOff + inputSize];
if (inputChar >= '\udc00' && inputChar <= '\udfff') {
if (subMode) {
if (subBytes.length == 1) {
outputByte[0] = 0x00;
outputByte[1] = subBytes[0];
} else {
outputByte[0] = subBytes[0];
outputByte[1] = subBytes[1];
}
bytesToBuffer(outputByte, output, outEnd);
inputSize++;
} else {
badInputLength = 2;
throw new UnknownCharacterException();
}
} else {
badInputLength = 1;
throw new MalformedInputException();
}
}
else
if (inputChar >= '\uDC00' && inputChar <= '\uDFFF') {
badInputLength = 1;
throw new MalformedInputException();
} else {
unicodeToBuffer(inputChar, output, outEnd);
}
charOff += inputSize;
}
}
return byteOff - outOff;
}
private char composeHangul() {
int lIndex, vIndex, tIndex;
lIndex = l - LBase;
vIndex = v - VBase;
tIndex = t - TBase;
return (char)((lIndex * VCount + vIndex) * TCount + tIndex + SBase);
}
private char composeLL(char l1, char l2) {
return l2;
}
private char composeVV(char v1, char v2) {
return v2;
}
private char composeTT(char t1, char t2) {
return t2;
}
private boolean isLeadingC(char c) {
return (c >= LBase && c <= '\u1159');
}
private boolean isVowel(char c) {
return (c >= VBase && c <= '\u11a2');
}
private boolean isTrailingC(char c) {
return (c >= TBase && c <= '\u11f9');
}
public int getMaxBytesPerChar() {
return 4;
}
public String getCharacterEncoding() {
return "Cp933";
}
private void bytesToBuffer(byte[] theBytes, byte[] output, int outEnd)
throws ConversionBufferFullException,
UnknownCharacterException {
int spaceNeeded;
if (byteState == DBCS && theBytes[0] == 0x00) {
if (byteOff >= outEnd)
throw new ConversionBufferFullException();
byteState = SBCS;
output[byteOff++] = SI;
} else
if (byteState == SBCS && theBytes[0] != 0x00) {
if (byteOff >= outEnd)
throw new ConversionBufferFullException();
byteState = DBCS;
output[byteOff++] = SO;
}
if (byteState == DBCS)
spaceNeeded = 2;
else
spaceNeeded = 1;
if (byteOff + spaceNeeded > outEnd)
throw new ConversionBufferFullException();
if (byteState == SBCS)
output[byteOff++] = theBytes[1];
else {
output[byteOff++] = theBytes[0];
output[byteOff++] = theBytes[1];
}
}
protected int encodeHangul(char unicode) {
int theBytes;
int index;
index = index1[((unicode & mask1) >> shift)] + (unicode & mask2);
if (index < 15000)
theBytes = (int)(index2.charAt(index));
else
theBytes = (int)(index2a.charAt(index-15000));
if (theBytes == 0 && unicode != '\u0000')
return -1;
return theBytes;
}
private void unicodeToBuffer(char unicode, byte[] output, int outEnd)
throws ConversionBufferFullException,
UnknownCharacterException {
int theBytes = encodeHangul(unicode);
if (theBytes == -1) {
if (subMode) {
if (subBytes.length == 1) {
outputByte[0] = 0x00;
outputByte[1] = subBytes[0];
} else {
outputByte[0] = subBytes[0];
outputByte[1] = subBytes[1];
}
} else {
badInputLength = 1;
throw new UnknownCharacterException();
}
} else {
outputByte[0] = (byte)((theBytes & 0x0000ff00)>>8);
outputByte[1] = (byte)(theBytes & 0x000000ff);
}
bytesToBuffer(outputByte, output, outEnd);
}
protected boolean doSBCS() {
return true;
}
}