src/share/classes/com/sun/codemodel/internal/util/SingleByteEncoder.java

Mon, 04 May 2009 21:10:41 -0700

author
tbell
date
Mon, 04 May 2009 21:10:41 -0700
changeset 50
42dfec6871f6
parent 45
31822b475baa
permissions
-rw-r--r--

6658158: Mutable statics in SAAJ (findbugs)
6658163: txw2.DatatypeWriter.BUILDIN is a mutable static (findbugs)
Reviewed-by: darcy

duke@1 1 /*
tbell@45 2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
tbell@45 26 /*
tbell@50 27 * @(#)SingleByteEncoder.java 1.14 03/01/23
tbell@45 28 */
tbell@45 29
duke@1 30 package com.sun.codemodel.internal.util;
duke@1 31
duke@1 32 import java.nio.ByteBuffer;
duke@1 33 import java.nio.CharBuffer;
duke@1 34 import java.nio.charset.Charset;
duke@1 35 import java.nio.charset.CharsetEncoder;
duke@1 36 import java.nio.charset.CoderResult;
duke@1 37
duke@1 38 import sun.nio.cs.Surrogate;
duke@1 39
duke@1 40
duke@1 41 abstract class SingleByteEncoder
duke@1 42 extends CharsetEncoder
duke@1 43 {
duke@1 44
duke@1 45 private final short index1[];
duke@1 46 private final String index2;
duke@1 47 private final int mask1;
duke@1 48 private final int mask2;
duke@1 49 private final int shift;
duke@1 50
duke@1 51 private final Surrogate.Parser sgp = new Surrogate.Parser();
duke@1 52
duke@1 53 protected SingleByteEncoder(Charset cs,
tbell@50 54 short[] index1, String index2,
tbell@50 55 int mask1, int mask2, int shift)
duke@1 56 {
tbell@50 57 super(cs, 1.0f, 1.0f);
tbell@50 58 this.index1 = index1;
tbell@50 59 this.index2 = index2;
tbell@50 60 this.mask1 = mask1;
tbell@50 61 this.mask2 = mask2;
tbell@50 62 this.shift = shift;
duke@1 63 }
duke@1 64
duke@1 65 public boolean canEncode(char c) {
tbell@50 66 char testEncode;
tbell@50 67 testEncode = index2.charAt(index1[(c & mask1) >> shift]
tbell@50 68 + (c & mask2));
tbell@50 69 if (testEncode == '\u0000')
tbell@50 70 return false;
tbell@50 71 else
tbell@50 72 return true;
duke@1 73 }
duke@1 74
duke@1 75 private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
tbell@50 76 char[] sa = src.array();
tbell@50 77 int sp = src.arrayOffset() + src.position();
tbell@50 78 int sl = src.arrayOffset() + src.limit();
tbell@50 79 sp = (sp <= sl ? sp : sl);
tbell@50 80 byte[] da = dst.array();
tbell@50 81 int dp = dst.arrayOffset() + dst.position();
tbell@50 82 int dl = dst.arrayOffset() + dst.limit();
tbell@50 83 dp = (dp <= dl ? dp : dl);
duke@1 84
tbell@50 85 try {
tbell@50 86 while (sp < sl) {
tbell@50 87 char c = sa[sp];
tbell@50 88 if (Surrogate.is(c)) {
tbell@50 89 if (sgp.parse(c, sa, sp, sl) < 0)
tbell@50 90 return sgp.error();
tbell@50 91 return sgp.unmappableResult();
tbell@50 92 }
tbell@50 93 if (c >= '\uFFFE')
tbell@50 94 return CoderResult.unmappableForLength(1);
tbell@50 95 if (dl - dp < 1)
tbell@50 96 return CoderResult.OVERFLOW;
duke@1 97
tbell@50 98 char e = index2.charAt(index1[(c & mask1) >> shift]
tbell@50 99 + (c & mask2));
duke@1 100
tbell@50 101 // If output byte is zero because input char is zero
tbell@50 102 // then character is mappable, o.w. fail
tbell@50 103 if (e == '\u0000' && c != '\u0000')
tbell@50 104 return CoderResult.unmappableForLength(1);
duke@1 105
tbell@50 106 sp++;
tbell@50 107 da[dp++] = (byte)e;
tbell@50 108 }
tbell@50 109 return CoderResult.UNDERFLOW;
tbell@50 110 } finally {
tbell@50 111 src.position(sp - src.arrayOffset());
tbell@50 112 dst.position(dp - dst.arrayOffset());
tbell@50 113 }
duke@1 114 }
duke@1 115
duke@1 116 private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
tbell@50 117 int mark = src.position();
tbell@50 118 try {
tbell@50 119 while (src.hasRemaining()) {
tbell@50 120 char c = src.get();
tbell@50 121 if (Surrogate.is(c)) {
tbell@50 122 if (sgp.parse(c, src) < 0)
tbell@50 123 return sgp.error();
tbell@50 124 return sgp.unmappableResult();
tbell@50 125 }
tbell@50 126 if (c >= '\uFFFE')
tbell@50 127 return CoderResult.unmappableForLength(1);
tbell@50 128 if (!dst.hasRemaining())
tbell@50 129 return CoderResult.OVERFLOW;
duke@1 130
tbell@50 131 char e = index2.charAt(index1[(c & mask1) >> shift]
tbell@50 132 + (c & mask2));
duke@1 133
tbell@50 134 // If output byte is zero because input char is zero
tbell@50 135 // then character is mappable, o.w. fail
tbell@50 136 if (e == '\u0000' && c != '\u0000')
tbell@50 137 return CoderResult.unmappableForLength(1);
duke@1 138
tbell@50 139 mark++;
tbell@50 140 dst.put((byte)e);
tbell@50 141 }
tbell@50 142 return CoderResult.UNDERFLOW;
tbell@50 143 } finally {
tbell@50 144 src.position(mark);
tbell@50 145 }
duke@1 146 }
duke@1 147
duke@1 148 protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
tbell@50 149 if (true && src.hasArray() && dst.hasArray())
tbell@50 150 return encodeArrayLoop(src, dst);
tbell@50 151 else
tbell@50 152 return encodeBufferLoop(src, dst);
duke@1 153 }
duke@1 154
duke@1 155 public byte encode(char inputChar) {
tbell@50 156 return (byte)index2.charAt(index1[(inputChar & mask1) >> shift] +
tbell@50 157 (inputChar & mask2));
duke@1 158 }
duke@1 159 }

mercurial