1306N/A/*
2362N/A * Copyright (c) 2002, 2006, Oracle and/or its affiliates. All rights reserved.
1306N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1306N/A *
1306N/A * This code is free software; you can redistribute it and/or modify it
1306N/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
1306N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1306N/A *
1306N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1306N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1306N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1306N/A * version 2 for more details (a copy is included in the LICENSE file that
1306N/A * accompanied this code).
1306N/A *
1306N/A * You should have received a copy of the GNU General Public License version
1306N/A * 2 along with this work; if not, write to the Free Software Foundation,
1306N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1306N/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.
1306N/A */
1306N/A
1306N/A/*
1306N/A */
1306N/A
1306N/A//package sun.nio.cs.ext;
1306N/A
1306N/Aimport java.nio.ByteBuffer;
1306N/Aimport java.nio.CharBuffer;
1306N/Aimport java.nio.charset.Charset;
1306N/Aimport java.nio.charset.CharsetEncoder;
1306N/Aimport java.nio.charset.CoderResult;
1306N/Aimport sun.nio.cs.Surrogate;
1306N/A
1306N/Apublic abstract class DoubleByteEncoder
1306N/A extends CharsetEncoder
1306N/A{
1306N/A
1306N/A private short index1[];
1306N/A private String index2[];
1306N/A
1306N/A private final Surrogate.Parser sgp = new Surrogate.Parser();
1306N/A
1306N/A protected DoubleByteEncoder(Charset cs,
1306N/A short[] index1, String[] index2)
1306N/A {
1306N/A super(cs, 2.0f, 2.0f);
1306N/A this.index1 = index1;
1306N/A this.index2 = index2;
1306N/A }
1306N/A
1306N/A protected DoubleByteEncoder(Charset cs,
1306N/A short[] index1, String[] index2,
1306N/A float avg, float max)
1306N/A {
1306N/A super(cs, avg, max);
1306N/A this.index1 = index1;
1306N/A this.index2 = index2;
1306N/A }
1306N/A
1306N/A protected DoubleByteEncoder(Charset cs,
1306N/A short[] index1, String[] index2, byte[] repl)
1306N/A {
1306N/A super(cs, 2.0f, 2.0f, repl);
1306N/A this.index1 = index1;
1306N/A this.index2 = index2;
1306N/A }
1306N/A
1306N/A
1306N/A protected DoubleByteEncoder(Charset cs,
1306N/A short[] index1, String[] index2,
1306N/A byte[] repl, float avg, float max)
1306N/A {
1306N/A super(cs, avg, max,repl);
1306N/A this.index1 = index1;
1306N/A this.index2 = index2;
1306N/A }
1306N/A
1306N/A public boolean canEncode(char c) {
1306N/A return (encodeSingle(c) != -1 ||
1306N/A encodeDouble(c) != 0);
1306N/A }
1306N/A
1306N/A private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
1306N/A char[] sa = src.array();
1306N/A int sp = src.arrayOffset() + src.position();
1306N/A int sl = src.arrayOffset() + src.limit();
1306N/A byte[] da = dst.array();
1306N/A int dp = dst.arrayOffset() + dst.position();
1306N/A int dl = dst.arrayOffset() + dst.limit();
1306N/A
1306N/A try {
1306N/A while (sp < sl) {
1306N/A char c = sa[sp];
1306N/A if (Surrogate.is(c)) {
1306N/A if (sgp.parse(c, sa, sp, sl) < 0)
1306N/A return sgp.error();
1306N/A if (sl - sp < 2)
1306N/A return CoderResult.UNDERFLOW;
1306N/A char c2 = sa[sp + 1];
1306N/A
1306N/A byte[] outputBytes = new byte[2];
1306N/A outputBytes = encodeSurrogate(c, c2);
1306N/A
1306N/A if (outputBytes == null) {
1306N/A return sgp.unmappableResult();
1306N/A }
1306N/A else {
1306N/A if (dl - dp < 2)
1306N/A return CoderResult.OVERFLOW;
1306N/A da[dp++] = outputBytes[0];
1306N/A da[dp++] = outputBytes[1];
1306N/A sp += 2;
1306N/A continue;
1306N/A }
1306N/A }
1306N/A if (c >= '\uFFFE')
1306N/A return CoderResult.unmappableForLength(1);
1306N/A
1306N/A int b = encodeSingle(c);
1306N/A if (b != -1) { // Single Byte
1306N/A if (dl - dp < 1)
1306N/A return CoderResult.OVERFLOW;
1306N/A da[dp++] = (byte)b;
1306N/A sp++;
1306N/A continue;
1306N/A }
1306N/A
1306N/A int ncode = encodeDouble(c);
1306N/A if (ncode != 0 && c != '\u0000' ) {
1306N/A if (dl - dp < 2)
1306N/A return CoderResult.OVERFLOW;
1306N/A da[dp++] = (byte) ((ncode & 0xff00) >> 8);
1306N/A da[dp++] = (byte) (ncode & 0xff);
1306N/A sp++;
1306N/A continue;
1306N/A }
1306N/A return CoderResult.unmappableForLength(1);
1306N/A }
1306N/A return CoderResult.UNDERFLOW;
1306N/A } finally {
1306N/A src.position(sp - src.arrayOffset());
1306N/A dst.position(dp - dst.arrayOffset());
1306N/A }
1306N/A }
1306N/A
1306N/A private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
1306N/A int mark = src.position();
1306N/A
1306N/A try {
1306N/A while (src.hasRemaining()) {
1306N/A char c = src.get();
1306N/A if (Surrogate.is(c)) {
1306N/A int surr;
1306N/A if ((surr = sgp.parse(c, src)) < 0)
1306N/A return sgp.error();
1306N/A char c2 = Surrogate.low(surr);
1306N/A byte[] outputBytes = new byte[2];
1306N/A outputBytes = encodeSurrogate(c, c2);
1306N/A
1306N/A if (outputBytes == null) {
1306N/A return sgp.unmappableResult();
1306N/A } else {
1306N/A if (dst.remaining() < 2)
1306N/A return CoderResult.OVERFLOW;
1306N/A mark += 2;
1306N/A dst.put(outputBytes[0]);
1306N/A dst.put(outputBytes[1]);
1306N/A continue;
1306N/A }
1306N/A }
1306N/A if (c >= '\uFFFE')
1306N/A return CoderResult.unmappableForLength(1);
1306N/A int b = encodeSingle(c);
1306N/A
1306N/A if (b != -1) { // Single-byte character
1306N/A if (dst.remaining() < 1)
1306N/A return CoderResult.OVERFLOW;
1306N/A mark++;
1306N/A dst.put((byte)b);
1306N/A continue;
1306N/A }
1306N/A // Double Byte character
1306N/A
1306N/A int ncode = encodeDouble(c);
1306N/A if (ncode != 0 && c != '\u0000') {
1306N/A if (dst.remaining() < 2)
1306N/A return CoderResult.OVERFLOW;
1306N/A mark++;
1306N/A dst.put((byte) ((ncode & 0xff00) >> 8));
1306N/A dst.put((byte) ncode);
1306N/A continue;
1306N/A }
1306N/A return CoderResult.unmappableForLength(1);
1306N/A }
1306N/A
1306N/A return CoderResult.UNDERFLOW;
1306N/A } finally {
1306N/A src.position(mark);
1306N/A }
1306N/A }
1306N/A
1306N/A protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
1306N/A if (true && src.hasArray() && dst.hasArray())
1306N/A return encodeArrayLoop(src, dst);
1306N/A else
1306N/A return encodeBufferLoop(src, dst);
1306N/A }
1306N/A
1306N/A /*
1306N/A * Can be changed by subclass
1306N/A */
1306N/A protected int encodeDouble(char ch) {
1306N/A int offset = index1[((ch & 0xff00) >> 8 )] << 8;
1306N/A return index2[offset >> 12].charAt((offset & 0xfff) + (ch & 0xff));
1306N/A }
1306N/A
1306N/A /*
1306N/A * Can be changed by subclass
1306N/A */
1306N/A protected int encodeSingle(char inputChar) {
1306N/A if (inputChar < 0x80)
1306N/A return (byte)inputChar;
1306N/A else
1306N/A return -1;
1306N/A }
1306N/A
1306N/A /**
1306N/A * Protected method which should be overridden by concrete DBCS
1306N/A * CharsetEncoder classes which included supplementary characters
1306N/A * within their mapping coverage.
1306N/A * null return value indicates surrogate values could not be
1306N/A * handled or encoded.
1306N/A */
1306N/A protected byte[] encodeSurrogate(char highSurrogate, char lowSurrogate) {
1306N/A return null;
1306N/A }
1306N/A}