< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2000, 2016, 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
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.io.BufferedInputStream;
  29 import java.io.DataInputStream;
  30 import java.io.File;
  31 import java.io.FileInputStream;
  32 import java.io.FileReader;
  33 import java.io.InputStream;
  34 import java.io.IOException;
  35 import java.io.Serializable;


  36 import java.security.AccessController;
  37 import java.security.PrivilegedAction;
  38 import java.text.ParseException;
  39 import java.text.SimpleDateFormat;
  40 import java.util.concurrent.ConcurrentHashMap;
  41 import java.util.concurrent.ConcurrentMap;
  42 import java.util.regex.Pattern;
  43 import java.util.regex.Matcher;
  44 import java.util.spi.CurrencyNameProvider;
  45 import sun.util.locale.provider.LocaleServiceProviderPool;
  46 import sun.util.logging.PlatformLogger;
  47 
  48 
  49 /**
  50  * Represents a currency. Currencies are identified by their ISO 4217 currency
  51  * codes. Visit the <a href="http://www.iso.org/iso/home/standards/currency_codes.htm">
  52  * ISO web site</a> for more information.
  53  * <p>
  54  * The class is designed so that there's never more than one
  55  * <code>Currency</code> instance for any given currency. Therefore, there's


 184     private static final int SPECIAL_CASE_COUNTRY_MASK = 0x00000200;
 185     // mask for special case country index
 186     private static final int SPECIAL_CASE_COUNTRY_INDEX_MASK = 0x0000001F;
 187     // delta from entry index component in main table to index into special case tables
 188     private static final int SPECIAL_CASE_COUNTRY_INDEX_DELTA = 1;
 189     // mask for distinguishing simple and special case countries
 190     private static final int COUNTRY_TYPE_MASK = SIMPLE_CASE_COUNTRY_MASK | SPECIAL_CASE_COUNTRY_MASK;
 191     // mask for the numeric code of the currency
 192     private static final int NUMERIC_CODE_MASK = 0x000FFC00;
 193     // shift count for the numeric code of the currency
 194     private static final int NUMERIC_CODE_SHIFT = 10;
 195 
 196     // Currency data format version
 197     private static final int VALID_FORMAT_VERSION = 3;
 198 
 199     static {
 200         AccessController.doPrivileged(new PrivilegedAction<>() {
 201             @Override
 202             public Void run() {
 203                 try {
 204                     try (InputStream in = getClass().getResourceAsStream("/java/util/currency.data")) {
 205                         if (in == null) {
 206                             throw new InternalError("Currency data not found");
 207                         }
 208                         DataInputStream dis = new DataInputStream(new BufferedInputStream(in));
 209                         if (dis.readInt() != MAGIC_NUMBER) {
 210                             throw new InternalError("Currency data is possibly corrupted");
 211                         }
 212                         formatVersion = dis.readInt();
 213                         if (formatVersion != VALID_FORMAT_VERSION) {
 214                             throw new InternalError("Currency data format is incorrect");
 215                         }
 216                         dataVersion = dis.readInt();
 217                         mainTable = readIntArray(dis, A_TO_Z * A_TO_Z);
 218                         int scCount = dis.readInt();
 219                         specialCasesList = readSpecialCases(dis, scCount);
 220                         int ocCount = dis.readInt();
 221                         otherCurrenciesList = readOtherCurrencies(dis, ocCount);
 222                     }
 223                 } catch (IOException e) {
 224                     throw new InternalError(e);
 225                 }
 226 
 227                 // look for the properties file for overrides
 228                 String propsFile = System.getProperty("java.util.currency.data");


   1 /*
   2  * Copyright (c) 2000, 2017, 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
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.io.BufferedInputStream;
  29 import java.io.DataInputStream;
  30 import java.io.File;
  31 import java.io.FileInputStream;
  32 import java.io.FileReader;
  33 import java.io.InputStream;
  34 import java.io.IOException;
  35 import java.io.Serializable;
  36 import java.nio.file.Files;
  37 import java.nio.file.Paths;
  38 import java.security.AccessController;
  39 import java.security.PrivilegedAction;
  40 import java.text.ParseException;
  41 import java.text.SimpleDateFormat;
  42 import java.util.concurrent.ConcurrentHashMap;
  43 import java.util.concurrent.ConcurrentMap;
  44 import java.util.regex.Pattern;
  45 import java.util.regex.Matcher;
  46 import java.util.spi.CurrencyNameProvider;
  47 import sun.util.locale.provider.LocaleServiceProviderPool;
  48 import sun.util.logging.PlatformLogger;
  49 
  50 
  51 /**
  52  * Represents a currency. Currencies are identified by their ISO 4217 currency
  53  * codes. Visit the <a href="http://www.iso.org/iso/home/standards/currency_codes.htm">
  54  * ISO web site</a> for more information.
  55  * <p>
  56  * The class is designed so that there's never more than one
  57  * <code>Currency</code> instance for any given currency. Therefore, there's


 186     private static final int SPECIAL_CASE_COUNTRY_MASK = 0x00000200;
 187     // mask for special case country index
 188     private static final int SPECIAL_CASE_COUNTRY_INDEX_MASK = 0x0000001F;
 189     // delta from entry index component in main table to index into special case tables
 190     private static final int SPECIAL_CASE_COUNTRY_INDEX_DELTA = 1;
 191     // mask for distinguishing simple and special case countries
 192     private static final int COUNTRY_TYPE_MASK = SIMPLE_CASE_COUNTRY_MASK | SPECIAL_CASE_COUNTRY_MASK;
 193     // mask for the numeric code of the currency
 194     private static final int NUMERIC_CODE_MASK = 0x000FFC00;
 195     // shift count for the numeric code of the currency
 196     private static final int NUMERIC_CODE_SHIFT = 10;
 197 
 198     // Currency data format version
 199     private static final int VALID_FORMAT_VERSION = 3;
 200 
 201     static {
 202         AccessController.doPrivileged(new PrivilegedAction<>() {
 203             @Override
 204             public Void run() {
 205                 try {
 206                     try (DataInputStream dis = new DataInputStream(
 207                             new BufferedInputStream(Files.newInputStream(Paths
 208                                     .get(System.getProperty("java.home"),
 209                                             "lib", "currency.data"))))) {

 210                         if (dis.readInt() != MAGIC_NUMBER) {
 211                             throw new InternalError("Currency data is possibly corrupted");
 212                         }
 213                         formatVersion = dis.readInt();
 214                         if (formatVersion != VALID_FORMAT_VERSION) {
 215                             throw new InternalError("Currency data format is incorrect");
 216                         }
 217                         dataVersion = dis.readInt();
 218                         mainTable = readIntArray(dis, A_TO_Z * A_TO_Z);
 219                         int scCount = dis.readInt();
 220                         specialCasesList = readSpecialCases(dis, scCount);
 221                         int ocCount = dis.readInt();
 222                         otherCurrenciesList = readOtherCurrencies(dis, ocCount);
 223                     }
 224                 } catch (IOException e) {
 225                     throw new InternalError(e);
 226                 }
 227 
 228                 // look for the properties file for overrides
 229                 String propsFile = System.getProperty("java.util.currency.data");


< prev index next >