0N/A/*
2362N/A * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/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
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/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.
0N/A */
0N/A
0N/A/*
0N/A */
0N/A
0N/Apackage sun.nio.cs;
0N/A
0N/Aimport java.nio.ByteBuffer;
0N/Aimport java.nio.CharBuffer;
0N/Aimport java.nio.charset.Charset;
0N/Aimport java.nio.charset.CharsetEncoder;
0N/Aimport java.nio.charset.CoderResult;
0N/Aimport java.nio.charset.CharacterCodingException;
0N/Aimport java.nio.charset.MalformedInputException;
0N/Aimport java.nio.charset.UnmappableCharacterException;
0N/Aimport sun.nio.cs.Surrogate;
0N/A
0N/A
0N/Apublic abstract class SingleByteEncoder
0N/A extends CharsetEncoder
0N/A{
0N/A
0N/A private final short index1[];
0N/A private final String index2;
0N/A private final int mask1;
0N/A private final int mask2;
0N/A private final int shift;
0N/A
0N/A private final Surrogate.Parser sgp = new Surrogate.Parser();
0N/A
0N/A protected SingleByteEncoder(Charset cs,
0N/A short[] index1, String index2,
0N/A int mask1, int mask2, int shift)
0N/A {
0N/A super(cs, 1.0f, 1.0f);
0N/A this.index1 = index1;
0N/A this.index2 = index2;
0N/A this.mask1 = mask1;
0N/A this.mask2 = mask2;
0N/A this.shift = shift;
0N/A }
0N/A
0N/A public boolean canEncode(char c) {
0N/A char testEncode = index2.charAt(index1[(c & mask1) >> shift]
0N/A + (c & mask2));
0N/A return testEncode != '\u0000' || c == '\u0000';
0N/A }
0N/A
0N/A private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
0N/A char[] sa = src.array();
0N/A int sp = src.arrayOffset() + src.position();
0N/A int sl = src.arrayOffset() + src.limit();
0N/A assert (sp <= sl);
0N/A sp = (sp <= sl ? sp : sl);
0N/A byte[] da = dst.array();
0N/A int dp = dst.arrayOffset() + dst.position();
0N/A int dl = dst.arrayOffset() + dst.limit();
0N/A assert (dp <= dl);
0N/A dp = (dp <= dl ? dp : dl);
0N/A
0N/A try {
0N/A while (sp < sl) {
0N/A char c = sa[sp];
1602N/A if (Character.isSurrogate(c)) {
0N/A if (sgp.parse(c, sa, sp, sl) < 0)
0N/A return sgp.error();
0N/A return sgp.unmappableResult();
0N/A }
0N/A if (c >= '\uFFFE')
0N/A return CoderResult.unmappableForLength(1);
0N/A if (dl - dp < 1)
0N/A return CoderResult.OVERFLOW;
0N/A
0N/A char e = index2.charAt(index1[(c & mask1) >> shift]
0N/A + (c & mask2));
0N/A
0N/A // If output byte is zero because input char is zero
0N/A // then character is mappable, o.w. fail
0N/A if (e == '\u0000' && c != '\u0000')
0N/A return CoderResult.unmappableForLength(1);
0N/A
0N/A sp++;
0N/A da[dp++] = (byte)e;
0N/A }
0N/A return CoderResult.UNDERFLOW;
0N/A } finally {
0N/A src.position(sp - src.arrayOffset());
0N/A dst.position(dp - dst.arrayOffset());
0N/A }
0N/A }
0N/A
0N/A private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
0N/A int mark = src.position();
0N/A try {
0N/A while (src.hasRemaining()) {
0N/A char c = src.get();
1602N/A if (Character.isSurrogate(c)) {
0N/A if (sgp.parse(c, src) < 0)
0N/A return sgp.error();
0N/A return sgp.unmappableResult();
0N/A }
0N/A if (c >= '\uFFFE')
0N/A return CoderResult.unmappableForLength(1);
0N/A if (!dst.hasRemaining())
0N/A return CoderResult.OVERFLOW;
0N/A
0N/A char e = index2.charAt(index1[(c & mask1) >> shift]
0N/A + (c & mask2));
0N/A
0N/A // If output byte is zero because input char is zero
0N/A // then character is mappable, o.w. fail
0N/A if (e == '\u0000' && c != '\u0000')
0N/A return CoderResult.unmappableForLength(1);
0N/A
0N/A mark++;
0N/A dst.put((byte)e);
0N/A }
0N/A return CoderResult.UNDERFLOW;
0N/A } finally {
0N/A src.position(mark);
0N/A }
0N/A }
0N/A
0N/A protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
0N/A if (true && src.hasArray() && dst.hasArray())
0N/A return encodeArrayLoop(src, dst);
0N/A else
0N/A return encodeBufferLoop(src, dst);
0N/A }
0N/A
0N/A public byte encode(char inputChar) {
0N/A return (byte)index2.charAt(index1[(inputChar & mask1) >> shift] +
0N/A (inputChar & mask2));
0N/A }
0N/A}