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