src/share/classes/java/util/Currency.java

Print this page
rev 10180 : 8039317: Read currency.data as a resource
Reviewed-by:
   1 /*
   2  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  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  *
  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


 195     // mask for special case country entries
 196     private static final int SPECIAL_CASE_COUNTRY_MASK = 0x0080;
 197     // mask for special case country index
 198     private static final int SPECIAL_CASE_COUNTRY_INDEX_MASK = 0x001F;
 199     // delta from entry index component in main table to index into special case tables
 200     private static final int SPECIAL_CASE_COUNTRY_INDEX_DELTA = 1;
 201     // mask for distinguishing simple and special case countries
 202     private static final int COUNTRY_TYPE_MASK = SIMPLE_CASE_COUNTRY_MASK | SPECIAL_CASE_COUNTRY_MASK;
 203     // mask for the numeric code of the currency
 204     private static final int NUMERIC_CODE_MASK = 0x0003FF00;
 205     // shift count for the numeric code of the currency
 206     private static final int NUMERIC_CODE_SHIFT = 8;
 207 
 208     // Currency data format version
 209     private static final int VALID_FORMAT_VERSION = 1;
 210 
 211     static {
 212         AccessController.doPrivileged(new PrivilegedAction<Void>() {
 213             @Override
 214             public Void run() {
 215                 String homeDir = System.getProperty("java.home");
 216                 try {
 217                     String dataFile = homeDir + File.separator +
 218                             "lib" + File.separator + "currency.data";
 219                     try (DataInputStream dis = new DataInputStream(
 220                              new BufferedInputStream(
 221                              new FileInputStream(dataFile)))) {
 222                         if (dis.readInt() != MAGIC_NUMBER) {
 223                             throw new InternalError("Currency data is possibly corrupted");
 224                         }
 225                         formatVersion = dis.readInt();
 226                         if (formatVersion != VALID_FORMAT_VERSION) {
 227                             throw new InternalError("Currency data format is incorrect");
 228                         }
 229                         dataVersion = dis.readInt();
 230                         mainTable = readIntArray(dis, A_TO_Z * A_TO_Z);
 231                         int scCount = dis.readInt();
 232                         scCutOverTimes = readLongArray(dis, scCount);
 233                         scOldCurrencies = readStringArray(dis, scCount);
 234                         scNewCurrencies = readStringArray(dis, scCount);
 235                         scOldCurrenciesDFD = readIntArray(dis, scCount);
 236                         scNewCurrenciesDFD = readIntArray(dis, scCount);
 237                         scOldCurrenciesNumericCode = readIntArray(dis, scCount);
 238                         scNewCurrenciesNumericCode = readIntArray(dis, scCount);
 239                         int ocCount = dis.readInt();
 240                         otherCurrencies = dis.readUTF();
 241                         otherCurrenciesDFD = readIntArray(dis, ocCount);
 242                         otherCurrenciesNumericCode = readIntArray(dis, ocCount);
 243                     }
 244                 } catch (IOException e) {
 245                     throw new InternalError(e);
 246                 }
 247 
 248                 // look for the properties file for overrides
 249                 String propsFile = System.getProperty("java.util.currency.data");
 250                 if (propsFile == null) {
 251                     propsFile = homeDir + File.separator + "lib" +
 252                         File.separator + "currency.properties";
 253                 }
 254                 try {
 255                     File propFile = new File(propsFile);
 256                     if (propFile.exists()) {
 257                         Properties props = new Properties();
 258                         try (FileReader fr = new FileReader(propFile)) {
 259                             props.load(fr);
 260                         }
 261                         Set<String> keys = props.stringPropertyNames();
 262                         Pattern propertiesPattern =
 263                             Pattern.compile("([A-Z]{3})\\s*,\\s*(\\d{3})\\s*,\\s*" +
 264                                 "([0-3])\\s*,?\\s*(\\d{4}-\\d{2}-\\d{2}T\\d{2}:" +
 265                                 "\\d{2}:\\d{2})?");
 266                         for (String key : keys) {
 267                            replaceCurrencyData(propertiesPattern,
 268                                key.toUpperCase(Locale.ROOT),
 269                                props.getProperty(key).toUpperCase(Locale.ROOT));
 270                         }
 271                     }


   1 /*
   2  * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  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  *
  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


 195     // mask for special case country entries
 196     private static final int SPECIAL_CASE_COUNTRY_MASK = 0x0080;
 197     // mask for special case country index
 198     private static final int SPECIAL_CASE_COUNTRY_INDEX_MASK = 0x001F;
 199     // delta from entry index component in main table to index into special case tables
 200     private static final int SPECIAL_CASE_COUNTRY_INDEX_DELTA = 1;
 201     // mask for distinguishing simple and special case countries
 202     private static final int COUNTRY_TYPE_MASK = SIMPLE_CASE_COUNTRY_MASK | SPECIAL_CASE_COUNTRY_MASK;
 203     // mask for the numeric code of the currency
 204     private static final int NUMERIC_CODE_MASK = 0x0003FF00;
 205     // shift count for the numeric code of the currency
 206     private static final int NUMERIC_CODE_SHIFT = 8;
 207 
 208     // Currency data format version
 209     private static final int VALID_FORMAT_VERSION = 1;
 210 
 211     static {
 212         AccessController.doPrivileged(new PrivilegedAction<Void>() {
 213             @Override
 214             public Void run() {

 215                 try {


 216                     try (DataInputStream dis = new DataInputStream(
 217                              new BufferedInputStream(getClass().getResourceAsStream("/java/util/currency.data")))) {

 218                         if (dis.readInt() != MAGIC_NUMBER) {
 219                             throw new InternalError("Currency data is possibly corrupted");
 220                         }
 221                         formatVersion = dis.readInt();
 222                         if (formatVersion != VALID_FORMAT_VERSION) {
 223                             throw new InternalError("Currency data format is incorrect");
 224                         }
 225                         dataVersion = dis.readInt();
 226                         mainTable = readIntArray(dis, A_TO_Z * A_TO_Z);
 227                         int scCount = dis.readInt();
 228                         scCutOverTimes = readLongArray(dis, scCount);
 229                         scOldCurrencies = readStringArray(dis, scCount);
 230                         scNewCurrencies = readStringArray(dis, scCount);
 231                         scOldCurrenciesDFD = readIntArray(dis, scCount);
 232                         scNewCurrenciesDFD = readIntArray(dis, scCount);
 233                         scOldCurrenciesNumericCode = readIntArray(dis, scCount);
 234                         scNewCurrenciesNumericCode = readIntArray(dis, scCount);
 235                         int ocCount = dis.readInt();
 236                         otherCurrencies = dis.readUTF();
 237                         otherCurrenciesDFD = readIntArray(dis, ocCount);
 238                         otherCurrenciesNumericCode = readIntArray(dis, ocCount);
 239                     }
 240                 } catch (IOException e) {
 241                     throw new InternalError(e);
 242                 }
 243 
 244                 // look for the properties file for overrides
 245                 String propsFile = System.getProperty("java.util.currency.data");
 246                 if (propsFile == null) {
 247                     propsFile = System.getProperty("java.home") + File.separator + "lib" +
 248                         File.separator + "currency.properties";
 249                 }
 250                 try {
 251                     File propFile = new File(propsFile);
 252                     if (propFile.exists()) {
 253                         Properties props = new Properties();
 254                         try (FileReader fr = new FileReader(propFile)) {
 255                             props.load(fr);
 256                         }
 257                         Set<String> keys = props.stringPropertyNames();
 258                         Pattern propertiesPattern =
 259                             Pattern.compile("([A-Z]{3})\\s*,\\s*(\\d{3})\\s*,\\s*" +
 260                                 "([0-3])\\s*,?\\s*(\\d{4}-\\d{2}-\\d{2}T\\d{2}:" +
 261                                 "\\d{2}:\\d{2})?");
 262                         for (String key : keys) {
 263                            replaceCurrencyData(propertiesPattern,
 264                                key.toUpperCase(Locale.ROOT),
 265                                props.getProperty(key).toUpperCase(Locale.ROOT));
 266                         }
 267                     }