author | igerasim |
Wed, 27 Aug 2014 22:08:19 +0400 | |
changeset 26219 | 1a19360ff122 |
parent 25859 | 3317bb8137f4 |
child 36511 | 9d0388c6b336 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
21278
diff
changeset
|
2 |
* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/* |
|
27 |
* |
|
28 |
* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved |
|
29 |
* (C) Copyright IBM Corp. 1996 - 2002 - All Rights Reserved |
|
30 |
* |
|
31 |
* The original version of this source code and documentation |
|
32 |
* is copyrighted and owned by Taligent, Inc., a wholly-owned |
|
33 |
* subsidiary of IBM. These materials are provided under terms |
|
34 |
* of a License Agreement between Taligent and Sun. This technology |
|
35 |
* is protected by multiple US and International patents. |
|
36 |
* |
|
37 |
* This notice and attribution to Taligent may not be removed. |
|
38 |
* Taligent is a registered trademark of Taligent, Inc. |
|
39 |
*/ |
|
40 |
||
13583 | 41 |
package sun.util.locale.provider; |
2 | 42 |
|
43 |
import java.io.BufferedInputStream; |
|
44 |
import java.io.IOException; |
|
45 |
import java.security.AccessController; |
|
46 |
import java.security.PrivilegedActionException; |
|
47 |
import java.security.PrivilegedExceptionAction; |
|
13583 | 48 |
import java.text.BreakIterator; |
2 | 49 |
import java.text.CharacterIterator; |
50 |
import java.text.StringCharacterIterator; |
|
13583 | 51 |
import java.util.MissingResourceException; |
2 | 52 |
import sun.text.CompactByteArray; |
53 |
import sun.text.SupplementaryCharacterData; |
|
54 |
||
55 |
/** |
|
56 |
* <p>A subclass of BreakIterator whose behavior is specified using a list of rules.</p> |
|
57 |
* |
|
58 |
* <p>There are two kinds of rules, which are separated by semicolons: <i>substitutions</i> |
|
59 |
* and <i>regular expressions.</i></p> |
|
60 |
* |
|
61 |
* <p>A substitution rule defines a name that can be used in place of an expression. It |
|
62 |
* consists of a name, which is a string of characters contained in angle brackets, an equals |
|
63 |
* sign, and an expression. (There can be no whitespace on either side of the equals sign.) |
|
64 |
* To keep its syntactic meaning intact, the expression must be enclosed in parentheses or |
|
65 |
* square brackets. A substitution is visible after its definition, and is filled in using |
|
66 |
* simple textual substitution. Substitution definitions can contain other substitutions, as |
|
67 |
* long as those substitutions have been defined first. Substitutions are generally used to |
|
68 |
* make the regular expressions (which can get quite complex) shorted and easier to read. |
|
69 |
* They typically define either character categories or commonly-used subexpressions.</p> |
|
70 |
* |
|
71 |
* <p>There is one special substitution. If the description defines a substitution |
|
72 |
* called "<ignore>", the expression must be a [] expression, and the |
|
73 |
* expression defines a set of characters (the "<em>ignore characters</em>") that |
|
74 |
* will be transparent to the BreakIterator. A sequence of characters will break the |
|
75 |
* same way it would if any ignore characters it contains are taken out. Break |
|
76 |
* positions never occur befoer ignore characters.</p> |
|
77 |
* |
|
78 |
* <p>A regular expression uses a subset of the normal Unix regular-expression syntax, and |
|
79 |
* defines a sequence of characters to be kept together. With one significant exception, the |
|
80 |
* iterator uses a longest-possible-match algorithm when matching text to regular |
|
81 |
* expressions. The iterator also treats descriptions containing multiple regular expressions |
|
82 |
* as if they were ORed together (i.e., as if they were separated by |).</p> |
|
83 |
* |
|
84 |
* <p>The special characters recognized by the regular-expression parser are as follows:</p> |
|
85 |
* |
|
86 |
* <blockquote> |
|
87 |
* <table border="1" width="100%"> |
|
88 |
* <tr> |
|
89 |
* <td width="6%">*</td> |
|
90 |
* <td width="94%">Specifies that the expression preceding the asterisk may occur any number |
|
91 |
* of times (including not at all).</td> |
|
92 |
* </tr> |
|
93 |
* <tr> |
|
94 |
* <td width="6%">{}</td> |
|
95 |
* <td width="94%">Encloses a sequence of characters that is optional.</td> |
|
96 |
* </tr> |
|
97 |
* <tr> |
|
98 |
* <td width="6%">()</td> |
|
99 |
* <td width="94%">Encloses a sequence of characters. If followed by *, the sequence |
|
100 |
* repeats. Otherwise, the parentheses are just a grouping device and a way to delimit |
|
101 |
* the ends of expressions containing |.</td> |
|
102 |
* </tr> |
|
103 |
* <tr> |
|
104 |
* <td width="6%">|</td> |
|
105 |
* <td width="94%">Separates two alternative sequences of characters. Either one |
|
106 |
* sequence or the other, but not both, matches this expression. The | character can |
|
107 |
* only occur inside ().</td> |
|
108 |
* </tr> |
|
109 |
* <tr> |
|
110 |
* <td width="6%">.</td> |
|
111 |
* <td width="94%">Matches any character.</td> |
|
112 |
* </tr> |
|
113 |
* <tr> |
|
114 |
* <td width="6%">*?</td> |
|
115 |
* <td width="94%">Specifies a non-greedy asterisk. *? works the same way as *, except |
|
116 |
* when there is overlap between the last group of characters in the expression preceding the |
|
117 |
* * and the first group of characters following the *. When there is this kind of |
|
118 |
* overlap, * will match the longest sequence of characters that match the expression before |
|
119 |
* the *, and *? will match the shortest sequence of characters matching the expression |
|
120 |
* before the *?. For example, if you have "xxyxyyyxyxyxxyxyxyy" in the text, |
|
121 |
* "x[xy]*x" will match through to the last x (i.e., "<strong>xxyxyyyxyxyxxyxyx</strong>yy", |
|
122 |
* but "x[xy]*?x" will only match the first two xes ("<strong>xx</strong>yxyyyxyxyxxyxyxyy").</td> |
|
123 |
* </tr> |
|
124 |
* <tr> |
|
125 |
* <td width="6%">[]</td> |
|
126 |
* <td width="94%">Specifies a group of alternative characters. A [] expression will |
|
127 |
* match any single character that is specified in the [] expression. For more on the |
|
128 |
* syntax of [] expressions, see below.</td> |
|
129 |
* </tr> |
|
130 |
* <tr> |
|
131 |
* <td width="6%">/</td> |
|
132 |
* <td width="94%">Specifies where the break position should go if text matches this |
|
133 |
* expression. (e.g., "[a-z]*/[:Zs:]*[1-0]" will match if the iterator sees a run |
|
134 |
* of letters, followed by a run of whitespace, followed by a digit, but the break position |
|
135 |
* will actually go before the whitespace). Expressions that don't contain / put the |
|
136 |
* break position at the end of the matching text.</td> |
|
137 |
* </tr> |
|
138 |
* <tr> |
|
139 |
* <td width="6%">\</td> |
|
140 |
* <td width="94%">Escape character. The \ itself is ignored, but causes the next |
|
141 |
* character to be treated as literal character. This has no effect for many |
|
142 |
* characters, but for the characters listed above, this deprives them of their special |
|
143 |
* meaning. (There are no special escape sequences for Unicode characters, or tabs and |
|
144 |
* newlines; these are all handled by a higher-level protocol. In a Java string, |
|
145 |
* "\n" will be converted to a literal newline character by the time the |
|
146 |
* regular-expression parser sees it. Of course, this means that \ sequences that are |
|
147 |
* visible to the regexp parser must be written as \\ when inside a Java string.) All |
|
148 |
* characters in the ASCII range except for letters, digits, and control characters are |
|
149 |
* reserved characters to the parser and must be preceded by \ even if they currently don't |
|
150 |
* mean anything.</td> |
|
151 |
* </tr> |
|
152 |
* <tr> |
|
153 |
* <td width="6%">!</td> |
|
154 |
* <td width="94%">If ! appears at the beginning of a regular expression, it tells the regexp |
|
155 |
* parser that this expression specifies the backwards-iteration behavior of the iterator, |
|
156 |
* and not its normal iteration behavior. This is generally only used in situations |
|
157 |
* where the automatically-generated backwards-iteration brhavior doesn't produce |
|
158 |
* satisfactory results and must be supplemented with extra client-specified rules.</td> |
|
159 |
* </tr> |
|
160 |
* <tr> |
|
161 |
* <td width="6%"><em>(all others)</em></td> |
|
162 |
* <td width="94%">All other characters are treated as literal characters, which must match |
|
163 |
* the corresponding character(s) in the text exactly.</td> |
|
164 |
* </tr> |
|
165 |
* </table> |
|
166 |
* </blockquote> |
|
167 |
* |
|
168 |
* <p>Within a [] expression, a number of other special characters can be used to specify |
|
169 |
* groups of characters:</p> |
|
170 |
* |
|
171 |
* <blockquote> |
|
172 |
* <table border="1" width="100%"> |
|
173 |
* <tr> |
|
174 |
* <td width="6%">-</td> |
|
175 |
* <td width="94%">Specifies a range of matching characters. For example |
|
176 |
* "[a-p]" matches all lowercase Latin letters from a to p (inclusive). The - |
|
177 |
* sign specifies ranges of continuous Unicode numeric values, not ranges of characters in a |
|
178 |
* language's alphabetical order: "[a-z]" doesn't include capital letters, nor does |
|
179 |
* it include accented letters such as a-umlaut.</td> |
|
180 |
* </tr> |
|
181 |
* <tr> |
|
182 |
* <td width="6%">::</td> |
|
183 |
* <td width="94%">A pair of colons containing a one- or two-letter code matches all |
|
184 |
* characters in the corresponding Unicode category. The two-letter codes are the same |
|
185 |
* as the two-letter codes in the Unicode database (for example, "[:Sc::Sm:]" |
|
186 |
* matches all currency symbols and all math symbols). Specifying a one-letter code is |
|
187 |
* the same as specifying all two-letter codes that begin with that letter (for example, |
|
188 |
* "[:L:]" matches all letters, and is equivalent to |
|
189 |
* "[:Lu::Ll::Lo::Lm::Lt:]"). Anything other than a valid two-letter Unicode |
|
190 |
* category code or a single letter that begins a Unicode category code is illegal within |
|
191 |
* colons.</td> |
|
192 |
* </tr> |
|
193 |
* <tr> |
|
194 |
* <td width="6%">[]</td> |
|
195 |
* <td width="94%">[] expressions can nest. This has no effect, except when used in |
|
196 |
* conjunction with the ^ token.</td> |
|
197 |
* </tr> |
|
198 |
* <tr> |
|
199 |
* <td width="6%">^</td> |
|
200 |
* <td width="94%">Excludes the character (or the characters in the [] expression) following |
|
201 |
* it from the group of characters. For example, "[a-z^p]" matches all Latin |
|
202 |
* lowercase letters except p. "[:L:^[\u4e00-\u9fff]]" matches all letters |
|
203 |
* except the Han ideographs.</td> |
|
204 |
* </tr> |
|
205 |
* <tr> |
|
206 |
* <td width="6%"><em>(all others)</em></td> |
|
207 |
* <td width="94%">All other characters are treated as literal characters. (For |
|
208 |
* example, "[aeiou]" specifies just the letters a, e, i, o, and u.)</td> |
|
209 |
* </tr> |
|
210 |
* </table> |
|
211 |
* </blockquote> |
|
212 |
* |
|
213 |
* <p>For a more complete explanation, see <a |
|
214 |
* href="http://www.ibm.com/java/education/boundaries/boundaries.html">http://www.ibm.com/java/education/boundaries/boundaries.html</a>. |
|
215 |
* For examples, see the resource data (which is annotated).</p> |
|
216 |
* |
|
217 |
* @author Richard Gillam |
|
218 |
*/ |
|
219 |
class RuleBasedBreakIterator extends BreakIterator { |
|
220 |
||
221 |
/** |
|
222 |
* A token used as a character-category value to identify ignore characters |
|
223 |
*/ |
|
224 |
protected static final byte IGNORE = -1; |
|
225 |
||
226 |
/** |
|
227 |
* The state number of the starting state |
|
228 |
*/ |
|
229 |
private static final short START_STATE = 1; |
|
230 |
||
231 |
/** |
|
232 |
* The state-transition value indicating "stop" |
|
233 |
*/ |
|
234 |
private static final short STOP_STATE = 0; |
|
235 |
||
236 |
/** |
|
237 |
* Magic number for the BreakIterator data file format. |
|
238 |
*/ |
|
239 |
static final byte[] LABEL = { |
|
240 |
(byte)'B', (byte)'I', (byte)'d', (byte)'a', (byte)'t', (byte)'a', |
|
241 |
(byte)'\0' |
|
242 |
}; |
|
243 |
static final int LABEL_LENGTH = LABEL.length; |
|
244 |
||
245 |
/** |
|
246 |
* Version number of the dictionary that was read in. |
|
247 |
*/ |
|
248 |
static final byte supportedVersion = 1; |
|
249 |
||
250 |
/** |
|
251 |
* Header size in byte count |
|
252 |
*/ |
|
253 |
private static final int HEADER_LENGTH = 36; |
|
254 |
||
255 |
/** |
|
256 |
* An array length of indices for BMP characters |
|
257 |
*/ |
|
258 |
private static final int BMP_INDICES_LENGTH = 512; |
|
259 |
||
260 |
/** |
|
261 |
* Tables that indexes from character values to character category numbers |
|
262 |
*/ |
|
263 |
private CompactByteArray charCategoryTable = null; |
|
264 |
private SupplementaryCharacterData supplementaryCharCategoryTable = null; |
|
265 |
||
266 |
/** |
|
267 |
* The table of state transitions used for forward iteration |
|
268 |
*/ |
|
269 |
private short[] stateTable = null; |
|
270 |
||
271 |
/** |
|
272 |
* The table of state transitions used to sync up the iterator with the |
|
273 |
* text in backwards and random-access iteration |
|
274 |
*/ |
|
275 |
private short[] backwardsStateTable = null; |
|
276 |
||
277 |
/** |
|
278 |
* A list of flags indicating which states in the state table are accepting |
|
279 |
* ("end") states |
|
280 |
*/ |
|
281 |
private boolean[] endStates = null; |
|
282 |
||
283 |
/** |
|
284 |
* A list of flags indicating which states in the state table are |
|
285 |
* lookahead states (states which turn lookahead on and off) |
|
286 |
*/ |
|
287 |
private boolean[] lookaheadStates = null; |
|
288 |
||
289 |
/** |
|
290 |
* A table for additional data. May be used by a subclass of |
|
291 |
* RuleBasedBreakIterator. |
|
292 |
*/ |
|
293 |
private byte[] additionalData = null; |
|
294 |
||
295 |
/** |
|
296 |
* The number of character categories (and, thus, the number of columns in |
|
297 |
* the state tables) |
|
298 |
*/ |
|
299 |
private int numCategories; |
|
300 |
||
301 |
/** |
|
302 |
* The character iterator through which this BreakIterator accesses the text |
|
303 |
*/ |
|
304 |
private CharacterIterator text = null; |
|
305 |
||
306 |
/** |
|
307 |
* A CRC32 value of all data in datafile |
|
308 |
*/ |
|
309 |
private long checksum; |
|
310 |
||
311 |
//======================================================================= |
|
312 |
// constructors |
|
313 |
//======================================================================= |
|
314 |
||
315 |
/** |
|
316 |
* Constructs a RuleBasedBreakIterator according to the datafile |
|
317 |
* provided. |
|
318 |
*/ |
|
13583 | 319 |
RuleBasedBreakIterator(String datafile) |
2 | 320 |
throws IOException, MissingResourceException { |
321 |
readTables(datafile); |
|
322 |
} |
|
323 |
||
324 |
/** |
|
325 |
* Read datafile. The datafile's format is as follows: |
|
326 |
* <pre> |
|
327 |
* BreakIteratorData { |
|
328 |
* u1 magic[7]; |
|
329 |
* u1 version; |
|
330 |
* u4 totalDataSize; |
|
331 |
* header_info header; |
|
332 |
* body value; |
|
333 |
* } |
|
334 |
* </pre> |
|
335 |
* <code>totalDataSize</code> is the summation of the size of |
|
336 |
* <code>header_info</code> and <code>body</code> in byte count. |
|
337 |
* <p> |
|
338 |
* In <code>header</code>, each field except for checksum implies the |
|
339 |
* length of each field. Since <code>BMPdataLength</code> is a fixed-length |
|
340 |
* data(512 entries), its length isn't included in <code>header</code>. |
|
341 |
* <code>checksum</code> is a CRC32 value of all in <code>body</code>. |
|
342 |
* <pre> |
|
343 |
* header_info { |
|
344 |
* u4 stateTableLength; |
|
345 |
* u4 backwardsStateTableLength; |
|
346 |
* u4 endStatesLength; |
|
347 |
* u4 lookaheadStatesLength; |
|
348 |
* u4 BMPdataLength; |
|
349 |
* u4 nonBMPdataLength; |
|
350 |
* u4 additionalDataLength; |
|
351 |
* u8 checksum; |
|
352 |
* } |
|
353 |
* </pre> |
|
354 |
* <p> |
|
355 |
* |
|
356 |
* Finally, <code>BMPindices</code> and <code>BMPdata</code> are set to |
|
357 |
* <code>charCategoryTable</code>. <code>nonBMPdata</code> is set to |
|
358 |
* <code>supplementaryCharCategoryTable</code>. |
|
359 |
* <pre> |
|
360 |
* body { |
|
361 |
* u2 stateTable[stateTableLength]; |
|
362 |
* u2 backwardsStateTable[backwardsStateTableLength]; |
|
363 |
* u1 endStates[endStatesLength]; |
|
364 |
* u1 lookaheadStates[lookaheadStatesLength]; |
|
365 |
* u2 BMPindices[512]; |
|
366 |
* u1 BMPdata[BMPdataLength]; |
|
367 |
* u4 nonBMPdata[numNonBMPdataLength]; |
|
368 |
* u1 additionalData[additionalDataLength]; |
|
369 |
* } |
|
370 |
* </pre> |
|
371 |
*/ |
|
13583 | 372 |
protected final void readTables(String datafile) |
2 | 373 |
throws IOException, MissingResourceException { |
374 |
||
375 |
byte[] buffer = readFile(datafile); |
|
376 |
||
377 |
/* Read header_info. */ |
|
13583 | 378 |
int stateTableLength = getInt(buffer, 0); |
379 |
int backwardsStateTableLength = getInt(buffer, 4); |
|
380 |
int endStatesLength = getInt(buffer, 8); |
|
381 |
int lookaheadStatesLength = getInt(buffer, 12); |
|
382 |
int BMPdataLength = getInt(buffer, 16); |
|
383 |
int nonBMPdataLength = getInt(buffer, 20); |
|
384 |
int additionalDataLength = getInt(buffer, 24); |
|
385 |
checksum = getLong(buffer, 28); |
|
2 | 386 |
|
387 |
/* Read stateTable[numCategories * numRows] */ |
|
388 |
stateTable = new short[stateTableLength]; |
|
389 |
int offset = HEADER_LENGTH; |
|
390 |
for (int i = 0; i < stateTableLength; i++, offset+=2) { |
|
13583 | 391 |
stateTable[i] = getShort(buffer, offset); |
2 | 392 |
} |
393 |
||
394 |
/* Read backwardsStateTable[numCategories * numRows] */ |
|
395 |
backwardsStateTable = new short[backwardsStateTableLength]; |
|
396 |
for (int i = 0; i < backwardsStateTableLength; i++, offset+=2) { |
|
13583 | 397 |
backwardsStateTable[i] = getShort(buffer, offset); |
2 | 398 |
} |
399 |
||
400 |
/* Read endStates[numRows] */ |
|
401 |
endStates = new boolean[endStatesLength]; |
|
402 |
for (int i = 0; i < endStatesLength; i++, offset++) { |
|
403 |
endStates[i] = buffer[offset] == 1; |
|
404 |
} |
|
405 |
||
406 |
/* Read lookaheadStates[numRows] */ |
|
407 |
lookaheadStates = new boolean[lookaheadStatesLength]; |
|
408 |
for (int i = 0; i < lookaheadStatesLength; i++, offset++) { |
|
409 |
lookaheadStates[i] = buffer[offset] == 1; |
|
410 |
} |
|
411 |
||
412 |
/* Read a category table and indices for BMP characters. */ |
|
413 |
short[] temp1 = new short[BMP_INDICES_LENGTH]; // BMPindices |
|
414 |
for (int i = 0; i < BMP_INDICES_LENGTH; i++, offset+=2) { |
|
13583 | 415 |
temp1[i] = getShort(buffer, offset); |
2 | 416 |
} |
417 |
byte[] temp2 = new byte[BMPdataLength]; // BMPdata |
|
418 |
System.arraycopy(buffer, offset, temp2, 0, BMPdataLength); |
|
419 |
offset += BMPdataLength; |
|
420 |
charCategoryTable = new CompactByteArray(temp1, temp2); |
|
421 |
||
422 |
/* Read a category table for non-BMP characters. */ |
|
423 |
int[] temp3 = new int[nonBMPdataLength]; |
|
424 |
for (int i = 0; i < nonBMPdataLength; i++, offset+=4) { |
|
13583 | 425 |
temp3[i] = getInt(buffer, offset); |
2 | 426 |
} |
427 |
supplementaryCharCategoryTable = new SupplementaryCharacterData(temp3); |
|
428 |
||
429 |
/* Read additional data */ |
|
430 |
if (additionalDataLength > 0) { |
|
431 |
additionalData = new byte[additionalDataLength]; |
|
432 |
System.arraycopy(buffer, offset, additionalData, 0, additionalDataLength); |
|
433 |
} |
|
434 |
||
435 |
/* Set numCategories */ |
|
436 |
numCategories = stateTable.length / endStates.length; |
|
437 |
} |
|
438 |
||
439 |
protected byte[] readFile(final String datafile) |
|
440 |
throws IOException, MissingResourceException { |
|
441 |
||
442 |
BufferedInputStream is; |
|
443 |
try { |
|
12848 | 444 |
is = AccessController.doPrivileged( |
445 |
new PrivilegedExceptionAction<BufferedInputStream>() { |
|
13583 | 446 |
@Override |
12848 | 447 |
public BufferedInputStream run() throws Exception { |
2 | 448 |
return new BufferedInputStream(getClass().getResourceAsStream("/sun/text/resources/" + datafile)); |
449 |
} |
|
450 |
} |
|
451 |
); |
|
452 |
} |
|
453 |
catch (PrivilegedActionException e) { |
|
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
7668
diff
changeset
|
454 |
throw new InternalError(e.toString(), e); |
2 | 455 |
} |
456 |
||
457 |
int offset = 0; |
|
458 |
||
459 |
/* First, read magic, version, and header_info. */ |
|
460 |
int len = LABEL_LENGTH + 5; |
|
461 |
byte[] buf = new byte[len]; |
|
462 |
if (is.read(buf) != len) { |
|
463 |
throw new MissingResourceException("Wrong header length", |
|
464 |
datafile, ""); |
|
465 |
} |
|
466 |
||
467 |
/* Validate the magic number. */ |
|
468 |
for (int i = 0; i < LABEL_LENGTH; i++, offset++) { |
|
469 |
if (buf[offset] != LABEL[offset]) { |
|
470 |
throw new MissingResourceException("Wrong magic number", |
|
471 |
datafile, ""); |
|
472 |
} |
|
473 |
} |
|
474 |
||
475 |
/* Validate the version number. */ |
|
476 |
if (buf[offset] != supportedVersion) { |
|
477 |
throw new MissingResourceException("Unsupported version(" + buf[offset] + ")", |
|
478 |
datafile, ""); |
|
479 |
} |
|
480 |
||
481 |
/* Read data: totalDataSize + 8(for checksum) */ |
|
13583 | 482 |
len = getInt(buf, ++offset); |
2 | 483 |
buf = new byte[len]; |
484 |
if (is.read(buf) != len) { |
|
485 |
throw new MissingResourceException("Wrong data length", |
|
486 |
datafile, ""); |
|
487 |
} |
|
488 |
||
489 |
is.close(); |
|
490 |
||
491 |
return buf; |
|
492 |
} |
|
493 |
||
494 |
byte[] getAdditionalData() { |
|
495 |
return additionalData; |
|
496 |
} |
|
497 |
||
498 |
void setAdditionalData(byte[] b) { |
|
499 |
additionalData = b; |
|
500 |
} |
|
501 |
||
502 |
//======================================================================= |
|
503 |
// boilerplate |
|
504 |
//======================================================================= |
|
505 |
/** |
|
506 |
* Clones this iterator. |
|
507 |
* @return A newly-constructed RuleBasedBreakIterator with the same |
|
508 |
* behavior as this one. |
|
509 |
*/ |
|
13583 | 510 |
@Override |
2 | 511 |
public Object clone() { |
512 |
RuleBasedBreakIterator result = (RuleBasedBreakIterator) super.clone(); |
|
513 |
if (text != null) { |
|
514 |
result.text = (CharacterIterator) text.clone(); |
|
515 |
} |
|
516 |
return result; |
|
517 |
} |
|
518 |
||
519 |
/** |
|
520 |
* Returns true if both BreakIterators are of the same class, have the same |
|
521 |
* rules, and iterate over the same text. |
|
522 |
*/ |
|
13583 | 523 |
@Override |
2 | 524 |
public boolean equals(Object that) { |
525 |
try { |
|
526 |
if (that == null) { |
|
527 |
return false; |
|
528 |
} |
|
529 |
||
530 |
RuleBasedBreakIterator other = (RuleBasedBreakIterator) that; |
|
531 |
if (checksum != other.checksum) { |
|
532 |
return false; |
|
533 |
} |
|
534 |
if (text == null) { |
|
535 |
return other.text == null; |
|
536 |
} else { |
|
537 |
return text.equals(other.text); |
|
538 |
} |
|
539 |
} |
|
540 |
catch(ClassCastException e) { |
|
541 |
return false; |
|
542 |
} |
|
543 |
} |
|
544 |
||
545 |
/** |
|
546 |
* Returns text |
|
547 |
*/ |
|
13583 | 548 |
@Override |
2 | 549 |
public String toString() { |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
550 |
return "[checksum=0x" + Long.toHexString(checksum) + ']'; |
2 | 551 |
} |
552 |
||
553 |
/** |
|
554 |
* Compute a hashcode for this BreakIterator |
|
555 |
* @return A hash code |
|
556 |
*/ |
|
13583 | 557 |
@Override |
2 | 558 |
public int hashCode() { |
559 |
return (int)checksum; |
|
560 |
} |
|
561 |
||
562 |
//======================================================================= |
|
563 |
// BreakIterator overrides |
|
564 |
//======================================================================= |
|
565 |
||
566 |
/** |
|
567 |
* Sets the current iteration position to the beginning of the text. |
|
568 |
* (i.e., the CharacterIterator's starting offset). |
|
569 |
* @return The offset of the beginning of the text. |
|
570 |
*/ |
|
13583 | 571 |
@Override |
2 | 572 |
public int first() { |
573 |
CharacterIterator t = getText(); |
|
574 |
||
575 |
t.first(); |
|
576 |
return t.getIndex(); |
|
577 |
} |
|
578 |
||
579 |
/** |
|
580 |
* Sets the current iteration position to the end of the text. |
|
581 |
* (i.e., the CharacterIterator's ending offset). |
|
582 |
* @return The text's past-the-end offset. |
|
583 |
*/ |
|
13583 | 584 |
@Override |
2 | 585 |
public int last() { |
586 |
CharacterIterator t = getText(); |
|
587 |
||
588 |
// I'm not sure why, but t.last() returns the offset of the last character, |
|
589 |
// rather than the past-the-end offset |
|
590 |
t.setIndex(t.getEndIndex()); |
|
591 |
return t.getIndex(); |
|
592 |
} |
|
593 |
||
594 |
/** |
|
595 |
* Advances the iterator either forward or backward the specified number of steps. |
|
596 |
* Negative values move backward, and positive values move forward. This is |
|
597 |
* equivalent to repeatedly calling next() or previous(). |
|
598 |
* @param n The number of steps to move. The sign indicates the direction |
|
599 |
* (negative is backwards, and positive is forwards). |
|
600 |
* @return The character offset of the boundary position n boundaries away from |
|
601 |
* the current one. |
|
602 |
*/ |
|
13583 | 603 |
@Override |
2 | 604 |
public int next(int n) { |
605 |
int result = current(); |
|
606 |
while (n > 0) { |
|
607 |
result = handleNext(); |
|
608 |
--n; |
|
609 |
} |
|
610 |
while (n < 0) { |
|
611 |
result = previous(); |
|
612 |
++n; |
|
613 |
} |
|
614 |
return result; |
|
615 |
} |
|
616 |
||
617 |
/** |
|
618 |
* Advances the iterator to the next boundary position. |
|
619 |
* @return The position of the first boundary after this one. |
|
620 |
*/ |
|
13583 | 621 |
@Override |
2 | 622 |
public int next() { |
623 |
return handleNext(); |
|
624 |
} |
|
625 |
||
4844 | 626 |
private int cachedLastKnownBreak = BreakIterator.DONE; |
627 |
||
2 | 628 |
/** |
629 |
* Advances the iterator backwards, to the last boundary preceding this one. |
|
630 |
* @return The position of the last boundary position preceding this one. |
|
631 |
*/ |
|
13583 | 632 |
@Override |
2 | 633 |
public int previous() { |
634 |
// if we're already sitting at the beginning of the text, return DONE |
|
635 |
CharacterIterator text = getText(); |
|
636 |
if (current() == text.getBeginIndex()) { |
|
637 |
return BreakIterator.DONE; |
|
638 |
} |
|
639 |
||
640 |
// set things up. handlePrevious() will back us up to some valid |
|
641 |
// break position before the current position (we back our internal |
|
642 |
// iterator up one step to prevent handlePrevious() from returning |
|
643 |
// the current position), but not necessarily the last one before |
|
644 |
// where we started |
|
645 |
int start = current(); |
|
4844 | 646 |
int lastResult = cachedLastKnownBreak; |
647 |
if (lastResult >= start || lastResult <= BreakIterator.DONE) { |
|
648 |
getPrevious(); |
|
649 |
lastResult = handlePrevious(); |
|
650 |
} else { |
|
651 |
//it might be better to check if handlePrevious() give us closer |
|
652 |
//safe value but handlePrevious() is slow too |
|
653 |
//So, this has to be done carefully |
|
654 |
text.setIndex(lastResult); |
|
655 |
} |
|
2 | 656 |
int result = lastResult; |
657 |
||
658 |
// iterate forward from the known break position until we pass our |
|
659 |
// starting point. The last break position before the starting |
|
660 |
// point is our return value |
|
661 |
while (result != BreakIterator.DONE && result < start) { |
|
662 |
lastResult = result; |
|
663 |
result = handleNext(); |
|
664 |
} |
|
665 |
||
666 |
// set the current iteration position to be the last break position |
|
667 |
// before where we started, and then return that value |
|
668 |
text.setIndex(lastResult); |
|
4844 | 669 |
cachedLastKnownBreak = lastResult; |
2 | 670 |
return lastResult; |
671 |
} |
|
672 |
||
673 |
/** |
|
674 |
* Returns previous character |
|
675 |
*/ |
|
676 |
private int getPrevious() { |
|
677 |
char c2 = text.previous(); |
|
678 |
if (Character.isLowSurrogate(c2) && |
|
679 |
text.getIndex() > text.getBeginIndex()) { |
|
680 |
char c1 = text.previous(); |
|
681 |
if (Character.isHighSurrogate(c1)) { |
|
682 |
return Character.toCodePoint(c1, c2); |
|
683 |
} else { |
|
684 |
text.next(); |
|
685 |
} |
|
686 |
} |
|
687 |
return (int)c2; |
|
688 |
} |
|
689 |
||
690 |
/** |
|
691 |
* Returns current character |
|
692 |
*/ |
|
693 |
int getCurrent() { |
|
694 |
char c1 = text.current(); |
|
695 |
if (Character.isHighSurrogate(c1) && |
|
696 |
text.getIndex() < text.getEndIndex()) { |
|
697 |
char c2 = text.next(); |
|
698 |
text.previous(); |
|
699 |
if (Character.isLowSurrogate(c2)) { |
|
700 |
return Character.toCodePoint(c1, c2); |
|
701 |
} |
|
702 |
} |
|
703 |
return (int)c1; |
|
704 |
} |
|
705 |
||
706 |
/** |
|
707 |
* Returns the count of next character. |
|
708 |
*/ |
|
709 |
private int getCurrentCodePointCount() { |
|
710 |
char c1 = text.current(); |
|
711 |
if (Character.isHighSurrogate(c1) && |
|
712 |
text.getIndex() < text.getEndIndex()) { |
|
713 |
char c2 = text.next(); |
|
714 |
text.previous(); |
|
715 |
if (Character.isLowSurrogate(c2)) { |
|
716 |
return 2; |
|
717 |
} |
|
718 |
} |
|
719 |
return 1; |
|
720 |
} |
|
721 |
||
722 |
/** |
|
723 |
* Returns next character |
|
724 |
*/ |
|
725 |
int getNext() { |
|
726 |
int index = text.getIndex(); |
|
727 |
int endIndex = text.getEndIndex(); |
|
728 |
if (index == endIndex || |
|
13583 | 729 |
(index += getCurrentCodePointCount()) >= endIndex) { |
2 | 730 |
return CharacterIterator.DONE; |
731 |
} |
|
732 |
text.setIndex(index); |
|
733 |
return getCurrent(); |
|
734 |
} |
|
735 |
||
736 |
/** |
|
737 |
* Returns the position of next character. |
|
738 |
*/ |
|
739 |
private int getNextIndex() { |
|
740 |
int index = text.getIndex() + getCurrentCodePointCount(); |
|
741 |
int endIndex = text.getEndIndex(); |
|
742 |
if (index > endIndex) { |
|
743 |
return endIndex; |
|
744 |
} else { |
|
745 |
return index; |
|
746 |
} |
|
747 |
} |
|
748 |
||
749 |
/** |
|
750 |
* Throw IllegalArgumentException unless begin <= offset < end. |
|
751 |
*/ |
|
752 |
protected static final void checkOffset(int offset, CharacterIterator text) { |
|
753 |
if (offset < text.getBeginIndex() || offset > text.getEndIndex()) { |
|
754 |
throw new IllegalArgumentException("offset out of bounds"); |
|
755 |
} |
|
756 |
} |
|
757 |
||
758 |
/** |
|
759 |
* Sets the iterator to refer to the first boundary position following |
|
760 |
* the specified position. |
|
761 |
* @offset The position from which to begin searching for a break position. |
|
762 |
* @return The position of the first break after the current position. |
|
763 |
*/ |
|
13583 | 764 |
@Override |
2 | 765 |
public int following(int offset) { |
766 |
||
767 |
CharacterIterator text = getText(); |
|
768 |
checkOffset(offset, text); |
|
769 |
||
770 |
// Set our internal iteration position (temporarily) |
|
771 |
// to the position passed in. If this is the _beginning_ position, |
|
772 |
// then we can just use next() to get our return value |
|
773 |
text.setIndex(offset); |
|
774 |
if (offset == text.getBeginIndex()) { |
|
4844 | 775 |
cachedLastKnownBreak = handleNext(); |
776 |
return cachedLastKnownBreak; |
|
2 | 777 |
} |
778 |
||
779 |
// otherwise, we have to sync up first. Use handlePrevious() to back |
|
780 |
// us up to a known break position before the specified position (if |
|
781 |
// we can determine that the specified position is a break position, |
|
782 |
// we don't back up at all). This may or may not be the last break |
|
783 |
// position at or before our starting position. Advance forward |
|
784 |
// from here until we've passed the starting position. The position |
|
785 |
// we stop on will be the first break position after the specified one. |
|
4844 | 786 |
int result = cachedLastKnownBreak; |
787 |
if (result >= offset || result <= BreakIterator.DONE) { |
|
788 |
result = handlePrevious(); |
|
789 |
} else { |
|
790 |
//it might be better to check if handlePrevious() give us closer |
|
791 |
//safe value but handlePrevious() is slow too |
|
792 |
//So, this has to be done carefully |
|
793 |
text.setIndex(result); |
|
794 |
} |
|
2 | 795 |
while (result != BreakIterator.DONE && result <= offset) { |
796 |
result = handleNext(); |
|
797 |
} |
|
4844 | 798 |
cachedLastKnownBreak = result; |
2 | 799 |
return result; |
800 |
} |
|
801 |
||
802 |
/** |
|
803 |
* Sets the iterator to refer to the last boundary position before the |
|
804 |
* specified position. |
|
805 |
* @offset The position to begin searching for a break from. |
|
806 |
* @return The position of the last boundary before the starting position. |
|
807 |
*/ |
|
13583 | 808 |
@Override |
2 | 809 |
public int preceding(int offset) { |
810 |
// if we start by updating the current iteration position to the |
|
811 |
// position specified by the caller, we can just use previous() |
|
812 |
// to carry out this operation |
|
813 |
CharacterIterator text = getText(); |
|
814 |
checkOffset(offset, text); |
|
815 |
text.setIndex(offset); |
|
816 |
return previous(); |
|
817 |
} |
|
818 |
||
819 |
/** |
|
21278 | 820 |
* Returns true if the specified position is a boundary position. As a side |
2 | 821 |
* effect, leaves the iterator pointing to the first boundary position at |
822 |
* or after "offset". |
|
823 |
* @param offset the offset to check. |
|
824 |
* @return True if "offset" is a boundary position. |
|
825 |
*/ |
|
13583 | 826 |
@Override |
2 | 827 |
public boolean isBoundary(int offset) { |
828 |
CharacterIterator text = getText(); |
|
829 |
checkOffset(offset, text); |
|
830 |
if (offset == text.getBeginIndex()) { |
|
831 |
return true; |
|
832 |
} |
|
833 |
||
834 |
// to check whether this is a boundary, we can use following() on the |
|
835 |
// position before the specified one and return true if the position we |
|
836 |
// get back is the one the user specified |
|
837 |
else { |
|
838 |
return following(offset - 1) == offset; |
|
839 |
} |
|
840 |
} |
|
841 |
||
842 |
/** |
|
843 |
* Returns the current iteration position. |
|
844 |
* @return The current iteration position. |
|
845 |
*/ |
|
13583 | 846 |
@Override |
2 | 847 |
public int current() { |
848 |
return getText().getIndex(); |
|
849 |
} |
|
850 |
||
851 |
/** |
|
852 |
* Return a CharacterIterator over the text being analyzed. This version |
|
853 |
* of this method returns the actual CharacterIterator we're using internally. |
|
854 |
* Changing the state of this iterator can have undefined consequences. If |
|
855 |
* you need to change it, clone it first. |
|
856 |
* @return An iterator over the text being analyzed. |
|
857 |
*/ |
|
13583 | 858 |
@Override |
2 | 859 |
public CharacterIterator getText() { |
860 |
// The iterator is initialized pointing to no text at all, so if this |
|
861 |
// function is called while we're in that state, we have to fudge an |
|
862 |
// iterator to return. |
|
863 |
if (text == null) { |
|
864 |
text = new StringCharacterIterator(""); |
|
865 |
} |
|
866 |
return text; |
|
867 |
} |
|
868 |
||
869 |
/** |
|
870 |
* Set the iterator to analyze a new piece of text. This function resets |
|
871 |
* the current iteration position to the beginning of the text. |
|
872 |
* @param newText An iterator over the text to analyze. |
|
873 |
*/ |
|
13583 | 874 |
@Override |
2 | 875 |
public void setText(CharacterIterator newText) { |
876 |
// Test iterator to see if we need to wrap it in a SafeCharIterator. |
|
877 |
// The correct behavior for CharacterIterators is to allow the |
|
878 |
// position to be set to the endpoint of the iterator. Many |
|
879 |
// CharacterIterators do not uphold this, so this is a workaround |
|
880 |
// to permit them to use this class. |
|
881 |
int end = newText.getEndIndex(); |
|
882 |
boolean goodIterator; |
|
883 |
try { |
|
884 |
newText.setIndex(end); // some buggy iterators throw an exception here |
|
885 |
goodIterator = newText.getIndex() == end; |
|
886 |
} |
|
887 |
catch(IllegalArgumentException e) { |
|
888 |
goodIterator = false; |
|
889 |
} |
|
890 |
||
891 |
if (goodIterator) { |
|
892 |
text = newText; |
|
893 |
} |
|
894 |
else { |
|
895 |
text = new SafeCharIterator(newText); |
|
896 |
} |
|
897 |
text.first(); |
|
4844 | 898 |
|
899 |
cachedLastKnownBreak = BreakIterator.DONE; |
|
2 | 900 |
} |
901 |
||
902 |
||
903 |
//======================================================================= |
|
904 |
// implementation |
|
905 |
//======================================================================= |
|
906 |
||
907 |
/** |
|
908 |
* This method is the actual implementation of the next() method. All iteration |
|
909 |
* vectors through here. This method initializes the state machine to state 1 |
|
910 |
* and advances through the text character by character until we reach the end |
|
911 |
* of the text or the state machine transitions to state 0. We update our return |
|
912 |
* value every time the state machine passes through a possible end state. |
|
913 |
*/ |
|
914 |
protected int handleNext() { |
|
915 |
// if we're already at the end of the text, return DONE. |
|
916 |
CharacterIterator text = getText(); |
|
917 |
if (text.getIndex() == text.getEndIndex()) { |
|
918 |
return BreakIterator.DONE; |
|
919 |
} |
|
920 |
||
921 |
// no matter what, we always advance at least one character forward |
|
922 |
int result = getNextIndex(); |
|
923 |
int lookaheadResult = 0; |
|
924 |
||
925 |
// begin in state 1 |
|
926 |
int state = START_STATE; |
|
927 |
int category; |
|
928 |
int c = getCurrent(); |
|
929 |
||
930 |
// loop until we reach the end of the text or transition to state 0 |
|
931 |
while (c != CharacterIterator.DONE && state != STOP_STATE) { |
|
932 |
||
933 |
// look up the current character's character category (which tells us |
|
934 |
// which column in the state table to look at) |
|
935 |
category = lookupCategory(c); |
|
936 |
||
937 |
// if the character isn't an ignore character, look up a state |
|
938 |
// transition in the state table |
|
939 |
if (category != IGNORE) { |
|
940 |
state = lookupState(state, category); |
|
941 |
} |
|
942 |
||
943 |
// if the state we've just transitioned to is a lookahead state, |
|
944 |
// (but not also an end state), save its position. If it's |
|
945 |
// both a lookahead state and an end state, update the break position |
|
946 |
// to the last saved lookup-state position |
|
947 |
if (lookaheadStates[state]) { |
|
948 |
if (endStates[state]) { |
|
949 |
result = lookaheadResult; |
|
950 |
} |
|
951 |
else { |
|
952 |
lookaheadResult = getNextIndex(); |
|
953 |
} |
|
954 |
} |
|
955 |
||
956 |
// otherwise, if the state we've just transitioned to is an accepting |
|
957 |
// state, update the break position to be the current iteration position |
|
958 |
else { |
|
959 |
if (endStates[state]) { |
|
960 |
result = getNextIndex(); |
|
961 |
} |
|
962 |
} |
|
963 |
||
964 |
c = getNext(); |
|
965 |
} |
|
966 |
||
967 |
// if we've run off the end of the text, and the very last character took us into |
|
968 |
// a lookahead state, advance the break position to the lookahead position |
|
969 |
// (the theory here is that if there are no characters at all after the lookahead |
|
970 |
// position, that always matches the lookahead criteria) |
|
971 |
if (c == CharacterIterator.DONE && lookaheadResult == text.getEndIndex()) { |
|
972 |
result = lookaheadResult; |
|
973 |
} |
|
974 |
||
975 |
text.setIndex(result); |
|
976 |
return result; |
|
977 |
} |
|
978 |
||
979 |
/** |
|
980 |
* This method backs the iterator back up to a "safe position" in the text. |
|
981 |
* This is a position that we know, without any context, must be a break position. |
|
982 |
* The various calling methods then iterate forward from this safe position to |
|
983 |
* the appropriate position to return. (For more information, see the description |
|
984 |
* of buildBackwardsStateTable() in RuleBasedBreakIterator.Builder.) |
|
985 |
*/ |
|
986 |
protected int handlePrevious() { |
|
987 |
CharacterIterator text = getText(); |
|
988 |
int state = START_STATE; |
|
989 |
int category = 0; |
|
990 |
int lastCategory = 0; |
|
991 |
int c = getCurrent(); |
|
992 |
||
993 |
// loop until we reach the beginning of the text or transition to state 0 |
|
994 |
while (c != CharacterIterator.DONE && state != STOP_STATE) { |
|
995 |
||
996 |
// save the last character's category and look up the current |
|
997 |
// character's category |
|
998 |
lastCategory = category; |
|
999 |
category = lookupCategory(c); |
|
1000 |
||
1001 |
// if the current character isn't an ignore character, look up a |
|
1002 |
// state transition in the backwards state table |
|
1003 |
if (category != IGNORE) { |
|
1004 |
state = lookupBackwardState(state, category); |
|
1005 |
} |
|
1006 |
||
1007 |
// then advance one character backwards |
|
1008 |
c = getPrevious(); |
|
1009 |
} |
|
1010 |
||
1011 |
// if we didn't march off the beginning of the text, we're either one or two |
|
1012 |
// positions away from the real break position. (One because of the call to |
|
1013 |
// previous() at the end of the loop above, and another because the character |
|
1014 |
// that takes us into the stop state will always be the character BEFORE |
|
1015 |
// the break position.) |
|
1016 |
if (c != CharacterIterator.DONE) { |
|
1017 |
if (lastCategory != IGNORE) { |
|
1018 |
getNext(); |
|
1019 |
getNext(); |
|
1020 |
} |
|
1021 |
else { |
|
1022 |
getNext(); |
|
1023 |
} |
|
1024 |
} |
|
1025 |
return text.getIndex(); |
|
1026 |
} |
|
1027 |
||
1028 |
/** |
|
1029 |
* Looks up a character's category (i.e., its category for breaking purposes, |
|
1030 |
* not its Unicode category) |
|
1031 |
*/ |
|
1032 |
protected int lookupCategory(int c) { |
|
1033 |
if (c < Character.MIN_SUPPLEMENTARY_CODE_POINT) { |
|
1034 |
return charCategoryTable.elementAt((char)c); |
|
1035 |
} else { |
|
1036 |
return supplementaryCharCategoryTable.getValue(c); |
|
1037 |
} |
|
1038 |
} |
|
1039 |
||
1040 |
/** |
|
1041 |
* Given a current state and a character category, looks up the |
|
1042 |
* next state to transition to in the state table. |
|
1043 |
*/ |
|
1044 |
protected int lookupState(int state, int category) { |
|
1045 |
return stateTable[state * numCategories + category]; |
|
1046 |
} |
|
1047 |
||
1048 |
/** |
|
1049 |
* Given a current state and a character category, looks up the |
|
1050 |
* next state to transition to in the backwards state table. |
|
1051 |
*/ |
|
1052 |
protected int lookupBackwardState(int state, int category) { |
|
1053 |
return backwardsStateTable[state * numCategories + category]; |
|
1054 |
} |
|
1055 |
||
13583 | 1056 |
static long getLong(byte[] buf, int offset) { |
1057 |
long num = buf[offset]&0xFF; |
|
1058 |
for (int i = 1; i < 8; i++) { |
|
1059 |
num = num<<8 | (buf[offset+i]&0xFF); |
|
1060 |
} |
|
1061 |
return num; |
|
1062 |
} |
|
1063 |
||
1064 |
static int getInt(byte[] buf, int offset) { |
|
1065 |
int num = buf[offset]&0xFF; |
|
1066 |
for (int i = 1; i < 4; i++) { |
|
1067 |
num = num<<8 | (buf[offset+i]&0xFF); |
|
1068 |
} |
|
1069 |
return num; |
|
1070 |
} |
|
1071 |
||
1072 |
static short getShort(byte[] buf, int offset) { |
|
1073 |
short num = (short)(buf[offset]&0xFF); |
|
1074 |
num = (short)(num<<8 | (buf[offset+1]&0xFF)); |
|
1075 |
return num; |
|
1076 |
} |
|
1077 |
||
2 | 1078 |
/* |
1079 |
* This class exists to work around a bug in incorrect implementations |
|
1080 |
* of CharacterIterator, which incorrectly handle setIndex(endIndex). |
|
1081 |
* This iterator relies only on base.setIndex(n) where n is less than |
|
1082 |
* endIndex. |
|
1083 |
* |
|
1084 |
* One caveat: if the base iterator's begin and end indices change |
|
1085 |
* the change will not be reflected by this wrapper. Does that matter? |
|
1086 |
*/ |
|
13583 | 1087 |
// TODO: Review this class to see if it's still required. |
2 | 1088 |
private static final class SafeCharIterator implements CharacterIterator, |
1089 |
Cloneable { |
|
1090 |
||
1091 |
private CharacterIterator base; |
|
1092 |
private int rangeStart; |
|
1093 |
private int rangeLimit; |
|
1094 |
private int currentIndex; |
|
1095 |
||
1096 |
SafeCharIterator(CharacterIterator base) { |
|
1097 |
this.base = base; |
|
1098 |
this.rangeStart = base.getBeginIndex(); |
|
1099 |
this.rangeLimit = base.getEndIndex(); |
|
1100 |
this.currentIndex = base.getIndex(); |
|
1101 |
} |
|
1102 |
||
13583 | 1103 |
@Override |
2 | 1104 |
public char first() { |
1105 |
return setIndex(rangeStart); |
|
1106 |
} |
|
1107 |
||
13583 | 1108 |
@Override |
2 | 1109 |
public char last() { |
1110 |
return setIndex(rangeLimit - 1); |
|
1111 |
} |
|
1112 |
||
13583 | 1113 |
@Override |
2 | 1114 |
public char current() { |
1115 |
if (currentIndex < rangeStart || currentIndex >= rangeLimit) { |
|
1116 |
return DONE; |
|
1117 |
} |
|
1118 |
else { |
|
1119 |
return base.setIndex(currentIndex); |
|
1120 |
} |
|
1121 |
} |
|
1122 |
||
13583 | 1123 |
@Override |
2 | 1124 |
public char next() { |
1125 |
||
1126 |
currentIndex++; |
|
1127 |
if (currentIndex >= rangeLimit) { |
|
1128 |
currentIndex = rangeLimit; |
|
1129 |
return DONE; |
|
1130 |
} |
|
1131 |
else { |
|
1132 |
return base.setIndex(currentIndex); |
|
1133 |
} |
|
1134 |
} |
|
1135 |
||
13583 | 1136 |
@Override |
2 | 1137 |
public char previous() { |
1138 |
||
1139 |
currentIndex--; |
|
1140 |
if (currentIndex < rangeStart) { |
|
1141 |
currentIndex = rangeStart; |
|
1142 |
return DONE; |
|
1143 |
} |
|
1144 |
else { |
|
1145 |
return base.setIndex(currentIndex); |
|
1146 |
} |
|
1147 |
} |
|
1148 |
||
13583 | 1149 |
@Override |
2 | 1150 |
public char setIndex(int i) { |
1151 |
||
1152 |
if (i < rangeStart || i > rangeLimit) { |
|
1153 |
throw new IllegalArgumentException("Invalid position"); |
|
1154 |
} |
|
1155 |
currentIndex = i; |
|
1156 |
return current(); |
|
1157 |
} |
|
1158 |
||
13583 | 1159 |
@Override |
2 | 1160 |
public int getBeginIndex() { |
1161 |
return rangeStart; |
|
1162 |
} |
|
1163 |
||
13583 | 1164 |
@Override |
2 | 1165 |
public int getEndIndex() { |
1166 |
return rangeLimit; |
|
1167 |
} |
|
1168 |
||
13583 | 1169 |
@Override |
2 | 1170 |
public int getIndex() { |
1171 |
return currentIndex; |
|
1172 |
} |
|
1173 |
||
13583 | 1174 |
@Override |
2 | 1175 |
public Object clone() { |
1176 |
||
1177 |
SafeCharIterator copy = null; |
|
1178 |
try { |
|
1179 |
copy = (SafeCharIterator) super.clone(); |
|
1180 |
} |
|
1181 |
catch(CloneNotSupportedException e) { |
|
1182 |
throw new Error("Clone not supported: " + e); |
|
1183 |
} |
|
1184 |
||
1185 |
CharacterIterator copyOfBase = (CharacterIterator) base.clone(); |
|
1186 |
copy.base = copyOfBase; |
|
1187 |
return copy; |
|
1188 |
} |
|
1189 |
} |
|
1190 |
} |