]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/classes/charset/src/com/ibm/icu/charset/Charset88591.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / classes / charset / src / com / ibm / icu / charset / Charset88591.java
1 /**
2  *******************************************************************************
3  * Copyright (C) 2006-2008, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7
8 package com.ibm.icu.charset;
9
10 import java.nio.BufferOverflowException;
11 import java.nio.BufferUnderflowException;
12 import java.nio.ByteBuffer;
13 import java.nio.CharBuffer;
14 import java.nio.charset.CharsetDecoder;
15 import java.nio.charset.CharsetEncoder;
16 import java.nio.charset.CoderResult;
17
18 import com.ibm.icu.text.UnicodeSet;
19
20 class Charset88591 extends CharsetASCII {
21     public Charset88591(String icuCanonicalName, String javaCanonicalName, String[] aliases) {
22         super(icuCanonicalName, javaCanonicalName, aliases);
23     }
24
25     class CharsetDecoder88591 extends CharsetDecoderASCII {
26         public CharsetDecoder88591(CharsetICU cs) {
27             super(cs);
28         }
29
30         protected CoderResult decodeLoopCoreOptimized(ByteBuffer source, CharBuffer target,
31                 byte[] sourceArray, char[] targetArray, int oldSource, int offset, int limit) {
32
33             /*
34              * perform 88591 conversion from the source array to the target array. no range check is
35              * necessary.
36              */
37             for (int i = oldSource; i < limit; i++)
38                 targetArray[i + offset] = (char) (sourceArray[i] & 0xff);
39
40             return null;
41         }
42
43         protected CoderResult decodeLoopCoreUnoptimized(ByteBuffer source, CharBuffer target)
44                 throws BufferUnderflowException, BufferOverflowException {
45
46             /*
47              * perform 88591 conversion from the source buffer to the target buffer. no range check
48              * is necessary (an exception will be generated to end the loop).
49              */
50             while (true)
51                 target.put((char) (source.get() & 0xff));
52         }
53     }
54
55     class CharsetEncoder88591 extends CharsetEncoderASCII {
56         public CharsetEncoder88591(CharsetICU cs) {
57             super(cs);
58         }
59
60         protected final CoderResult encodeLoopCoreOptimized(CharBuffer source, ByteBuffer target,
61                 char[] sourceArray, byte[] targetArray, int oldSource, int offset, int limit,
62                 boolean flush) {
63             int i, ch = 0;
64
65             /*
66              * perform 88591 conversion from the source array to the target array, making sure each
67              * char in the source is within the correct range
68              */
69             for (i = oldSource; i < limit; i++) {
70                 ch = (int) sourceArray[i];
71                 if ((ch & 0xff00) == 0) {
72                     targetArray[i + offset] = (byte) ch;
73                 } else {
74                     break;
75                 }
76             }
77
78             /*
79              * if some byte was not in the correct range, we need to deal with this byte by calling
80              * encodeMalformedOrUnmappable and move the source and target positions to reflect the
81              * early termination of the loop
82              */
83             if ((ch & 0xff00) != 0) {
84                 source.position(i + 1);
85                 target.position(i + offset);
86                 return encodeMalformedOrUnmappable(source, ch, flush);
87             } else
88                 return null;
89         }
90
91         protected final CoderResult encodeLoopCoreUnoptimized(CharBuffer source, ByteBuffer target,
92                 boolean flush) throws BufferUnderflowException, BufferOverflowException {
93             int ch;
94
95             /*
96              * perform 88591 conversion from the source buffer to the target buffer, making sure
97              * each char in the source is within the correct range
98              */
99             
100             while (true) {
101                 ch = (int) source.get();
102                 if ((ch & 0xff00) == 0) {
103                     target.put((byte) ch);
104                 } else {
105                     break;
106                 }
107             }
108             /*
109              * if we reach here, it's because a character was not in the correct range, and we need
110              * to deak with this by calling encodeMalformedOrUnmappable.
111              */
112             return encodeMalformedOrUnmappable(source, ch, flush);
113         }
114
115     }
116
117     public CharsetDecoder newDecoder() {
118         return new CharsetDecoder88591(this);
119     }
120
121     public CharsetEncoder newEncoder() {
122         return new CharsetEncoder88591(this);
123     }
124     
125     void getUnicodeSetImpl( UnicodeSet setFillIn, int which){
126         setFillIn.add(0,0xff);
127      }
128 }