src/share/classes/java/util/Formatter.java

Print this page

        

*** 48,57 **** --- 48,59 ---- import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; + import java.nio.charset.Charset; + import java.nio.charset.IllegalCharsetNameException; import sun.misc.FpUtils; import sun.misc.DoubleConsts; import sun.misc.FormattedFloatingDecimal;
*** 1836,1861 **** * @author Iris Clark * @since 1.5 */ public final class Formatter implements Closeable, Flushable { private Appendable a; ! private Locale l; private IOException lastException; ! private char zero = '0'; private static double scaleUp; // 1 (sign) + 19 (max # sig digits) + 1 ('.') + 1 ('e') + 1 (sign) // + 3 (max # exp digits) + 4 (error) = 30 private static final int MAX_FD_CHARS = 30; ! // Initialize internal data. ! private void init(Appendable a, Locale l) { this.a = a; this.l = l; ! setZero(); } /** * Constructs a new formatter. * --- 1838,1892 ---- * @author Iris Clark * @since 1.5 */ public final class Formatter implements Closeable, Flushable { private Appendable a; ! private final Locale l; private IOException lastException; ! private final char zero; private static double scaleUp; // 1 (sign) + 19 (max # sig digits) + 1 ('.') + 1 ('e') + 1 (sign) // + 3 (max # exp digits) + 4 (error) = 30 private static final int MAX_FD_CHARS = 30; ! /** ! * Verifies that the given charset is supported. ! * @throws NullPointerException is csn is null ! * @throws UnsupportedEncodingException if the charset is not supported ! */ ! private static Void verifyCharsetName(String csn) ! throws UnsupportedEncodingException { ! Objects.nonNull(csn, "charsetName"); ! try { ! if (Charset.isSupported(csn)) ! return null; ! } catch (IllegalCharsetNameException unused) { ! /* swallow this exception since UnsupportedEncodingException ! * will be thrown */ ! } ! throw new UnsupportedEncodingException(csn); ! } ! ! private static final Appendable nonNullAppendable(Appendable a) { ! if (a == null) ! return new StringBuilder(); ! ! return a; ! } ! ! // Private constructors ! private Formatter(Void unused, Locale l, Appendable a) { ! this(l, a); ! } ! ! private Formatter(Locale l, Appendable a) { this.a = a; this.l = l; ! this.zero = getZero(l); } /** * Constructs a new formatter. *
*** 1865,1875 **** * #toString toString()}. The locale used is the {@linkplain * Locale#getDefault() default locale} for this instance of the Java * virtual machine. */ public Formatter() { ! init(new StringBuilder(), Locale.getDefault(Locale.Category.FORMAT)); } /** * Constructs a new formatter with the specified destination. * --- 1896,1906 ---- * #toString toString()}. The locale used is the {@linkplain * Locale#getDefault() default locale} for this instance of the Java * virtual machine. */ public Formatter() { ! this(Locale.getDefault(Locale.Category.FORMAT), new StringBuilder()); } /** * Constructs a new formatter with the specified destination. *
*** 1879,1891 **** * @param a * Destination for the formatted output. If {@code a} is * {@code null} then a {@link StringBuilder} will be created. */ public Formatter(Appendable a) { ! if (a == null) ! a = new StringBuilder(); ! init(a, Locale.getDefault(Locale.Category.FORMAT)); } /** * Constructs a new formatter with the specified locale. * --- 1910,1920 ---- * @param a * Destination for the formatted output. If {@code a} is * {@code null} then a {@link StringBuilder} will be created. */ public Formatter(Appendable a) { ! this(Locale.getDefault(Locale.Category.FORMAT), nonNullAppendable(a)); } /** * Constructs a new formatter with the specified locale. *
*** 1898,1908 **** * The {@linkplain java.util.Locale locale} to apply during * formatting. If {@code l} is {@code null} then no localization * is applied. */ public Formatter(Locale l) { ! init(new StringBuilder(), l); } /** * Constructs a new formatter with the specified destination and locale. * --- 1927,1937 ---- * The {@linkplain java.util.Locale locale} to apply during * formatting. If {@code l} is {@code null} then no localization * is applied. */ public Formatter(Locale l) { ! this(l, new StringBuilder()); } /** * Constructs a new formatter with the specified destination and locale. *
*** 1914,1926 **** * The {@linkplain java.util.Locale locale} to apply during * formatting. If {@code l} is {@code null} then no localization * is applied. */ public Formatter(Appendable a, Locale l) { ! if (a == null) ! a = new StringBuilder(); ! init(a, l); } /** * Constructs a new formatter with the specified file name. * --- 1943,1953 ---- * The {@linkplain java.util.Locale locale} to apply during * formatting. If {@code l} is {@code null} then no localization * is applied. */ public Formatter(Appendable a, Locale l) { ! this(l, nonNullAppendable(a)); } /** * Constructs a new formatter with the specified file name. *
*** 1947,1958 **** * regular file and a new regular file of that name cannot be * created, or if some other error occurs while opening or * creating the file */ public Formatter(String fileName) throws FileNotFoundException { ! init(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))), ! Locale.getDefault(Locale.Category.FORMAT)); } /** * Constructs a new formatter with the specified file name and charset. * --- 1974,1985 ---- * regular file and a new regular file of that name cannot be * created, or if some other error occurs while opening or * creating the file */ public Formatter(String fileName) throws FileNotFoundException { ! this(Locale.getDefault(Locale.Category.FORMAT), ! new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName)))); } /** * Constructs a new formatter with the specified file name and charset. *
*** 2023,2034 **** * If the named charset is not supported */ public Formatter(String fileName, String csn, Locale l) throws FileNotFoundException, UnsupportedEncodingException { ! init(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), csn)), ! l); } /** * Constructs a new formatter with the specified file. * --- 2050,2062 ---- * If the named charset is not supported */ public Formatter(String fileName, String csn, Locale l) throws FileNotFoundException, UnsupportedEncodingException { ! this(verifyCharsetName(csn), ! l, ! new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), csn))); } /** * Constructs a new formatter with the specified file. *
*** 2055,2066 **** * regular file and a new regular file of that name cannot be * created, or if some other error occurs while opening or * creating the file */ public Formatter(File file) throws FileNotFoundException { ! init(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))), ! Locale.getDefault(Locale.Category.FORMAT)); } /** * Constructs a new formatter with the specified file and charset. * --- 2083,2094 ---- * regular file and a new regular file of that name cannot be * created, or if some other error occurs while opening or * creating the file */ public Formatter(File file) throws FileNotFoundException { ! this(Locale.getDefault(Locale.Category.FORMAT), ! new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)))); } /** * Constructs a new formatter with the specified file and charset. *
*** 2131,2142 **** * If the named charset is not supported */ public Formatter(File file, String csn, Locale l) throws FileNotFoundException, UnsupportedEncodingException { ! init(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), csn)), ! l); } /** * Constructs a new formatter with the specified print stream. * --- 2159,2171 ---- * If the named charset is not supported */ public Formatter(File file, String csn, Locale l) throws FileNotFoundException, UnsupportedEncodingException { ! this(verifyCharsetName(csn), ! l, ! new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), csn))); } /** * Constructs a new formatter with the specified print stream. *
*** 2149,2161 **** * * @param ps * The stream to use as the destination of this formatter. */ public Formatter(PrintStream ps) { ! if (ps == null) ! throw new NullPointerException(); ! init((Appendable)ps, Locale.getDefault(Locale.Category.FORMAT)); } /** * Constructs a new formatter with the specified output stream. * --- 2178,2189 ---- * * @param ps * The stream to use as the destination of this formatter. */ public Formatter(PrintStream ps) { ! this(Locale.getDefault(Locale.Category.FORMAT), ! (Appendable)Objects.nonNull(ps)); } /** * Constructs a new formatter with the specified output stream. *
*** 2169,2180 **** * @param os * The output stream to use as the destination of this formatter. * The output will be buffered. */ public Formatter(OutputStream os) { ! init(new BufferedWriter(new OutputStreamWriter(os)), ! Locale.getDefault(Locale.Category.FORMAT)); } /** * Constructs a new formatter with the specified output stream and * charset. --- 2197,2208 ---- * @param os * The output stream to use as the destination of this formatter. * The output will be buffered. */ public Formatter(OutputStream os) { ! this(Locale.getDefault(Locale.Category.FORMAT), ! new BufferedWriter(new OutputStreamWriter(os))); } /** * Constructs a new formatter with the specified output stream and * charset.
*** 2220,2236 **** * If the named charset is not supported */ public Formatter(OutputStream os, String csn, Locale l) throws UnsupportedEncodingException { ! init(new BufferedWriter(new OutputStreamWriter(os, csn)), l); } ! private void setZero() { if ((l != null) && !l.equals(Locale.US)) { DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(l); ! zero = dfs.getZeroDigit(); } } /** * Returns the locale set by the construction of this formatter. --- 2248,2266 ---- * If the named charset is not supported */ public Formatter(OutputStream os, String csn, Locale l) throws UnsupportedEncodingException { ! this(l, new BufferedWriter(new OutputStreamWriter(os, csn))); } ! private static final char getZero(Locale l) { if ((l != null) && !l.equals(Locale.US)) { DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(l); ! return dfs.getZeroDigit(); ! } else { ! return '0'; } } /** * Returns the locale set by the construction of this formatter.