1245N/A/*
2362N/A * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
1245N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1245N/A *
1245N/A * This code is free software; you can redistribute it and/or modify it
1245N/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
1245N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1245N/A *
1245N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1245N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1245N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1245N/A * version 2 for more details (a copy is included in the LICENSE file that
1245N/A * accompanied this code).
1245N/A *
1245N/A * You should have received a copy of the GNU General Public License version
1245N/A * 2 along with this work; if not, write to the Free Software Foundation,
1245N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1245N/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.
1245N/A */
1245N/A
1245N/A/*
1245N/A */
1245N/A
1245N/A
1245N/Aimport java.nio.ByteBuffer;
1245N/Aimport java.nio.CharBuffer;
1245N/Aimport java.nio.charset.Charset;
1245N/Aimport java.nio.charset.CharsetEncoder;
1245N/Aimport java.nio.charset.CoderResult;
1245N/Aimport sun.nio.cs.Surrogate;
1245N/A
1245N/A/**
1245N/A * An abstract base class for subclasses which encodes
1245N/A * IBM double byte host encodings such as ibm code
1245N/A * pages 942,943,948, etc.
1245N/A *
1245N/A */
1245N/A
1245N/Aabstract class DBCS_IBM_ASCII_Encoder extends CharsetEncoder
1245N/A{
1245N/A
1245N/A protected static final char REPLACE_CHAR='\uFFFD';
1245N/A private byte b1;
1245N/A private byte b2;
1245N/A
1245N/A
1245N/A protected short index1[];
1245N/A protected String index2;
1245N/A protected String index2a;
1245N/A protected int mask1;
1245N/A protected int mask2;
1245N/A protected int shift;
1245N/A
1245N/A private final Surrogate.Parser sgp = new Surrogate.Parser();
1245N/A
1245N/A protected DBCS_IBM_ASCII_Encoder(Charset cs) {
1245N/A super(cs, 2.0f, 2.0f);
1245N/A }
1245N/A
1245N/A /**
1245N/A * Returns true if the given character can be converted to the
1245N/A * target character encoding.
1245N/A */
1245N/A public boolean canEncode(char ch) {
1245N/A int index;
1245N/A int theBytes;
1245N/A
1245N/A index = index1[((ch & mask1) >> shift)] + (ch & mask2);
1245N/A if (index < 15000)
1245N/A theBytes = (int)(index2.charAt(index));
1245N/A else
1245N/A theBytes = (int)(index2a.charAt(index-15000));
1245N/A
1245N/A if (theBytes != 0)
1245N/A return (true);
1245N/A
1245N/A // only return true if input char was unicode null - all others are
1245N/A // undefined
1245N/A return( ch == '\u0000');
1245N/A
1245N/A }
1245N/A
1245N/A private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
1245N/A char[] sa = src.array();
1245N/A int sp = src.arrayOffset() + src.position();
1245N/A int sl = src.arrayOffset() + src.limit();
1245N/A byte[] da = dst.array();
1245N/A int dp = dst.arrayOffset() + dst.position();
1245N/A int dl = dst.arrayOffset() + dst.limit();
1245N/A int outputSize = 0; // size of output
1245N/A
1245N/A try {
1245N/A while (sp < sl) {
1245N/A int index;
1245N/A int theBytes;
1245N/A char c = sa[sp];
1245N/A if (Surrogate.is(c)) {
1245N/A if (sgp.parse(c, sa, sp, sl) < 0)
1245N/A return sgp.error();
1245N/A return sgp.unmappableResult();
1245N/A }
1245N/A if (c >= '\uFFFE')
1245N/A return CoderResult.unmappableForLength(1);
1245N/A
1245N/A index = index1[((c & mask1) >> shift)]
1245N/A + (c & mask2);
1245N/A if (index < 15000)
1245N/A theBytes = (int)(index2.charAt(index));
1245N/A else
1245N/A theBytes = (int)(index2a.charAt(index-15000));
1245N/A b1 = (byte)((theBytes & 0x0000ff00)>>8);
1245N/A b2 = (byte)(theBytes & 0x000000ff);
1245N/A
1245N/A if (b1 == 0x00 && b2 == 0x00
1245N/A && c != '\u0000') {
1245N/A return CoderResult.unmappableForLength(1);
1245N/A }
1245N/A
1245N/A if (b1 == 0) {
1245N/A if (dl - dp < 1)
1245N/A return CoderResult.OVERFLOW;
1245N/A da[dp++] = (byte) b2;
1245N/A } else {
1245N/A if (dl - dp < 2)
1245N/A return CoderResult.OVERFLOW;
1245N/A da[dp++] = (byte) b1;
1245N/A da[dp++] = (byte) b2;
1245N/A }
1245N/A sp++;
1245N/A }
1245N/A return CoderResult.UNDERFLOW;
1245N/A } finally {
1245N/A src.position(sp - src.arrayOffset());
1245N/A dst.position(dp - dst.arrayOffset());
1245N/A }
1245N/A }
1245N/A
1245N/A private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
1245N/A int mark = src.position();
1245N/A int outputSize = 0; // size of output
1245N/A
1245N/A try {
1245N/A while (src.hasRemaining()) {
1245N/A int index;
1245N/A int theBytes;
1245N/A char c = src.get();
1245N/A if (Surrogate.is(c)) {
1245N/A if (sgp.parse(c, src) < 0)
1245N/A return sgp.error();
1245N/A return sgp.unmappableResult();
1245N/A }
1245N/A if (c >= '\uFFFE')
1245N/A return CoderResult.unmappableForLength(1);
1245N/A
1245N/A index = index1[((c & mask1) >> shift)]
1245N/A + (c & mask2);
1245N/A if (index < 15000)
1245N/A theBytes = (int)(index2.charAt(index));
1245N/A else
1245N/A theBytes = (int)(index2a.charAt(index-15000));
1245N/A b1 = (byte)((theBytes & 0x0000ff00)>>8);
1245N/A b2 = (byte)(theBytes & 0x000000ff);
1245N/A
1245N/A if (b1 == 0x00 && b2 == 0x00
1245N/A && c != '\u0000') {
1245N/A return CoderResult.unmappableForLength(1);
1245N/A }
1245N/A
1245N/A if (b1 == 0) {
1245N/A if (dst.remaining() < 1)
1245N/A return CoderResult.OVERFLOW;
1245N/A dst.put((byte) b2);
1245N/A } else {
1245N/A if (dst.remaining() < 2)
1245N/A return CoderResult.OVERFLOW;
1245N/A dst.put((byte) b1);
1245N/A dst.put((byte) b2);
1245N/A }
1245N/A mark++;
1245N/A }
1245N/A return CoderResult.UNDERFLOW;
1245N/A } finally {
1245N/A src.position(mark);
1245N/A }
1245N/A }
1245N/A
1245N/A protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
1245N/A if (true && src.hasArray() && dst.hasArray())
1245N/A return encodeArrayLoop(src, dst);
1245N/A else
1245N/A return encodeBufferLoop(src, dst);
1245N/A }
1245N/A
1245N/A}