1 /* |
1 /* |
2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
39 */ |
39 */ |
40 |
40 |
41 package sun.util.locale.provider; |
41 package sun.util.locale.provider; |
42 |
42 |
43 import java.io.BufferedInputStream; |
43 import java.io.BufferedInputStream; |
|
44 import java.io.InputStream; |
44 import java.io.IOException; |
45 import java.io.IOException; |
|
46 import java.lang.reflect.Module; |
45 import java.security.AccessController; |
47 import java.security.AccessController; |
46 import java.security.PrivilegedActionException; |
48 import java.security.PrivilegedActionException; |
47 import java.security.PrivilegedExceptionAction; |
49 import java.security.PrivilegedExceptionAction; |
48 import java.text.BreakIterator; |
50 import java.text.BreakIterator; |
49 import java.text.CharacterIterator; |
51 import java.text.CharacterIterator; |
311 //======================================================================= |
313 //======================================================================= |
312 // constructors |
314 // constructors |
313 //======================================================================= |
315 //======================================================================= |
314 |
316 |
315 /** |
317 /** |
316 * Constructs a RuleBasedBreakIterator according to the datafile |
318 * Constructs a RuleBasedBreakIterator according to the module and the datafile |
317 * provided. |
319 * provided. |
318 */ |
320 */ |
319 RuleBasedBreakIterator(String datafile) |
321 RuleBasedBreakIterator(Module module, String datafile) |
320 throws IOException, MissingResourceException { |
322 throws IOException, MissingResourceException { |
321 readTables(datafile); |
323 readTables(module, datafile); |
322 } |
324 } |
323 |
325 |
324 /** |
326 /** |
325 * Read datafile. The datafile's format is as follows: |
327 * Read datafile. The datafile's format is as follows: |
326 * <pre> |
328 * <pre> |
367 * u4 nonBMPdata[numNonBMPdataLength]; |
369 * u4 nonBMPdata[numNonBMPdataLength]; |
368 * u1 additionalData[additionalDataLength]; |
370 * u1 additionalData[additionalDataLength]; |
369 * } |
371 * } |
370 * </pre> |
372 * </pre> |
371 */ |
373 */ |
372 protected final void readTables(String datafile) |
374 protected final void readTables(Module module, String datafile) |
373 throws IOException, MissingResourceException { |
375 throws IOException, MissingResourceException { |
374 |
376 |
375 byte[] buffer = readFile(datafile); |
377 byte[] buffer = readFile(module, datafile); |
376 |
378 |
377 /* Read header_info. */ |
379 /* Read header_info. */ |
378 int stateTableLength = getInt(buffer, 0); |
380 int stateTableLength = getInt(buffer, 0); |
379 int backwardsStateTableLength = getInt(buffer, 4); |
381 int backwardsStateTableLength = getInt(buffer, 4); |
380 int endStatesLength = getInt(buffer, 8); |
382 int endStatesLength = getInt(buffer, 8); |
434 |
436 |
435 /* Set numCategories */ |
437 /* Set numCategories */ |
436 numCategories = stateTable.length / endStates.length; |
438 numCategories = stateTable.length / endStates.length; |
437 } |
439 } |
438 |
440 |
439 protected byte[] readFile(final String datafile) |
441 protected byte[] readFile(final Module module, final String datafile) |
440 throws IOException, MissingResourceException { |
442 throws IOException, MissingResourceException { |
441 |
443 |
442 BufferedInputStream is; |
444 BufferedInputStream is; |
443 try { |
445 try { |
444 is = AccessController.doPrivileged( |
446 PrivilegedExceptionAction<BufferedInputStream> pa = () -> { |
445 new PrivilegedExceptionAction<BufferedInputStream>() { |
447 InputStream in = module.getResourceAsStream("sun/text/resources/" + datafile); |
446 @Override |
448 if (in == null) { |
447 public BufferedInputStream run() throws Exception { |
449 // Try to load the file with "java.base" module instance. Assumption |
448 return new BufferedInputStream(getClass().getResourceAsStream("/sun/text/resources/" + datafile)); |
450 // here is that the fall back data files to be read should reside in |
449 } |
451 // java.base. |
|
452 in = RuleBasedBreakIterator.class.getModule().getResourceAsStream("sun/text/resources/" + datafile); |
450 } |
453 } |
451 ); |
454 |
452 } |
455 return new BufferedInputStream(in); |
453 catch (PrivilegedActionException e) { |
456 }; |
|
457 is = AccessController.doPrivileged(pa); |
|
458 } catch (PrivilegedActionException e) { |
454 throw new InternalError(e.toString(), e); |
459 throw new InternalError(e.toString(), e); |
455 } |
460 } |
456 |
461 |
457 int offset = 0; |
462 int offset = 0; |
458 |
463 |