001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020:
021: /*
022: * @(#)SingleByteEncoder.java 1.14 03/01/23
023: */
024:
025: package com.sun.codemodel.util;
026:
027: import java.nio.ByteBuffer;
028: import java.nio.CharBuffer;
029: import java.nio.charset.Charset;
030: import java.nio.charset.CharsetEncoder;
031: import java.nio.charset.CoderResult;
032:
033: import sun.nio.cs.Surrogate;
034:
035: abstract class SingleByteEncoder extends CharsetEncoder {
036:
037: private final short index1[];
038: private final String index2;
039: private final int mask1;
040: private final int mask2;
041: private final int shift;
042:
043: private final Surrogate.Parser sgp = new Surrogate.Parser();
044:
045: protected SingleByteEncoder(Charset cs, short[] index1,
046: String index2, int mask1, int mask2, int shift) {
047: super (cs, 1.0f, 1.0f);
048: this .index1 = index1;
049: this .index2 = index2;
050: this .mask1 = mask1;
051: this .mask2 = mask2;
052: this .shift = shift;
053: }
054:
055: public boolean canEncode(char c) {
056: char testEncode;
057: testEncode = index2.charAt(index1[(c & mask1) >> shift]
058: + (c & mask2));
059: if (testEncode == '\u0000')
060: return false;
061: else
062: return true;
063: }
064:
065: private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
066: char[] sa = src.array();
067: int sp = src.arrayOffset() + src.position();
068: int sl = src.arrayOffset() + src.limit();
069: sp = (sp <= sl ? sp : sl);
070: byte[] da = dst.array();
071: int dp = dst.arrayOffset() + dst.position();
072: int dl = dst.arrayOffset() + dst.limit();
073: dp = (dp <= dl ? dp : dl);
074:
075: try {
076: while (sp < sl) {
077: char c = sa[sp];
078: if (Surrogate.is(c)) {
079: if (sgp.parse(c, sa, sp, sl) < 0)
080: return sgp.error();
081: return sgp.unmappableResult();
082: }
083: if (c >= '\uFFFE')
084: return CoderResult.unmappableForLength(1);
085: if (dl - dp < 1)
086: return CoderResult.OVERFLOW;
087:
088: char e = index2.charAt(index1[(c & mask1) >> shift]
089: + (c & mask2));
090:
091: // If output byte is zero because input char is zero
092: // then character is mappable, o.w. fail
093: if (e == '\u0000' && c != '\u0000')
094: return CoderResult.unmappableForLength(1);
095:
096: sp++;
097: da[dp++] = (byte) e;
098: }
099: return CoderResult.UNDERFLOW;
100: } finally {
101: src.position(sp - src.arrayOffset());
102: dst.position(dp - dst.arrayOffset());
103: }
104: }
105:
106: private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
107: int mark = src.position();
108: try {
109: while (src.hasRemaining()) {
110: char c = src.get();
111: if (Surrogate.is(c)) {
112: if (sgp.parse(c, src) < 0)
113: return sgp.error();
114: return sgp.unmappableResult();
115: }
116: if (c >= '\uFFFE')
117: return CoderResult.unmappableForLength(1);
118: if (!dst.hasRemaining())
119: return CoderResult.OVERFLOW;
120:
121: char e = index2.charAt(index1[(c & mask1) >> shift]
122: + (c & mask2));
123:
124: // If output byte is zero because input char is zero
125: // then character is mappable, o.w. fail
126: if (e == '\u0000' && c != '\u0000')
127: return CoderResult.unmappableForLength(1);
128:
129: mark++;
130: dst.put((byte) e);
131: }
132: return CoderResult.UNDERFLOW;
133: } finally {
134: src.position(mark);
135: }
136: }
137:
138: protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
139: if (true && src.hasArray() && dst.hasArray())
140: return encodeArrayLoop(src, dst);
141: else
142: return encodeBufferLoop(src, dst);
143: }
144:
145: public byte encode(char inputChar) {
146: return (byte) index2
147: .charAt(index1[(inputChar & mask1) >> shift]
148: + (inputChar & mask2));
149: }
150: }
|