merging latest changes from default canvas
authorAnton Epple <toni.epple@eppleton.de>
Sun, 08 Sep 2013 11:22:51 +0200
branchcanvas
changeset 12687937df26a5a7
parent 1267 0f775bd8d210
parent 1261 50c09cb0a3fb
child 1269 8dce385fd361
merging latest changes from default
pom.xml
rt/emul/compact/src/main/java/java/lang/System.java
     1.1 --- a/ko/archetype-test/src/test/java/org/apidesign/bck2brwsr/ko/archetype/test/ArchetypeVersionTest.java	Sun Sep 08 10:58:10 2013 +0200
     1.2 +++ b/ko/archetype-test/src/test/java/org/apidesign/bck2brwsr/ko/archetype/test/ArchetypeVersionTest.java	Sun Sep 08 11:22:51 2013 +0200
     1.3 @@ -64,7 +64,9 @@
     1.4          String arch = (String) xp2.evaluate(dom, XPathConstants.STRING);
     1.5          
     1.6          int snapshot = arch.indexOf("-SNAPSHOT");
     1.7 -        assertEquals(snapshot, -1, "Don't depend on snapshots: " + arch);
     1.8 +        if (snapshot >= 0) {
     1.9 +            arch = arch.substring(0, snapshot);
    1.10 +        }
    1.11  
    1.12          assertTrue(arch.matches("[0-9\\.]+"), "net.java.html.json version seems valid: " + arch);
    1.13      }
     2.1 --- a/pom.xml	Sun Sep 08 10:58:10 2013 +0200
     2.2 +++ b/pom.xml	Sun Sep 08 11:22:51 2013 +0200
     2.3 @@ -65,7 +65,7 @@
     2.4    </pluginRepositories>
     2.5    <build>
     2.6        <plugins>
     2.7 -         <plugin>
     2.8 +<!--         <plugin>
     2.9                <inherited>false</inherited>
    2.10                <groupId>com.mycila.maven-license-plugin</groupId>
    2.11                <artifactId>maven-license-plugin</artifactId>
    2.12 @@ -96,7 +96,7 @@
    2.13                         <exclude>ko/*/src/main/resources/org/apidesign/*/*/knockout-2.2.1.js</exclude>
    2.14                    </excludes>
    2.15                </configuration>
    2.16 -          </plugin>
    2.17 +          </plugin>-->
    2.18             <plugin>
    2.19              <artifactId>maven-release-plugin</artifactId>
    2.20              <version>2.4</version>
    2.21 @@ -114,6 +114,9 @@
    2.22                  <groupId>org.apache.maven.plugins</groupId>
    2.23                  <artifactId>maven-surefire-plugin</artifactId>
    2.24                  <version>2.13</version>
    2.25 +                <configuration>
    2.26 +                    <skipTests>true</skipTests>
    2.27 +                </configuration>
    2.28                </plugin>
    2.29                <plugin>
    2.30                  <groupId>org.apache.maven.plugins</groupId>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/rt/emul/compact/src/main/java/java/io/BufferedWriter.java	Sun Sep 08 11:22:51 2013 +0200
     3.3 @@ -0,0 +1,271 @@
     3.4 +/*
     3.5 + * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + *
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.  Oracle designates this
    3.11 + * particular file as subject to the "Classpath" exception as provided
    3.12 + * by Oracle in the LICENSE file that accompanied this code.
    3.13 + *
    3.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.17 + * version 2 for more details (a copy is included in the LICENSE file that
    3.18 + * accompanied this code).
    3.19 + *
    3.20 + * You should have received a copy of the GNU General Public License version
    3.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.23 + *
    3.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.25 + * or visit www.oracle.com if you need additional information or have any
    3.26 + * questions.
    3.27 + */
    3.28 +
    3.29 +package java.io;
    3.30 +
    3.31 +
    3.32 +/**
    3.33 + * Writes text to a character-output stream, buffering characters so as to
    3.34 + * provide for the efficient writing of single characters, arrays, and strings.
    3.35 + *
    3.36 + * <p> The buffer size may be specified, or the default size may be accepted.
    3.37 + * The default is large enough for most purposes.
    3.38 + *
    3.39 + * <p> A newLine() method is provided, which uses the platform's own notion of
    3.40 + * line separator as defined by the system property <tt>line.separator</tt>.
    3.41 + * Not all platforms use the newline character ('\n') to terminate lines.
    3.42 + * Calling this method to terminate each output line is therefore preferred to
    3.43 + * writing a newline character directly.
    3.44 + *
    3.45 + * <p> In general, a Writer sends its output immediately to the underlying
    3.46 + * character or byte stream.  Unless prompt output is required, it is advisable
    3.47 + * to wrap a BufferedWriter around any Writer whose write() operations may be
    3.48 + * costly, such as FileWriters and OutputStreamWriters.  For example,
    3.49 + *
    3.50 + * <pre>
    3.51 + * PrintWriter out
    3.52 + *   = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
    3.53 + * </pre>
    3.54 + *
    3.55 + * will buffer the PrintWriter's output to the file.  Without buffering, each
    3.56 + * invocation of a print() method would cause characters to be converted into
    3.57 + * bytes that would then be written immediately to the file, which can be very
    3.58 + * inefficient.
    3.59 + *
    3.60 + * @see PrintWriter
    3.61 + * @see FileWriter
    3.62 + * @see OutputStreamWriter
    3.63 + * @see java.nio.file.Files#newBufferedWriter
    3.64 + *
    3.65 + * @author      Mark Reinhold
    3.66 + * @since       JDK1.1
    3.67 + */
    3.68 +
    3.69 +public class BufferedWriter extends Writer {
    3.70 +
    3.71 +    private Writer out;
    3.72 +
    3.73 +    private char cb[];
    3.74 +    private int nChars, nextChar;
    3.75 +
    3.76 +    private static int defaultCharBufferSize = 8192;
    3.77 +
    3.78 +    /**
    3.79 +     * Line separator string.  This is the value of the line.separator
    3.80 +     * property at the moment that the stream was created.
    3.81 +     */
    3.82 +    private String lineSeparator;
    3.83 +
    3.84 +    /**
    3.85 +     * Creates a buffered character-output stream that uses a default-sized
    3.86 +     * output buffer.
    3.87 +     *
    3.88 +     * @param  out  A Writer
    3.89 +     */
    3.90 +    public BufferedWriter(Writer out) {
    3.91 +        this(out, defaultCharBufferSize);
    3.92 +    }
    3.93 +
    3.94 +    /**
    3.95 +     * Creates a new buffered character-output stream that uses an output
    3.96 +     * buffer of the given size.
    3.97 +     *
    3.98 +     * @param  out  A Writer
    3.99 +     * @param  sz   Output-buffer size, a positive integer
   3.100 +     *
   3.101 +     * @exception  IllegalArgumentException  If sz is <= 0
   3.102 +     */
   3.103 +    public BufferedWriter(Writer out, int sz) {
   3.104 +        super(out);
   3.105 +        if (sz <= 0)
   3.106 +            throw new IllegalArgumentException("Buffer size <= 0");
   3.107 +        this.out = out;
   3.108 +        cb = new char[sz];
   3.109 +        nChars = sz;
   3.110 +        nextChar = 0;
   3.111 +
   3.112 +        lineSeparator = "\n";
   3.113 +    }
   3.114 +
   3.115 +    /** Checks to make sure that the stream has not been closed */
   3.116 +    private void ensureOpen() throws IOException {
   3.117 +        if (out == null)
   3.118 +            throw new IOException("Stream closed");
   3.119 +    }
   3.120 +
   3.121 +    /**
   3.122 +     * Flushes the output buffer to the underlying character stream, without
   3.123 +     * flushing the stream itself.  This method is non-private only so that it
   3.124 +     * may be invoked by PrintStream.
   3.125 +     */
   3.126 +    void flushBuffer() throws IOException {
   3.127 +        synchronized (lock) {
   3.128 +            ensureOpen();
   3.129 +            if (nextChar == 0)
   3.130 +                return;
   3.131 +            out.write(cb, 0, nextChar);
   3.132 +            nextChar = 0;
   3.133 +        }
   3.134 +    }
   3.135 +
   3.136 +    /**
   3.137 +     * Writes a single character.
   3.138 +     *
   3.139 +     * @exception  IOException  If an I/O error occurs
   3.140 +     */
   3.141 +    public void write(int c) throws IOException {
   3.142 +        synchronized (lock) {
   3.143 +            ensureOpen();
   3.144 +            if (nextChar >= nChars)
   3.145 +                flushBuffer();
   3.146 +            cb[nextChar++] = (char) c;
   3.147 +        }
   3.148 +    }
   3.149 +
   3.150 +    /**
   3.151 +     * Our own little min method, to avoid loading java.lang.Math if we've run
   3.152 +     * out of file descriptors and we're trying to print a stack trace.
   3.153 +     */
   3.154 +    private int min(int a, int b) {
   3.155 +        if (a < b) return a;
   3.156 +        return b;
   3.157 +    }
   3.158 +
   3.159 +    /**
   3.160 +     * Writes a portion of an array of characters.
   3.161 +     *
   3.162 +     * <p> Ordinarily this method stores characters from the given array into
   3.163 +     * this stream's buffer, flushing the buffer to the underlying stream as
   3.164 +     * needed.  If the requested length is at least as large as the buffer,
   3.165 +     * however, then this method will flush the buffer and write the characters
   3.166 +     * directly to the underlying stream.  Thus redundant
   3.167 +     * <code>BufferedWriter</code>s will not copy data unnecessarily.
   3.168 +     *
   3.169 +     * @param  cbuf  A character array
   3.170 +     * @param  off   Offset from which to start reading characters
   3.171 +     * @param  len   Number of characters to write
   3.172 +     *
   3.173 +     * @exception  IOException  If an I/O error occurs
   3.174 +     */
   3.175 +    public void write(char cbuf[], int off, int len) throws IOException {
   3.176 +        synchronized (lock) {
   3.177 +            ensureOpen();
   3.178 +            if ((off < 0) || (off > cbuf.length) || (len < 0) ||
   3.179 +                ((off + len) > cbuf.length) || ((off + len) < 0)) {
   3.180 +                throw new IndexOutOfBoundsException();
   3.181 +            } else if (len == 0) {
   3.182 +                return;
   3.183 +            }
   3.184 +
   3.185 +            if (len >= nChars) {
   3.186 +                /* If the request length exceeds the size of the output buffer,
   3.187 +                   flush the buffer and then write the data directly.  In this
   3.188 +                   way buffered streams will cascade harmlessly. */
   3.189 +                flushBuffer();
   3.190 +                out.write(cbuf, off, len);
   3.191 +                return;
   3.192 +            }
   3.193 +
   3.194 +            int b = off, t = off + len;
   3.195 +            while (b < t) {
   3.196 +                int d = min(nChars - nextChar, t - b);
   3.197 +                System.arraycopy(cbuf, b, cb, nextChar, d);
   3.198 +                b += d;
   3.199 +                nextChar += d;
   3.200 +                if (nextChar >= nChars)
   3.201 +                    flushBuffer();
   3.202 +            }
   3.203 +        }
   3.204 +    }
   3.205 +
   3.206 +    /**
   3.207 +     * Writes a portion of a String.
   3.208 +     *
   3.209 +     * <p> If the value of the <tt>len</tt> parameter is negative then no
   3.210 +     * characters are written.  This is contrary to the specification of this
   3.211 +     * method in the {@linkplain java.io.Writer#write(java.lang.String,int,int)
   3.212 +     * superclass}, which requires that an {@link IndexOutOfBoundsException} be
   3.213 +     * thrown.
   3.214 +     *
   3.215 +     * @param  s     String to be written
   3.216 +     * @param  off   Offset from which to start reading characters
   3.217 +     * @param  len   Number of characters to be written
   3.218 +     *
   3.219 +     * @exception  IOException  If an I/O error occurs
   3.220 +     */
   3.221 +    public void write(String s, int off, int len) throws IOException {
   3.222 +        synchronized (lock) {
   3.223 +            ensureOpen();
   3.224 +
   3.225 +            int b = off, t = off + len;
   3.226 +            while (b < t) {
   3.227 +                int d = min(nChars - nextChar, t - b);
   3.228 +                s.getChars(b, b + d, cb, nextChar);
   3.229 +                b += d;
   3.230 +                nextChar += d;
   3.231 +                if (nextChar >= nChars)
   3.232 +                    flushBuffer();
   3.233 +            }
   3.234 +        }
   3.235 +    }
   3.236 +
   3.237 +    /**
   3.238 +     * Writes a line separator.  The line separator string is defined by the
   3.239 +     * system property <tt>line.separator</tt>, and is not necessarily a single
   3.240 +     * newline ('\n') character.
   3.241 +     *
   3.242 +     * @exception  IOException  If an I/O error occurs
   3.243 +     */
   3.244 +    public void newLine() throws IOException {
   3.245 +        write(lineSeparator);
   3.246 +    }
   3.247 +
   3.248 +    /**
   3.249 +     * Flushes the stream.
   3.250 +     *
   3.251 +     * @exception  IOException  If an I/O error occurs
   3.252 +     */
   3.253 +    public void flush() throws IOException {
   3.254 +        synchronized (lock) {
   3.255 +            flushBuffer();
   3.256 +            out.flush();
   3.257 +        }
   3.258 +    }
   3.259 +
   3.260 +    public void close() throws IOException {
   3.261 +        synchronized (lock) {
   3.262 +            if (out == null) {
   3.263 +                return;
   3.264 +            }
   3.265 +            try {
   3.266 +                flushBuffer();
   3.267 +            } finally {
   3.268 +                out.close();
   3.269 +                out = null;
   3.270 +                cb = null;
   3.271 +            }
   3.272 +        }
   3.273 +    }
   3.274 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/rt/emul/compact/src/main/java/java/io/File.java	Sun Sep 08 11:22:51 2013 +0200
     4.3 @@ -0,0 +1,1927 @@
     4.4 +/*
     4.5 + * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
     4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4.7 + *
     4.8 + * This code is free software; you can redistribute it and/or modify it
     4.9 + * under the terms of the GNU General Public License version 2 only, as
    4.10 + * published by the Free Software Foundation.  Oracle designates this
    4.11 + * particular file as subject to the "Classpath" exception as provided
    4.12 + * by Oracle in the LICENSE file that accompanied this code.
    4.13 + *
    4.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    4.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    4.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    4.17 + * version 2 for more details (a copy is included in the LICENSE file that
    4.18 + * accompanied this code).
    4.19 + *
    4.20 + * You should have received a copy of the GNU General Public License version
    4.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    4.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    4.23 + *
    4.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    4.25 + * or visit www.oracle.com if you need additional information or have any
    4.26 + * questions.
    4.27 + */
    4.28 +
    4.29 +package java.io;
    4.30 +
    4.31 +import java.net.URI;
    4.32 +import java.net.URL;
    4.33 +import java.net.MalformedURLException;
    4.34 +import java.net.URISyntaxException;
    4.35 +
    4.36 +/**
    4.37 + * An abstract representation of file and directory pathnames.
    4.38 + *
    4.39 + * <p> User interfaces and operating systems use system-dependent <em>pathname
    4.40 + * strings</em> to name files and directories.  This class presents an
    4.41 + * abstract, system-independent view of hierarchical pathnames.  An
    4.42 + * <em>abstract pathname</em> has two components:
    4.43 + *
    4.44 + * <ol>
    4.45 + * <li> An optional system-dependent <em>prefix</em> string,
    4.46 + *      such as a disk-drive specifier, <code>"/"</code>&nbsp;for the UNIX root
    4.47 + *      directory, or <code>"\\\\"</code>&nbsp;for a Microsoft Windows UNC pathname, and
    4.48 + * <li> A sequence of zero or more string <em>names</em>.
    4.49 + * </ol>
    4.50 + *
    4.51 + * The first name in an abstract pathname may be a directory name or, in the
    4.52 + * case of Microsoft Windows UNC pathnames, a hostname.  Each subsequent name
    4.53 + * in an abstract pathname denotes a directory; the last name may denote
    4.54 + * either a directory or a file.  The <em>empty</em> abstract pathname has no
    4.55 + * prefix and an empty name sequence.
    4.56 + *
    4.57 + * <p> The conversion of a pathname string to or from an abstract pathname is
    4.58 + * inherently system-dependent.  When an abstract pathname is converted into a
    4.59 + * pathname string, each name is separated from the next by a single copy of
    4.60 + * the default <em>separator character</em>.  The default name-separator
    4.61 + * character is defined by the system property <code>file.separator</code>, and
    4.62 + * is made available in the public static fields <code>{@link
    4.63 + * #separator}</code> and <code>{@link #separatorChar}</code> of this class.
    4.64 + * When a pathname string is converted into an abstract pathname, the names
    4.65 + * within it may be separated by the default name-separator character or by any
    4.66 + * other name-separator character that is supported by the underlying system.
    4.67 + *
    4.68 + * <p> A pathname, whether abstract or in string form, may be either
    4.69 + * <em>absolute</em> or <em>relative</em>.  An absolute pathname is complete in
    4.70 + * that no other information is required in order to locate the file that it
    4.71 + * denotes.  A relative pathname, in contrast, must be interpreted in terms of
    4.72 + * information taken from some other pathname.  By default the classes in the
    4.73 + * <code>java.io</code> package always resolve relative pathnames against the
    4.74 + * current user directory.  This directory is named by the system property
    4.75 + * <code>user.dir</code>, and is typically the directory in which the Java
    4.76 + * virtual machine was invoked.
    4.77 + *
    4.78 + * <p> The <em>parent</em> of an abstract pathname may be obtained by invoking
    4.79 + * the {@link #getParent} method of this class and consists of the pathname's
    4.80 + * prefix and each name in the pathname's name sequence except for the last.
    4.81 + * Each directory's absolute pathname is an ancestor of any <tt>File</tt>
    4.82 + * object with an absolute abstract pathname which begins with the directory's
    4.83 + * absolute pathname.  For example, the directory denoted by the abstract
    4.84 + * pathname <tt>"/usr"</tt> is an ancestor of the directory denoted by the
    4.85 + * pathname <tt>"/usr/local/bin"</tt>.
    4.86 + *
    4.87 + * <p> The prefix concept is used to handle root directories on UNIX platforms,
    4.88 + * and drive specifiers, root directories and UNC pathnames on Microsoft Windows platforms,
    4.89 + * as follows:
    4.90 + *
    4.91 + * <ul>
    4.92 + *
    4.93 + * <li> For UNIX platforms, the prefix of an absolute pathname is always
    4.94 + * <code>"/"</code>.  Relative pathnames have no prefix.  The abstract pathname
    4.95 + * denoting the root directory has the prefix <code>"/"</code> and an empty
    4.96 + * name sequence.
    4.97 + *
    4.98 + * <li> For Microsoft Windows platforms, the prefix of a pathname that contains a drive
    4.99 + * specifier consists of the drive letter followed by <code>":"</code> and
   4.100 + * possibly followed by <code>"\\"</code> if the pathname is absolute.  The
   4.101 + * prefix of a UNC pathname is <code>"\\\\"</code>; the hostname and the share
   4.102 + * name are the first two names in the name sequence.  A relative pathname that
   4.103 + * does not specify a drive has no prefix.
   4.104 + *
   4.105 + * </ul>
   4.106 + *
   4.107 + * <p> Instances of this class may or may not denote an actual file-system
   4.108 + * object such as a file or a directory.  If it does denote such an object
   4.109 + * then that object resides in a <i>partition</i>.  A partition is an
   4.110 + * operating system-specific portion of storage for a file system.  A single
   4.111 + * storage device (e.g. a physical disk-drive, flash memory, CD-ROM) may
   4.112 + * contain multiple partitions.  The object, if any, will reside on the
   4.113 + * partition <a name="partName">named</a> by some ancestor of the absolute
   4.114 + * form of this pathname.
   4.115 + *
   4.116 + * <p> A file system may implement restrictions to certain operations on the
   4.117 + * actual file-system object, such as reading, writing, and executing.  These
   4.118 + * restrictions are collectively known as <i>access permissions</i>.  The file
   4.119 + * system may have multiple sets of access permissions on a single object.
   4.120 + * For example, one set may apply to the object's <i>owner</i>, and another
   4.121 + * may apply to all other users.  The access permissions on an object may
   4.122 + * cause some methods in this class to fail.
   4.123 + *
   4.124 + * <p> Instances of the <code>File</code> class are immutable; that is, once
   4.125 + * created, the abstract pathname represented by a <code>File</code> object
   4.126 + * will never change.
   4.127 + *
   4.128 + * <h4>Interoperability with {@code java.nio.file} package</h4>
   4.129 + *
   4.130 + * <p> The <a href="../../java/nio/file/package-summary.html">{@code java.nio.file}</a>
   4.131 + * package defines interfaces and classes for the Java virtual machine to access
   4.132 + * files, file attributes, and file systems. This API may be used to overcome
   4.133 + * many of the limitations of the {@code java.io.File} class.
   4.134 + * The {@link #toPath toPath} method may be used to obtain a {@link
   4.135 + * Path} that uses the abstract path represented by a {@code File} object to
   4.136 + * locate a file. The resulting {@code Path} may be used with the {@link
   4.137 + * java.nio.file.Files} class to provide more efficient and extensive access to
   4.138 + * additional file operations, file attributes, and I/O exceptions to help
   4.139 + * diagnose errors when an operation on a file fails.
   4.140 + *
   4.141 + * @author  unascribed
   4.142 + * @since   JDK1.0
   4.143 + */
   4.144 +
   4.145 +public class File
   4.146 +    implements Serializable, Comparable<File>
   4.147 +{
   4.148 +
   4.149 +    /**
   4.150 +     * The FileSystem object representing the platform's local file system.
   4.151 +     */
   4.152 +    static private FileSystem fs = new FileSystem();
   4.153 +    private static class FileSystem {
   4.154 +
   4.155 +        private char getSeparator() {
   4.156 +            return '/';
   4.157 +        }
   4.158 +
   4.159 +        private String resolve(String path, String child) {
   4.160 +            return path + '/' + child;
   4.161 +        }
   4.162 +
   4.163 +        private String normalize(String pathname) {
   4.164 +            return pathname;
   4.165 +        }
   4.166 +
   4.167 +        private int prefixLength(String path) {
   4.168 +            return 0;
   4.169 +        }
   4.170 +
   4.171 +        private String getDefaultParent() {
   4.172 +            return "/";
   4.173 +        }
   4.174 +
   4.175 +        private String fromURIPath(String p) {
   4.176 +            return p;
   4.177 +        }
   4.178 +
   4.179 +        private boolean isAbsolute(File aThis) {
   4.180 +            return aThis.getPath().startsWith("/");
   4.181 +        }
   4.182 +
   4.183 +        private int compare(File one, File two) {
   4.184 +            return one.getPath().compareTo(two.getPath());
   4.185 +        }
   4.186 +
   4.187 +        private int hashCode(File aThis) {
   4.188 +            return aThis.getPath().hashCode();
   4.189 +        }
   4.190 +
   4.191 +        private char getPathSeparator() {
   4.192 +            return ':';
   4.193 +        }
   4.194 +        
   4.195 +    }
   4.196 +
   4.197 +    /**
   4.198 +     * This abstract pathname's normalized pathname string.  A normalized
   4.199 +     * pathname string uses the default name-separator character and does not
   4.200 +     * contain any duplicate or redundant separators.
   4.201 +     *
   4.202 +     * @serial
   4.203 +     */
   4.204 +    private String path;
   4.205 +
   4.206 +    /**
   4.207 +     * The length of this abstract pathname's prefix, or zero if it has no
   4.208 +     * prefix.
   4.209 +     */
   4.210 +    private transient int prefixLength;
   4.211 +
   4.212 +    /**
   4.213 +     * Returns the length of this abstract pathname's prefix.
   4.214 +     * For use by FileSystem classes.
   4.215 +     */
   4.216 +    int getPrefixLength() {
   4.217 +        return prefixLength;
   4.218 +    }
   4.219 +
   4.220 +    /**
   4.221 +     * The system-dependent default name-separator character.  This field is
   4.222 +     * initialized to contain the first character of the value of the system
   4.223 +     * property <code>file.separator</code>.  On UNIX systems the value of this
   4.224 +     * field is <code>'/'</code>; on Microsoft Windows systems it is <code>'\\'</code>.
   4.225 +     *
   4.226 +     * @see     java.lang.System#getProperty(java.lang.String)
   4.227 +     */
   4.228 +    public static final char separatorChar = fs.getSeparator();
   4.229 +
   4.230 +    /**
   4.231 +     * The system-dependent default name-separator character, represented as a
   4.232 +     * string for convenience.  This string contains a single character, namely
   4.233 +     * <code>{@link #separatorChar}</code>.
   4.234 +     */
   4.235 +    public static final String separator = "" + separatorChar;
   4.236 +
   4.237 +    /**
   4.238 +     * The system-dependent path-separator character.  This field is
   4.239 +     * initialized to contain the first character of the value of the system
   4.240 +     * property <code>path.separator</code>.  This character is used to
   4.241 +     * separate filenames in a sequence of files given as a <em>path list</em>.
   4.242 +     * On UNIX systems, this character is <code>':'</code>; on Microsoft Windows systems it
   4.243 +     * is <code>';'</code>.
   4.244 +     *
   4.245 +     * @see     java.lang.System#getProperty(java.lang.String)
   4.246 +     */
   4.247 +    public static final char pathSeparatorChar = fs.getPathSeparator();
   4.248 +
   4.249 +    /**
   4.250 +     * The system-dependent path-separator character, represented as a string
   4.251 +     * for convenience.  This string contains a single character, namely
   4.252 +     * <code>{@link #pathSeparatorChar}</code>.
   4.253 +     */
   4.254 +    public static final String pathSeparator = "" + pathSeparatorChar;
   4.255 +
   4.256 +
   4.257 +    /* -- Constructors -- */
   4.258 +
   4.259 +    /**
   4.260 +     * Internal constructor for already-normalized pathname strings.
   4.261 +     */
   4.262 +    private File(String pathname, int prefixLength) {
   4.263 +        this.path = pathname;
   4.264 +        this.prefixLength = prefixLength;
   4.265 +    }
   4.266 +
   4.267 +    /**
   4.268 +     * Internal constructor for already-normalized pathname strings.
   4.269 +     * The parameter order is used to disambiguate this method from the
   4.270 +     * public(File, String) constructor.
   4.271 +     */
   4.272 +    private File(String child, File parent) {
   4.273 +        assert parent.path != null;
   4.274 +        assert (!parent.path.equals(""));
   4.275 +        this.path = fs.resolve(parent.path, child);
   4.276 +        this.prefixLength = parent.prefixLength;
   4.277 +    }
   4.278 +
   4.279 +    /**
   4.280 +     * Creates a new <code>File</code> instance by converting the given
   4.281 +     * pathname string into an abstract pathname.  If the given string is
   4.282 +     * the empty string, then the result is the empty abstract pathname.
   4.283 +     *
   4.284 +     * @param   pathname  A pathname string
   4.285 +     * @throws  NullPointerException
   4.286 +     *          If the <code>pathname</code> argument is <code>null</code>
   4.287 +     */
   4.288 +    public File(String pathname) {
   4.289 +        if (pathname == null) {
   4.290 +            throw new NullPointerException();
   4.291 +        }
   4.292 +        this.path = fs.normalize(pathname);
   4.293 +        this.prefixLength = fs.prefixLength(this.path);
   4.294 +    }
   4.295 +
   4.296 +    /* Note: The two-argument File constructors do not interpret an empty
   4.297 +       parent abstract pathname as the current user directory.  An empty parent
   4.298 +       instead causes the child to be resolved against the system-dependent
   4.299 +       directory defined by the FileSystem.getDefaultParent method.  On Unix
   4.300 +       this default is "/", while on Microsoft Windows it is "\\".  This is required for
   4.301 +       compatibility with the original behavior of this class. */
   4.302 +
   4.303 +    /**
   4.304 +     * Creates a new <code>File</code> instance from a parent pathname string
   4.305 +     * and a child pathname string.
   4.306 +     *
   4.307 +     * <p> If <code>parent</code> is <code>null</code> then the new
   4.308 +     * <code>File</code> instance is created as if by invoking the
   4.309 +     * single-argument <code>File</code> constructor on the given
   4.310 +     * <code>child</code> pathname string.
   4.311 +     *
   4.312 +     * <p> Otherwise the <code>parent</code> pathname string is taken to denote
   4.313 +     * a directory, and the <code>child</code> pathname string is taken to
   4.314 +     * denote either a directory or a file.  If the <code>child</code> pathname
   4.315 +     * string is absolute then it is converted into a relative pathname in a
   4.316 +     * system-dependent way.  If <code>parent</code> is the empty string then
   4.317 +     * the new <code>File</code> instance is created by converting
   4.318 +     * <code>child</code> into an abstract pathname and resolving the result
   4.319 +     * against a system-dependent default directory.  Otherwise each pathname
   4.320 +     * string is converted into an abstract pathname and the child abstract
   4.321 +     * pathname is resolved against the parent.
   4.322 +     *
   4.323 +     * @param   parent  The parent pathname string
   4.324 +     * @param   child   The child pathname string
   4.325 +     * @throws  NullPointerException
   4.326 +     *          If <code>child</code> is <code>null</code>
   4.327 +     */
   4.328 +    public File(String parent, String child) {
   4.329 +        if (child == null) {
   4.330 +            throw new NullPointerException();
   4.331 +        }
   4.332 +        if (parent != null) {
   4.333 +            if (parent.equals("")) {
   4.334 +                this.path = fs.resolve(fs.getDefaultParent(),
   4.335 +                                       fs.normalize(child));
   4.336 +            } else {
   4.337 +                this.path = fs.resolve(fs.normalize(parent),
   4.338 +                                       fs.normalize(child));
   4.339 +            }
   4.340 +        } else {
   4.341 +            this.path = fs.normalize(child);
   4.342 +        }
   4.343 +        this.prefixLength = fs.prefixLength(this.path);
   4.344 +    }
   4.345 +
   4.346 +    /**
   4.347 +     * Creates a new <code>File</code> instance from a parent abstract
   4.348 +     * pathname and a child pathname string.
   4.349 +     *
   4.350 +     * <p> If <code>parent</code> is <code>null</code> then the new
   4.351 +     * <code>File</code> instance is created as if by invoking the
   4.352 +     * single-argument <code>File</code> constructor on the given
   4.353 +     * <code>child</code> pathname string.
   4.354 +     *
   4.355 +     * <p> Otherwise the <code>parent</code> abstract pathname is taken to
   4.356 +     * denote a directory, and the <code>child</code> pathname string is taken
   4.357 +     * to denote either a directory or a file.  If the <code>child</code>
   4.358 +     * pathname string is absolute then it is converted into a relative
   4.359 +     * pathname in a system-dependent way.  If <code>parent</code> is the empty
   4.360 +     * abstract pathname then the new <code>File</code> instance is created by
   4.361 +     * converting <code>child</code> into an abstract pathname and resolving
   4.362 +     * the result against a system-dependent default directory.  Otherwise each
   4.363 +     * pathname string is converted into an abstract pathname and the child
   4.364 +     * abstract pathname is resolved against the parent.
   4.365 +     *
   4.366 +     * @param   parent  The parent abstract pathname
   4.367 +     * @param   child   The child pathname string
   4.368 +     * @throws  NullPointerException
   4.369 +     *          If <code>child</code> is <code>null</code>
   4.370 +     */
   4.371 +    public File(File parent, String child) {
   4.372 +        if (child == null) {
   4.373 +            throw new NullPointerException();
   4.374 +        }
   4.375 +        if (parent != null) {
   4.376 +            if (parent.path.equals("")) {
   4.377 +                this.path = fs.resolve(fs.getDefaultParent(),
   4.378 +                                       fs.normalize(child));
   4.379 +            } else {
   4.380 +                this.path = fs.resolve(parent.path,
   4.381 +                                       fs.normalize(child));
   4.382 +            }
   4.383 +        } else {
   4.384 +            this.path = fs.normalize(child);
   4.385 +        }
   4.386 +        this.prefixLength = fs.prefixLength(this.path);
   4.387 +    }
   4.388 +
   4.389 +    /**
   4.390 +     * Creates a new <tt>File</tt> instance by converting the given
   4.391 +     * <tt>file:</tt> URI into an abstract pathname.
   4.392 +     *
   4.393 +     * <p> The exact form of a <tt>file:</tt> URI is system-dependent, hence
   4.394 +     * the transformation performed by this constructor is also
   4.395 +     * system-dependent.
   4.396 +     *
   4.397 +     * <p> For a given abstract pathname <i>f</i> it is guaranteed that
   4.398 +     *
   4.399 +     * <blockquote><tt>
   4.400 +     * new File(</tt><i>&nbsp;f</i><tt>.{@link #toURI() toURI}()).equals(</tt><i>&nbsp;f</i><tt>.{@link #getAbsoluteFile() getAbsoluteFile}())
   4.401 +     * </tt></blockquote>
   4.402 +     *
   4.403 +     * so long as the original abstract pathname, the URI, and the new abstract
   4.404 +     * pathname are all created in (possibly different invocations of) the same
   4.405 +     * Java virtual machine.  This relationship typically does not hold,
   4.406 +     * however, when a <tt>file:</tt> URI that is created in a virtual machine
   4.407 +     * on one operating system is converted into an abstract pathname in a
   4.408 +     * virtual machine on a different operating system.
   4.409 +     *
   4.410 +     * @param  uri
   4.411 +     *         An absolute, hierarchical URI with a scheme equal to
   4.412 +     *         <tt>"file"</tt>, a non-empty path component, and undefined
   4.413 +     *         authority, query, and fragment components
   4.414 +     *
   4.415 +     * @throws  NullPointerException
   4.416 +     *          If <tt>uri</tt> is <tt>null</tt>
   4.417 +     *
   4.418 +     * @throws  IllegalArgumentException
   4.419 +     *          If the preconditions on the parameter do not hold
   4.420 +     *
   4.421 +     * @see #toURI()
   4.422 +     * @see java.net.URI
   4.423 +     * @since 1.4
   4.424 +     */
   4.425 +    public File(URI uri) {
   4.426 +
   4.427 +        // Check our many preconditions
   4.428 +        if (!uri.isAbsolute())
   4.429 +            throw new IllegalArgumentException("URI is not absolute");
   4.430 +        if (uri.isOpaque())
   4.431 +            throw new IllegalArgumentException("URI is not hierarchical");
   4.432 +        String scheme = uri.getScheme();
   4.433 +        if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
   4.434 +            throw new IllegalArgumentException("URI scheme is not \"file\"");
   4.435 +        if (uri.getAuthority() != null)
   4.436 +            throw new IllegalArgumentException("URI has an authority component");
   4.437 +        if (uri.getFragment() != null)
   4.438 +            throw new IllegalArgumentException("URI has a fragment component");
   4.439 +        if (uri.getQuery() != null)
   4.440 +            throw new IllegalArgumentException("URI has a query component");
   4.441 +        String p = uri.getPath();
   4.442 +        if (p.equals(""))
   4.443 +            throw new IllegalArgumentException("URI path component is empty");
   4.444 +
   4.445 +        // Okay, now initialize
   4.446 +        p = fs.fromURIPath(p);
   4.447 +        if (File.separatorChar != '/')
   4.448 +            p = p.replace('/', File.separatorChar);
   4.449 +        this.path = fs.normalize(p);
   4.450 +        this.prefixLength = fs.prefixLength(this.path);
   4.451 +    }
   4.452 +
   4.453 +
   4.454 +    /* -- Path-component accessors -- */
   4.455 +
   4.456 +    /**
   4.457 +     * Returns the name of the file or directory denoted by this abstract
   4.458 +     * pathname.  This is just the last name in the pathname's name
   4.459 +     * sequence.  If the pathname's name sequence is empty, then the empty
   4.460 +     * string is returned.
   4.461 +     *
   4.462 +     * @return  The name of the file or directory denoted by this abstract
   4.463 +     *          pathname, or the empty string if this pathname's name sequence
   4.464 +     *          is empty
   4.465 +     */
   4.466 +    public String getName() {
   4.467 +        int index = path.lastIndexOf(separatorChar);
   4.468 +        if (index < prefixLength) return path.substring(prefixLength);
   4.469 +        return path.substring(index + 1);
   4.470 +    }
   4.471 +
   4.472 +    /**
   4.473 +     * Returns the pathname string of this abstract pathname's parent, or
   4.474 +     * <code>null</code> if this pathname does not name a parent directory.
   4.475 +     *
   4.476 +     * <p> The <em>parent</em> of an abstract pathname consists of the
   4.477 +     * pathname's prefix, if any, and each name in the pathname's name
   4.478 +     * sequence except for the last.  If the name sequence is empty then
   4.479 +     * the pathname does not name a parent directory.
   4.480 +     *
   4.481 +     * @return  The pathname string of the parent directory named by this
   4.482 +     *          abstract pathname, or <code>null</code> if this pathname
   4.483 +     *          does not name a parent
   4.484 +     */
   4.485 +    public String getParent() {
   4.486 +        int index = path.lastIndexOf(separatorChar);
   4.487 +        if (index < prefixLength) {
   4.488 +            if ((prefixLength > 0) && (path.length() > prefixLength))
   4.489 +                return path.substring(0, prefixLength);
   4.490 +            return null;
   4.491 +        }
   4.492 +        return path.substring(0, index);
   4.493 +    }
   4.494 +
   4.495 +    /**
   4.496 +     * Returns the abstract pathname of this abstract pathname's parent,
   4.497 +     * or <code>null</code> if this pathname does not name a parent
   4.498 +     * directory.
   4.499 +     *
   4.500 +     * <p> The <em>parent</em> of an abstract pathname consists of the
   4.501 +     * pathname's prefix, if any, and each name in the pathname's name
   4.502 +     * sequence except for the last.  If the name sequence is empty then
   4.503 +     * the pathname does not name a parent directory.
   4.504 +     *
   4.505 +     * @return  The abstract pathname of the parent directory named by this
   4.506 +     *          abstract pathname, or <code>null</code> if this pathname
   4.507 +     *          does not name a parent
   4.508 +     *
   4.509 +     * @since 1.2
   4.510 +     */
   4.511 +    public File getParentFile() {
   4.512 +        String p = this.getParent();
   4.513 +        if (p == null) return null;
   4.514 +        return new File(p, this.prefixLength);
   4.515 +    }
   4.516 +
   4.517 +    /**
   4.518 +     * Converts this abstract pathname into a pathname string.  The resulting
   4.519 +     * string uses the {@link #separator default name-separator character} to
   4.520 +     * separate the names in the name sequence.
   4.521 +     *
   4.522 +     * @return  The string form of this abstract pathname
   4.523 +     */
   4.524 +    public String getPath() {
   4.525 +        return path;
   4.526 +    }
   4.527 +
   4.528 +
   4.529 +    /* -- Path operations -- */
   4.530 +
   4.531 +    /**
   4.532 +     * Tests whether this abstract pathname is absolute.  The definition of
   4.533 +     * absolute pathname is system dependent.  On UNIX systems, a pathname is
   4.534 +     * absolute if its prefix is <code>"/"</code>.  On Microsoft Windows systems, a
   4.535 +     * pathname is absolute if its prefix is a drive specifier followed by
   4.536 +     * <code>"\\"</code>, or if its prefix is <code>"\\\\"</code>.
   4.537 +     *
   4.538 +     * @return  <code>true</code> if this abstract pathname is absolute,
   4.539 +     *          <code>false</code> otherwise
   4.540 +     */
   4.541 +    public boolean isAbsolute() {
   4.542 +        return fs.isAbsolute(this);
   4.543 +    }
   4.544 +
   4.545 +    /**
   4.546 +     * Returns the absolute pathname string of this abstract pathname.
   4.547 +     *
   4.548 +     * <p> If this abstract pathname is already absolute, then the pathname
   4.549 +     * string is simply returned as if by the <code>{@link #getPath}</code>
   4.550 +     * method.  If this abstract pathname is the empty abstract pathname then
   4.551 +     * the pathname string of the current user directory, which is named by the
   4.552 +     * system property <code>user.dir</code>, is returned.  Otherwise this
   4.553 +     * pathname is resolved in a system-dependent way.  On UNIX systems, a
   4.554 +     * relative pathname is made absolute by resolving it against the current
   4.555 +     * user directory.  On Microsoft Windows systems, a relative pathname is made absolute
   4.556 +     * by resolving it against the current directory of the drive named by the
   4.557 +     * pathname, if any; if not, it is resolved against the current user
   4.558 +     * directory.
   4.559 +     *
   4.560 +     * @return  The absolute pathname string denoting the same file or
   4.561 +     *          directory as this abstract pathname
   4.562 +     *
   4.563 +     * @throws  SecurityException
   4.564 +     *          If a required system property value cannot be accessed.
   4.565 +     *
   4.566 +     * @see     java.io.File#isAbsolute()
   4.567 +     */
   4.568 +    public String getAbsolutePath() {
   4.569 +        throw new SecurityException();
   4.570 +    }
   4.571 +
   4.572 +    /**
   4.573 +     * Returns the absolute form of this abstract pathname.  Equivalent to
   4.574 +     * <code>new&nbsp;File(this.{@link #getAbsolutePath})</code>.
   4.575 +     *
   4.576 +     * @return  The absolute abstract pathname denoting the same file or
   4.577 +     *          directory as this abstract pathname
   4.578 +     *
   4.579 +     * @throws  SecurityException
   4.580 +     *          If a required system property value cannot be accessed.
   4.581 +     *
   4.582 +     * @since 1.2
   4.583 +     */
   4.584 +    public File getAbsoluteFile() {
   4.585 +        String absPath = getAbsolutePath();
   4.586 +        return new File(absPath, fs.prefixLength(absPath));
   4.587 +    }
   4.588 +
   4.589 +    /**
   4.590 +     * Returns the canonical pathname string of this abstract pathname.
   4.591 +     *
   4.592 +     * <p> A canonical pathname is both absolute and unique.  The precise
   4.593 +     * definition of canonical form is system-dependent.  This method first
   4.594 +     * converts this pathname to absolute form if necessary, as if by invoking the
   4.595 +     * {@link #getAbsolutePath} method, and then maps it to its unique form in a
   4.596 +     * system-dependent way.  This typically involves removing redundant names
   4.597 +     * such as <tt>"."</tt> and <tt>".."</tt> from the pathname, resolving
   4.598 +     * symbolic links (on UNIX platforms), and converting drive letters to a
   4.599 +     * standard case (on Microsoft Windows platforms).
   4.600 +     *
   4.601 +     * <p> Every pathname that denotes an existing file or directory has a
   4.602 +     * unique canonical form.  Every pathname that denotes a nonexistent file
   4.603 +     * or directory also has a unique canonical form.  The canonical form of
   4.604 +     * the pathname of a nonexistent file or directory may be different from
   4.605 +     * the canonical form of the same pathname after the file or directory is
   4.606 +     * created.  Similarly, the canonical form of the pathname of an existing
   4.607 +     * file or directory may be different from the canonical form of the same
   4.608 +     * pathname after the file or directory is deleted.
   4.609 +     *
   4.610 +     * @return  The canonical pathname string denoting the same file or
   4.611 +     *          directory as this abstract pathname
   4.612 +     *
   4.613 +     * @throws  IOException
   4.614 +     *          If an I/O error occurs, which is possible because the
   4.615 +     *          construction of the canonical pathname may require
   4.616 +     *          filesystem queries
   4.617 +     *
   4.618 +     * @throws  SecurityException
   4.619 +     *          If a required system property value cannot be accessed, or
   4.620 +     *          if a security manager exists and its <code>{@link
   4.621 +     *          java.lang.SecurityManager#checkRead}</code> method denies
   4.622 +     *          read access to the file
   4.623 +     *
   4.624 +     * @since   JDK1.1
   4.625 +     * @see     Path#toRealPath
   4.626 +     */
   4.627 +    public String getCanonicalPath() throws IOException {
   4.628 +        throw new SecurityException();
   4.629 +    }
   4.630 +
   4.631 +    /**
   4.632 +     * Returns the canonical form of this abstract pathname.  Equivalent to
   4.633 +     * <code>new&nbsp;File(this.{@link #getCanonicalPath})</code>.
   4.634 +     *
   4.635 +     * @return  The canonical pathname string denoting the same file or
   4.636 +     *          directory as this abstract pathname
   4.637 +     *
   4.638 +     * @throws  IOException
   4.639 +     *          If an I/O error occurs, which is possible because the
   4.640 +     *          construction of the canonical pathname may require
   4.641 +     *          filesystem queries
   4.642 +     *
   4.643 +     * @throws  SecurityException
   4.644 +     *          If a required system property value cannot be accessed, or
   4.645 +     *          if a security manager exists and its <code>{@link
   4.646 +     *          java.lang.SecurityManager#checkRead}</code> method denies
   4.647 +     *          read access to the file
   4.648 +     *
   4.649 +     * @since 1.2
   4.650 +     * @see     Path#toRealPath
   4.651 +     */
   4.652 +    public File getCanonicalFile() throws IOException {
   4.653 +        String canonPath = getCanonicalPath();
   4.654 +        return new File(canonPath, fs.prefixLength(canonPath));
   4.655 +    }
   4.656 +
   4.657 +    private static String slashify(String path, boolean isDirectory) {
   4.658 +        String p = path;
   4.659 +        if (File.separatorChar != '/')
   4.660 +            p = p.replace(File.separatorChar, '/');
   4.661 +        if (!p.startsWith("/"))
   4.662 +            p = "/" + p;
   4.663 +        if (!p.endsWith("/") && isDirectory)
   4.664 +            p = p + "/";
   4.665 +        return p;
   4.666 +    }
   4.667 +
   4.668 +    /**
   4.669 +     * Converts this abstract pathname into a <code>file:</code> URL.  The
   4.670 +     * exact form of the URL is system-dependent.  If it can be determined that
   4.671 +     * the file denoted by this abstract pathname is a directory, then the
   4.672 +     * resulting URL will end with a slash.
   4.673 +     *
   4.674 +     * @return  A URL object representing the equivalent file URL
   4.675 +     *
   4.676 +     * @throws  MalformedURLException
   4.677 +     *          If the path cannot be parsed as a URL
   4.678 +     *
   4.679 +     * @see     #toURI()
   4.680 +     * @see     java.net.URI
   4.681 +     * @see     java.net.URI#toURL()
   4.682 +     * @see     java.net.URL
   4.683 +     * @since   1.2
   4.684 +     *
   4.685 +     * @deprecated This method does not automatically escape characters that
   4.686 +     * are illegal in URLs.  It is recommended that new code convert an
   4.687 +     * abstract pathname into a URL by first converting it into a URI, via the
   4.688 +     * {@link #toURI() toURI} method, and then converting the URI into a URL
   4.689 +     * via the {@link java.net.URI#toURL() URI.toURL} method.
   4.690 +     */
   4.691 +    @Deprecated
   4.692 +    public URL toURL() throws MalformedURLException {
   4.693 +        return new URL("file", "", slashify(getAbsolutePath(), isDirectory()));
   4.694 +    }
   4.695 +
   4.696 +    /**
   4.697 +     * Constructs a <tt>file:</tt> URI that represents this abstract pathname.
   4.698 +     *
   4.699 +     * <p> The exact form of the URI is system-dependent.  If it can be
   4.700 +     * determined that the file denoted by this abstract pathname is a
   4.701 +     * directory, then the resulting URI will end with a slash.
   4.702 +     *
   4.703 +     * <p> For a given abstract pathname <i>f</i>, it is guaranteed that
   4.704 +     *
   4.705 +     * <blockquote><tt>
   4.706 +     * new {@link #File(java.net.URI) File}(</tt><i>&nbsp;f</i><tt>.toURI()).equals(</tt><i>&nbsp;f</i><tt>.{@link #getAbsoluteFile() getAbsoluteFile}())
   4.707 +     * </tt></blockquote>
   4.708 +     *
   4.709 +     * so long as the original abstract pathname, the URI, and the new abstract
   4.710 +     * pathname are all created in (possibly different invocations of) the same
   4.711 +     * Java virtual machine.  Due to the system-dependent nature of abstract
   4.712 +     * pathnames, however, this relationship typically does not hold when a
   4.713 +     * <tt>file:</tt> URI that is created in a virtual machine on one operating
   4.714 +     * system is converted into an abstract pathname in a virtual machine on a
   4.715 +     * different operating system.
   4.716 +     *
   4.717 +     * <p> Note that when this abstract pathname represents a UNC pathname then
   4.718 +     * all components of the UNC (including the server name component) are encoded
   4.719 +     * in the {@code URI} path. The authority component is undefined, meaning
   4.720 +     * that it is represented as {@code null}. The {@link Path} class defines the
   4.721 +     * {@link Path#toUri toUri} method to encode the server name in the authority
   4.722 +     * component of the resulting {@code URI}. The {@link #toPath toPath} method
   4.723 +     * may be used to obtain a {@code Path} representing this abstract pathname.
   4.724 +     *
   4.725 +     * @return  An absolute, hierarchical URI with a scheme equal to
   4.726 +     *          <tt>"file"</tt>, a path representing this abstract pathname,
   4.727 +     *          and undefined authority, query, and fragment components
   4.728 +     * @throws SecurityException If a required system property value cannot
   4.729 +     * be accessed.
   4.730 +     *
   4.731 +     * @see #File(java.net.URI)
   4.732 +     * @see java.net.URI
   4.733 +     * @see java.net.URI#toURL()
   4.734 +     * @since 1.4
   4.735 +     */
   4.736 +    public URI toURI() {
   4.737 +        try {
   4.738 +            File f = getAbsoluteFile();
   4.739 +            String sp = slashify(f.getPath(), f.isDirectory());
   4.740 +            if (sp.startsWith("//"))
   4.741 +                sp = "//" + sp;
   4.742 +            return new URI("file", null, sp, null);
   4.743 +        } catch (URISyntaxException x) {
   4.744 +            throw new Error(x);         // Can't happen
   4.745 +        }
   4.746 +    }
   4.747 +
   4.748 +
   4.749 +    /* -- Attribute accessors -- */
   4.750 +
   4.751 +    /**
   4.752 +     * Tests whether the application can read the file denoted by this
   4.753 +     * abstract pathname.
   4.754 +     *
   4.755 +     * @return  <code>true</code> if and only if the file specified by this
   4.756 +     *          abstract pathname exists <em>and</em> can be read by the
   4.757 +     *          application; <code>false</code> otherwise
   4.758 +     *
   4.759 +     * @throws  SecurityException
   4.760 +     *          If a security manager exists and its <code>{@link
   4.761 +     *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
   4.762 +     *          method denies read access to the file
   4.763 +     */
   4.764 +    public boolean canRead() {
   4.765 +        throw new SecurityException();
   4.766 +    }
   4.767 +
   4.768 +    /**
   4.769 +     * Tests whether the application can modify the file denoted by this
   4.770 +     * abstract pathname.
   4.771 +     *
   4.772 +     * @return  <code>true</code> if and only if the file system actually
   4.773 +     *          contains a file denoted by this abstract pathname <em>and</em>
   4.774 +     *          the application is allowed to write to the file;
   4.775 +     *          <code>false</code> otherwise.
   4.776 +     *
   4.777 +     * @throws  SecurityException
   4.778 +     *          If a security manager exists and its <code>{@link
   4.779 +     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
   4.780 +     *          method denies write access to the file
   4.781 +     */
   4.782 +    public boolean canWrite() {
   4.783 +        throw new SecurityException();
   4.784 +    }
   4.785 +
   4.786 +    /**
   4.787 +     * Tests whether the file or directory denoted by this abstract pathname
   4.788 +     * exists.
   4.789 +     *
   4.790 +     * @return  <code>true</code> if and only if the file or directory denoted
   4.791 +     *          by this abstract pathname exists; <code>false</code> otherwise
   4.792 +     *
   4.793 +     * @throws  SecurityException
   4.794 +     *          If a security manager exists and its <code>{@link
   4.795 +     *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
   4.796 +     *          method denies read access to the file or directory
   4.797 +     */
   4.798 +    public boolean exists() {
   4.799 +        throw new SecurityException();
   4.800 +    }
   4.801 +
   4.802 +    /**
   4.803 +     * Tests whether the file denoted by this abstract pathname is a
   4.804 +     * directory.
   4.805 +     *
   4.806 +     * <p> Where it is required to distinguish an I/O exception from the case
   4.807 +     * that the file is not a directory, or where several attributes of the
   4.808 +     * same file are required at the same time, then the {@link
   4.809 +     * java.nio.file.Files#readAttributes(Path,Class,LinkOption[])
   4.810 +     * Files.readAttributes} method may be used.
   4.811 +     *
   4.812 +     * @return <code>true</code> if and only if the file denoted by this
   4.813 +     *          abstract pathname exists <em>and</em> is a directory;
   4.814 +     *          <code>false</code> otherwise
   4.815 +     *
   4.816 +     * @throws  SecurityException
   4.817 +     *          If a security manager exists and its <code>{@link
   4.818 +     *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
   4.819 +     *          method denies read access to the file
   4.820 +     */
   4.821 +    public boolean isDirectory() {
   4.822 +        throw new SecurityException();
   4.823 +    }
   4.824 +
   4.825 +    /**
   4.826 +     * Tests whether the file denoted by this abstract pathname is a normal
   4.827 +     * file.  A file is <em>normal</em> if it is not a directory and, in
   4.828 +     * addition, satisfies other system-dependent criteria.  Any non-directory
   4.829 +     * file created by a Java application is guaranteed to be a normal file.
   4.830 +     *
   4.831 +     * <p> Where it is required to distinguish an I/O exception from the case
   4.832 +     * that the file is not a normal file, or where several attributes of the
   4.833 +     * same file are required at the same time, then the {@link
   4.834 +     * java.nio.file.Files#readAttributes(Path,Class,LinkOption[])
   4.835 +     * Files.readAttributes} method may be used.
   4.836 +     *
   4.837 +     * @return  <code>true</code> if and only if the file denoted by this
   4.838 +     *          abstract pathname exists <em>and</em> is a normal file;
   4.839 +     *          <code>false</code> otherwise
   4.840 +     *
   4.841 +     * @throws  SecurityException
   4.842 +     *          If a security manager exists and its <code>{@link
   4.843 +     *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
   4.844 +     *          method denies read access to the file
   4.845 +     */
   4.846 +    public boolean isFile() {
   4.847 +        throw new SecurityException();
   4.848 +    }
   4.849 +
   4.850 +    /**
   4.851 +     * Tests whether the file named by this abstract pathname is a hidden
   4.852 +     * file.  The exact definition of <em>hidden</em> is system-dependent.  On
   4.853 +     * UNIX systems, a file is considered to be hidden if its name begins with
   4.854 +     * a period character (<code>'.'</code>).  On Microsoft Windows systems, a file is
   4.855 +     * considered to be hidden if it has been marked as such in the filesystem.
   4.856 +     *
   4.857 +     * @return  <code>true</code> if and only if the file denoted by this
   4.858 +     *          abstract pathname is hidden according to the conventions of the
   4.859 +     *          underlying platform
   4.860 +     *
   4.861 +     * @throws  SecurityException
   4.862 +     *          If a security manager exists and its <code>{@link
   4.863 +     *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
   4.864 +     *          method denies read access to the file
   4.865 +     *
   4.866 +     * @since 1.2
   4.867 +     */
   4.868 +    public boolean isHidden() {
   4.869 +        throw new SecurityException();
   4.870 +    }
   4.871 +
   4.872 +    /**
   4.873 +     * Returns the time that the file denoted by this abstract pathname was
   4.874 +     * last modified.
   4.875 +     *
   4.876 +     * <p> Where it is required to distinguish an I/O exception from the case
   4.877 +     * where {@code 0L} is returned, or where several attributes of the
   4.878 +     * same file are required at the same time, or where the time of last
   4.879 +     * access or the creation time are required, then the {@link
   4.880 +     * java.nio.file.Files#readAttributes(Path,Class,LinkOption[])
   4.881 +     * Files.readAttributes} method may be used.
   4.882 +     *
   4.883 +     * @return  A <code>long</code> value representing the time the file was
   4.884 +     *          last modified, measured in milliseconds since the epoch
   4.885 +     *          (00:00:00 GMT, January 1, 1970), or <code>0L</code> if the
   4.886 +     *          file does not exist or if an I/O error occurs
   4.887 +     *
   4.888 +     * @throws  SecurityException
   4.889 +     *          If a security manager exists and its <code>{@link
   4.890 +     *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
   4.891 +     *          method denies read access to the file
   4.892 +     */
   4.893 +    public long lastModified() {
   4.894 +        throw new SecurityException();
   4.895 +    }
   4.896 +
   4.897 +    /**
   4.898 +     * Returns the length of the file denoted by this abstract pathname.
   4.899 +     * The return value is unspecified if this pathname denotes a directory.
   4.900 +     *
   4.901 +     * <p> Where it is required to distinguish an I/O exception from the case
   4.902 +     * that {@code 0L} is returned, or where several attributes of the same file
   4.903 +     * are required at the same time, then the {@link
   4.904 +     * java.nio.file.Files#readAttributes(Path,Class,LinkOption[])
   4.905 +     * Files.readAttributes} method may be used.
   4.906 +     *
   4.907 +     * @return  The length, in bytes, of the file denoted by this abstract
   4.908 +     *          pathname, or <code>0L</code> if the file does not exist.  Some
   4.909 +     *          operating systems may return <code>0L</code> for pathnames
   4.910 +     *          denoting system-dependent entities such as devices or pipes.
   4.911 +     *
   4.912 +     * @throws  SecurityException
   4.913 +     *          If a security manager exists and its <code>{@link
   4.914 +     *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
   4.915 +     *          method denies read access to the file
   4.916 +     */
   4.917 +    public long length() {
   4.918 +        throw new SecurityException();
   4.919 +    }
   4.920 +
   4.921 +
   4.922 +    /* -- File operations -- */
   4.923 +
   4.924 +    /**
   4.925 +     * Atomically creates a new, empty file named by this abstract pathname if
   4.926 +     * and only if a file with this name does not yet exist.  The check for the
   4.927 +     * existence of the file and the creation of the file if it does not exist
   4.928 +     * are a single operation that is atomic with respect to all other
   4.929 +     * filesystem activities that might affect the file.
   4.930 +     * <P>
   4.931 +     * Note: this method should <i>not</i> be used for file-locking, as
   4.932 +     * the resulting protocol cannot be made to work reliably. The
   4.933 +     * {@link java.nio.channels.FileLock FileLock}
   4.934 +     * facility should be used instead.
   4.935 +     *
   4.936 +     * @return  <code>true</code> if the named file does not exist and was
   4.937 +     *          successfully created; <code>false</code> if the named file
   4.938 +     *          already exists
   4.939 +     *
   4.940 +     * @throws  IOException
   4.941 +     *          If an I/O error occurred
   4.942 +     *
   4.943 +     * @throws  SecurityException
   4.944 +     *          If a security manager exists and its <code>{@link
   4.945 +     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
   4.946 +     *          method denies write access to the file
   4.947 +     *
   4.948 +     * @since 1.2
   4.949 +     */
   4.950 +    public boolean createNewFile() throws IOException {
   4.951 +        throw new SecurityException();
   4.952 +    }
   4.953 +
   4.954 +    /**
   4.955 +     * Deletes the file or directory denoted by this abstract pathname.  If
   4.956 +     * this pathname denotes a directory, then the directory must be empty in
   4.957 +     * order to be deleted.
   4.958 +     *
   4.959 +     * <p> Note that the {@link java.nio.file.Files} class defines the {@link
   4.960 +     * java.nio.file.Files#delete(Path) delete} method to throw an {@link IOException}
   4.961 +     * when a file cannot be deleted. This is useful for error reporting and to
   4.962 +     * diagnose why a file cannot be deleted.
   4.963 +     *
   4.964 +     * @return  <code>true</code> if and only if the file or directory is
   4.965 +     *          successfully deleted; <code>false</code> otherwise
   4.966 +     *
   4.967 +     * @throws  SecurityException
   4.968 +     *          If a security manager exists and its <code>{@link
   4.969 +     *          java.lang.SecurityManager#checkDelete}</code> method denies
   4.970 +     *          delete access to the file
   4.971 +     */
   4.972 +    public boolean delete() {
   4.973 +        throw new SecurityException();
   4.974 +    }
   4.975 +
   4.976 +    /**
   4.977 +     * Requests that the file or directory denoted by this abstract
   4.978 +     * pathname be deleted when the virtual machine terminates.
   4.979 +     * Files (or directories) are deleted in the reverse order that
   4.980 +     * they are registered. Invoking this method to delete a file or
   4.981 +     * directory that is already registered for deletion has no effect.
   4.982 +     * Deletion will be attempted only for normal termination of the
   4.983 +     * virtual machine, as defined by the Java Language Specification.
   4.984 +     *
   4.985 +     * <p> Once deletion has been requested, it is not possible to cancel the
   4.986 +     * request.  This method should therefore be used with care.
   4.987 +     *
   4.988 +     * <P>
   4.989 +     * Note: this method should <i>not</i> be used for file-locking, as
   4.990 +     * the resulting protocol cannot be made to work reliably. The
   4.991 +     * {@link java.nio.channels.FileLock FileLock}
   4.992 +     * facility should be used instead.
   4.993 +     *
   4.994 +     * @throws  SecurityException
   4.995 +     *          If a security manager exists and its <code>{@link
   4.996 +     *          java.lang.SecurityManager#checkDelete}</code> method denies
   4.997 +     *          delete access to the file
   4.998 +     *
   4.999 +     * @see #delete
  4.1000 +     *
  4.1001 +     * @since 1.2
  4.1002 +     */
  4.1003 +    public void deleteOnExit() {
  4.1004 +        throw new SecurityException();
  4.1005 +    }
  4.1006 +
  4.1007 +    /**
  4.1008 +     * Returns an array of strings naming the files and directories in the
  4.1009 +     * directory denoted by this abstract pathname.
  4.1010 +     *
  4.1011 +     * <p> If this abstract pathname does not denote a directory, then this
  4.1012 +     * method returns {@code null}.  Otherwise an array of strings is
  4.1013 +     * returned, one for each file or directory in the directory.  Names
  4.1014 +     * denoting the directory itself and the directory's parent directory are
  4.1015 +     * not included in the result.  Each string is a file name rather than a
  4.1016 +     * complete path.
  4.1017 +     *
  4.1018 +     * <p> There is no guarantee that the name strings in the resulting array
  4.1019 +     * will appear in any specific order; they are not, in particular,
  4.1020 +     * guaranteed to appear in alphabetical order.
  4.1021 +     *
  4.1022 +     * <p> Note that the {@link java.nio.file.Files} class defines the {@link
  4.1023 +     * java.nio.file.Files#newDirectoryStream(Path) newDirectoryStream} method to
  4.1024 +     * open a directory and iterate over the names of the files in the directory.
  4.1025 +     * This may use less resources when working with very large directories, and
  4.1026 +     * may be more responsive when working with remote directories.
  4.1027 +     *
  4.1028 +     * @return  An array of strings naming the files and directories in the
  4.1029 +     *          directory denoted by this abstract pathname.  The array will be
  4.1030 +     *          empty if the directory is empty.  Returns {@code null} if
  4.1031 +     *          this abstract pathname does not denote a directory, or if an
  4.1032 +     *          I/O error occurs.
  4.1033 +     *
  4.1034 +     * @throws  SecurityException
  4.1035 +     *          If a security manager exists and its {@link
  4.1036 +     *          SecurityManager#checkRead(String)} method denies read access to
  4.1037 +     *          the directory
  4.1038 +     */
  4.1039 +    public String[] list() {
  4.1040 +        throw new SecurityException();
  4.1041 +    }
  4.1042 +
  4.1043 +    /**
  4.1044 +     * Returns an array of strings naming the files and directories in the
  4.1045 +     * directory denoted by this abstract pathname that satisfy the specified
  4.1046 +     * filter.  The behavior of this method is the same as that of the
  4.1047 +     * {@link #list()} method, except that the strings in the returned array
  4.1048 +     * must satisfy the filter.  If the given {@code filter} is {@code null}
  4.1049 +     * then all names are accepted.  Otherwise, a name satisfies the filter if
  4.1050 +     * and only if the value {@code true} results when the {@link
  4.1051 +     * FilenameFilter#accept FilenameFilter.accept(File,&nbsp;String)} method
  4.1052 +     * of the filter is invoked on this abstract pathname and the name of a
  4.1053 +     * file or directory in the directory that it denotes.
  4.1054 +     *
  4.1055 +     * @param  filter
  4.1056 +     *         A filename filter
  4.1057 +     *
  4.1058 +     * @return  An array of strings naming the files and directories in the
  4.1059 +     *          directory denoted by this abstract pathname that were accepted
  4.1060 +     *          by the given {@code filter}.  The array will be empty if the
  4.1061 +     *          directory is empty or if no names were accepted by the filter.
  4.1062 +     *          Returns {@code null} if this abstract pathname does not denote
  4.1063 +     *          a directory, or if an I/O error occurs.
  4.1064 +     *
  4.1065 +     * @throws  SecurityException
  4.1066 +     *          If a security manager exists and its {@link
  4.1067 +     *          SecurityManager#checkRead(String)} method denies read access to
  4.1068 +     *          the directory
  4.1069 +     *
  4.1070 +     * @see java.nio.file.Files#newDirectoryStream(Path,String)
  4.1071 +     */
  4.1072 +    public String[] list(FilenameFilter filter) {
  4.1073 +        throw new SecurityException();
  4.1074 +    }
  4.1075 +
  4.1076 +    /**
  4.1077 +     * Returns an array of abstract pathnames denoting the files in the
  4.1078 +     * directory denoted by this abstract pathname.
  4.1079 +     *
  4.1080 +     * <p> If this abstract pathname does not denote a directory, then this
  4.1081 +     * method returns {@code null}.  Otherwise an array of {@code File} objects
  4.1082 +     * is returned, one for each file or directory in the directory.  Pathnames
  4.1083 +     * denoting the directory itself and the directory's parent directory are
  4.1084 +     * not included in the result.  Each resulting abstract pathname is
  4.1085 +     * constructed from this abstract pathname using the {@link #File(File,
  4.1086 +     * String) File(File,&nbsp;String)} constructor.  Therefore if this
  4.1087 +     * pathname is absolute then each resulting pathname is absolute; if this
  4.1088 +     * pathname is relative then each resulting pathname will be relative to
  4.1089 +     * the same directory.
  4.1090 +     *
  4.1091 +     * <p> There is no guarantee that the name strings in the resulting array
  4.1092 +     * will appear in any specific order; they are not, in particular,
  4.1093 +     * guaranteed to appear in alphabetical order.
  4.1094 +     *
  4.1095 +     * <p> Note that the {@link java.nio.file.Files} class defines the {@link
  4.1096 +     * java.nio.file.Files#newDirectoryStream(Path) newDirectoryStream} method
  4.1097 +     * to open a directory and iterate over the names of the files in the
  4.1098 +     * directory. This may use less resources when working with very large
  4.1099 +     * directories.
  4.1100 +     *
  4.1101 +     * @return  An array of abstract pathnames denoting the files and
  4.1102 +     *          directories in the directory denoted by this abstract pathname.
  4.1103 +     *          The array will be empty if the directory is empty.  Returns
  4.1104 +     *          {@code null} if this abstract pathname does not denote a
  4.1105 +     *          directory, or if an I/O error occurs.
  4.1106 +     *
  4.1107 +     * @throws  SecurityException
  4.1108 +     *          If a security manager exists and its {@link
  4.1109 +     *          SecurityManager#checkRead(String)} method denies read access to
  4.1110 +     *          the directory
  4.1111 +     *
  4.1112 +     * @since  1.2
  4.1113 +     */
  4.1114 +    public File[] listFiles() {
  4.1115 +        throw new SecurityException();
  4.1116 +    }
  4.1117 +
  4.1118 +    /**
  4.1119 +     * Returns an array of abstract pathnames denoting the files and
  4.1120 +     * directories in the directory denoted by this abstract pathname that
  4.1121 +     * satisfy the specified filter.  The behavior of this method is the same
  4.1122 +     * as that of the {@link #listFiles()} method, except that the pathnames in
  4.1123 +     * the returned array must satisfy the filter.  If the given {@code filter}
  4.1124 +     * is {@code null} then all pathnames are accepted.  Otherwise, a pathname
  4.1125 +     * satisfies the filter if and only if the value {@code true} results when
  4.1126 +     * the {@link FilenameFilter#accept
  4.1127 +     * FilenameFilter.accept(File,&nbsp;String)} method of the filter is
  4.1128 +     * invoked on this abstract pathname and the name of a file or directory in
  4.1129 +     * the directory that it denotes.
  4.1130 +     *
  4.1131 +     * @param  filter
  4.1132 +     *         A filename filter
  4.1133 +     *
  4.1134 +     * @return  An array of abstract pathnames denoting the files and
  4.1135 +     *          directories in the directory denoted by this abstract pathname.
  4.1136 +     *          The array will be empty if the directory is empty.  Returns
  4.1137 +     *          {@code null} if this abstract pathname does not denote a
  4.1138 +     *          directory, or if an I/O error occurs.
  4.1139 +     *
  4.1140 +     * @throws  SecurityException
  4.1141 +     *          If a security manager exists and its {@link
  4.1142 +     *          SecurityManager#checkRead(String)} method denies read access to
  4.1143 +     *          the directory
  4.1144 +     *
  4.1145 +     * @since  1.2
  4.1146 +     * @see java.nio.file.Files#newDirectoryStream(Path,String)
  4.1147 +     */
  4.1148 +    public File[] listFiles(FilenameFilter filter) {
  4.1149 +        throw new SecurityException();
  4.1150 +    }
  4.1151 +
  4.1152 +    /**
  4.1153 +     * Returns an array of abstract pathnames denoting the files and
  4.1154 +     * directories in the directory denoted by this abstract pathname that
  4.1155 +     * satisfy the specified filter.  The behavior of this method is the same
  4.1156 +     * as that of the {@link #listFiles()} method, except that the pathnames in
  4.1157 +     * the returned array must satisfy the filter.  If the given {@code filter}
  4.1158 +     * is {@code null} then all pathnames are accepted.  Otherwise, a pathname
  4.1159 +     * satisfies the filter if and only if the value {@code true} results when
  4.1160 +     * the {@link FileFilter#accept FileFilter.accept(File)} method of the
  4.1161 +     * filter is invoked on the pathname.
  4.1162 +     *
  4.1163 +     * @param  filter
  4.1164 +     *         A file filter
  4.1165 +     *
  4.1166 +     * @return  An array of abstract pathnames denoting the files and
  4.1167 +     *          directories in the directory denoted by this abstract pathname.
  4.1168 +     *          The array will be empty if the directory is empty.  Returns
  4.1169 +     *          {@code null} if this abstract pathname does not denote a
  4.1170 +     *          directory, or if an I/O error occurs.
  4.1171 +     *
  4.1172 +     * @throws  SecurityException
  4.1173 +     *          If a security manager exists and its {@link
  4.1174 +     *          SecurityManager#checkRead(String)} method denies read access to
  4.1175 +     *          the directory
  4.1176 +     *
  4.1177 +     * @since  1.2
  4.1178 +     * @see java.nio.file.Files#newDirectoryStream(Path,java.nio.file.DirectoryStream.Filter)
  4.1179 +     */
  4.1180 +    public File[] listFiles(FileFilter filter) {
  4.1181 +        throw new SecurityException();
  4.1182 +    }
  4.1183 +
  4.1184 +    /**
  4.1185 +     * Creates the directory named by this abstract pathname.
  4.1186 +     *
  4.1187 +     * @return  <code>true</code> if and only if the directory was
  4.1188 +     *          created; <code>false</code> otherwise
  4.1189 +     *
  4.1190 +     * @throws  SecurityException
  4.1191 +     *          If a security manager exists and its <code>{@link
  4.1192 +     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
  4.1193 +     *          method does not permit the named directory to be created
  4.1194 +     */
  4.1195 +    public boolean mkdir() {
  4.1196 +        throw new SecurityException();
  4.1197 +    }
  4.1198 +
  4.1199 +    /**
  4.1200 +     * Creates the directory named by this abstract pathname, including any
  4.1201 +     * necessary but nonexistent parent directories.  Note that if this
  4.1202 +     * operation fails it may have succeeded in creating some of the necessary
  4.1203 +     * parent directories.
  4.1204 +     *
  4.1205 +     * @return  <code>true</code> if and only if the directory was created,
  4.1206 +     *          along with all necessary parent directories; <code>false</code>
  4.1207 +     *          otherwise
  4.1208 +     *
  4.1209 +     * @throws  SecurityException
  4.1210 +     *          If a security manager exists and its <code>{@link
  4.1211 +     *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
  4.1212 +     *          method does not permit verification of the existence of the
  4.1213 +     *          named directory and all necessary parent directories; or if
  4.1214 +     *          the <code>{@link
  4.1215 +     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
  4.1216 +     *          method does not permit the named directory and all necessary
  4.1217 +     *          parent directories to be created
  4.1218 +     */
  4.1219 +    public boolean mkdirs() {
  4.1220 +        throw new SecurityException();
  4.1221 +    }
  4.1222 +
  4.1223 +    /**
  4.1224 +     * Renames the file denoted by this abstract pathname.
  4.1225 +     *
  4.1226 +     * <p> Many aspects of the behavior of this method are inherently
  4.1227 +     * platform-dependent: The rename operation might not be able to move a
  4.1228 +     * file from one filesystem to another, it might not be atomic, and it
  4.1229 +     * might not succeed if a file with the destination abstract pathname
  4.1230 +     * already exists.  The return value should always be checked to make sure
  4.1231 +     * that the rename operation was successful.
  4.1232 +     *
  4.1233 +     * <p> Note that the {@link java.nio.file.Files} class defines the {@link
  4.1234 +     * java.nio.file.Files#move move} method to move or rename a file in a
  4.1235 +     * platform independent manner.
  4.1236 +     *
  4.1237 +     * @param  dest  The new abstract pathname for the named file
  4.1238 +     *
  4.1239 +     * @return  <code>true</code> if and only if the renaming succeeded;
  4.1240 +     *          <code>false</code> otherwise
  4.1241 +     *
  4.1242 +     * @throws  SecurityException
  4.1243 +     *          If a security manager exists and its <code>{@link
  4.1244 +     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
  4.1245 +     *          method denies write access to either the old or new pathnames
  4.1246 +     *
  4.1247 +     * @throws  NullPointerException
  4.1248 +     *          If parameter <code>dest</code> is <code>null</code>
  4.1249 +     */
  4.1250 +    public boolean renameTo(File dest) {
  4.1251 +        throw new SecurityException();
  4.1252 +    }
  4.1253 +
  4.1254 +    /**
  4.1255 +     * Sets the last-modified time of the file or directory named by this
  4.1256 +     * abstract pathname.
  4.1257 +     *
  4.1258 +     * <p> All platforms support file-modification times to the nearest second,
  4.1259 +     * but some provide more precision.  The argument will be truncated to fit
  4.1260 +     * the supported precision.  If the operation succeeds and no intervening
  4.1261 +     * operations on the file take place, then the next invocation of the
  4.1262 +     * <code>{@link #lastModified}</code> method will return the (possibly
  4.1263 +     * truncated) <code>time</code> argument that was passed to this method.
  4.1264 +     *
  4.1265 +     * @param  time  The new last-modified time, measured in milliseconds since
  4.1266 +     *               the epoch (00:00:00 GMT, January 1, 1970)
  4.1267 +     *
  4.1268 +     * @return <code>true</code> if and only if the operation succeeded;
  4.1269 +     *          <code>false</code> otherwise
  4.1270 +     *
  4.1271 +     * @throws  IllegalArgumentException  If the argument is negative
  4.1272 +     *
  4.1273 +     * @throws  SecurityException
  4.1274 +     *          If a security manager exists and its <code>{@link
  4.1275 +     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
  4.1276 +     *          method denies write access to the named file
  4.1277 +     *
  4.1278 +     * @since 1.2
  4.1279 +     */
  4.1280 +    public boolean setLastModified(long time) {
  4.1281 +        throw new SecurityException();
  4.1282 +    }
  4.1283 +
  4.1284 +    /**
  4.1285 +     * Marks the file or directory named by this abstract pathname so that
  4.1286 +     * only read operations are allowed.  After invoking this method the file
  4.1287 +     * or directory is guaranteed not to change until it is either deleted or
  4.1288 +     * marked to allow write access.  Whether or not a read-only file or
  4.1289 +     * directory may be deleted depends upon the underlying system.
  4.1290 +     *
  4.1291 +     * @return <code>true</code> if and only if the operation succeeded;
  4.1292 +     *          <code>false</code> otherwise
  4.1293 +     *
  4.1294 +     * @throws  SecurityException
  4.1295 +     *          If a security manager exists and its <code>{@link
  4.1296 +     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
  4.1297 +     *          method denies write access to the named file
  4.1298 +     *
  4.1299 +     * @since 1.2
  4.1300 +     */
  4.1301 +    public boolean setReadOnly() {
  4.1302 +        throw new SecurityException();
  4.1303 +    }
  4.1304 +
  4.1305 +    /**
  4.1306 +     * Sets the owner's or everybody's write permission for this abstract
  4.1307 +     * pathname.
  4.1308 +     *
  4.1309 +     * <p> The {@link java.nio.file.Files} class defines methods that operate on
  4.1310 +     * file attributes including file permissions. This may be used when finer
  4.1311 +     * manipulation of file permissions is required.
  4.1312 +     *
  4.1313 +     * @param   writable
  4.1314 +     *          If <code>true</code>, sets the access permission to allow write
  4.1315 +     *          operations; if <code>false</code> to disallow write operations
  4.1316 +     *
  4.1317 +     * @param   ownerOnly
  4.1318 +     *          If <code>true</code>, the write permission applies only to the
  4.1319 +     *          owner's write permission; otherwise, it applies to everybody.  If
  4.1320 +     *          the underlying file system can not distinguish the owner's write
  4.1321 +     *          permission from that of others, then the permission will apply to
  4.1322 +     *          everybody, regardless of this value.
  4.1323 +     *
  4.1324 +     * @return  <code>true</code> if and only if the operation succeeded. The
  4.1325 +     *          operation will fail if the user does not have permission to change
  4.1326 +     *          the access permissions of this abstract pathname.
  4.1327 +     *
  4.1328 +     * @throws  SecurityException
  4.1329 +     *          If a security manager exists and its <code>{@link
  4.1330 +     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
  4.1331 +     *          method denies write access to the named file
  4.1332 +     *
  4.1333 +     * @since 1.6
  4.1334 +     */
  4.1335 +    public boolean setWritable(boolean writable, boolean ownerOnly) {
  4.1336 +        throw new SecurityException();
  4.1337 +    }
  4.1338 +
  4.1339 +    /**
  4.1340 +     * A convenience method to set the owner's write permission for this abstract
  4.1341 +     * pathname.
  4.1342 +     *
  4.1343 +     * <p> An invocation of this method of the form <tt>file.setWritable(arg)</tt>
  4.1344 +     * behaves in exactly the same way as the invocation
  4.1345 +     *
  4.1346 +     * <pre>
  4.1347 +     *     file.setWritable(arg, true) </pre>
  4.1348 +     *
  4.1349 +     * @param   writable
  4.1350 +     *          If <code>true</code>, sets the access permission to allow write
  4.1351 +     *          operations; if <code>false</code> to disallow write operations
  4.1352 +     *
  4.1353 +     * @return  <code>true</code> if and only if the operation succeeded.  The
  4.1354 +     *          operation will fail if the user does not have permission to
  4.1355 +     *          change the access permissions of this abstract pathname.
  4.1356 +     *
  4.1357 +     * @throws  SecurityException
  4.1358 +     *          If a security manager exists and its <code>{@link
  4.1359 +     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
  4.1360 +     *          method denies write access to the file
  4.1361 +     *
  4.1362 +     * @since 1.6
  4.1363 +     */
  4.1364 +    public boolean setWritable(boolean writable) {
  4.1365 +        return setWritable(writable, true);
  4.1366 +    }
  4.1367 +
  4.1368 +    /**
  4.1369 +     * Sets the owner's or everybody's read permission for this abstract
  4.1370 +     * pathname.
  4.1371 +     *
  4.1372 +     * <p> The {@link java.nio.file.Files} class defines methods that operate on
  4.1373 +     * file attributes including file permissions. This may be used when finer
  4.1374 +     * manipulation of file permissions is required.
  4.1375 +     *
  4.1376 +     * @param   readable
  4.1377 +     *          If <code>true</code>, sets the access permission to allow read
  4.1378 +     *          operations; if <code>false</code> to disallow read operations
  4.1379 +     *
  4.1380 +     * @param   ownerOnly
  4.1381 +     *          If <code>true</code>, the read permission applies only to the
  4.1382 +     *          owner's read permission; otherwise, it applies to everybody.  If
  4.1383 +     *          the underlying file system can not distinguish the owner's read
  4.1384 +     *          permission from that of others, then the permission will apply to
  4.1385 +     *          everybody, regardless of this value.
  4.1386 +     *
  4.1387 +     * @return  <code>true</code> if and only if the operation succeeded.  The
  4.1388 +     *          operation will fail if the user does not have permission to
  4.1389 +     *          change the access permissions of this abstract pathname.  If
  4.1390 +     *          <code>readable</code> is <code>false</code> and the underlying
  4.1391 +     *          file system does not implement a read permission, then the
  4.1392 +     *          operation will fail.
  4.1393 +     *
  4.1394 +     * @throws  SecurityException
  4.1395 +     *          If a security manager exists and its <code>{@link
  4.1396 +     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
  4.1397 +     *          method denies write access to the file
  4.1398 +     *
  4.1399 +     * @since 1.6
  4.1400 +     */
  4.1401 +    public boolean setReadable(boolean readable, boolean ownerOnly) {
  4.1402 +        throw new SecurityException();
  4.1403 +    }
  4.1404 +
  4.1405 +    /**
  4.1406 +     * A convenience method to set the owner's read permission for this abstract
  4.1407 +     * pathname.
  4.1408 +     *
  4.1409 +     * <p>An invocation of this method of the form <tt>file.setReadable(arg)</tt>
  4.1410 +     * behaves in exactly the same way as the invocation
  4.1411 +     *
  4.1412 +     * <pre>
  4.1413 +     *     file.setReadable(arg, true) </pre>
  4.1414 +     *
  4.1415 +     * @param  readable
  4.1416 +     *          If <code>true</code>, sets the access permission to allow read
  4.1417 +     *          operations; if <code>false</code> to disallow read operations
  4.1418 +     *
  4.1419 +     * @return  <code>true</code> if and only if the operation succeeded.  The
  4.1420 +     *          operation will fail if the user does not have permission to
  4.1421 +     *          change the access permissions of this abstract pathname.  If
  4.1422 +     *          <code>readable</code> is <code>false</code> and the underlying
  4.1423 +     *          file system does not implement a read permission, then the
  4.1424 +     *          operation will fail.
  4.1425 +     *
  4.1426 +     * @throws  SecurityException
  4.1427 +     *          If a security manager exists and its <code>{@link
  4.1428 +     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
  4.1429 +     *          method denies write access to the file
  4.1430 +     *
  4.1431 +     * @since 1.6
  4.1432 +     */
  4.1433 +    public boolean setReadable(boolean readable) {
  4.1434 +        return setReadable(readable, true);
  4.1435 +    }
  4.1436 +
  4.1437 +    /**
  4.1438 +     * Sets the owner's or everybody's execute permission for this abstract
  4.1439 +     * pathname.
  4.1440 +     *
  4.1441 +     * <p> The {@link java.nio.file.Files} class defines methods that operate on
  4.1442 +     * file attributes including file permissions. This may be used when finer
  4.1443 +     * manipulation of file permissions is required.
  4.1444 +     *
  4.1445 +     * @param   executable
  4.1446 +     *          If <code>true</code>, sets the access permission to allow execute
  4.1447 +     *          operations; if <code>false</code> to disallow execute operations
  4.1448 +     *
  4.1449 +     * @param   ownerOnly
  4.1450 +     *          If <code>true</code>, the execute permission applies only to the
  4.1451 +     *          owner's execute permission; otherwise, it applies to everybody.
  4.1452 +     *          If the underlying file system can not distinguish the owner's
  4.1453 +     *          execute permission from that of others, then the permission will
  4.1454 +     *          apply to everybody, regardless of this value.
  4.1455 +     *
  4.1456 +     * @return  <code>true</code> if and only if the operation succeeded.  The
  4.1457 +     *          operation will fail if the user does not have permission to
  4.1458 +     *          change the access permissions of this abstract pathname.  If
  4.1459 +     *          <code>executable</code> is <code>false</code> and the underlying
  4.1460 +     *          file system does not implement an execute permission, then the
  4.1461 +     *          operation will fail.
  4.1462 +     *
  4.1463 +     * @throws  SecurityException
  4.1464 +     *          If a security manager exists and its <code>{@link
  4.1465 +     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
  4.1466 +     *          method denies write access to the file
  4.1467 +     *
  4.1468 +     * @since 1.6
  4.1469 +     */
  4.1470 +    public boolean setExecutable(boolean executable, boolean ownerOnly) {
  4.1471 +        throw new SecurityException();
  4.1472 +    }
  4.1473 +
  4.1474 +    /**
  4.1475 +     * A convenience method to set the owner's execute permission for this abstract
  4.1476 +     * pathname.
  4.1477 +     *
  4.1478 +     * <p>An invocation of this method of the form <tt>file.setExcutable(arg)</tt>
  4.1479 +     * behaves in exactly the same way as the invocation
  4.1480 +     *
  4.1481 +     * <pre>
  4.1482 +     *     file.setExecutable(arg, true) </pre>
  4.1483 +     *
  4.1484 +     * @param   executable
  4.1485 +     *          If <code>true</code>, sets the access permission to allow execute
  4.1486 +     *          operations; if <code>false</code> to disallow execute operations
  4.1487 +     *
  4.1488 +     * @return   <code>true</code> if and only if the operation succeeded.  The
  4.1489 +     *           operation will fail if the user does not have permission to
  4.1490 +     *           change the access permissions of this abstract pathname.  If
  4.1491 +     *           <code>executable</code> is <code>false</code> and the underlying
  4.1492 +     *           file system does not implement an excute permission, then the
  4.1493 +     *           operation will fail.
  4.1494 +     *
  4.1495 +     * @throws  SecurityException
  4.1496 +     *          If a security manager exists and its <code>{@link
  4.1497 +     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
  4.1498 +     *          method denies write access to the file
  4.1499 +     *
  4.1500 +     * @since 1.6
  4.1501 +     */
  4.1502 +    public boolean setExecutable(boolean executable) {
  4.1503 +        return setExecutable(executable, true);
  4.1504 +    }
  4.1505 +
  4.1506 +    /**
  4.1507 +     * Tests whether the application can execute the file denoted by this
  4.1508 +     * abstract pathname.
  4.1509 +     *
  4.1510 +     * @return  <code>true</code> if and only if the abstract pathname exists
  4.1511 +     *          <em>and</em> the application is allowed to execute the file
  4.1512 +     *
  4.1513 +     * @throws  SecurityException
  4.1514 +     *          If a security manager exists and its <code>{@link
  4.1515 +     *          java.lang.SecurityManager#checkExec(java.lang.String)}</code>
  4.1516 +     *          method denies execute access to the file
  4.1517 +     *
  4.1518 +     * @since 1.6
  4.1519 +     */
  4.1520 +    public boolean canExecute() {
  4.1521 +        throw new SecurityException();
  4.1522 +    }
  4.1523 +
  4.1524 +
  4.1525 +    /* -- Filesystem interface -- */
  4.1526 +
  4.1527 +    /**
  4.1528 +     * List the available filesystem roots.
  4.1529 +     *
  4.1530 +     * <p> A particular Java platform may support zero or more
  4.1531 +     * hierarchically-organized file systems.  Each file system has a
  4.1532 +     * {@code root} directory from which all other files in that file system
  4.1533 +     * can be reached.  Windows platforms, for example, have a root directory
  4.1534 +     * for each active drive; UNIX platforms have a single root directory,
  4.1535 +     * namely {@code "/"}.  The set of available filesystem roots is affected
  4.1536 +     * by various system-level operations such as the insertion or ejection of
  4.1537 +     * removable media and the disconnecting or unmounting of physical or
  4.1538 +     * virtual disk drives.
  4.1539 +     *
  4.1540 +     * <p> This method returns an array of {@code File} objects that denote the
  4.1541 +     * root directories of the available filesystem roots.  It is guaranteed
  4.1542 +     * that the canonical pathname of any file physically present on the local
  4.1543 +     * machine will begin with one of the roots returned by this method.
  4.1544 +     *
  4.1545 +     * <p> The canonical pathname of a file that resides on some other machine
  4.1546 +     * and is accessed via a remote-filesystem protocol such as SMB or NFS may
  4.1547 +     * or may not begin with one of the roots returned by this method.  If the
  4.1548 +     * pathname of a remote file is syntactically indistinguishable from the
  4.1549 +     * pathname of a local file then it will begin with one of the roots
  4.1550 +     * returned by this method.  Thus, for example, {@code File} objects
  4.1551 +     * denoting the root directories of the mapped network drives of a Windows
  4.1552 +     * platform will be returned by this method, while {@code File} objects
  4.1553 +     * containing UNC pathnames will not be returned by this method.
  4.1554 +     *
  4.1555 +     * <p> Unlike most methods in this class, this method does not throw
  4.1556 +     * security exceptions.  If a security manager exists and its {@link
  4.1557 +     * SecurityManager#checkRead(String)} method denies read access to a
  4.1558 +     * particular root directory, then that directory will not appear in the
  4.1559 +     * result.
  4.1560 +     *
  4.1561 +     * @return  An array of {@code File} objects denoting the available
  4.1562 +     *          filesystem roots, or {@code null} if the set of roots could not
  4.1563 +     *          be determined.  The array will be empty if there are no
  4.1564 +     *          filesystem roots.
  4.1565 +     *
  4.1566 +     * @since  1.2
  4.1567 +     * @see java.nio.file.FileStore
  4.1568 +     */
  4.1569 +    public static File[] listRoots() {
  4.1570 +        throw new SecurityException();
  4.1571 +    }
  4.1572 +
  4.1573 +
  4.1574 +    /* -- Disk usage -- */
  4.1575 +
  4.1576 +    /**
  4.1577 +     * Returns the size of the partition <a href="#partName">named</a> by this
  4.1578 +     * abstract pathname.
  4.1579 +     *
  4.1580 +     * @return  The size, in bytes, of the partition or <tt>0L</tt> if this
  4.1581 +     *          abstract pathname does not name a partition
  4.1582 +     *
  4.1583 +     * @throws  SecurityException
  4.1584 +     *          If a security manager has been installed and it denies
  4.1585 +     *          {@link RuntimePermission}<tt>("getFileSystemAttributes")</tt>
  4.1586 +     *          or its {@link SecurityManager#checkRead(String)} method denies
  4.1587 +     *          read access to the file named by this abstract pathname
  4.1588 +     *
  4.1589 +     * @since  1.6
  4.1590 +     */
  4.1591 +    public long getTotalSpace() {
  4.1592 +        throw new SecurityException();
  4.1593 +    }
  4.1594 +
  4.1595 +    /**
  4.1596 +     * Returns the number of unallocated bytes in the partition <a
  4.1597 +     * href="#partName">named</a> by this abstract path name.
  4.1598 +     *
  4.1599 +     * <p> The returned number of unallocated bytes is a hint, but not
  4.1600 +     * a guarantee, that it is possible to use most or any of these
  4.1601 +     * bytes.  The number of unallocated bytes is most likely to be
  4.1602 +     * accurate immediately after this call.  It is likely to be made
  4.1603 +     * inaccurate by any external I/O operations including those made
  4.1604 +     * on the system outside of this virtual machine.  This method
  4.1605 +     * makes no guarantee that write operations to this file system
  4.1606 +     * will succeed.
  4.1607 +     *
  4.1608 +     * @return  The number of unallocated bytes on the partition <tt>0L</tt>
  4.1609 +     *          if the abstract pathname does not name a partition.  This
  4.1610 +     *          value will be less than or equal to the total file system size
  4.1611 +     *          returned by {@link #getTotalSpace}.
  4.1612 +     *
  4.1613 +     * @throws  SecurityException
  4.1614 +     *          If a security manager has been installed and it denies
  4.1615 +     *          {@link RuntimePermission}<tt>("getFileSystemAttributes")</tt>
  4.1616 +     *          or its {@link SecurityManager#checkRead(String)} method denies
  4.1617 +     *          read access to the file named by this abstract pathname
  4.1618 +     *
  4.1619 +     * @since  1.6
  4.1620 +     */
  4.1621 +    public long getFreeSpace() {
  4.1622 +        throw new SecurityException();
  4.1623 +    }
  4.1624 +
  4.1625 +    /**
  4.1626 +     * Returns the number of bytes available to this virtual machine on the
  4.1627 +     * partition <a href="#partName">named</a> by this abstract pathname.  When
  4.1628 +     * possible, this method checks for write permissions and other operating
  4.1629 +     * system restrictions and will therefore usually provide a more accurate
  4.1630 +     * estimate of how much new data can actually be written than {@link
  4.1631 +     * #getFreeSpace}.
  4.1632 +     *
  4.1633 +     * <p> The returned number of available bytes is a hint, but not a
  4.1634 +     * guarantee, that it is possible to use most or any of these bytes.  The
  4.1635 +     * number of unallocated bytes is most likely to be accurate immediately
  4.1636 +     * after this call.  It is likely to be made inaccurate by any external
  4.1637 +     * I/O operations including those made on the system outside of this
  4.1638 +     * virtual machine.  This method makes no guarantee that write operations
  4.1639 +     * to this file system will succeed.
  4.1640 +     *
  4.1641 +     * @return  The number of available bytes on the partition or <tt>0L</tt>
  4.1642 +     *          if the abstract pathname does not name a partition.  On
  4.1643 +     *          systems where this information is not available, this method
  4.1644 +     *          will be equivalent to a call to {@link #getFreeSpace}.
  4.1645 +     *
  4.1646 +     * @throws  SecurityException
  4.1647 +     *          If a security manager has been installed and it denies
  4.1648 +     *          {@link RuntimePermission}<tt>("getFileSystemAttributes")</tt>
  4.1649 +     *          or its {@link SecurityManager#checkRead(String)} method denies
  4.1650 +     *          read access to the file named by this abstract pathname
  4.1651 +     *
  4.1652 +     * @since  1.6
  4.1653 +     */
  4.1654 +    public long getUsableSpace() {
  4.1655 +        throw new SecurityException();
  4.1656 +    }
  4.1657 +
  4.1658 +    /* -- Temporary files -- */
  4.1659 +
  4.1660 +
  4.1661 +    /**
  4.1662 +     * <p> Creates a new empty file in the specified directory, using the
  4.1663 +     * given prefix and suffix strings to generate its name.  If this method
  4.1664 +     * returns successfully then it is guaranteed that:
  4.1665 +     *
  4.1666 +     * <ol>
  4.1667 +     * <li> The file denoted by the returned abstract pathname did not exist
  4.1668 +     *      before this method was invoked, and
  4.1669 +     * <li> Neither this method nor any of its variants will return the same
  4.1670 +     *      abstract pathname again in the current invocation of the virtual
  4.1671 +     *      machine.
  4.1672 +     * </ol>
  4.1673 +     *
  4.1674 +     * This method provides only part of a temporary-file facility.  To arrange
  4.1675 +     * for a file created by this method to be deleted automatically, use the
  4.1676 +     * <code>{@link #deleteOnExit}</code> method.
  4.1677 +     *
  4.1678 +     * <p> The <code>prefix</code> argument must be at least three characters
  4.1679 +     * long.  It is recommended that the prefix be a short, meaningful string
  4.1680 +     * such as <code>"hjb"</code> or <code>"mail"</code>.  The
  4.1681 +     * <code>suffix</code> argument may be <code>null</code>, in which case the
  4.1682 +     * suffix <code>".tmp"</code> will be used.
  4.1683 +     *
  4.1684 +     * <p> To create the new file, the prefix and the suffix may first be
  4.1685 +     * adjusted to fit the limitations of the underlying platform.  If the
  4.1686 +     * prefix is too long then it will be truncated, but its first three
  4.1687 +     * characters will always be preserved.  If the suffix is too long then it
  4.1688 +     * too will be truncated, but if it begins with a period character
  4.1689 +     * (<code>'.'</code>) then the period and the first three characters
  4.1690 +     * following it will always be preserved.  Once these adjustments have been
  4.1691 +     * made the name of the new file will be generated by concatenating the
  4.1692 +     * prefix, five or more internally-generated characters, and the suffix.
  4.1693 +     *
  4.1694 +     * <p> If the <code>directory</code> argument is <code>null</code> then the
  4.1695 +     * system-dependent default temporary-file directory will be used.  The
  4.1696 +     * default temporary-file directory is specified by the system property
  4.1697 +     * <code>java.io.tmpdir</code>.  On UNIX systems the default value of this
  4.1698 +     * property is typically <code>"/tmp"</code> or <code>"/var/tmp"</code>; on
  4.1699 +     * Microsoft Windows systems it is typically <code>"C:\\WINNT\\TEMP"</code>.  A different
  4.1700 +     * value may be given to this system property when the Java virtual machine
  4.1701 +     * is invoked, but programmatic changes to this property are not guaranteed
  4.1702 +     * to have any effect upon the temporary directory used by this method.
  4.1703 +     *
  4.1704 +     * @param  prefix     The prefix string to be used in generating the file's
  4.1705 +     *                    name; must be at least three characters long
  4.1706 +     *
  4.1707 +     * @param  suffix     The suffix string to be used in generating the file's
  4.1708 +     *                    name; may be <code>null</code>, in which case the
  4.1709 +     *                    suffix <code>".tmp"</code> will be used
  4.1710 +     *
  4.1711 +     * @param  directory  The directory in which the file is to be created, or
  4.1712 +     *                    <code>null</code> if the default temporary-file
  4.1713 +     *                    directory is to be used
  4.1714 +     *
  4.1715 +     * @return  An abstract pathname denoting a newly-created empty file
  4.1716 +     *
  4.1717 +     * @throws  IllegalArgumentException
  4.1718 +     *          If the <code>prefix</code> argument contains fewer than three
  4.1719 +     *          characters
  4.1720 +     *
  4.1721 +     * @throws  IOException  If a file could not be created
  4.1722 +     *
  4.1723 +     * @throws  SecurityException
  4.1724 +     *          If a security manager exists and its <code>{@link
  4.1725 +     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
  4.1726 +     *          method does not allow a file to be created
  4.1727 +     *
  4.1728 +     * @since 1.2
  4.1729 +     */
  4.1730 +    public static File createTempFile(String prefix, String suffix,
  4.1731 +                                      File directory)
  4.1732 +        throws IOException
  4.1733 +    {
  4.1734 +        throw new SecurityException();
  4.1735 +    }
  4.1736 +
  4.1737 +    /**
  4.1738 +     * Creates an empty file in the default temporary-file directory, using
  4.1739 +     * the given prefix and suffix to generate its name. Invoking this method
  4.1740 +     * is equivalent to invoking <code>{@link #createTempFile(java.lang.String,
  4.1741 +     * java.lang.String, java.io.File)
  4.1742 +     * createTempFile(prefix,&nbsp;suffix,&nbsp;null)}</code>.
  4.1743 +     *
  4.1744 +     * <p> The {@link
  4.1745 +     * java.nio.file.Files#createTempFile(String,String,java.nio.file.attribute.FileAttribute[])
  4.1746 +     * Files.createTempFile} method provides an alternative method to create an
  4.1747 +     * empty file in the temporary-file directory. Files created by that method
  4.1748 +     * may have more restrictive access permissions to files created by this
  4.1749 +     * method and so may be more suited to security-sensitive applications.
  4.1750 +     *
  4.1751 +     * @param  prefix     The prefix string to be used in generating the file's
  4.1752 +     *                    name; must be at least three characters long
  4.1753 +     *
  4.1754 +     * @param  suffix     The suffix string to be used in generating the file's
  4.1755 +     *                    name; may be <code>null</code>, in which case the
  4.1756 +     *                    suffix <code>".tmp"</code> will be used
  4.1757 +     *
  4.1758 +     * @return  An abstract pathname denoting a newly-created empty file
  4.1759 +     *
  4.1760 +     * @throws  IllegalArgumentException
  4.1761 +     *          If the <code>prefix</code> argument contains fewer than three
  4.1762 +     *          characters
  4.1763 +     *
  4.1764 +     * @throws  IOException  If a file could not be created
  4.1765 +     *
  4.1766 +     * @throws  SecurityException
  4.1767 +     *          If a security manager exists and its <code>{@link
  4.1768 +     *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
  4.1769 +     *          method does not allow a file to be created
  4.1770 +     *
  4.1771 +     * @since 1.2
  4.1772 +     * @see java.nio.file.Files#createTempDirectory(String,FileAttribute[])
  4.1773 +     */
  4.1774 +    public static File createTempFile(String prefix, String suffix)
  4.1775 +        throws IOException
  4.1776 +    {
  4.1777 +        return createTempFile(prefix, suffix, null);
  4.1778 +    }
  4.1779 +
  4.1780 +    /* -- Basic infrastructure -- */
  4.1781 +
  4.1782 +    /**
  4.1783 +     * Compares two abstract pathnames lexicographically.  The ordering
  4.1784 +     * defined by this method depends upon the underlying system.  On UNIX
  4.1785 +     * systems, alphabetic case is significant in comparing pathnames; on Microsoft Windows
  4.1786 +     * systems it is not.
  4.1787 +     *
  4.1788 +     * @param   pathname  The abstract pathname to be compared to this abstract
  4.1789 +     *                    pathname
  4.1790 +     *
  4.1791 +     * @return  Zero if the argument is equal to this abstract pathname, a
  4.1792 +     *          value less than zero if this abstract pathname is
  4.1793 +     *          lexicographically less than the argument, or a value greater
  4.1794 +     *          than zero if this abstract pathname is lexicographically
  4.1795 +     *          greater than the argument
  4.1796 +     *
  4.1797 +     * @since   1.2
  4.1798 +     */
  4.1799 +    public int compareTo(File pathname) {
  4.1800 +        return fs.compare(this, pathname);
  4.1801 +    }
  4.1802 +
  4.1803 +    /**
  4.1804 +     * Tests this abstract pathname for equality with the given object.
  4.1805 +     * Returns <code>true</code> if and only if the argument is not
  4.1806 +     * <code>null</code> and is an abstract pathname that denotes the same file
  4.1807 +     * or directory as this abstract pathname.  Whether or not two abstract
  4.1808 +     * pathnames are equal depends upon the underlying system.  On UNIX
  4.1809 +     * systems, alphabetic case is significant in comparing pathnames; on Microsoft Windows
  4.1810 +     * systems it is not.
  4.1811 +     *
  4.1812 +     * @param   obj   The object to be compared with this abstract pathname
  4.1813 +     *
  4.1814 +     * @return  <code>true</code> if and only if the objects are the same;
  4.1815 +     *          <code>false</code> otherwise
  4.1816 +     */
  4.1817 +    public boolean equals(Object obj) {
  4.1818 +        if ((obj != null) && (obj instanceof File)) {
  4.1819 +            return compareTo((File)obj) == 0;
  4.1820 +        }
  4.1821 +        return false;
  4.1822 +    }
  4.1823 +
  4.1824 +    /**
  4.1825 +     * Computes a hash code for this abstract pathname.  Because equality of
  4.1826 +     * abstract pathnames is inherently system-dependent, so is the computation
  4.1827 +     * of their hash codes.  On UNIX systems, the hash code of an abstract
  4.1828 +     * pathname is equal to the exclusive <em>or</em> of the hash code
  4.1829 +     * of its pathname string and the decimal value
  4.1830 +     * <code>1234321</code>.  On Microsoft Windows systems, the hash
  4.1831 +     * code is equal to the exclusive <em>or</em> of the hash code of
  4.1832 +     * its pathname string converted to lower case and the decimal
  4.1833 +     * value <code>1234321</code>.  Locale is not taken into account on
  4.1834 +     * lowercasing the pathname string.
  4.1835 +     *
  4.1836 +     * @return  A hash code for this abstract pathname
  4.1837 +     */
  4.1838 +    public int hashCode() {
  4.1839 +        return fs.hashCode(this);
  4.1840 +    }
  4.1841 +
  4.1842 +    /**
  4.1843 +     * Returns the pathname string of this abstract pathname.  This is just the
  4.1844 +     * string returned by the <code>{@link #getPath}</code> method.
  4.1845 +     *
  4.1846 +     * @return  The string form of this abstract pathname
  4.1847 +     */
  4.1848 +    public String toString() {
  4.1849 +        return getPath();
  4.1850 +    }
  4.1851 +
  4.1852 +    /**
  4.1853 +     * WriteObject is called to save this filename.
  4.1854 +     * The separator character is saved also so it can be replaced
  4.1855 +     * in case the path is reconstituted on a different host type.
  4.1856 +     * <p>
  4.1857 +     * @serialData  Default fields followed by separator character.
  4.1858 +     */
  4.1859 +    private synchronized void writeObject(java.io.ObjectOutputStream s)
  4.1860 +        throws IOException
  4.1861 +    {
  4.1862 +        s.defaultWriteObject();
  4.1863 +        s.writeChar(this.separatorChar); // Add the separator character
  4.1864 +    }
  4.1865 +
  4.1866 +    /**
  4.1867 +     * readObject is called to restore this filename.
  4.1868 +     * The original separator character is read.  If it is different
  4.1869 +     * than the separator character on this system, then the old separator
  4.1870 +     * is replaced by the local separator.
  4.1871 +     */
  4.1872 +    private synchronized void readObject(java.io.ObjectInputStream s)
  4.1873 +         throws IOException, ClassNotFoundException
  4.1874 +    {
  4.1875 +        ObjectInputStream.GetField fields = s.readFields();
  4.1876 +        String pathField = (String)fields.get("path", null);
  4.1877 +        char sep = s.readChar(); // read the previous separator char
  4.1878 +        if (sep != separatorChar)
  4.1879 +            pathField = pathField.replace(sep, separatorChar);
  4.1880 +        this.path = fs.normalize(pathField);
  4.1881 +        this.prefixLength = fs.prefixLength(this.path);
  4.1882 +    }
  4.1883 +
  4.1884 +    /** use serialVersionUID from JDK 1.0.2 for interoperability */
  4.1885 +    private static final long serialVersionUID = 301077366599181567L;
  4.1886 +
  4.1887 +    // -- Integration with java.nio.file --
  4.1888 +/*
  4.1889 +    private volatile transient Path filePath;
  4.1890 +
  4.1891 +    /**
  4.1892 +     * Returns a {@link Path java.nio.file.Path} object constructed from the
  4.1893 +     * this abstract path. The resulting {@code Path} is associated with the
  4.1894 +     * {@link java.nio.file.FileSystems#getDefault default-filesystem}.
  4.1895 +     *
  4.1896 +     * <p> The first invocation of this method works as if invoking it were
  4.1897 +     * equivalent to evaluating the expression:
  4.1898 +     * <blockquote><pre>
  4.1899 +     * {@link java.nio.file.FileSystems#getDefault FileSystems.getDefault}().{@link
  4.1900 +     * java.nio.file.FileSystem#getPath getPath}(this.{@link #getPath getPath}());
  4.1901 +     * </pre></blockquote>
  4.1902 +     * Subsequent invocations of this method return the same {@code Path}.
  4.1903 +     *
  4.1904 +     * <p> If this abstract pathname is the empty abstract pathname then this
  4.1905 +     * method returns a {@code Path} that may be used to access the current
  4.1906 +     * user directory.
  4.1907 +     *
  4.1908 +     * @return  a {@code Path} constructed from this abstract path
  4.1909 +     *
  4.1910 +     * @throws  java.nio.file.InvalidPathException
  4.1911 +     *          if a {@code Path} object cannot be constructed from the abstract
  4.1912 +     *          path (see {@link java.nio.file.FileSystem#getPath FileSystem.getPath})
  4.1913 +     *
  4.1914 +     * @since   1.7
  4.1915 +     * @see Path#toFile
  4.1916 +     */
  4.1917 +//    public Path toPath() {
  4.1918 +//        Path result = filePath;
  4.1919 +//        if (result == null) {
  4.1920 +//            synchronized (this) {
  4.1921 +//                result = filePath;
  4.1922 +//                if (result == null) {
  4.1923 +//                    result = FileSystems.getDefault().getPath(path);
  4.1924 +//                    filePath = result;
  4.1925 +//                }
  4.1926 +//            }
  4.1927 +//        }
  4.1928 +//        return result;
  4.1929 +//    }
  4.1930 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/rt/emul/compact/src/main/java/java/io/FileFilter.java	Sun Sep 08 11:22:51 2013 +0200
     5.3 @@ -0,0 +1,50 @@
     5.4 +/*
     5.5 + * Copyright (c) 1998, 2002, Oracle and/or its affiliates. All rights reserved.
     5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     5.7 + *
     5.8 + * This code is free software; you can redistribute it and/or modify it
     5.9 + * under the terms of the GNU General Public License version 2 only, as
    5.10 + * published by the Free Software Foundation.  Oracle designates this
    5.11 + * particular file as subject to the "Classpath" exception as provided
    5.12 + * by Oracle in the LICENSE file that accompanied this code.
    5.13 + *
    5.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    5.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    5.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    5.17 + * version 2 for more details (a copy is included in the LICENSE file that
    5.18 + * accompanied this code).
    5.19 + *
    5.20 + * You should have received a copy of the GNU General Public License version
    5.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    5.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    5.23 + *
    5.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    5.25 + * or visit www.oracle.com if you need additional information or have any
    5.26 + * questions.
    5.27 + */
    5.28 +
    5.29 +package java.io;
    5.30 +
    5.31 +
    5.32 +/**
    5.33 + * A filter for abstract pathnames.
    5.34 + *
    5.35 + * <p> Instances of this interface may be passed to the <code>{@link
    5.36 + * File#listFiles(java.io.FileFilter) listFiles(FileFilter)}</code> method
    5.37 + * of the <code>{@link java.io.File}</code> class.
    5.38 + *
    5.39 + * @since 1.2
    5.40 + */
    5.41 +public interface FileFilter {
    5.42 +
    5.43 +    /**
    5.44 +     * Tests whether or not the specified abstract pathname should be
    5.45 +     * included in a pathname list.
    5.46 +     *
    5.47 +     * @param  pathname  The abstract pathname to be tested
    5.48 +     * @return  <code>true</code> if and only if <code>pathname</code>
    5.49 +     *          should be included
    5.50 +     */
    5.51 +    boolean accept(File pathname);
    5.52 +
    5.53 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/rt/emul/compact/src/main/java/java/io/FileNotFoundException.java	Sun Sep 08 11:22:51 2013 +0200
     6.3 @@ -0,0 +1,82 @@
     6.4 +/*
     6.5 + * Copyright (c) 1994, 2008, Oracle and/or its affiliates. All rights reserved.
     6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     6.7 + *
     6.8 + * This code is free software; you can redistribute it and/or modify it
     6.9 + * under the terms of the GNU General Public License version 2 only, as
    6.10 + * published by the Free Software Foundation.  Oracle designates this
    6.11 + * particular file as subject to the "Classpath" exception as provided
    6.12 + * by Oracle in the LICENSE file that accompanied this code.
    6.13 + *
    6.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    6.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    6.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    6.17 + * version 2 for more details (a copy is included in the LICENSE file that
    6.18 + * accompanied this code).
    6.19 + *
    6.20 + * You should have received a copy of the GNU General Public License version
    6.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    6.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    6.23 + *
    6.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    6.25 + * or visit www.oracle.com if you need additional information or have any
    6.26 + * questions.
    6.27 + */
    6.28 +
    6.29 +package java.io;
    6.30 +
    6.31 +
    6.32 +/**
    6.33 + * Signals that an attempt to open the file denoted by a specified pathname
    6.34 + * has failed.
    6.35 + *
    6.36 + * <p> This exception will be thrown by the {@link FileInputStream}, {@link
    6.37 + * FileOutputStream}, and {@link RandomAccessFile} constructors when a file
    6.38 + * with the specified pathname does not exist.  It will also be thrown by these
    6.39 + * constructors if the file does exist but for some reason is inaccessible, for
    6.40 + * example when an attempt is made to open a read-only file for writing.
    6.41 + *
    6.42 + * @author  unascribed
    6.43 + * @since   JDK1.0
    6.44 + */
    6.45 +
    6.46 +public class FileNotFoundException extends IOException {
    6.47 +    private static final long serialVersionUID = -897856973823710492L;
    6.48 +
    6.49 +    /**
    6.50 +     * Constructs a <code>FileNotFoundException</code> with
    6.51 +     * <code>null</code> as its error detail message.
    6.52 +     */
    6.53 +    public FileNotFoundException() {
    6.54 +        super();
    6.55 +    }
    6.56 +
    6.57 +    /**
    6.58 +     * Constructs a <code>FileNotFoundException</code> with the
    6.59 +     * specified detail message. The string <code>s</code> can be
    6.60 +     * retrieved later by the
    6.61 +     * <code>{@link java.lang.Throwable#getMessage}</code>
    6.62 +     * method of class <code>java.lang.Throwable</code>.
    6.63 +     *
    6.64 +     * @param   s   the detail message.
    6.65 +     */
    6.66 +    public FileNotFoundException(String s) {
    6.67 +        super(s);
    6.68 +    }
    6.69 +
    6.70 +    /**
    6.71 +     * Constructs a <code>FileNotFoundException</code> with a detail message
    6.72 +     * consisting of the given pathname string followed by the given reason
    6.73 +     * string.  If the <code>reason</code> argument is <code>null</code> then
    6.74 +     * it will be omitted.  This private constructor is invoked only by native
    6.75 +     * I/O methods.
    6.76 +     *
    6.77 +     * @since 1.2
    6.78 +     */
    6.79 +    private FileNotFoundException(String path, String reason) {
    6.80 +        super(path + ((reason == null)
    6.81 +                      ? ""
    6.82 +                      : " (" + reason + ")"));
    6.83 +    }
    6.84 +
    6.85 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/rt/emul/compact/src/main/java/java/io/FilenameFilter.java	Sun Sep 08 11:22:51 2013 +0200
     7.3 @@ -0,0 +1,53 @@
     7.4 +/*
     7.5 + * Copyright (c) 1994, 1998, Oracle and/or its affiliates. All rights reserved.
     7.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     7.7 + *
     7.8 + * This code is free software; you can redistribute it and/or modify it
     7.9 + * under the terms of the GNU General Public License version 2 only, as
    7.10 + * published by the Free Software Foundation.  Oracle designates this
    7.11 + * particular file as subject to the "Classpath" exception as provided
    7.12 + * by Oracle in the LICENSE file that accompanied this code.
    7.13 + *
    7.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    7.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    7.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    7.17 + * version 2 for more details (a copy is included in the LICENSE file that
    7.18 + * accompanied this code).
    7.19 + *
    7.20 + * You should have received a copy of the GNU General Public License version
    7.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    7.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    7.23 + *
    7.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    7.25 + * or visit www.oracle.com if you need additional information or have any
    7.26 + * questions.
    7.27 + */
    7.28 +
    7.29 +package java.io;
    7.30 +
    7.31 +/**
    7.32 + * Instances of classes that implement this interface are used to
    7.33 + * filter filenames. These instances are used to filter directory
    7.34 + * listings in the <code>list</code> method of class
    7.35 + * <code>File</code>, and by the Abstract Window Toolkit's file
    7.36 + * dialog component.
    7.37 + *
    7.38 + * @author  Arthur van Hoff
    7.39 + * @author  Jonathan Payne
    7.40 + * @see     java.awt.FileDialog#setFilenameFilter(java.io.FilenameFilter)
    7.41 + * @see     java.io.File
    7.42 + * @see     java.io.File#list(java.io.FilenameFilter)
    7.43 + * @since   JDK1.0
    7.44 + */
    7.45 +public
    7.46 +interface FilenameFilter {
    7.47 +    /**
    7.48 +     * Tests if a specified file should be included in a file list.
    7.49 +     *
    7.50 +     * @param   dir    the directory in which the file was found.
    7.51 +     * @param   name   the name of the file.
    7.52 +     * @return  <code>true</code> if and only if the name should be
    7.53 +     * included in the file list; <code>false</code> otherwise.
    7.54 +     */
    7.55 +    boolean accept(File dir, String name);
    7.56 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/rt/emul/compact/src/main/java/java/io/InterruptedIOException.java	Sun Sep 08 11:22:51 2013 +0200
     8.3 @@ -0,0 +1,74 @@
     8.4 +/*
     8.5 + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
     8.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     8.7 + *
     8.8 + * This code is free software; you can redistribute it and/or modify it
     8.9 + * under the terms of the GNU General Public License version 2 only, as
    8.10 + * published by the Free Software Foundation.  Oracle designates this
    8.11 + * particular file as subject to the "Classpath" exception as provided
    8.12 + * by Oracle in the LICENSE file that accompanied this code.
    8.13 + *
    8.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    8.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    8.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    8.17 + * version 2 for more details (a copy is included in the LICENSE file that
    8.18 + * accompanied this code).
    8.19 + *
    8.20 + * You should have received a copy of the GNU General Public License version
    8.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    8.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    8.23 + *
    8.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    8.25 + * or visit www.oracle.com if you need additional information or have any
    8.26 + * questions.
    8.27 + */
    8.28 +
    8.29 +package java.io;
    8.30 +
    8.31 +/**
    8.32 + * Signals that an I/O operation has been interrupted. An
    8.33 + * <code>InterruptedIOException</code> is thrown to indicate that an
    8.34 + * input or output transfer has been terminated because the thread
    8.35 + * performing it was interrupted. The field {@link #bytesTransferred}
    8.36 + * indicates how many bytes were successfully transferred before
    8.37 + * the interruption occurred.
    8.38 + *
    8.39 + * @author  unascribed
    8.40 + * @see     java.io.InputStream
    8.41 + * @see     java.io.OutputStream
    8.42 + * @see     java.lang.Thread#interrupt()
    8.43 + * @since   JDK1.0
    8.44 + */
    8.45 +public
    8.46 +class InterruptedIOException extends IOException {
    8.47 +    private static final long serialVersionUID = 4020568460727500567L;
    8.48 +
    8.49 +    /**
    8.50 +     * Constructs an <code>InterruptedIOException</code> with
    8.51 +     * <code>null</code> as its error detail message.
    8.52 +     */
    8.53 +    public InterruptedIOException() {
    8.54 +        super();
    8.55 +    }
    8.56 +
    8.57 +    /**
    8.58 +     * Constructs an <code>InterruptedIOException</code> with the
    8.59 +     * specified detail message. The string <code>s</code> can be
    8.60 +     * retrieved later by the
    8.61 +     * <code>{@link java.lang.Throwable#getMessage}</code>
    8.62 +     * method of class <code>java.lang.Throwable</code>.
    8.63 +     *
    8.64 +     * @param   s   the detail message.
    8.65 +     */
    8.66 +    public InterruptedIOException(String s) {
    8.67 +        super(s);
    8.68 +    }
    8.69 +
    8.70 +    /**
    8.71 +     * Reports how many bytes had been transferred as part of the I/O
    8.72 +     * operation before it was interrupted.
    8.73 +     *
    8.74 +     * @serial
    8.75 +     */
    8.76 +    public int bytesTransferred = 0;
    8.77 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/rt/emul/compact/src/main/java/java/io/OutputStreamWriter.java	Sun Sep 08 11:22:51 2013 +0200
     9.3 @@ -0,0 +1,242 @@
     9.4 +/*
     9.5 + * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
     9.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     9.7 + *
     9.8 + * This code is free software; you can redistribute it and/or modify it
     9.9 + * under the terms of the GNU General Public License version 2 only, as
    9.10 + * published by the Free Software Foundation.  Oracle designates this
    9.11 + * particular file as subject to the "Classpath" exception as provided
    9.12 + * by Oracle in the LICENSE file that accompanied this code.
    9.13 + *
    9.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    9.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    9.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    9.17 + * version 2 for more details (a copy is included in the LICENSE file that
    9.18 + * accompanied this code).
    9.19 + *
    9.20 + * You should have received a copy of the GNU General Public License version
    9.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    9.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    9.23 + *
    9.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    9.25 + * or visit www.oracle.com if you need additional information or have any
    9.26 + * questions.
    9.27 + */
    9.28 +
    9.29 +package java.io;
    9.30 +
    9.31 +/**
    9.32 + * An OutputStreamWriter is a bridge from character streams to byte streams:
    9.33 + * Characters written to it are encoded into bytes using a specified {@link
    9.34 + * java.nio.charset.Charset <code>charset</code>}.  The charset that it uses
    9.35 + * may be specified by name or may be given explicitly, or the platform's
    9.36 + * default charset may be accepted.
    9.37 + *
    9.38 + * <p> Each invocation of a write() method causes the encoding converter to be
    9.39 + * invoked on the given character(s).  The resulting bytes are accumulated in a
    9.40 + * buffer before being written to the underlying output stream.  The size of
    9.41 + * this buffer may be specified, but by default it is large enough for most
    9.42 + * purposes.  Note that the characters passed to the write() methods are not
    9.43 + * buffered.
    9.44 + *
    9.45 + * <p> For top efficiency, consider wrapping an OutputStreamWriter within a
    9.46 + * BufferedWriter so as to avoid frequent converter invocations.  For example:
    9.47 + *
    9.48 + * <pre>
    9.49 + * Writer out
    9.50 + *   = new BufferedWriter(new OutputStreamWriter(System.out));
    9.51 + * </pre>
    9.52 + *
    9.53 + * <p> A <i>surrogate pair</i> is a character represented by a sequence of two
    9.54 + * <tt>char</tt> values: A <i>high</i> surrogate in the range '&#92;uD800' to
    9.55 + * '&#92;uDBFF' followed by a <i>low</i> surrogate in the range '&#92;uDC00' to
    9.56 + * '&#92;uDFFF'.
    9.57 + *
    9.58 + * <p> A <i>malformed surrogate element</i> is a high surrogate that is not
    9.59 + * followed by a low surrogate or a low surrogate that is not preceded by a
    9.60 + * high surrogate.
    9.61 + *
    9.62 + * <p> This class always replaces malformed surrogate elements and unmappable
    9.63 + * character sequences with the charset's default <i>substitution sequence</i>.
    9.64 + * The {@linkplain java.nio.charset.CharsetEncoder} class should be used when more
    9.65 + * control over the encoding process is required.
    9.66 + *
    9.67 + * @see BufferedWriter
    9.68 + * @see OutputStream
    9.69 + * @see java.nio.charset.Charset
    9.70 + *
    9.71 + * @author      Mark Reinhold
    9.72 + * @since       JDK1.1
    9.73 + */
    9.74 +
    9.75 +public class OutputStreamWriter extends Writer {
    9.76 +    
    9.77 +    /**
    9.78 +     * Creates an OutputStreamWriter that uses the named charset.
    9.79 +     *
    9.80 +     * @param  out
    9.81 +     *         An OutputStream
    9.82 +     *
    9.83 +     * @param  charsetName
    9.84 +     *         The name of a supported
    9.85 +     *         {@link java.nio.charset.Charset </code>charset<code>}
    9.86 +     *
    9.87 +     * @exception  UnsupportedEncodingException
    9.88 +     *             If the named encoding is not supported
    9.89 +     */
    9.90 +    public OutputStreamWriter(OutputStream out, String charsetName)
    9.91 +        throws UnsupportedEncodingException
    9.92 +    {
    9.93 +        super(out);
    9.94 +        if (charsetName == null)
    9.95 +            throw new NullPointerException("charsetName");
    9.96 +        if (!charsetName.toUpperCase().equals("UTF-8")) {
    9.97 +            throw new UnsupportedEncodingException(charsetName);
    9.98 +        }
    9.99 +    }
   9.100 +
   9.101 +    /**
   9.102 +     * Creates an OutputStreamWriter that uses the default character encoding.
   9.103 +     *
   9.104 +     * @param  out  An OutputStream
   9.105 +     */
   9.106 +    public OutputStreamWriter(OutputStream out) {
   9.107 +        super(out);
   9.108 +    }
   9.109 +
   9.110 +    /**
   9.111 +     * Creates an OutputStreamWriter that uses the given charset. </p>
   9.112 +     *
   9.113 +     * @param  out
   9.114 +     *         An OutputStream
   9.115 +     *
   9.116 +     * @param  cs
   9.117 +     *         A charset
   9.118 +     *
   9.119 +     * @since 1.4
   9.120 +     * @spec JSR-51
   9.121 +     */
   9.122 +//    public OutputStreamWriter(OutputStream out, Charset cs) {
   9.123 +//        super(out);
   9.124 +//        if (cs == null)
   9.125 +//            throw new NullPointerException("charset");
   9.126 +//        se = StreamEncoder.forOutputStreamWriter(out, this, cs);
   9.127 +//    }
   9.128 +
   9.129 +    /**
   9.130 +     * Creates an OutputStreamWriter that uses the given charset encoder.  </p>
   9.131 +     *
   9.132 +     * @param  out
   9.133 +     *         An OutputStream
   9.134 +     *
   9.135 +     * @param  enc
   9.136 +     *         A charset encoder
   9.137 +     *
   9.138 +     * @since 1.4
   9.139 +     * @spec JSR-51
   9.140 +     */
   9.141 +//    public OutputStreamWriter(OutputStream out, CharsetEncoder enc) {
   9.142 +//        super(out);
   9.143 +//        if (enc == null)
   9.144 +//            throw new NullPointerException("charset encoder");
   9.145 +//        se = StreamEncoder.forOutputStreamWriter(out, this, enc);
   9.146 +//    }
   9.147 +
   9.148 +    /**
   9.149 +     * Returns the name of the character encoding being used by this stream.
   9.150 +     *
   9.151 +     * <p> If the encoding has an historical name then that name is returned;
   9.152 +     * otherwise the encoding's canonical name is returned.
   9.153 +     *
   9.154 +     * <p> If this instance was created with the {@link
   9.155 +     * #OutputStreamWriter(OutputStream, String)} constructor then the returned
   9.156 +     * name, being unique for the encoding, may differ from the name passed to
   9.157 +     * the constructor.  This method may return <tt>null</tt> if the stream has
   9.158 +     * been closed. </p>
   9.159 +     *
   9.160 +     * @return The historical name of this encoding, or possibly
   9.161 +     *         <code>null</code> if the stream has been closed
   9.162 +     *
   9.163 +     * @see java.nio.charset.Charset
   9.164 +     *
   9.165 +     * @revised 1.4
   9.166 +     * @spec JSR-51
   9.167 +     */
   9.168 +    public String getEncoding() {
   9.169 +        return "UTF-8";
   9.170 +    }
   9.171 +
   9.172 +    /**
   9.173 +     * Flushes the output buffer to the underlying byte stream, without flushing
   9.174 +     * the byte stream itself.  This method is non-private only so that it may
   9.175 +     * be invoked by PrintStream.
   9.176 +     */
   9.177 +    void flushBuffer() throws IOException {
   9.178 +        out().flush();
   9.179 +    }
   9.180 +
   9.181 +    /**
   9.182 +     * Writes a single character.
   9.183 +     *
   9.184 +     * @exception  IOException  If an I/O error occurs
   9.185 +     */
   9.186 +    public void write(int c) throws IOException {
   9.187 +        if (c <= 0x7F) {
   9.188 +            out().write(c);
   9.189 +        } else if (c <= 0x7FF) {
   9.190 +            out().write(0xC0 | (c >> 6));
   9.191 +            out().write(0x80 | (c & 0x3F));
   9.192 +        } else {
   9.193 +            out().write(0xE0 | (c >> 12));
   9.194 +            out().write(0x80 | ((c >> 6) & 0x3F));
   9.195 +            out().write(0x80 | (c & 0x3F));
   9.196 +        }
   9.197 +    }
   9.198 +
   9.199 +    /**
   9.200 +     * Writes a portion of an array of characters.
   9.201 +     *
   9.202 +     * @param  cbuf  Buffer of characters
   9.203 +     * @param  off   Offset from which to start writing characters
   9.204 +     * @param  len   Number of characters to write
   9.205 +     *
   9.206 +     * @exception  IOException  If an I/O error occurs
   9.207 +     */
   9.208 +    public void write(char cbuf[], int off, int len) throws IOException {
   9.209 +        while (len-- > 0) {
   9.210 +            write(cbuf[off++]);
   9.211 +        }
   9.212 +    }
   9.213 +
   9.214 +    /**
   9.215 +     * Writes a portion of a string.
   9.216 +     *
   9.217 +     * @param  str  A String
   9.218 +     * @param  off  Offset from which to start writing characters
   9.219 +     * @param  len  Number of characters to write
   9.220 +     *
   9.221 +     * @exception  IOException  If an I/O error occurs
   9.222 +     */
   9.223 +    public void write(String str, int off, int len) throws IOException {
   9.224 +        while (len-- > 0) {
   9.225 +            write(str.charAt(off++));
   9.226 +        }
   9.227 +    }
   9.228 +
   9.229 +    /**
   9.230 +     * Flushes the stream.
   9.231 +     *
   9.232 +     * @exception  IOException  If an I/O error occurs
   9.233 +     */
   9.234 +    public void flush() throws IOException {
   9.235 +        out().flush();
   9.236 +    }
   9.237 +
   9.238 +    public void close() throws IOException {
   9.239 +        out().close();
   9.240 +    }
   9.241 +    
   9.242 +    private OutputStream out() {
   9.243 +        return (OutputStream) lock;
   9.244 +    }
   9.245 +}
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/rt/emul/compact/src/main/java/java/io/PrintStream.java	Sun Sep 08 11:22:51 2013 +0200
    10.3 @@ -0,0 +1,1125 @@
    10.4 +/*
    10.5 + * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
    10.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    10.7 + *
    10.8 + * This code is free software; you can redistribute it and/or modify it
    10.9 + * under the terms of the GNU General Public License version 2 only, as
   10.10 + * published by the Free Software Foundation.  Oracle designates this
   10.11 + * particular file as subject to the "Classpath" exception as provided
   10.12 + * by Oracle in the LICENSE file that accompanied this code.
   10.13 + *
   10.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   10.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   10.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   10.17 + * version 2 for more details (a copy is included in the LICENSE file that
   10.18 + * accompanied this code).
   10.19 + *
   10.20 + * You should have received a copy of the GNU General Public License version
   10.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   10.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   10.23 + *
   10.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   10.25 + * or visit www.oracle.com if you need additional information or have any
   10.26 + * questions.
   10.27 + */
   10.28 +
   10.29 +package java.io;
   10.30 +
   10.31 +import java.util.Arrays;
   10.32 +
   10.33 +
   10.34 +/**
   10.35 + * A <code>PrintStream</code> adds functionality to another output stream,
   10.36 + * namely the ability to print representations of various data values
   10.37 + * conveniently.  Two other features are provided as well.  Unlike other output
   10.38 + * streams, a <code>PrintStream</code> never throws an
   10.39 + * <code>IOException</code>; instead, exceptional situations merely set an
   10.40 + * internal flag that can be tested via the <code>checkError</code> method.
   10.41 + * Optionally, a <code>PrintStream</code> can be created so as to flush
   10.42 + * automatically; this means that the <code>flush</code> method is
   10.43 + * automatically invoked after a byte array is written, one of the
   10.44 + * <code>println</code> methods is invoked, or a newline character or byte
   10.45 + * (<code>'\n'</code>) is written.
   10.46 + *
   10.47 + * <p> All characters printed by a <code>PrintStream</code> are converted into
   10.48 + * bytes using the platform's default character encoding.  The <code>{@link
   10.49 + * PrintWriter}</code> class should be used in situations that require writing
   10.50 + * characters rather than bytes.
   10.51 + *
   10.52 + * @author     Frank Yellin
   10.53 + * @author     Mark Reinhold
   10.54 + * @since      JDK1.0
   10.55 + */
   10.56 +
   10.57 +public class PrintStream extends FilterOutputStream
   10.58 +    implements Appendable, Closeable
   10.59 +{
   10.60 +
   10.61 +    private final boolean autoFlush;
   10.62 +    private boolean trouble = false;
   10.63 +    private Formatter formatter;
   10.64 +
   10.65 +    /**
   10.66 +     * Track both the text- and character-output streams, so that their buffers
   10.67 +     * can be flushed without flushing the entire stream.
   10.68 +     */
   10.69 +    private BufferedWriter textOut;
   10.70 +    private OutputStreamWriter charOut;
   10.71 +
   10.72 +    /**
   10.73 +     * requireNonNull is explicitly declared here so as not to create an extra
   10.74 +     * dependency on java.util.Objects.requireNonNull. PrintStream is loaded
   10.75 +     * early during system initialization.
   10.76 +     */
   10.77 +    private static <T> T requireNonNull(T obj, String message) {
   10.78 +        if (obj == null)
   10.79 +            throw new NullPointerException(message);
   10.80 +        return obj;
   10.81 +    }
   10.82 +
   10.83 +    /* Private constructors */
   10.84 +    private PrintStream(boolean autoFlush, OutputStream out) {
   10.85 +        super(out);
   10.86 +        this.autoFlush = autoFlush;
   10.87 +        this.charOut = new OutputStreamWriter(this);
   10.88 +        this.textOut = new BufferedWriter(charOut);
   10.89 +    }
   10.90 +    
   10.91 +    static final class Formatter {
   10.92 +    }
   10.93 +    
   10.94 +    static final class Charset {
   10.95 +    }
   10.96 +    
   10.97 +    static Charset toCharset(String ch) throws UnsupportedEncodingException {
   10.98 +        if (!"UTF-8".equals(ch)) {
   10.99 +            throw new UnsupportedEncodingException();
  10.100 +        }
  10.101 +        return null;
  10.102 +    }
  10.103 +
  10.104 +    private PrintStream(boolean autoFlush, OutputStream out, Charset charset) {
  10.105 +        super(out);
  10.106 +        this.autoFlush = autoFlush;
  10.107 +        this.charOut = new OutputStreamWriter(this);
  10.108 +        this.textOut = new BufferedWriter(charOut);
  10.109 +    }
  10.110 +
  10.111 +    /* Variant of the private constructor so that the given charset name
  10.112 +     * can be verified before evaluating the OutputStream argument. Used
  10.113 +     * by constructors creating a FileOutputStream that also take a
  10.114 +     * charset name.
  10.115 +     */
  10.116 +    private PrintStream(boolean autoFlush, Charset charset, OutputStream out)
  10.117 +        throws UnsupportedEncodingException
  10.118 +    {
  10.119 +        this(autoFlush, out, charset);
  10.120 +    }
  10.121 +
  10.122 +    /**
  10.123 +     * Creates a new print stream.  This stream will not flush automatically.
  10.124 +     *
  10.125 +     * @param  out        The output stream to which values and objects will be
  10.126 +     *                    printed
  10.127 +     *
  10.128 +     * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream)
  10.129 +     */
  10.130 +    public PrintStream(OutputStream out) {
  10.131 +        this(out, false);
  10.132 +    }
  10.133 +
  10.134 +    /**
  10.135 +     * Creates a new print stream.
  10.136 +     *
  10.137 +     * @param  out        The output stream to which values and objects will be
  10.138 +     *                    printed
  10.139 +     * @param  autoFlush  A boolean; if true, the output buffer will be flushed
  10.140 +     *                    whenever a byte array is written, one of the
  10.141 +     *                    <code>println</code> methods is invoked, or a newline
  10.142 +     *                    character or byte (<code>'\n'</code>) is written
  10.143 +     *
  10.144 +     * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream, boolean)
  10.145 +     */
  10.146 +    public PrintStream(OutputStream out, boolean autoFlush) {
  10.147 +        this(autoFlush, requireNonNull(out, "Null output stream"));
  10.148 +    }
  10.149 +
  10.150 +    /**
  10.151 +     * Creates a new print stream.
  10.152 +     *
  10.153 +     * @param  out        The output stream to which values and objects will be
  10.154 +     *                    printed
  10.155 +     * @param  autoFlush  A boolean; if true, the output buffer will be flushed
  10.156 +     *                    whenever a byte array is written, one of the
  10.157 +     *                    <code>println</code> methods is invoked, or a newline
  10.158 +     *                    character or byte (<code>'\n'</code>) is written
  10.159 +     * @param  encoding   The name of a supported
  10.160 +     *                    <a href="../lang/package-summary.html#charenc">
  10.161 +     *                    character encoding</a>
  10.162 +     *
  10.163 +     * @throws  UnsupportedEncodingException
  10.164 +     *          If the named encoding is not supported
  10.165 +     *
  10.166 +     * @since  1.4
  10.167 +     */
  10.168 +    public PrintStream(OutputStream out, boolean autoFlush, String encoding)
  10.169 +        throws UnsupportedEncodingException
  10.170 +    {
  10.171 +        this(autoFlush,
  10.172 +             requireNonNull(out, "Null output stream"),
  10.173 +             toCharset(encoding));
  10.174 +    }
  10.175 +
  10.176 +    /**
  10.177 +     * Creates a new print stream, without automatic line flushing, with the
  10.178 +     * specified file name.  This convenience constructor creates
  10.179 +     * the necessary intermediate {@link java.io.OutputStreamWriter
  10.180 +     * OutputStreamWriter}, which will encode characters using the
  10.181 +     * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}
  10.182 +     * for this instance of the Java virtual machine.
  10.183 +     *
  10.184 +     * @param  fileName
  10.185 +     *         The name of the file to use as the destination of this print
  10.186 +     *         stream.  If the file exists, then it will be truncated to
  10.187 +     *         zero size; otherwise, a new file will be created.  The output
  10.188 +     *         will be written to the file and is buffered.
  10.189 +     *
  10.190 +     * @throws  FileNotFoundException
  10.191 +     *          If the given file object does not denote an existing, writable
  10.192 +     *          regular file and a new regular file of that name cannot be
  10.193 +     *          created, or if some other error occurs while opening or
  10.194 +     *          creating the file
  10.195 +     *
  10.196 +     * @throws  SecurityException
  10.197 +     *          If a security manager is present and {@link
  10.198 +     *          SecurityManager#checkWrite checkWrite(fileName)} denies write
  10.199 +     *          access to the file
  10.200 +     *
  10.201 +     * @since  1.5
  10.202 +     */
  10.203 +    public PrintStream(String fileName) throws FileNotFoundException {
  10.204 +        super(null);
  10.205 +        throw new FileNotFoundException();
  10.206 +    }
  10.207 +
  10.208 +    /**
  10.209 +     * Creates a new print stream, without automatic line flushing, with the
  10.210 +     * specified file name and charset.  This convenience constructor creates
  10.211 +     * the necessary intermediate {@link java.io.OutputStreamWriter
  10.212 +     * OutputStreamWriter}, which will encode characters using the provided
  10.213 +     * charset.
  10.214 +     *
  10.215 +     * @param  fileName
  10.216 +     *         The name of the file to use as the destination of this print
  10.217 +     *         stream.  If the file exists, then it will be truncated to
  10.218 +     *         zero size; otherwise, a new file will be created.  The output
  10.219 +     *         will be written to the file and is buffered.
  10.220 +     *
  10.221 +     * @param  csn
  10.222 +     *         The name of a supported {@linkplain java.nio.charset.Charset
  10.223 +     *         charset}
  10.224 +     *
  10.225 +     * @throws  FileNotFoundException
  10.226 +     *          If the given file object does not denote an existing, writable
  10.227 +     *          regular file and a new regular file of that name cannot be
  10.228 +     *          created, or if some other error occurs while opening or
  10.229 +     *          creating the file
  10.230 +     *
  10.231 +     * @throws  SecurityException
  10.232 +     *          If a security manager is present and {@link
  10.233 +     *          SecurityManager#checkWrite checkWrite(fileName)} denies write
  10.234 +     *          access to the file
  10.235 +     *
  10.236 +     * @throws  UnsupportedEncodingException
  10.237 +     *          If the named charset is not supported
  10.238 +     *
  10.239 +     * @since  1.5
  10.240 +     */
  10.241 +    public PrintStream(String fileName, String csn)
  10.242 +        throws FileNotFoundException, UnsupportedEncodingException
  10.243 +    {
  10.244 +        super(null);
  10.245 +        throw new FileNotFoundException();
  10.246 +    }
  10.247 +
  10.248 +    /**
  10.249 +     * Creates a new print stream, without automatic line flushing, with the
  10.250 +     * specified file.  This convenience constructor creates the necessary
  10.251 +     * intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
  10.252 +     * which will encode characters using the {@linkplain
  10.253 +     * java.nio.charset.Charset#defaultCharset() default charset} for this
  10.254 +     * instance of the Java virtual machine.
  10.255 +     *
  10.256 +     * @param  file
  10.257 +     *         The file to use as the destination of this print stream.  If the
  10.258 +     *         file exists, then it will be truncated to zero size; otherwise,
  10.259 +     *         a new file will be created.  The output will be written to the
  10.260 +     *         file and is buffered.
  10.261 +     *
  10.262 +     * @throws  FileNotFoundException
  10.263 +     *          If the given file object does not denote an existing, writable
  10.264 +     *          regular file and a new regular file of that name cannot be
  10.265 +     *          created, or if some other error occurs while opening or
  10.266 +     *          creating the file
  10.267 +     *
  10.268 +     * @throws  SecurityException
  10.269 +     *          If a security manager is present and {@link
  10.270 +     *          SecurityManager#checkWrite checkWrite(file.getPath())}
  10.271 +     *          denies write access to the file
  10.272 +     *
  10.273 +     * @since  1.5
  10.274 +     */
  10.275 +    public PrintStream(File file) throws FileNotFoundException {
  10.276 +        super(null);
  10.277 +        throw new FileNotFoundException();
  10.278 +    }
  10.279 +
  10.280 +    /**
  10.281 +     * Creates a new print stream, without automatic line flushing, with the
  10.282 +     * specified file and charset.  This convenience constructor creates
  10.283 +     * the necessary intermediate {@link java.io.OutputStreamWriter
  10.284 +     * OutputStreamWriter}, which will encode characters using the provided
  10.285 +     * charset.
  10.286 +     *
  10.287 +     * @param  file
  10.288 +     *         The file to use as the destination of this print stream.  If the
  10.289 +     *         file exists, then it will be truncated to zero size; otherwise,
  10.290 +     *         a new file will be created.  The output will be written to the
  10.291 +     *         file and is buffered.
  10.292 +     *
  10.293 +     * @param  csn
  10.294 +     *         The name of a supported {@linkplain java.nio.charset.Charset
  10.295 +     *         charset}
  10.296 +     *
  10.297 +     * @throws  FileNotFoundException
  10.298 +     *          If the given file object does not denote an existing, writable
  10.299 +     *          regular file and a new regular file of that name cannot be
  10.300 +     *          created, or if some other error occurs while opening or
  10.301 +     *          creating the file
  10.302 +     *
  10.303 +     * @throws  SecurityException
  10.304 +     *          If a security manager is presentand {@link
  10.305 +     *          SecurityManager#checkWrite checkWrite(file.getPath())}
  10.306 +     *          denies write access to the file
  10.307 +     *
  10.308 +     * @throws  UnsupportedEncodingException
  10.309 +     *          If the named charset is not supported
  10.310 +     *
  10.311 +     * @since  1.5
  10.312 +     */
  10.313 +    public PrintStream(File file, String csn)
  10.314 +        throws FileNotFoundException, UnsupportedEncodingException
  10.315 +    {
  10.316 +        super(null);
  10.317 +        throw new FileNotFoundException();
  10.318 +    }
  10.319 +
  10.320 +    /** Check to make sure that the stream has not been closed */
  10.321 +    private void ensureOpen() throws IOException {
  10.322 +        if (out == null)
  10.323 +            throw new IOException("Stream closed");
  10.324 +    }
  10.325 +
  10.326 +    /**
  10.327 +     * Flushes the stream.  This is done by writing any buffered output bytes to
  10.328 +     * the underlying output stream and then flushing that stream.
  10.329 +     *
  10.330 +     * @see        java.io.OutputStream#flush()
  10.331 +     */
  10.332 +    public void flush() {
  10.333 +        synchronized (this) {
  10.334 +            try {
  10.335 +                ensureOpen();
  10.336 +                out.flush();
  10.337 +            }
  10.338 +            catch (IOException x) {
  10.339 +                trouble = true;
  10.340 +            }
  10.341 +        }
  10.342 +    }
  10.343 +
  10.344 +    private boolean closing = false; /* To avoid recursive closing */
  10.345 +
  10.346 +    /**
  10.347 +     * Closes the stream.  This is done by flushing the stream and then closing
  10.348 +     * the underlying output stream.
  10.349 +     *
  10.350 +     * @see        java.io.OutputStream#close()
  10.351 +     */
  10.352 +    public void close() {
  10.353 +        synchronized (this) {
  10.354 +            if (! closing) {
  10.355 +                closing = true;
  10.356 +                try {
  10.357 +                    textOut.close();
  10.358 +                    out.close();
  10.359 +                }
  10.360 +                catch (IOException x) {
  10.361 +                    trouble = true;
  10.362 +                }
  10.363 +                textOut = null;
  10.364 +                charOut = null;
  10.365 +                out = null;
  10.366 +            }
  10.367 +        }
  10.368 +    }
  10.369 +
  10.370 +    /**
  10.371 +     * Flushes the stream and checks its error state. The internal error state
  10.372 +     * is set to <code>true</code> when the underlying output stream throws an
  10.373 +     * <code>IOException</code> other than <code>InterruptedIOException</code>,
  10.374 +     * and when the <code>setError</code> method is invoked.  If an operation
  10.375 +     * on the underlying output stream throws an
  10.376 +     * <code>InterruptedIOException</code>, then the <code>PrintStream</code>
  10.377 +     * converts the exception back into an interrupt by doing:
  10.378 +     * <pre>
  10.379 +     *     Thread.currentThread().interrupt();
  10.380 +     * </pre>
  10.381 +     * or the equivalent.
  10.382 +     *
  10.383 +     * @return <code>true</code> if and only if this stream has encountered an
  10.384 +     *         <code>IOException</code> other than
  10.385 +     *         <code>InterruptedIOException</code>, or the
  10.386 +     *         <code>setError</code> method has been invoked
  10.387 +     */
  10.388 +    public boolean checkError() {
  10.389 +        if (out != null)
  10.390 +            flush();
  10.391 +        if (out instanceof java.io.PrintStream) {
  10.392 +            PrintStream ps = (PrintStream) out;
  10.393 +            return ps.checkError();
  10.394 +        }
  10.395 +        return trouble;
  10.396 +    }
  10.397 +
  10.398 +    /**
  10.399 +     * Sets the error state of the stream to <code>true</code>.
  10.400 +     *
  10.401 +     * <p> This method will cause subsequent invocations of {@link
  10.402 +     * #checkError()} to return <tt>true</tt> until {@link
  10.403 +     * #clearError()} is invoked.
  10.404 +     *
  10.405 +     * @since JDK1.1
  10.406 +     */
  10.407 +    protected void setError() {
  10.408 +        trouble = true;
  10.409 +    }
  10.410 +
  10.411 +    /**
  10.412 +     * Clears the internal error state of this stream.
  10.413 +     *
  10.414 +     * <p> This method will cause subsequent invocations of {@link
  10.415 +     * #checkError()} to return <tt>false</tt> until another write
  10.416 +     * operation fails and invokes {@link #setError()}.
  10.417 +     *
  10.418 +     * @since 1.6
  10.419 +     */
  10.420 +    protected void clearError() {
  10.421 +        trouble = false;
  10.422 +    }
  10.423 +
  10.424 +    /*
  10.425 +     * Exception-catching, synchronized output operations,
  10.426 +     * which also implement the write() methods of OutputStream
  10.427 +     */
  10.428 +
  10.429 +    /**
  10.430 +     * Writes the specified byte to this stream.  If the byte is a newline and
  10.431 +     * automatic flushing is enabled then the <code>flush</code> method will be
  10.432 +     * invoked.
  10.433 +     *
  10.434 +     * <p> Note that the byte is written as given; to write a character that
  10.435 +     * will be translated according to the platform's default character
  10.436 +     * encoding, use the <code>print(char)</code> or <code>println(char)</code>
  10.437 +     * methods.
  10.438 +     *
  10.439 +     * @param  b  The byte to be written
  10.440 +     * @see #print(char)
  10.441 +     * @see #println(char)
  10.442 +     */
  10.443 +    public void write(int b) {
  10.444 +        try {
  10.445 +            synchronized (this) {
  10.446 +                ensureOpen();
  10.447 +                out.write(b);
  10.448 +                if ((b == '\n') && autoFlush)
  10.449 +                    out.flush();
  10.450 +            }
  10.451 +        }
  10.452 +        catch (InterruptedIOException x) {
  10.453 +            Thread.currentThread().interrupt();
  10.454 +        }
  10.455 +        catch (IOException x) {
  10.456 +            trouble = true;
  10.457 +        }
  10.458 +    }
  10.459 +
  10.460 +    /**
  10.461 +     * Writes <code>len</code> bytes from the specified byte array starting at
  10.462 +     * offset <code>off</code> to this stream.  If automatic flushing is
  10.463 +     * enabled then the <code>flush</code> method will be invoked.
  10.464 +     *
  10.465 +     * <p> Note that the bytes will be written as given; to write characters
  10.466 +     * that will be translated according to the platform's default character
  10.467 +     * encoding, use the <code>print(char)</code> or <code>println(char)</code>
  10.468 +     * methods.
  10.469 +     *
  10.470 +     * @param  buf   A byte array
  10.471 +     * @param  off   Offset from which to start taking bytes
  10.472 +     * @param  len   Number of bytes to write
  10.473 +     */
  10.474 +    public void write(byte buf[], int off, int len) {
  10.475 +        try {
  10.476 +            synchronized (this) {
  10.477 +                ensureOpen();
  10.478 +                out.write(buf, off, len);
  10.479 +                if (autoFlush)
  10.480 +                    out.flush();
  10.481 +            }
  10.482 +        }
  10.483 +        catch (InterruptedIOException x) {
  10.484 +            Thread.currentThread().interrupt();
  10.485 +        }
  10.486 +        catch (IOException x) {
  10.487 +            trouble = true;
  10.488 +        }
  10.489 +    }
  10.490 +
  10.491 +    /*
  10.492 +     * The following private methods on the text- and character-output streams
  10.493 +     * always flush the stream buffers, so that writes to the underlying byte
  10.494 +     * stream occur as promptly as with the original PrintStream.
  10.495 +     */
  10.496 +
  10.497 +    private void write(char buf[]) {
  10.498 +        try {
  10.499 +            synchronized (this) {
  10.500 +                ensureOpen();
  10.501 +                textOut.write(buf);
  10.502 +                textOut.flushBuffer();
  10.503 +                charOut.flushBuffer();
  10.504 +                if (autoFlush) {
  10.505 +                    for (int i = 0; i < buf.length; i++)
  10.506 +                        if (buf[i] == '\n')
  10.507 +                            out.flush();
  10.508 +                }
  10.509 +            }
  10.510 +        }
  10.511 +        catch (InterruptedIOException x) {
  10.512 +            Thread.currentThread().interrupt();
  10.513 +        }
  10.514 +        catch (IOException x) {
  10.515 +            trouble = true;
  10.516 +        }
  10.517 +    }
  10.518 +
  10.519 +    private void write(String s) {
  10.520 +        try {
  10.521 +            synchronized (this) {
  10.522 +                ensureOpen();
  10.523 +                textOut.write(s);
  10.524 +                textOut.flushBuffer();
  10.525 +                charOut.flushBuffer();
  10.526 +                if (autoFlush && (s.indexOf('\n') >= 0))
  10.527 +                    out.flush();
  10.528 +            }
  10.529 +        }
  10.530 +        catch (InterruptedIOException x) {
  10.531 +            Thread.currentThread().interrupt();
  10.532 +        }
  10.533 +        catch (IOException x) {
  10.534 +            trouble = true;
  10.535 +        }
  10.536 +    }
  10.537 +
  10.538 +    private void newLine() {
  10.539 +        try {
  10.540 +            synchronized (this) {
  10.541 +                ensureOpen();
  10.542 +                textOut.newLine();
  10.543 +                textOut.flushBuffer();
  10.544 +                charOut.flushBuffer();
  10.545 +                if (autoFlush)
  10.546 +                    out.flush();
  10.547 +            }
  10.548 +        }
  10.549 +        catch (InterruptedIOException x) {
  10.550 +            Thread.currentThread().interrupt();
  10.551 +        }
  10.552 +        catch (IOException x) {
  10.553 +            trouble = true;
  10.554 +        }
  10.555 +    }
  10.556 +
  10.557 +    /* Methods that do not terminate lines */
  10.558 +
  10.559 +    /**
  10.560 +     * Prints a boolean value.  The string produced by <code>{@link
  10.561 +     * java.lang.String#valueOf(boolean)}</code> is translated into bytes
  10.562 +     * according to the platform's default character encoding, and these bytes
  10.563 +     * are written in exactly the manner of the
  10.564 +     * <code>{@link #write(int)}</code> method.
  10.565 +     *
  10.566 +     * @param      b   The <code>boolean</code> to be printed
  10.567 +     */
  10.568 +    public void print(boolean b) {
  10.569 +        write(b ? "true" : "false");
  10.570 +    }
  10.571 +
  10.572 +    /**
  10.573 +     * Prints a character.  The character is translated into one or more bytes
  10.574 +     * according to the platform's default character encoding, and these bytes
  10.575 +     * are written in exactly the manner of the
  10.576 +     * <code>{@link #write(int)}</code> method.
  10.577 +     *
  10.578 +     * @param      c   The <code>char</code> to be printed
  10.579 +     */
  10.580 +    public void print(char c) {
  10.581 +        write(String.valueOf(c));
  10.582 +    }
  10.583 +
  10.584 +    /**
  10.585 +     * Prints an integer.  The string produced by <code>{@link
  10.586 +     * java.lang.String#valueOf(int)}</code> is translated into bytes
  10.587 +     * according to the platform's default character encoding, and these bytes
  10.588 +     * are written in exactly the manner of the
  10.589 +     * <code>{@link #write(int)}</code> method.
  10.590 +     *
  10.591 +     * @param      i   The <code>int</code> to be printed
  10.592 +     * @see        java.lang.Integer#toString(int)
  10.593 +     */
  10.594 +    public void print(int i) {
  10.595 +        write(String.valueOf(i));
  10.596 +    }
  10.597 +
  10.598 +    /**
  10.599 +     * Prints a long integer.  The string produced by <code>{@link
  10.600 +     * java.lang.String#valueOf(long)}</code> is translated into bytes
  10.601 +     * according to the platform's default character encoding, and these bytes
  10.602 +     * are written in exactly the manner of the
  10.603 +     * <code>{@link #write(int)}</code> method.
  10.604 +     *
  10.605 +     * @param      l   The <code>long</code> to be printed
  10.606 +     * @see        java.lang.Long#toString(long)
  10.607 +     */
  10.608 +    public void print(long l) {
  10.609 +        write(String.valueOf(l));
  10.610 +    }
  10.611 +
  10.612 +    /**
  10.613 +     * Prints a floating-point number.  The string produced by <code>{@link
  10.614 +     * java.lang.String#valueOf(float)}</code> is translated into bytes
  10.615 +     * according to the platform's default character encoding, and these bytes
  10.616 +     * are written in exactly the manner of the
  10.617 +     * <code>{@link #write(int)}</code> method.
  10.618 +     *
  10.619 +     * @param      f   The <code>float</code> to be printed
  10.620 +     * @see        java.lang.Float#toString(float)
  10.621 +     */
  10.622 +    public void print(float f) {
  10.623 +        write(String.valueOf(f));
  10.624 +    }
  10.625 +
  10.626 +    /**
  10.627 +     * Prints a double-precision floating-point number.  The string produced by
  10.628 +     * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
  10.629 +     * bytes according to the platform's default character encoding, and these
  10.630 +     * bytes are written in exactly the manner of the <code>{@link
  10.631 +     * #write(int)}</code> method.
  10.632 +     *
  10.633 +     * @param      d   The <code>double</code> to be printed
  10.634 +     * @see        java.lang.Double#toString(double)
  10.635 +     */
  10.636 +    public void print(double d) {
  10.637 +        write(String.valueOf(d));
  10.638 +    }
  10.639 +
  10.640 +    /**
  10.641 +     * Prints an array of characters.  The characters are converted into bytes
  10.642 +     * according to the platform's default character encoding, and these bytes
  10.643 +     * are written in exactly the manner of the
  10.644 +     * <code>{@link #write(int)}</code> method.
  10.645 +     *
  10.646 +     * @param      s   The array of chars to be printed
  10.647 +     *
  10.648 +     * @throws  NullPointerException  If <code>s</code> is <code>null</code>
  10.649 +     */
  10.650 +    public void print(char s[]) {
  10.651 +        write(s);
  10.652 +    }
  10.653 +
  10.654 +    /**
  10.655 +     * Prints a string.  If the argument is <code>null</code> then the string
  10.656 +     * <code>"null"</code> is printed.  Otherwise, the string's characters are
  10.657 +     * converted into bytes according to the platform's default character
  10.658 +     * encoding, and these bytes are written in exactly the manner of the
  10.659 +     * <code>{@link #write(int)}</code> method.
  10.660 +     *
  10.661 +     * @param      s   The <code>String</code> to be printed
  10.662 +     */
  10.663 +    public void print(String s) {
  10.664 +        if (s == null) {
  10.665 +            s = "null";
  10.666 +        }
  10.667 +        write(s);
  10.668 +    }
  10.669 +
  10.670 +    /**
  10.671 +     * Prints an object.  The string produced by the <code>{@link
  10.672 +     * java.lang.String#valueOf(Object)}</code> method is translated into bytes
  10.673 +     * according to the platform's default character encoding, and these bytes
  10.674 +     * are written in exactly the manner of the
  10.675 +     * <code>{@link #write(int)}</code> method.
  10.676 +     *
  10.677 +     * @param      obj   The <code>Object</code> to be printed
  10.678 +     * @see        java.lang.Object#toString()
  10.679 +     */
  10.680 +    public void print(Object obj) {
  10.681 +        write(String.valueOf(obj));
  10.682 +    }
  10.683 +
  10.684 +
  10.685 +    /* Methods that do terminate lines */
  10.686 +
  10.687 +    /**
  10.688 +     * Terminates the current line by writing the line separator string.  The
  10.689 +     * line separator string is defined by the system property
  10.690 +     * <code>line.separator</code>, and is not necessarily a single newline
  10.691 +     * character (<code>'\n'</code>).
  10.692 +     */
  10.693 +    public void println() {
  10.694 +        newLine();
  10.695 +    }
  10.696 +
  10.697 +    /**
  10.698 +     * Prints a boolean and then terminate the line.  This method behaves as
  10.699 +     * though it invokes <code>{@link #print(boolean)}</code> and then
  10.700 +     * <code>{@link #println()}</code>.
  10.701 +     *
  10.702 +     * @param x  The <code>boolean</code> to be printed
  10.703 +     */
  10.704 +    public void println(boolean x) {
  10.705 +        synchronized (this) {
  10.706 +            print(x);
  10.707 +            newLine();
  10.708 +        }
  10.709 +    }
  10.710 +
  10.711 +    /**
  10.712 +     * Prints a character and then terminate the line.  This method behaves as
  10.713 +     * though it invokes <code>{@link #print(char)}</code> and then
  10.714 +     * <code>{@link #println()}</code>.
  10.715 +     *
  10.716 +     * @param x  The <code>char</code> to be printed.
  10.717 +     */
  10.718 +    public void println(char x) {
  10.719 +        synchronized (this) {
  10.720 +            print(x);
  10.721 +            newLine();
  10.722 +        }
  10.723 +    }
  10.724 +
  10.725 +    /**
  10.726 +     * Prints an integer and then terminate the line.  This method behaves as
  10.727 +     * though it invokes <code>{@link #print(int)}</code> and then
  10.728 +     * <code>{@link #println()}</code>.
  10.729 +     *
  10.730 +     * @param x  The <code>int</code> to be printed.
  10.731 +     */
  10.732 +    public void println(int x) {
  10.733 +        synchronized (this) {
  10.734 +            print(x);
  10.735 +            newLine();
  10.736 +        }
  10.737 +    }
  10.738 +
  10.739 +    /**
  10.740 +     * Prints a long and then terminate the line.  This method behaves as
  10.741 +     * though it invokes <code>{@link #print(long)}</code> and then
  10.742 +     * <code>{@link #println()}</code>.
  10.743 +     *
  10.744 +     * @param x  a The <code>long</code> to be printed.
  10.745 +     */
  10.746 +    public void println(long x) {
  10.747 +        synchronized (this) {
  10.748 +            print(x);
  10.749 +            newLine();
  10.750 +        }
  10.751 +    }
  10.752 +
  10.753 +    /**
  10.754 +     * Prints a float and then terminate the line.  This method behaves as
  10.755 +     * though it invokes <code>{@link #print(float)}</code> and then
  10.756 +     * <code>{@link #println()}</code>.
  10.757 +     *
  10.758 +     * @param x  The <code>float</code> to be printed.
  10.759 +     */
  10.760 +    public void println(float x) {
  10.761 +        synchronized (this) {
  10.762 +            print(x);
  10.763 +            newLine();
  10.764 +        }
  10.765 +    }
  10.766 +
  10.767 +    /**
  10.768 +     * Prints a double and then terminate the line.  This method behaves as
  10.769 +     * though it invokes <code>{@link #print(double)}</code> and then
  10.770 +     * <code>{@link #println()}</code>.
  10.771 +     *
  10.772 +     * @param x  The <code>double</code> to be printed.
  10.773 +     */
  10.774 +    public void println(double x) {
  10.775 +        synchronized (this) {
  10.776 +            print(x);
  10.777 +            newLine();
  10.778 +        }
  10.779 +    }
  10.780 +
  10.781 +    /**
  10.782 +     * Prints an array of characters and then terminate the line.  This method
  10.783 +     * behaves as though it invokes <code>{@link #print(char[])}</code> and
  10.784 +     * then <code>{@link #println()}</code>.
  10.785 +     *
  10.786 +     * @param x  an array of chars to print.
  10.787 +     */
  10.788 +    public void println(char x[]) {
  10.789 +        synchronized (this) {
  10.790 +            print(x);
  10.791 +            newLine();
  10.792 +        }
  10.793 +    }
  10.794 +
  10.795 +    /**
  10.796 +     * Prints a String and then terminate the line.  This method behaves as
  10.797 +     * though it invokes <code>{@link #print(String)}</code> and then
  10.798 +     * <code>{@link #println()}</code>.
  10.799 +     *
  10.800 +     * @param x  The <code>String</code> to be printed.
  10.801 +     */
  10.802 +    public void println(String x) {
  10.803 +        synchronized (this) {
  10.804 +            print(x);
  10.805 +            newLine();
  10.806 +        }
  10.807 +    }
  10.808 +
  10.809 +    /**
  10.810 +     * Prints an Object and then terminate the line.  This method calls
  10.811 +     * at first String.valueOf(x) to get the printed object's string value,
  10.812 +     * then behaves as
  10.813 +     * though it invokes <code>{@link #print(String)}</code> and then
  10.814 +     * <code>{@link #println()}</code>.
  10.815 +     *
  10.816 +     * @param x  The <code>Object</code> to be printed.
  10.817 +     */
  10.818 +    public void println(Object x) {
  10.819 +        String s = String.valueOf(x);
  10.820 +        synchronized (this) {
  10.821 +            print(s);
  10.822 +            newLine();
  10.823 +        }
  10.824 +    }
  10.825 +
  10.826 +
  10.827 +    /**
  10.828 +     * A convenience method to write a formatted string to this output stream
  10.829 +     * using the specified format string and arguments.
  10.830 +     *
  10.831 +     * <p> An invocation of this method of the form <tt>out.printf(format,
  10.832 +     * args)</tt> behaves in exactly the same way as the invocation
  10.833 +     *
  10.834 +     * <pre>
  10.835 +     *     out.format(format, args) </pre>
  10.836 +     *
  10.837 +     * @param  format
  10.838 +     *         A format string as described in <a
  10.839 +     *         href="../util/Formatter.html#syntax">Format string syntax</a>
  10.840 +     *
  10.841 +     * @param  args
  10.842 +     *         Arguments referenced by the format specifiers in the format
  10.843 +     *         string.  If there are more arguments than format specifiers, the
  10.844 +     *         extra arguments are ignored.  The number of arguments is
  10.845 +     *         variable and may be zero.  The maximum number of arguments is
  10.846 +     *         limited by the maximum dimension of a Java array as defined by
  10.847 +     *         <cite>The Java&trade; Virtual Machine Specification</cite>.
  10.848 +     *         The behaviour on a
  10.849 +     *         <tt>null</tt> argument depends on the <a
  10.850 +     *         href="../util/Formatter.html#syntax">conversion</a>.
  10.851 +     *
  10.852 +     * @throws  IllegalFormatException
  10.853 +     *          If a format string contains an illegal syntax, a format
  10.854 +     *          specifier that is incompatible with the given arguments,
  10.855 +     *          insufficient arguments given the format string, or other
  10.856 +     *          illegal conditions.  For specification of all possible
  10.857 +     *          formatting errors, see the <a
  10.858 +     *          href="../util/Formatter.html#detail">Details</a> section of the
  10.859 +     *          formatter class specification.
  10.860 +     *
  10.861 +     * @throws  NullPointerException
  10.862 +     *          If the <tt>format</tt> is <tt>null</tt>
  10.863 +     *
  10.864 +     * @return  This output stream
  10.865 +     *
  10.866 +     * @since  1.5
  10.867 +     */
  10.868 +    public PrintStream printf(String format, Object ... args) {
  10.869 +        append(format).append(Arrays.toString(args));
  10.870 +        return this;
  10.871 +    }
  10.872 +
  10.873 +    /**
  10.874 +     * A convenience method to write a formatted string to this output stream
  10.875 +     * using the specified format string and arguments.
  10.876 +     *
  10.877 +     * <p> An invocation of this method of the form <tt>out.printf(l, format,
  10.878 +     * args)</tt> behaves in exactly the same way as the invocation
  10.879 +     *
  10.880 +     * <pre>
  10.881 +     *     out.format(l, format, args) </pre>
  10.882 +     *
  10.883 +     * @param  l
  10.884 +     *         The {@linkplain java.util.Locale locale} to apply during
  10.885 +     *         formatting.  If <tt>l</tt> is <tt>null</tt> then no localization
  10.886 +     *         is applied.
  10.887 +     *
  10.888 +     * @param  format
  10.889 +     *         A format string as described in <a
  10.890 +     *         href="../util/Formatter.html#syntax">Format string syntax</a>
  10.891 +     *
  10.892 +     * @param  args
  10.893 +     *         Arguments referenced by the format specifiers in the format
  10.894 +     *         string.  If there are more arguments than format specifiers, the
  10.895 +     *         extra arguments are ignored.  The number of arguments is
  10.896 +     *         variable and may be zero.  The maximum number of arguments is
  10.897 +     *         limited by the maximum dimension of a Java array as defined by
  10.898 +     *         <cite>The Java&trade; Virtual Machine Specification</cite>.
  10.899 +     *         The behaviour on a
  10.900 +     *         <tt>null</tt> argument depends on the <a
  10.901 +     *         href="../util/Formatter.html#syntax">conversion</a>.
  10.902 +     *
  10.903 +     * @throws  IllegalFormatException
  10.904 +     *          If a format string contains an illegal syntax, a format
  10.905 +     *          specifier that is incompatible with the given arguments,
  10.906 +     *          insufficient arguments given the format string, or other
  10.907 +     *          illegal conditions.  For specification of all possible
  10.908 +     *          formatting errors, see the <a
  10.909 +     *          href="../util/Formatter.html#detail">Details</a> section of the
  10.910 +     *          formatter class specification.
  10.911 +     *
  10.912 +     * @throws  NullPointerException
  10.913 +     *          If the <tt>format</tt> is <tt>null</tt>
  10.914 +     *
  10.915 +     * @return  This output stream
  10.916 +     *
  10.917 +     * @since  1.5
  10.918 +     */
  10.919 +//    public PrintStream printf(Locale l, String format, Object ... args) {
  10.920 +//        return format(l, format, args);
  10.921 +//    }
  10.922 +
  10.923 +    /**
  10.924 +     * Writes a formatted string to this output stream using the specified
  10.925 +     * format string and arguments.
  10.926 +     *
  10.927 +     * <p> The locale always used is the one returned by {@link
  10.928 +     * java.util.Locale#getDefault() Locale.getDefault()}, regardless of any
  10.929 +     * previous invocations of other formatting methods on this object.
  10.930 +     *
  10.931 +     * @param  format
  10.932 +     *         A format string as described in <a
  10.933 +     *         href="../util/Formatter.html#syntax">Format string syntax</a>
  10.934 +     *
  10.935 +     * @param  args
  10.936 +     *         Arguments referenced by the format specifiers in the format
  10.937 +     *         string.  If there are more arguments than format specifiers, the
  10.938 +     *         extra arguments are ignored.  The number of arguments is
  10.939 +     *         variable and may be zero.  The maximum number of arguments is
  10.940 +     *         limited by the maximum dimension of a Java array as defined by
  10.941 +     *         <cite>The Java&trade; Virtual Machine Specification</cite>.
  10.942 +     *         The behaviour on a
  10.943 +     *         <tt>null</tt> argument depends on the <a
  10.944 +     *         href="../util/Formatter.html#syntax">conversion</a>.
  10.945 +     *
  10.946 +     * @throws  IllegalFormatException
  10.947 +     *          If a format string contains an illegal syntax, a format
  10.948 +     *          specifier that is incompatible with the given arguments,
  10.949 +     *          insufficient arguments given the format string, or other
  10.950 +     *          illegal conditions.  For specification of all possible
  10.951 +     *          formatting errors, see the <a
  10.952 +     *          href="../util/Formatter.html#detail">Details</a> section of the
  10.953 +     *          formatter class specification.
  10.954 +     *
  10.955 +     * @throws  NullPointerException
  10.956 +     *          If the <tt>format</tt> is <tt>null</tt>
  10.957 +     *
  10.958 +     * @return  This output stream
  10.959 +     *
  10.960 +     * @since  1.5
  10.961 +     */
  10.962 +//    public PrintStream format(String format, Object ... args) {
  10.963 +//        try {
  10.964 +//            synchronized (this) {
  10.965 +//                ensureOpen();
  10.966 +//                if ((formatter == null)
  10.967 +//                    || (formatter.locale() != Locale.getDefault()))
  10.968 +//                    formatter = new Formatter((Appendable) this);
  10.969 +//                formatter.format(Locale.getDefault(), format, args);
  10.970 +//            }
  10.971 +//        } catch (InterruptedIOException x) {
  10.972 +//            Thread.currentThread().interrupt();
  10.973 +//        } catch (IOException x) {
  10.974 +//            trouble = true;
  10.975 +//        }
  10.976 +//        return this;
  10.977 +//    }
  10.978 +
  10.979 +    /**
  10.980 +     * Writes a formatted string to this output stream using the specified
  10.981 +     * format string and arguments.
  10.982 +     *
  10.983 +     * @param  l
  10.984 +     *         The {@linkplain java.util.Locale locale} to apply during
  10.985 +     *         formatting.  If <tt>l</tt> is <tt>null</tt> then no localization
  10.986 +     *         is applied.
  10.987 +     *
  10.988 +     * @param  format
  10.989 +     *         A format string as described in <a
  10.990 +     *         href="../util/Formatter.html#syntax">Format string syntax</a>
  10.991 +     *
  10.992 +     * @param  args
  10.993 +     *         Arguments referenced by the format specifiers in the format
  10.994 +     *         string.  If there are more arguments than format specifiers, the
  10.995 +     *         extra arguments are ignored.  The number of arguments is
  10.996 +     *         variable and may be zero.  The maximum number of arguments is
  10.997 +     *         limited by the maximum dimension of a Java array as defined by
  10.998 +     *         <cite>The Java&trade; Virtual Machine Specification</cite>.
  10.999 +     *         The behaviour on a
 10.1000 +     *         <tt>null</tt> argument depends on the <a
 10.1001 +     *         href="../util/Formatter.html#syntax">conversion</a>.
 10.1002 +     *
 10.1003 +     * @throws  IllegalFormatException
 10.1004 +     *          If a format string contains an illegal syntax, a format
 10.1005 +     *          specifier that is incompatible with the given arguments,
 10.1006 +     *          insufficient arguments given the format string, or other
 10.1007 +     *          illegal conditions.  For specification of all possible
 10.1008 +     *          formatting errors, see the <a
 10.1009 +     *          href="../util/Formatter.html#detail">Details</a> section of the
 10.1010 +     *          formatter class specification.
 10.1011 +     *
 10.1012 +     * @throws  NullPointerException
 10.1013 +     *          If the <tt>format</tt> is <tt>null</tt>
 10.1014 +     *
 10.1015 +     * @return  This output stream
 10.1016 +     *
 10.1017 +     * @since  1.5
 10.1018 +     */
 10.1019 +////    public PrintStream format(Locale l, String format, Object ... args) {
 10.1020 +////        try {
 10.1021 +////            synchronized (this) {
 10.1022 +////                ensureOpen();
 10.1023 +////                if ((formatter == null)
 10.1024 +////                    || (formatter.locale() != l))
 10.1025 +////                    formatter = new Formatter(this, l);
 10.1026 +////                formatter.format(l, format, args);
 10.1027 +////            }
 10.1028 +////        } catch (InterruptedIOException x) {
 10.1029 +////            Thread.currentThread().interrupt();
 10.1030 +////        } catch (IOException x) {
 10.1031 +////            trouble = true;
 10.1032 +////        }
 10.1033 +////        return this;
 10.1034 +////    }
 10.1035 +
 10.1036 +    /**
 10.1037 +     * Appends the specified character sequence to this output stream.
 10.1038 +     *
 10.1039 +     * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
 10.1040 +     * behaves in exactly the same way as the invocation
 10.1041 +     *
 10.1042 +     * <pre>
 10.1043 +     *     out.print(csq.toString()) </pre>
 10.1044 +     *
 10.1045 +     * <p> Depending on the specification of <tt>toString</tt> for the
 10.1046 +     * character sequence <tt>csq</tt>, the entire sequence may not be
 10.1047 +     * appended.  For instance, invoking then <tt>toString</tt> method of a
 10.1048 +     * character buffer will return a subsequence whose content depends upon
 10.1049 +     * the buffer's position and limit.
 10.1050 +     *
 10.1051 +     * @param  csq
 10.1052 +     *         The character sequence to append.  If <tt>csq</tt> is
 10.1053 +     *         <tt>null</tt>, then the four characters <tt>"null"</tt> are
 10.1054 +     *         appended to this output stream.
 10.1055 +     *
 10.1056 +     * @return  This output stream
 10.1057 +     *
 10.1058 +     * @since  1.5
 10.1059 +     */
 10.1060 +    public PrintStream append(CharSequence csq) {
 10.1061 +        if (csq == null)
 10.1062 +            print("null");
 10.1063 +        else
 10.1064 +            print(csq.toString());
 10.1065 +        return this;
 10.1066 +    }
 10.1067 +
 10.1068 +    /**
 10.1069 +     * Appends a subsequence of the specified character sequence to this output
 10.1070 +     * stream.
 10.1071 +     *
 10.1072 +     * <p> An invocation of this method of the form <tt>out.append(csq, start,
 10.1073 +     * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
 10.1074 +     * exactly the same way as the invocation
 10.1075 +     *
 10.1076 +     * <pre>
 10.1077 +     *     out.print(csq.subSequence(start, end).toString()) </pre>
 10.1078 +     *
 10.1079 +     * @param  csq
 10.1080 +     *         The character sequence from which a subsequence will be
 10.1081 +     *         appended.  If <tt>csq</tt> is <tt>null</tt>, then characters
 10.1082 +     *         will be appended as if <tt>csq</tt> contained the four
 10.1083 +     *         characters <tt>"null"</tt>.
 10.1084 +     *
 10.1085 +     * @param  start
 10.1086 +     *         The index of the first character in the subsequence
 10.1087 +     *
 10.1088 +     * @param  end
 10.1089 +     *         The index of the character following the last character in the
 10.1090 +     *         subsequence
 10.1091 +     *
 10.1092 +     * @return  This output stream
 10.1093 +     *
 10.1094 +     * @throws  IndexOutOfBoundsException
 10.1095 +     *          If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
 10.1096 +     *          is greater than <tt>end</tt>, or <tt>end</tt> is greater than
 10.1097 +     *          <tt>csq.length()</tt>
 10.1098 +     *
 10.1099 +     * @since  1.5
 10.1100 +     */
 10.1101 +    public PrintStream append(CharSequence csq, int start, int end) {
 10.1102 +        CharSequence cs = (csq == null ? "null" : csq);
 10.1103 +        write(cs.subSequence(start, end).toString());
 10.1104 +        return this;
 10.1105 +    }
 10.1106 +
 10.1107 +    /**
 10.1108 +     * Appends the specified character to this output stream.
 10.1109 +     *
 10.1110 +     * <p> An invocation of this method of the form <tt>out.append(c)</tt>
 10.1111 +     * behaves in exactly the same way as the invocation
 10.1112 +     *
 10.1113 +     * <pre>
 10.1114 +     *     out.print(c) </pre>
 10.1115 +     *
 10.1116 +     * @param  c
 10.1117 +     *         The 16-bit character to append
 10.1118 +     *
 10.1119 +     * @return  This output stream
 10.1120 +     *
 10.1121 +     * @since  1.5
 10.1122 +     */
 10.1123 +    public PrintStream append(char c) {
 10.1124 +        print(c);
 10.1125 +        return this;
 10.1126 +    }
 10.1127 +
 10.1128 +}
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/rt/emul/compact/src/main/java/java/io/PrintWriter.java	Sun Sep 08 11:22:51 2013 +0200
    11.3 @@ -0,0 +1,1030 @@
    11.4 +/*
    11.5 + * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
    11.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    11.7 + *
    11.8 + * This code is free software; you can redistribute it and/or modify it
    11.9 + * under the terms of the GNU General Public License version 2 only, as
   11.10 + * published by the Free Software Foundation.  Oracle designates this
   11.11 + * particular file as subject to the "Classpath" exception as provided
   11.12 + * by Oracle in the LICENSE file that accompanied this code.
   11.13 + *
   11.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   11.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   11.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   11.17 + * version 2 for more details (a copy is included in the LICENSE file that
   11.18 + * accompanied this code).
   11.19 + *
   11.20 + * You should have received a copy of the GNU General Public License version
   11.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   11.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   11.23 + *
   11.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   11.25 + * or visit www.oracle.com if you need additional information or have any
   11.26 + * questions.
   11.27 + */
   11.28 +
   11.29 +package java.io;
   11.30 +
   11.31 +import java.io.PrintStream.Charset;
   11.32 +import java.io.PrintStream.Formatter;
   11.33 +import java.util.Arrays;
   11.34 +import java.util.Objects;
   11.35 +
   11.36 +/**
   11.37 + * Prints formatted representations of objects to a text-output stream.  This
   11.38 + * class implements all of the <tt>print</tt> methods found in {@link
   11.39 + * PrintStream}.  It does not contain methods for writing raw bytes, for which
   11.40 + * a program should use unencoded byte streams.
   11.41 + *
   11.42 + * <p> Unlike the {@link PrintStream} class, if automatic flushing is enabled
   11.43 + * it will be done only when one of the <tt>println</tt>, <tt>printf</tt>, or
   11.44 + * <tt>format</tt> methods is invoked, rather than whenever a newline character
   11.45 + * happens to be output.  These methods use the platform's own notion of line
   11.46 + * separator rather than the newline character.
   11.47 + *
   11.48 + * <p> Methods in this class never throw I/O exceptions, although some of its
   11.49 + * constructors may.  The client may inquire as to whether any errors have
   11.50 + * occurred by invoking {@link #checkError checkError()}.
   11.51 + *
   11.52 + * @author      Frank Yellin
   11.53 + * @author      Mark Reinhold
   11.54 + * @since       JDK1.1
   11.55 + */
   11.56 +
   11.57 +public class PrintWriter extends Writer {
   11.58 +
   11.59 +    /**
   11.60 +     * The underlying character-output stream of this
   11.61 +     * <code>PrintWriter</code>.
   11.62 +     *
   11.63 +     * @since 1.2
   11.64 +     */
   11.65 +    protected Writer out;
   11.66 +
   11.67 +    private final boolean autoFlush;
   11.68 +    private boolean trouble = false;
   11.69 +    private Formatter formatter;
   11.70 +//    private PrintStream psOut = null;
   11.71 +
   11.72 +    /**
   11.73 +     * Line separator string.  This is the value of the line.separator
   11.74 +     * property at the moment that the stream was created.
   11.75 +     */
   11.76 +    private final String lineSeparator;
   11.77 +
   11.78 +    /**
   11.79 +     * Returns a charset object for the given charset name.
   11.80 +     * @throws NullPointerException          is csn is null
   11.81 +     * @throws UnsupportedEncodingException  if the charset is not supported
   11.82 +     */
   11.83 +    private static Charset toCharset(String csn)
   11.84 +        throws UnsupportedEncodingException
   11.85 +    {
   11.86 +        return PrintStream.toCharset(csn);
   11.87 +    }
   11.88 +
   11.89 +    /**
   11.90 +     * Creates a new PrintWriter, without automatic line flushing.
   11.91 +     *
   11.92 +     * @param  out        A character-output stream
   11.93 +     */
   11.94 +    public PrintWriter (Writer out) {
   11.95 +        this(out, false);
   11.96 +    }
   11.97 +
   11.98 +    /**
   11.99 +     * Creates a new PrintWriter.
  11.100 +     *
  11.101 +     * @param  out        A character-output stream
  11.102 +     * @param  autoFlush  A boolean; if true, the <tt>println</tt>,
  11.103 +     *                    <tt>printf</tt>, or <tt>format</tt> methods will
  11.104 +     *                    flush the output buffer
  11.105 +     */
  11.106 +    public PrintWriter(Writer out,
  11.107 +                       boolean autoFlush) {
  11.108 +        super(out);
  11.109 +        this.out = out;
  11.110 +        this.autoFlush = autoFlush;
  11.111 +        lineSeparator = "\n";
  11.112 +    }
  11.113 +
  11.114 +    /**
  11.115 +     * Creates a new PrintWriter, without automatic line flushing, from an
  11.116 +     * existing OutputStream.  This convenience constructor creates the
  11.117 +     * necessary intermediate OutputStreamWriter, which will convert characters
  11.118 +     * into bytes using the default character encoding.
  11.119 +     *
  11.120 +     * @param  out        An output stream
  11.121 +     *
  11.122 +     * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
  11.123 +     */
  11.124 +    public PrintWriter(OutputStream out) {
  11.125 +        this(out, false);
  11.126 +    }
  11.127 +
  11.128 +    /**
  11.129 +     * Creates a new PrintWriter from an existing OutputStream.  This
  11.130 +     * convenience constructor creates the necessary intermediate
  11.131 +     * OutputStreamWriter, which will convert characters into bytes using the
  11.132 +     * default character encoding.
  11.133 +     *
  11.134 +     * @param  out        An output stream
  11.135 +     * @param  autoFlush  A boolean; if true, the <tt>println</tt>,
  11.136 +     *                    <tt>printf</tt>, or <tt>format</tt> methods will
  11.137 +     *                    flush the output buffer
  11.138 +     *
  11.139 +     * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
  11.140 +     */
  11.141 +    public PrintWriter(OutputStream out, boolean autoFlush) {
  11.142 +        this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
  11.143 +
  11.144 +        // save print stream for error propagation
  11.145 +//        if (out instanceof java.io.PrintStream) {
  11.146 +//            psOut = (PrintStream) out;
  11.147 +//        }
  11.148 +    }
  11.149 +
  11.150 +    /**
  11.151 +     * Creates a new PrintWriter, without automatic line flushing, with the
  11.152 +     * specified file name.  This convenience constructor creates the necessary
  11.153 +     * intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
  11.154 +     * which will encode characters using the {@linkplain
  11.155 +     * java.nio.charset.Charset#defaultCharset() default charset} for this
  11.156 +     * instance of the Java virtual machine.
  11.157 +     *
  11.158 +     * @param  fileName
  11.159 +     *         The name of the file to use as the destination of this writer.
  11.160 +     *         If the file exists then it will be truncated to zero size;
  11.161 +     *         otherwise, a new file will be created.  The output will be
  11.162 +     *         written to the file and is buffered.
  11.163 +     *
  11.164 +     * @throws  FileNotFoundException
  11.165 +     *          If the given string does not denote an existing, writable
  11.166 +     *          regular file and a new regular file of that name cannot be
  11.167 +     *          created, or if some other error occurs while opening or
  11.168 +     *          creating the file
  11.169 +     *
  11.170 +     * @throws  SecurityException
  11.171 +     *          If a security manager is present and {@link
  11.172 +     *          SecurityManager#checkWrite checkWrite(fileName)} denies write
  11.173 +     *          access to the file
  11.174 +     *
  11.175 +     * @since  1.5
  11.176 +     */
  11.177 +    public PrintWriter(String fileName) throws FileNotFoundException {
  11.178 +        super();
  11.179 +        throw new FileNotFoundException();
  11.180 +    }
  11.181 +
  11.182 +    /* Private constructor */
  11.183 +    private PrintWriter(Charset charset, File file)
  11.184 +        throws FileNotFoundException
  11.185 +    {
  11.186 +        super();
  11.187 +        throw new FileNotFoundException();
  11.188 +    }
  11.189 +
  11.190 +    /**
  11.191 +     * Creates a new PrintWriter, without automatic line flushing, with the
  11.192 +     * specified file name and charset.  This convenience constructor creates
  11.193 +     * the necessary intermediate {@link java.io.OutputStreamWriter
  11.194 +     * OutputStreamWriter}, which will encode characters using the provided
  11.195 +     * charset.
  11.196 +     *
  11.197 +     * @param  fileName
  11.198 +     *         The name of the file to use as the destination of this writer.
  11.199 +     *         If the file exists then it will be truncated to zero size;
  11.200 +     *         otherwise, a new file will be created.  The output will be
  11.201 +     *         written to the file and is buffered.
  11.202 +     *
  11.203 +     * @param  csn
  11.204 +     *         The name of a supported {@linkplain java.nio.charset.Charset
  11.205 +     *         charset}
  11.206 +     *
  11.207 +     * @throws  FileNotFoundException
  11.208 +     *          If the given string does not denote an existing, writable
  11.209 +     *          regular file and a new regular file of that name cannot be
  11.210 +     *          created, or if some other error occurs while opening or
  11.211 +     *          creating the file
  11.212 +     *
  11.213 +     * @throws  SecurityException
  11.214 +     *          If a security manager is present and {@link
  11.215 +     *          SecurityManager#checkWrite checkWrite(fileName)} denies write
  11.216 +     *          access to the file
  11.217 +     *
  11.218 +     * @throws  UnsupportedEncodingException
  11.219 +     *          If the named charset is not supported
  11.220 +     *
  11.221 +     * @since  1.5
  11.222 +     */
  11.223 +    public PrintWriter(String fileName, String csn)
  11.224 +        throws FileNotFoundException, UnsupportedEncodingException
  11.225 +    {
  11.226 +        this(toCharset(csn), new File(fileName));
  11.227 +    }
  11.228 +
  11.229 +    /**
  11.230 +     * Creates a new PrintWriter, without automatic line flushing, with the
  11.231 +     * specified file.  This convenience constructor creates the necessary
  11.232 +     * intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
  11.233 +     * which will encode characters using the {@linkplain
  11.234 +     * java.nio.charset.Charset#defaultCharset() default charset} for this
  11.235 +     * instance of the Java virtual machine.
  11.236 +     *
  11.237 +     * @param  file
  11.238 +     *         The file to use as the destination of this writer.  If the file
  11.239 +     *         exists then it will be truncated to zero size; otherwise, a new
  11.240 +     *         file will be created.  The output will be written to the file
  11.241 +     *         and is buffered.
  11.242 +     *
  11.243 +     * @throws  FileNotFoundException
  11.244 +     *          If the given file object does not denote an existing, writable
  11.245 +     *          regular file and a new regular file of that name cannot be
  11.246 +     *          created, or if some other error occurs while opening or
  11.247 +     *          creating the file
  11.248 +     *
  11.249 +     * @throws  SecurityException
  11.250 +     *          If a security manager is present and {@link
  11.251 +     *          SecurityManager#checkWrite checkWrite(file.getPath())}
  11.252 +     *          denies write access to the file
  11.253 +     *
  11.254 +     * @since  1.5
  11.255 +     */
  11.256 +    public PrintWriter(File file) throws FileNotFoundException {
  11.257 +        super();
  11.258 +        throw new FileNotFoundException();
  11.259 +    }
  11.260 +
  11.261 +    /**
  11.262 +     * Creates a new PrintWriter, without automatic line flushing, with the
  11.263 +     * specified file and charset.  This convenience constructor creates the
  11.264 +     * necessary intermediate {@link java.io.OutputStreamWriter
  11.265 +     * OutputStreamWriter}, which will encode characters using the provided
  11.266 +     * charset.
  11.267 +     *
  11.268 +     * @param  file
  11.269 +     *         The file to use as the destination of this writer.  If the file
  11.270 +     *         exists then it will be truncated to zero size; otherwise, a new
  11.271 +     *         file will be created.  The output will be written to the file
  11.272 +     *         and is buffered.
  11.273 +     *
  11.274 +     * @param  csn
  11.275 +     *         The name of a supported {@linkplain java.nio.charset.Charset
  11.276 +     *         charset}
  11.277 +     *
  11.278 +     * @throws  FileNotFoundException
  11.279 +     *          If the given file object does not denote an existing, writable
  11.280 +     *          regular file and a new regular file of that name cannot be
  11.281 +     *          created, or if some other error occurs while opening or
  11.282 +     *          creating the file
  11.283 +     *
  11.284 +     * @throws  SecurityException
  11.285 +     *          If a security manager is present and {@link
  11.286 +     *          SecurityManager#checkWrite checkWrite(file.getPath())}
  11.287 +     *          denies write access to the file
  11.288 +     *
  11.289 +     * @throws  UnsupportedEncodingException
  11.290 +     *          If the named charset is not supported
  11.291 +     *
  11.292 +     * @since  1.5
  11.293 +     */
  11.294 +    public PrintWriter(File file, String csn)
  11.295 +        throws FileNotFoundException, UnsupportedEncodingException
  11.296 +    {
  11.297 +        this(toCharset(csn), file);
  11.298 +    }
  11.299 +
  11.300 +    /** Checks to make sure that the stream has not been closed */
  11.301 +    private void ensureOpen() throws IOException {
  11.302 +        if (out == null)
  11.303 +            throw new IOException("Stream closed");
  11.304 +    }
  11.305 +
  11.306 +    /**
  11.307 +     * Flushes the stream.
  11.308 +     * @see #checkError()
  11.309 +     */
  11.310 +    public void flush() {
  11.311 +        try {
  11.312 +            synchronized (lock) {
  11.313 +                ensureOpen();
  11.314 +                out.flush();
  11.315 +            }
  11.316 +        }
  11.317 +        catch (IOException x) {
  11.318 +            trouble = true;
  11.319 +        }
  11.320 +    }
  11.321 +
  11.322 +    /**
  11.323 +     * Closes the stream and releases any system resources associated
  11.324 +     * with it. Closing a previously closed stream has no effect.
  11.325 +     *
  11.326 +     * @see #checkError()
  11.327 +     */
  11.328 +    public void close() {
  11.329 +        try {
  11.330 +            synchronized (lock) {
  11.331 +                if (out == null)
  11.332 +                    return;
  11.333 +                out.close();
  11.334 +                out = null;
  11.335 +            }
  11.336 +        }
  11.337 +        catch (IOException x) {
  11.338 +            trouble = true;
  11.339 +        }
  11.340 +    }
  11.341 +
  11.342 +    /**
  11.343 +     * Flushes the stream if it's not closed and checks its error state.
  11.344 +     *
  11.345 +     * @return <code>true</code> if the print stream has encountered an error,
  11.346 +     *          either on the underlying output stream or during a format
  11.347 +     *          conversion.
  11.348 +     */
  11.349 +    public boolean checkError() {
  11.350 +        if (out != null) {
  11.351 +            flush();
  11.352 +        }
  11.353 +        if (out instanceof java.io.PrintWriter) {
  11.354 +            PrintWriter pw = (PrintWriter) out;
  11.355 +            return pw.checkError();
  11.356 +        } else 
  11.357 +//        if (psOut != null) {
  11.358 +//            return psOut.checkError();
  11.359 +//        }
  11.360 +        return trouble;
  11.361 +    }
  11.362 +
  11.363 +    /**
  11.364 +     * Indicates that an error has occurred.
  11.365 +     *
  11.366 +     * <p> This method will cause subsequent invocations of {@link
  11.367 +     * #checkError()} to return <tt>true</tt> until {@link
  11.368 +     * #clearError()} is invoked.
  11.369 +     */
  11.370 +    protected void setError() {
  11.371 +        trouble = true;
  11.372 +    }
  11.373 +
  11.374 +    /**
  11.375 +     * Clears the error state of this stream.
  11.376 +     *
  11.377 +     * <p> This method will cause subsequent invocations of {@link
  11.378 +     * #checkError()} to return <tt>false</tt> until another write
  11.379 +     * operation fails and invokes {@link #setError()}.
  11.380 +     *
  11.381 +     * @since 1.6
  11.382 +     */
  11.383 +    protected void clearError() {
  11.384 +        trouble = false;
  11.385 +    }
  11.386 +
  11.387 +    /*
  11.388 +     * Exception-catching, synchronized output operations,
  11.389 +     * which also implement the write() methods of Writer
  11.390 +     */
  11.391 +
  11.392 +    /**
  11.393 +     * Writes a single character.
  11.394 +     * @param c int specifying a character to be written.
  11.395 +     */
  11.396 +    public void write(int c) {
  11.397 +        try {
  11.398 +            synchronized (lock) {
  11.399 +                ensureOpen();
  11.400 +                out.write(c);
  11.401 +            }
  11.402 +        }
  11.403 +        catch (InterruptedIOException x) {
  11.404 +            Thread.currentThread().interrupt();
  11.405 +        }
  11.406 +        catch (IOException x) {
  11.407 +            trouble = true;
  11.408 +        }
  11.409 +    }
  11.410 +
  11.411 +    /**
  11.412 +     * Writes A Portion of an array of characters.
  11.413 +     * @param buf Array of characters
  11.414 +     * @param off Offset from which to start writing characters
  11.415 +     * @param len Number of characters to write
  11.416 +     */
  11.417 +    public void write(char buf[], int off, int len) {
  11.418 +        try {
  11.419 +            synchronized (lock) {
  11.420 +                ensureOpen();
  11.421 +                out.write(buf, off, len);
  11.422 +            }
  11.423 +        }
  11.424 +        catch (InterruptedIOException x) {
  11.425 +            Thread.currentThread().interrupt();
  11.426 +        }
  11.427 +        catch (IOException x) {
  11.428 +            trouble = true;
  11.429 +        }
  11.430 +    }
  11.431 +
  11.432 +    /**
  11.433 +     * Writes an array of characters.  This method cannot be inherited from the
  11.434 +     * Writer class because it must suppress I/O exceptions.
  11.435 +     * @param buf Array of characters to be written
  11.436 +     */
  11.437 +    public void write(char buf[]) {
  11.438 +        write(buf, 0, buf.length);
  11.439 +    }
  11.440 +
  11.441 +    /**
  11.442 +     * Writes a portion of a string.
  11.443 +     * @param s A String
  11.444 +     * @param off Offset from which to start writing characters
  11.445 +     * @param len Number of characters to write
  11.446 +     */
  11.447 +    public void write(String s, int off, int len) {
  11.448 +        try {
  11.449 +            synchronized (lock) {
  11.450 +                ensureOpen();
  11.451 +                out.write(s, off, len);
  11.452 +            }
  11.453 +        }
  11.454 +        catch (InterruptedIOException x) {
  11.455 +            Thread.currentThread().interrupt();
  11.456 +        }
  11.457 +        catch (IOException x) {
  11.458 +            trouble = true;
  11.459 +        }
  11.460 +    }
  11.461 +
  11.462 +    /**
  11.463 +     * Writes a string.  This method cannot be inherited from the Writer class
  11.464 +     * because it must suppress I/O exceptions.
  11.465 +     * @param s String to be written
  11.466 +     */
  11.467 +    public void write(String s) {
  11.468 +        write(s, 0, s.length());
  11.469 +    }
  11.470 +
  11.471 +    private void newLine() {
  11.472 +        try {
  11.473 +            synchronized (lock) {
  11.474 +                ensureOpen();
  11.475 +                out.write(lineSeparator);
  11.476 +                if (autoFlush)
  11.477 +                    out.flush();
  11.478 +            }
  11.479 +        }
  11.480 +        catch (InterruptedIOException x) {
  11.481 +            Thread.currentThread().interrupt();
  11.482 +        }
  11.483 +        catch (IOException x) {
  11.484 +            trouble = true;
  11.485 +        }
  11.486 +    }
  11.487 +
  11.488 +    /* Methods that do not terminate lines */
  11.489 +
  11.490 +    /**
  11.491 +     * Prints a boolean value.  The string produced by <code>{@link
  11.492 +     * java.lang.String#valueOf(boolean)}</code> is translated into bytes
  11.493 +     * according to the platform's default character encoding, and these bytes
  11.494 +     * are written in exactly the manner of the <code>{@link
  11.495 +     * #write(int)}</code> method.
  11.496 +     *
  11.497 +     * @param      b   The <code>boolean</code> to be printed
  11.498 +     */
  11.499 +    public void print(boolean b) {
  11.500 +        write(b ? "true" : "false");
  11.501 +    }
  11.502 +
  11.503 +    /**
  11.504 +     * Prints a character.  The character is translated into one or more bytes
  11.505 +     * according to the platform's default character encoding, and these bytes
  11.506 +     * are written in exactly the manner of the <code>{@link
  11.507 +     * #write(int)}</code> method.
  11.508 +     *
  11.509 +     * @param      c   The <code>char</code> to be printed
  11.510 +     */
  11.511 +    public void print(char c) {
  11.512 +        write(c);
  11.513 +    }
  11.514 +
  11.515 +    /**
  11.516 +     * Prints an integer.  The string produced by <code>{@link
  11.517 +     * java.lang.String#valueOf(int)}</code> is translated into bytes according
  11.518 +     * to the platform's default character encoding, and these bytes are
  11.519 +     * written in exactly the manner of the <code>{@link #write(int)}</code>
  11.520 +     * method.
  11.521 +     *
  11.522 +     * @param      i   The <code>int</code> to be printed
  11.523 +     * @see        java.lang.Integer#toString(int)
  11.524 +     */
  11.525 +    public void print(int i) {
  11.526 +        write(String.valueOf(i));
  11.527 +    }
  11.528 +
  11.529 +    /**
  11.530 +     * Prints a long integer.  The string produced by <code>{@link
  11.531 +     * java.lang.String#valueOf(long)}</code> is translated into bytes
  11.532 +     * according to the platform's default character encoding, and these bytes
  11.533 +     * are written in exactly the manner of the <code>{@link #write(int)}</code>
  11.534 +     * method.
  11.535 +     *
  11.536 +     * @param      l   The <code>long</code> to be printed
  11.537 +     * @see        java.lang.Long#toString(long)
  11.538 +     */
  11.539 +    public void print(long l) {
  11.540 +        write(String.valueOf(l));
  11.541 +    }
  11.542 +
  11.543 +    /**
  11.544 +     * Prints a floating-point number.  The string produced by <code>{@link
  11.545 +     * java.lang.String#valueOf(float)}</code> is translated into bytes
  11.546 +     * according to the platform's default character encoding, and these bytes
  11.547 +     * are written in exactly the manner of the <code>{@link #write(int)}</code>
  11.548 +     * method.
  11.549 +     *
  11.550 +     * @param      f   The <code>float</code> to be printed
  11.551 +     * @see        java.lang.Float#toString(float)
  11.552 +     */
  11.553 +    public void print(float f) {
  11.554 +        write(String.valueOf(f));
  11.555 +    }
  11.556 +
  11.557 +    /**
  11.558 +     * Prints a double-precision floating-point number.  The string produced by
  11.559 +     * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
  11.560 +     * bytes according to the platform's default character encoding, and these
  11.561 +     * bytes are written in exactly the manner of the <code>{@link
  11.562 +     * #write(int)}</code> method.
  11.563 +     *
  11.564 +     * @param      d   The <code>double</code> to be printed
  11.565 +     * @see        java.lang.Double#toString(double)
  11.566 +     */
  11.567 +    public void print(double d) {
  11.568 +        write(String.valueOf(d));
  11.569 +    }
  11.570 +
  11.571 +    /**
  11.572 +     * Prints an array of characters.  The characters are converted into bytes
  11.573 +     * according to the platform's default character encoding, and these bytes
  11.574 +     * are written in exactly the manner of the <code>{@link #write(int)}</code>
  11.575 +     * method.
  11.576 +     *
  11.577 +     * @param      s   The array of chars to be printed
  11.578 +     *
  11.579 +     * @throws  NullPointerException  If <code>s</code> is <code>null</code>
  11.580 +     */
  11.581 +    public void print(char s[]) {
  11.582 +        write(s);
  11.583 +    }
  11.584 +
  11.585 +    /**
  11.586 +     * Prints a string.  If the argument is <code>null</code> then the string
  11.587 +     * <code>"null"</code> is printed.  Otherwise, the string's characters are
  11.588 +     * converted into bytes according to the platform's default character
  11.589 +     * encoding, and these bytes are written in exactly the manner of the
  11.590 +     * <code>{@link #write(int)}</code> method.
  11.591 +     *
  11.592 +     * @param      s   The <code>String</code> to be printed
  11.593 +     */
  11.594 +    public void print(String s) {
  11.595 +        if (s == null) {
  11.596 +            s = "null";
  11.597 +        }
  11.598 +        write(s);
  11.599 +    }
  11.600 +
  11.601 +    /**
  11.602 +     * Prints an object.  The string produced by the <code>{@link
  11.603 +     * java.lang.String#valueOf(Object)}</code> method is translated into bytes
  11.604 +     * according to the platform's default character encoding, and these bytes
  11.605 +     * are written in exactly the manner of the <code>{@link #write(int)}</code>
  11.606 +     * method.
  11.607 +     *
  11.608 +     * @param      obj   The <code>Object</code> to be printed
  11.609 +     * @see        java.lang.Object#toString()
  11.610 +     */
  11.611 +    public void print(Object obj) {
  11.612 +        write(String.valueOf(obj));
  11.613 +    }
  11.614 +
  11.615 +    /* Methods that do terminate lines */
  11.616 +
  11.617 +    /**
  11.618 +     * Terminates the current line by writing the line separator string.  The
  11.619 +     * line separator string is defined by the system property
  11.620 +     * <code>line.separator</code>, and is not necessarily a single newline
  11.621 +     * character (<code>'\n'</code>).
  11.622 +     */
  11.623 +    public void println() {
  11.624 +        newLine();
  11.625 +    }
  11.626 +
  11.627 +    /**
  11.628 +     * Prints a boolean value and then terminates the line.  This method behaves
  11.629 +     * as though it invokes <code>{@link #print(boolean)}</code> and then
  11.630 +     * <code>{@link #println()}</code>.
  11.631 +     *
  11.632 +     * @param x the <code>boolean</code> value to be printed
  11.633 +     */
  11.634 +    public void println(boolean x) {
  11.635 +        synchronized (lock) {
  11.636 +            print(x);
  11.637 +            println();
  11.638 +        }
  11.639 +    }
  11.640 +
  11.641 +    /**
  11.642 +     * Prints a character and then terminates the line.  This method behaves as
  11.643 +     * though it invokes <code>{@link #print(char)}</code> and then <code>{@link
  11.644 +     * #println()}</code>.
  11.645 +     *
  11.646 +     * @param x the <code>char</code> value to be printed
  11.647 +     */
  11.648 +    public void println(char x) {
  11.649 +        synchronized (lock) {
  11.650 +            print(x);
  11.651 +            println();
  11.652 +        }
  11.653 +    }
  11.654 +
  11.655 +    /**
  11.656 +     * Prints an integer and then terminates the line.  This method behaves as
  11.657 +     * though it invokes <code>{@link #print(int)}</code> and then <code>{@link
  11.658 +     * #println()}</code>.
  11.659 +     *
  11.660 +     * @param x the <code>int</code> value to be printed
  11.661 +     */
  11.662 +    public void println(int x) {
  11.663 +        synchronized (lock) {
  11.664 +            print(x);
  11.665 +            println();
  11.666 +        }
  11.667 +    }
  11.668 +
  11.669 +    /**
  11.670 +     * Prints a long integer and then terminates the line.  This method behaves
  11.671 +     * as though it invokes <code>{@link #print(long)}</code> and then
  11.672 +     * <code>{@link #println()}</code>.
  11.673 +     *
  11.674 +     * @param x the <code>long</code> value to be printed
  11.675 +     */
  11.676 +    public void println(long x) {
  11.677 +        synchronized (lock) {
  11.678 +            print(x);
  11.679 +            println();
  11.680 +        }
  11.681 +    }
  11.682 +
  11.683 +    /**
  11.684 +     * Prints a floating-point number and then terminates the line.  This method
  11.685 +     * behaves as though it invokes <code>{@link #print(float)}</code> and then
  11.686 +     * <code>{@link #println()}</code>.
  11.687 +     *
  11.688 +     * @param x the <code>float</code> value to be printed
  11.689 +     */
  11.690 +    public void println(float x) {
  11.691 +        synchronized (lock) {
  11.692 +            print(x);
  11.693 +            println();
  11.694 +        }
  11.695 +    }
  11.696 +
  11.697 +    /**
  11.698 +     * Prints a double-precision floating-point number and then terminates the
  11.699 +     * line.  This method behaves as though it invokes <code>{@link
  11.700 +     * #print(double)}</code> and then <code>{@link #println()}</code>.
  11.701 +     *
  11.702 +     * @param x the <code>double</code> value to be printed
  11.703 +     */
  11.704 +    public void println(double x) {
  11.705 +        synchronized (lock) {
  11.706 +            print(x);
  11.707 +            println();
  11.708 +        }
  11.709 +    }
  11.710 +
  11.711 +    /**
  11.712 +     * Prints an array of characters and then terminates the line.  This method
  11.713 +     * behaves as though it invokes <code>{@link #print(char[])}</code> and then
  11.714 +     * <code>{@link #println()}</code>.
  11.715 +     *
  11.716 +     * @param x the array of <code>char</code> values to be printed
  11.717 +     */
  11.718 +    public void println(char x[]) {
  11.719 +        synchronized (lock) {
  11.720 +            print(x);
  11.721 +            println();
  11.722 +        }
  11.723 +    }
  11.724 +
  11.725 +    /**
  11.726 +     * Prints a String and then terminates the line.  This method behaves as
  11.727 +     * though it invokes <code>{@link #print(String)}</code> and then
  11.728 +     * <code>{@link #println()}</code>.
  11.729 +     *
  11.730 +     * @param x the <code>String</code> value to be printed
  11.731 +     */
  11.732 +    public void println(String x) {
  11.733 +        synchronized (lock) {
  11.734 +            print(x);
  11.735 +            println();
  11.736 +        }
  11.737 +    }
  11.738 +
  11.739 +    /**
  11.740 +     * Prints an Object and then terminates the line.  This method calls
  11.741 +     * at first String.valueOf(x) to get the printed object's string value,
  11.742 +     * then behaves as
  11.743 +     * though it invokes <code>{@link #print(String)}</code> and then
  11.744 +     * <code>{@link #println()}</code>.
  11.745 +     *
  11.746 +     * @param x  The <code>Object</code> to be printed.
  11.747 +     */
  11.748 +    public void println(Object x) {
  11.749 +        String s = String.valueOf(x);
  11.750 +        synchronized (lock) {
  11.751 +            print(s);
  11.752 +            println();
  11.753 +        }
  11.754 +    }
  11.755 +
  11.756 +    /**
  11.757 +     * A convenience method to write a formatted string to this writer using
  11.758 +     * the specified format string and arguments.  If automatic flushing is
  11.759 +     * enabled, calls to this method will flush the output buffer.
  11.760 +     *
  11.761 +     * <p> An invocation of this method of the form <tt>out.printf(format,
  11.762 +     * args)</tt> behaves in exactly the same way as the invocation
  11.763 +     *
  11.764 +     * <pre>
  11.765 +     *     out.format(format, args) </pre>
  11.766 +     *
  11.767 +     * @param  format
  11.768 +     *         A format string as described in <a
  11.769 +     *         href="../util/Formatter.html#syntax">Format string syntax</a>.
  11.770 +     *
  11.771 +     * @param  args
  11.772 +     *         Arguments referenced by the format specifiers in the format
  11.773 +     *         string.  If there are more arguments than format specifiers, the
  11.774 +     *         extra arguments are ignored.  The number of arguments is
  11.775 +     *         variable and may be zero.  The maximum number of arguments is
  11.776 +     *         limited by the maximum dimension of a Java array as defined by
  11.777 +     *         <cite>The Java&trade; Virtual Machine Specification</cite>.
  11.778 +     *         The behaviour on a
  11.779 +     *         <tt>null</tt> argument depends on the <a
  11.780 +     *         href="../util/Formatter.html#syntax">conversion</a>.
  11.781 +     *
  11.782 +     * @throws  IllegalFormatException
  11.783 +     *          If a format string contains an illegal syntax, a format
  11.784 +     *          specifier that is incompatible with the given arguments,
  11.785 +     *          insufficient arguments given the format string, or other
  11.786 +     *          illegal conditions.  For specification of all possible
  11.787 +     *          formatting errors, see the <a
  11.788 +     *          href="../util/Formatter.html#detail">Details</a> section of the
  11.789 +     *          formatter class specification.
  11.790 +     *
  11.791 +     * @throws  NullPointerException
  11.792 +     *          If the <tt>format</tt> is <tt>null</tt>
  11.793 +     *
  11.794 +     * @return  This writer
  11.795 +     *
  11.796 +     * @since  1.5
  11.797 +     */
  11.798 +    public PrintWriter printf(String format, Object ... args) {
  11.799 +        return format(format, args);
  11.800 +    }
  11.801 +
  11.802 +    /**
  11.803 +     * A convenience method to write a formatted string to this writer using
  11.804 +     * the specified format string and arguments.  If automatic flushing is
  11.805 +     * enabled, calls to this method will flush the output buffer.
  11.806 +     *
  11.807 +     * <p> An invocation of this method of the form <tt>out.printf(l, format,
  11.808 +     * args)</tt> behaves in exactly the same way as the invocation
  11.809 +     *
  11.810 +     * <pre>
  11.811 +     *     out.format(l, format, args) </pre>
  11.812 +     *
  11.813 +     * @param  l
  11.814 +     *         The {@linkplain java.util.Locale locale} to apply during
  11.815 +     *         formatting.  If <tt>l</tt> is <tt>null</tt> then no localization
  11.816 +     *         is applied.
  11.817 +     *
  11.818 +     * @param  format
  11.819 +     *         A format string as described in <a
  11.820 +     *         href="../util/Formatter.html#syntax">Format string syntax</a>.
  11.821 +     *
  11.822 +     * @param  args
  11.823 +     *         Arguments referenced by the format specifiers in the format
  11.824 +     *         string.  If there are more arguments than format specifiers, the
  11.825 +     *         extra arguments are ignored.  The number of arguments is
  11.826 +     *         variable and may be zero.  The maximum number of arguments is
  11.827 +     *         limited by the maximum dimension of a Java array as defined by
  11.828 +     *         <cite>The Java&trade; Virtual Machine Specification</cite>.
  11.829 +     *         The behaviour on a
  11.830 +     *         <tt>null</tt> argument depends on the <a
  11.831 +     *         href="../util/Formatter.html#syntax">conversion</a>.
  11.832 +     *
  11.833 +     * @throws  IllegalFormatException
  11.834 +     *          If a format string contains an illegal syntax, a format
  11.835 +     *          specifier that is incompatible with the given arguments,
  11.836 +     *          insufficient arguments given the format string, or other
  11.837 +     *          illegal conditions.  For specification of all possible
  11.838 +     *          formatting errors, see the <a
  11.839 +     *          href="../util/Formatter.html#detail">Details</a> section of the
  11.840 +     *          formatter class specification.
  11.841 +     *
  11.842 +     * @throws  NullPointerException
  11.843 +     *          If the <tt>format</tt> is <tt>null</tt>
  11.844 +     *
  11.845 +     * @return  This writer
  11.846 +     *
  11.847 +     * @since  1.5
  11.848 +     */
  11.849 +//    public PrintWriter printf(Locale l, String format, Object ... args) {
  11.850 +//        return format(l, format, args);
  11.851 +//    }
  11.852 +
  11.853 +    /**
  11.854 +     * Writes a formatted string to this writer using the specified format
  11.855 +     * string and arguments.  If automatic flushing is enabled, calls to this
  11.856 +     * method will flush the output buffer.
  11.857 +     *
  11.858 +     * <p> The locale always used is the one returned by {@link
  11.859 +     * java.util.Locale#getDefault() Locale.getDefault()}, regardless of any
  11.860 +     * previous invocations of other formatting methods on this object.
  11.861 +     *
  11.862 +     * @param  format
  11.863 +     *         A format string as described in <a
  11.864 +     *         href="../util/Formatter.html#syntax">Format string syntax</a>.
  11.865 +     *
  11.866 +     * @param  args
  11.867 +     *         Arguments referenced by the format specifiers in the format
  11.868 +     *         string.  If there are more arguments than format specifiers, the
  11.869 +     *         extra arguments are ignored.  The number of arguments is
  11.870 +     *         variable and may be zero.  The maximum number of arguments is
  11.871 +     *         limited by the maximum dimension of a Java array as defined by
  11.872 +     *         <cite>The Java&trade; Virtual Machine Specification</cite>.
  11.873 +     *         The behaviour on a
  11.874 +     *         <tt>null</tt> argument depends on the <a
  11.875 +     *         href="../util/Formatter.html#syntax">conversion</a>.
  11.876 +     *
  11.877 +     * @throws  IllegalFormatException
  11.878 +     *          If a format string contains an illegal syntax, a format
  11.879 +     *          specifier that is incompatible with the given arguments,
  11.880 +     *          insufficient arguments given the format string, or other
  11.881 +     *          illegal conditions.  For specification of all possible
  11.882 +     *          formatting errors, see the <a
  11.883 +     *          href="../util/Formatter.html#detail">Details</a> section of the
  11.884 +     *          Formatter class specification.
  11.885 +     *
  11.886 +     * @throws  NullPointerException
  11.887 +     *          If the <tt>format</tt> is <tt>null</tt>
  11.888 +     *
  11.889 +     * @return  This writer
  11.890 +     *
  11.891 +     * @since  1.5
  11.892 +     */
  11.893 +    public PrintWriter format(String format, Object ... args) {
  11.894 +        append(format).append(Arrays.toString(args));
  11.895 +        return this;
  11.896 +    }
  11.897 +
  11.898 +    /**
  11.899 +     * Writes a formatted string to this writer using the specified format
  11.900 +     * string and arguments.  If automatic flushing is enabled, calls to this
  11.901 +     * method will flush the output buffer.
  11.902 +     *
  11.903 +     * @param  l
  11.904 +     *         The {@linkplain java.util.Locale locale} to apply during
  11.905 +     *         formatting.  If <tt>l</tt> is <tt>null</tt> then no localization
  11.906 +     *         is applied.
  11.907 +     *
  11.908 +     * @param  format
  11.909 +     *         A format string as described in <a
  11.910 +     *         href="../util/Formatter.html#syntax">Format string syntax</a>.
  11.911 +     *
  11.912 +     * @param  args
  11.913 +     *         Arguments referenced by the format specifiers in the format
  11.914 +     *         string.  If there are more arguments than format specifiers, the
  11.915 +     *         extra arguments are ignored.  The number of arguments is
  11.916 +     *         variable and may be zero.  The maximum number of arguments is
  11.917 +     *         limited by the maximum dimension of a Java array as defined by
  11.918 +     *         <cite>The Java&trade; Virtual Machine Specification</cite>.
  11.919 +     *         The behaviour on a
  11.920 +     *         <tt>null</tt> argument depends on the <a
  11.921 +     *         href="../util/Formatter.html#syntax">conversion</a>.
  11.922 +     *
  11.923 +     * @throws  IllegalFormatException
  11.924 +     *          If a format string contains an illegal syntax, a format
  11.925 +     *          specifier that is incompatible with the given arguments,
  11.926 +     *          insufficient arguments given the format string, or other
  11.927 +     *          illegal conditions.  For specification of all possible
  11.928 +     *          formatting errors, see the <a
  11.929 +     *          href="../util/Formatter.html#detail">Details</a> section of the
  11.930 +     *          formatter class specification.
  11.931 +     *
  11.932 +     * @throws  NullPointerException
  11.933 +     *          If the <tt>format</tt> is <tt>null</tt>
  11.934 +     *
  11.935 +     * @return  This writer
  11.936 +     *
  11.937 +     * @since  1.5
  11.938 +     */
  11.939 +//    public PrintWriter format(Locale l, String format, Object ... args) {
  11.940 +//        return format(format, args);
  11.941 +//    }
  11.942 +
  11.943 +    /**
  11.944 +     * Appends the specified character sequence to this writer.
  11.945 +     *
  11.946 +     * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
  11.947 +     * behaves in exactly the same way as the invocation
  11.948 +     *
  11.949 +     * <pre>
  11.950 +     *     out.write(csq.toString()) </pre>
  11.951 +     *
  11.952 +     * <p> Depending on the specification of <tt>toString</tt> for the
  11.953 +     * character sequence <tt>csq</tt>, the entire sequence may not be
  11.954 +     * appended. For instance, invoking the <tt>toString</tt> method of a
  11.955 +     * character buffer will return a subsequence whose content depends upon
  11.956 +     * the buffer's position and limit.
  11.957 +     *
  11.958 +     * @param  csq
  11.959 +     *         The character sequence to append.  If <tt>csq</tt> is
  11.960 +     *         <tt>null</tt>, then the four characters <tt>"null"</tt> are
  11.961 +     *         appended to this writer.
  11.962 +     *
  11.963 +     * @return  This writer
  11.964 +     *
  11.965 +     * @since  1.5
  11.966 +     */
  11.967 +    public PrintWriter append(CharSequence csq) {
  11.968 +        if (csq == null)
  11.969 +            write("null");
  11.970 +        else
  11.971 +            write(csq.toString());
  11.972 +        return this;
  11.973 +    }
  11.974 +
  11.975 +    /**
  11.976 +     * Appends a subsequence of the specified character sequence to this writer.
  11.977 +     *
  11.978 +     * <p> An invocation of this method of the form <tt>out.append(csq, start,
  11.979 +     * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
  11.980 +     * exactly the same way as the invocation
  11.981 +     *
  11.982 +     * <pre>
  11.983 +     *     out.write(csq.subSequence(start, end).toString()) </pre>
  11.984 +     *
  11.985 +     * @param  csq
  11.986 +     *         The character sequence from which a subsequence will be
  11.987 +     *         appended.  If <tt>csq</tt> is <tt>null</tt>, then characters
  11.988 +     *         will be appended as if <tt>csq</tt> contained the four
  11.989 +     *         characters <tt>"null"</tt>.
  11.990 +     *
  11.991 +     * @param  start
  11.992 +     *         The index of the first character in the subsequence
  11.993 +     *
  11.994 +     * @param  end
  11.995 +     *         The index of the character following the last character in the
  11.996 +     *         subsequence
  11.997 +     *
  11.998 +     * @return  This writer
  11.999 +     *
 11.1000 +     * @throws  IndexOutOfBoundsException
 11.1001 +     *          If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
 11.1002 +     *          is greater than <tt>end</tt>, or <tt>end</tt> is greater than
 11.1003 +     *          <tt>csq.length()</tt>
 11.1004 +     *
 11.1005 +     * @since  1.5
 11.1006 +     */
 11.1007 +    public PrintWriter append(CharSequence csq, int start, int end) {
 11.1008 +        CharSequence cs = (csq == null ? "null" : csq);
 11.1009 +        write(cs.subSequence(start, end).toString());
 11.1010 +        return this;
 11.1011 +    }
 11.1012 +
 11.1013 +    /**
 11.1014 +     * Appends the specified character to this writer.
 11.1015 +     *
 11.1016 +     * <p> An invocation of this method of the form <tt>out.append(c)</tt>
 11.1017 +     * behaves in exactly the same way as the invocation
 11.1018 +     *
 11.1019 +     * <pre>
 11.1020 +     *     out.write(c) </pre>
 11.1021 +     *
 11.1022 +     * @param  c
 11.1023 +     *         The 16-bit character to append
 11.1024 +     *
 11.1025 +     * @return  This writer
 11.1026 +     *
 11.1027 +     * @since 1.5
 11.1028 +     */
 11.1029 +    public PrintWriter append(char c) {
 11.1030 +        write(c);
 11.1031 +        return this;
 11.1032 +    }
 11.1033 +}
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/rt/emul/compact/src/main/java/java/io/Writer.java	Sun Sep 08 11:22:51 2013 +0200
    12.3 @@ -0,0 +1,325 @@
    12.4 +/*
    12.5 + * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
    12.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    12.7 + *
    12.8 + * This code is free software; you can redistribute it and/or modify it
    12.9 + * under the terms of the GNU General Public License version 2 only, as
   12.10 + * published by the Free Software Foundation.  Oracle designates this
   12.11 + * particular file as subject to the "Classpath" exception as provided
   12.12 + * by Oracle in the LICENSE file that accompanied this code.
   12.13 + *
   12.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   12.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   12.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   12.17 + * version 2 for more details (a copy is included in the LICENSE file that
   12.18 + * accompanied this code).
   12.19 + *
   12.20 + * You should have received a copy of the GNU General Public License version
   12.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   12.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   12.23 + *
   12.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   12.25 + * or visit www.oracle.com if you need additional information or have any
   12.26 + * questions.
   12.27 + */
   12.28 +
   12.29 +package java.io;
   12.30 +
   12.31 +
   12.32 +/**
   12.33 + * Abstract class for writing to character streams.  The only methods that a
   12.34 + * subclass must implement are write(char[], int, int), flush(), and close().
   12.35 + * Most subclasses, however, will override some of the methods defined here in
   12.36 + * order to provide higher efficiency, additional functionality, or both.
   12.37 + *
   12.38 + * @see Writer
   12.39 + * @see   BufferedWriter
   12.40 + * @see   CharArrayWriter
   12.41 + * @see   FilterWriter
   12.42 + * @see   OutputStreamWriter
   12.43 + * @see     FileWriter
   12.44 + * @see   PipedWriter
   12.45 + * @see   PrintWriter
   12.46 + * @see   StringWriter
   12.47 + * @see Reader
   12.48 + *
   12.49 + * @author      Mark Reinhold
   12.50 + * @since       JDK1.1
   12.51 + */
   12.52 +
   12.53 +public abstract class Writer implements Appendable, Closeable, Flushable {
   12.54 +
   12.55 +    /**
   12.56 +     * Temporary buffer used to hold writes of strings and single characters
   12.57 +     */
   12.58 +    private char[] writeBuffer;
   12.59 +
   12.60 +    /**
   12.61 +     * Size of writeBuffer, must be >= 1
   12.62 +     */
   12.63 +    private final int writeBufferSize = 1024;
   12.64 +
   12.65 +    /**
   12.66 +     * The object used to synchronize operations on this stream.  For
   12.67 +     * efficiency, a character-stream object may use an object other than
   12.68 +     * itself to protect critical sections.  A subclass should therefore use
   12.69 +     * the object in this field rather than <tt>this</tt> or a synchronized
   12.70 +     * method.
   12.71 +     */
   12.72 +    protected Object lock;
   12.73 +
   12.74 +    /**
   12.75 +     * Creates a new character-stream writer whose critical sections will
   12.76 +     * synchronize on the writer itself.
   12.77 +     */
   12.78 +    protected Writer() {
   12.79 +        this.lock = this;
   12.80 +    }
   12.81 +
   12.82 +    /**
   12.83 +     * Creates a new character-stream writer whose critical sections will
   12.84 +     * synchronize on the given object.
   12.85 +     *
   12.86 +     * @param  lock
   12.87 +     *         Object to synchronize on
   12.88 +     */
   12.89 +    protected Writer(Object lock) {
   12.90 +        if (lock == null) {
   12.91 +            throw new NullPointerException();
   12.92 +        }
   12.93 +        this.lock = lock;
   12.94 +    }
   12.95 +
   12.96 +    /**
   12.97 +     * Writes a single character.  The character to be written is contained in
   12.98 +     * the 16 low-order bits of the given integer value; the 16 high-order bits
   12.99 +     * are ignored.
  12.100 +     *
  12.101 +     * <p> Subclasses that intend to support efficient single-character output
  12.102 +     * should override this method.
  12.103 +     *
  12.104 +     * @param  c
  12.105 +     *         int specifying a character to be written
  12.106 +     *
  12.107 +     * @throws  IOException
  12.108 +     *          If an I/O error occurs
  12.109 +     */
  12.110 +    public void write(int c) throws IOException {
  12.111 +        synchronized (lock) {
  12.112 +            if (writeBuffer == null){
  12.113 +                writeBuffer = new char[writeBufferSize];
  12.114 +            }
  12.115 +            writeBuffer[0] = (char) c;
  12.116 +            write(writeBuffer, 0, 1);
  12.117 +        }
  12.118 +    }
  12.119 +
  12.120 +    /**
  12.121 +     * Writes an array of characters.
  12.122 +     *
  12.123 +     * @param  cbuf
  12.124 +     *         Array of characters to be written
  12.125 +     *
  12.126 +     * @throws  IOException
  12.127 +     *          If an I/O error occurs
  12.128 +     */
  12.129 +    public void write(char cbuf[]) throws IOException {
  12.130 +        write(cbuf, 0, cbuf.length);
  12.131 +    }
  12.132 +
  12.133 +    /**
  12.134 +     * Writes a portion of an array of characters.
  12.135 +     *
  12.136 +     * @param  cbuf
  12.137 +     *         Array of characters
  12.138 +     *
  12.139 +     * @param  off
  12.140 +     *         Offset from which to start writing characters
  12.141 +     *
  12.142 +     * @param  len
  12.143 +     *         Number of characters to write
  12.144 +     *
  12.145 +     * @throws  IOException
  12.146 +     *          If an I/O error occurs
  12.147 +     */
  12.148 +    abstract public void write(char cbuf[], int off, int len) throws IOException;
  12.149 +
  12.150 +    /**
  12.151 +     * Writes a string.
  12.152 +     *
  12.153 +     * @param  str
  12.154 +     *         String to be written
  12.155 +     *
  12.156 +     * @throws  IOException
  12.157 +     *          If an I/O error occurs
  12.158 +     */
  12.159 +    public void write(String str) throws IOException {
  12.160 +        write(str, 0, str.length());
  12.161 +    }
  12.162 +
  12.163 +    /**
  12.164 +     * Writes a portion of a string.
  12.165 +     *
  12.166 +     * @param  str
  12.167 +     *         A String
  12.168 +     *
  12.169 +     * @param  off
  12.170 +     *         Offset from which to start writing characters
  12.171 +     *
  12.172 +     * @param  len
  12.173 +     *         Number of characters to write
  12.174 +     *
  12.175 +     * @throws  IndexOutOfBoundsException
  12.176 +     *          If <tt>off</tt> is negative, or <tt>len</tt> is negative,
  12.177 +     *          or <tt>off+len</tt> is negative or greater than the length
  12.178 +     *          of the given string
  12.179 +     *
  12.180 +     * @throws  IOException
  12.181 +     *          If an I/O error occurs
  12.182 +     */
  12.183 +    public void write(String str, int off, int len) throws IOException {
  12.184 +        synchronized (lock) {
  12.185 +            char cbuf[];
  12.186 +            if (len <= writeBufferSize) {
  12.187 +                if (writeBuffer == null) {
  12.188 +                    writeBuffer = new char[writeBufferSize];
  12.189 +                }
  12.190 +                cbuf = writeBuffer;
  12.191 +            } else {    // Don't permanently allocate very large buffers.
  12.192 +                cbuf = new char[len];
  12.193 +            }
  12.194 +            str.getChars(off, (off + len), cbuf, 0);
  12.195 +            write(cbuf, 0, len);
  12.196 +        }
  12.197 +    }
  12.198 +
  12.199 +    /**
  12.200 +     * Appends the specified character sequence to this writer.
  12.201 +     *
  12.202 +     * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
  12.203 +     * behaves in exactly the same way as the invocation
  12.204 +     *
  12.205 +     * <pre>
  12.206 +     *     out.write(csq.toString()) </pre>
  12.207 +     *
  12.208 +     * <p> Depending on the specification of <tt>toString</tt> for the
  12.209 +     * character sequence <tt>csq</tt>, the entire sequence may not be
  12.210 +     * appended. For instance, invoking the <tt>toString</tt> method of a
  12.211 +     * character buffer will return a subsequence whose content depends upon
  12.212 +     * the buffer's position and limit.
  12.213 +     *
  12.214 +     * @param  csq
  12.215 +     *         The character sequence to append.  If <tt>csq</tt> is
  12.216 +     *         <tt>null</tt>, then the four characters <tt>"null"</tt> are
  12.217 +     *         appended to this writer.
  12.218 +     *
  12.219 +     * @return  This writer
  12.220 +     *
  12.221 +     * @throws  IOException
  12.222 +     *          If an I/O error occurs
  12.223 +     *
  12.224 +     * @since  1.5
  12.225 +     */
  12.226 +    public Writer append(CharSequence csq) throws IOException {
  12.227 +        if (csq == null)
  12.228 +            write("null");
  12.229 +        else
  12.230 +            write(csq.toString());
  12.231 +        return this;
  12.232 +    }
  12.233 +
  12.234 +    /**
  12.235 +     * Appends a subsequence of the specified character sequence to this writer.
  12.236 +     * <tt>Appendable</tt>.
  12.237 +     *
  12.238 +     * <p> An invocation of this method of the form <tt>out.append(csq, start,
  12.239 +     * end)</tt> when <tt>csq</tt> is not <tt>null</tt> behaves in exactly the
  12.240 +     * same way as the invocation
  12.241 +     *
  12.242 +     * <pre>
  12.243 +     *     out.write(csq.subSequence(start, end).toString()) </pre>
  12.244 +     *
  12.245 +     * @param  csq
  12.246 +     *         The character sequence from which a subsequence will be
  12.247 +     *         appended.  If <tt>csq</tt> is <tt>null</tt>, then characters
  12.248 +     *         will be appended as if <tt>csq</tt> contained the four
  12.249 +     *         characters <tt>"null"</tt>.
  12.250 +     *
  12.251 +     * @param  start
  12.252 +     *         The index of the first character in the subsequence
  12.253 +     *
  12.254 +     * @param  end
  12.255 +     *         The index of the character following the last character in the
  12.256 +     *         subsequence
  12.257 +     *
  12.258 +     * @return  This writer
  12.259 +     *
  12.260 +     * @throws  IndexOutOfBoundsException
  12.261 +     *          If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
  12.262 +     *          is greater than <tt>end</tt>, or <tt>end</tt> is greater than
  12.263 +     *          <tt>csq.length()</tt>
  12.264 +     *
  12.265 +     * @throws  IOException
  12.266 +     *          If an I/O error occurs
  12.267 +     *
  12.268 +     * @since  1.5
  12.269 +     */
  12.270 +    public Writer append(CharSequence csq, int start, int end) throws IOException {
  12.271 +        CharSequence cs = (csq == null ? "null" : csq);
  12.272 +        write(cs.subSequence(start, end).toString());
  12.273 +        return this;
  12.274 +    }
  12.275 +
  12.276 +    /**
  12.277 +     * Appends the specified character to this writer.
  12.278 +     *
  12.279 +     * <p> An invocation of this method of the form <tt>out.append(c)</tt>
  12.280 +     * behaves in exactly the same way as the invocation
  12.281 +     *
  12.282 +     * <pre>
  12.283 +     *     out.write(c) </pre>
  12.284 +     *
  12.285 +     * @param  c
  12.286 +     *         The 16-bit character to append
  12.287 +     *
  12.288 +     * @return  This writer
  12.289 +     *
  12.290 +     * @throws  IOException
  12.291 +     *          If an I/O error occurs
  12.292 +     *
  12.293 +     * @since 1.5
  12.294 +     */
  12.295 +    public Writer append(char c) throws IOException {
  12.296 +        write(c);
  12.297 +        return this;
  12.298 +    }
  12.299 +
  12.300 +    /**
  12.301 +     * Flushes the stream.  If the stream has saved any characters from the
  12.302 +     * various write() methods in a buffer, write them immediately to their
  12.303 +     * intended destination.  Then, if that destination is another character or
  12.304 +     * byte stream, flush it.  Thus one flush() invocation will flush all the
  12.305 +     * buffers in a chain of Writers and OutputStreams.
  12.306 +     *
  12.307 +     * <p> If the intended destination of this stream is an abstraction provided
  12.308 +     * by the underlying operating system, for example a file, then flushing the
  12.309 +     * stream guarantees only that bytes previously written to the stream are
  12.310 +     * passed to the operating system for writing; it does not guarantee that
  12.311 +     * they are actually written to a physical device such as a disk drive.
  12.312 +     *
  12.313 +     * @throws  IOException
  12.314 +     *          If an I/O error occurs
  12.315 +     */
  12.316 +    abstract public void flush() throws IOException;
  12.317 +
  12.318 +    /**
  12.319 +     * Closes the stream, flushing it first. Once the stream has been closed,
  12.320 +     * further write() or flush() invocations will cause an IOException to be
  12.321 +     * thrown. Closing a previously closed stream has no effect.
  12.322 +     *
  12.323 +     * @throws  IOException
  12.324 +     *          If an I/O error occurs
  12.325 +     */
  12.326 +    abstract public void close() throws IOException;
  12.327 +
  12.328 +}
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/rt/emul/compact/src/main/java/java/lang/StackOverflowError.java	Sun Sep 08 11:22:51 2013 +0200
    13.3 @@ -0,0 +1,55 @@
    13.4 +/*
    13.5 + * Copyright (c) 1994, 2008, Oracle and/or its affiliates. All rights reserved.
    13.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    13.7 + *
    13.8 + * This code is free software; you can redistribute it and/or modify it
    13.9 + * under the terms of the GNU General Public License version 2 only, as
   13.10 + * published by the Free Software Foundation.  Oracle designates this
   13.11 + * particular file as subject to the "Classpath" exception as provided
   13.12 + * by Oracle in the LICENSE file that accompanied this code.
   13.13 + *
   13.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   13.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   13.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   13.17 + * version 2 for more details (a copy is included in the LICENSE file that
   13.18 + * accompanied this code).
   13.19 + *
   13.20 + * You should have received a copy of the GNU General Public License version
   13.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   13.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   13.23 + *
   13.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   13.25 + * or visit www.oracle.com if you need additional information or have any
   13.26 + * questions.
   13.27 + */
   13.28 +
   13.29 +package java.lang;
   13.30 +
   13.31 +/**
   13.32 + * Thrown when a stack overflow occurs because an application
   13.33 + * recurses too deeply.
   13.34 + *
   13.35 + * @author unascribed
   13.36 + * @since   JDK1.0
   13.37 + */
   13.38 +public
   13.39 +class StackOverflowError extends VirtualMachineError {
   13.40 +    private static final long serialVersionUID = 8609175038441759607L;
   13.41 +
   13.42 +    /**
   13.43 +     * Constructs a <code>StackOverflowError</code> with no detail message.
   13.44 +     */
   13.45 +    public StackOverflowError() {
   13.46 +        super();
   13.47 +    }
   13.48 +
   13.49 +    /**
   13.50 +     * Constructs a <code>StackOverflowError</code> with the specified
   13.51 +     * detail message.
   13.52 +     *
   13.53 +     * @param   s   the detail message.
   13.54 +     */
   13.55 +    public StackOverflowError(String s) {
   13.56 +        super(s);
   13.57 +    }
   13.58 +}
    14.1 --- a/rt/emul/compact/src/main/java/java/lang/System.java	Sun Sep 08 10:58:10 2013 +0200
    14.2 +++ b/rt/emul/compact/src/main/java/java/lang/System.java	Sun Sep 08 11:22:51 2013 +0200
    14.3 @@ -33,8 +33,27 @@
    14.4          return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis();
    14.5      }
    14.6      
    14.7 -    public static long nanoTime() {
    14.8 -        return org.apidesign.bck2brwsr.emul.lang.System.nanoTime();
    14.9 +    public static int identityHashCode(Object obj) {
   14.10 +        return obj.defaultHashCode();
   14.11 +    }
   14.12 +
   14.13 +    public static String getProperty(String name) {
   14.14 +        return null;
   14.15      }
   14.16      
   14.17 +    public static String getProperty(String key, String def) {
   14.18 +        return def;
   14.19 +    }
   14.20 +    
   14.21 +    /**
   14.22 +     * Returns the system-dependent line separator string.  It always
   14.23 +     * returns the same value - the initial value of the {@linkplain
   14.24 +     * #getProperty(String) system property} {@code line.separator}.
   14.25 +     *
   14.26 +     * <p>On UNIX systems, it returns {@code "\n"}; on Microsoft
   14.27 +     * Windows systems it returns {@code "\r\n"}.
   14.28 +     */
   14.29 +    public static String lineSeparator() {
   14.30 +        return "\n";
   14.31 +    }
   14.32  }
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/rt/emul/compact/src/main/java/java/lang/Thread.java	Sun Sep 08 11:22:51 2013 +0200
    15.3 @@ -0,0 +1,1544 @@
    15.4 +/*
    15.5 + * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
    15.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    15.7 + *
    15.8 + * This code is free software; you can redistribute it and/or modify it
    15.9 + * under the terms of the GNU General Public License version 2 only, as
   15.10 + * published by the Free Software Foundation.  Oracle designates this
   15.11 + * particular file as subject to the "Classpath" exception as provided
   15.12 + * by Oracle in the LICENSE file that accompanied this code.
   15.13 + *
   15.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   15.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   15.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   15.17 + * version 2 for more details (a copy is included in the LICENSE file that
   15.18 + * accompanied this code).
   15.19 + *
   15.20 + * You should have received a copy of the GNU General Public License version
   15.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   15.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   15.23 + *
   15.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   15.25 + * or visit www.oracle.com if you need additional information or have any
   15.26 + * questions.
   15.27 + */
   15.28 +
   15.29 +package java.lang;
   15.30 +
   15.31 +import java.util.Map;
   15.32 +
   15.33 +
   15.34 +/**
   15.35 + * A <i>thread</i> is a thread of execution in a program. The Java
   15.36 + * Virtual Machine allows an application to have multiple threads of
   15.37 + * execution running concurrently.
   15.38 + * <p>
   15.39 + * Every thread has a priority. Threads with higher priority are
   15.40 + * executed in preference to threads with lower priority. Each thread
   15.41 + * may or may not also be marked as a daemon. When code running in
   15.42 + * some thread creates a new <code>Thread</code> object, the new
   15.43 + * thread has its priority initially set equal to the priority of the
   15.44 + * creating thread, and is a daemon thread if and only if the
   15.45 + * creating thread is a daemon.
   15.46 + * <p>
   15.47 + * When a Java Virtual Machine starts up, there is usually a single
   15.48 + * non-daemon thread (which typically calls the method named
   15.49 + * <code>main</code> of some designated class). The Java Virtual
   15.50 + * Machine continues to execute threads until either of the following
   15.51 + * occurs:
   15.52 + * <ul>
   15.53 + * <li>The <code>exit</code> method of class <code>Runtime</code> has been
   15.54 + *     called and the security manager has permitted the exit operation
   15.55 + *     to take place.
   15.56 + * <li>All threads that are not daemon threads have died, either by
   15.57 + *     returning from the call to the <code>run</code> method or by
   15.58 + *     throwing an exception that propagates beyond the <code>run</code>
   15.59 + *     method.
   15.60 + * </ul>
   15.61 + * <p>
   15.62 + * There are two ways to create a new thread of execution. One is to
   15.63 + * declare a class to be a subclass of <code>Thread</code>. This
   15.64 + * subclass should override the <code>run</code> method of class
   15.65 + * <code>Thread</code>. An instance of the subclass can then be
   15.66 + * allocated and started. For example, a thread that computes primes
   15.67 + * larger than a stated value could be written as follows:
   15.68 + * <p><hr><blockquote><pre>
   15.69 + *     class PrimeThread extends Thread {
   15.70 + *         long minPrime;
   15.71 + *         PrimeThread(long minPrime) {
   15.72 + *             this.minPrime = minPrime;
   15.73 + *         }
   15.74 + *
   15.75 + *         public void run() {
   15.76 + *             // compute primes larger than minPrime
   15.77 + *             &nbsp;.&nbsp;.&nbsp;.
   15.78 + *         }
   15.79 + *     }
   15.80 + * </pre></blockquote><hr>
   15.81 + * <p>
   15.82 + * The following code would then create a thread and start it running:
   15.83 + * <p><blockquote><pre>
   15.84 + *     PrimeThread p = new PrimeThread(143);
   15.85 + *     p.start();
   15.86 + * </pre></blockquote>
   15.87 + * <p>
   15.88 + * The other way to create a thread is to declare a class that
   15.89 + * implements the <code>Runnable</code> interface. That class then
   15.90 + * implements the <code>run</code> method. An instance of the class can
   15.91 + * then be allocated, passed as an argument when creating
   15.92 + * <code>Thread</code>, and started. The same example in this other
   15.93 + * style looks like the following:
   15.94 + * <p><hr><blockquote><pre>
   15.95 + *     class PrimeRun implements Runnable {
   15.96 + *         long minPrime;
   15.97 + *         PrimeRun(long minPrime) {
   15.98 + *             this.minPrime = minPrime;
   15.99 + *         }
  15.100 + *
  15.101 + *         public void run() {
  15.102 + *             // compute primes larger than minPrime
  15.103 + *             &nbsp;.&nbsp;.&nbsp;.
  15.104 + *         }
  15.105 + *     }
  15.106 + * </pre></blockquote><hr>
  15.107 + * <p>
  15.108 + * The following code would then create a thread and start it running:
  15.109 + * <p><blockquote><pre>
  15.110 + *     PrimeRun p = new PrimeRun(143);
  15.111 + *     new Thread(p).start();
  15.112 + * </pre></blockquote>
  15.113 + * <p>
  15.114 + * Every thread has a name for identification purposes. More than
  15.115 + * one thread may have the same name. If a name is not specified when
  15.116 + * a thread is created, a new name is generated for it.
  15.117 + * <p>
  15.118 + * Unless otherwise noted, passing a {@code null} argument to a constructor
  15.119 + * or method in this class will cause a {@link NullPointerException} to be
  15.120 + * thrown.
  15.121 + *
  15.122 + * @author  unascribed
  15.123 + * @see     Runnable
  15.124 + * @see     Runtime#exit(int)
  15.125 + * @see     #run()
  15.126 + * @see     #stop()
  15.127 + * @since   JDK1.0
  15.128 + */
  15.129 +public
  15.130 +class Thread implements Runnable {
  15.131 +
  15.132 +    /**
  15.133 +     * The minimum priority that a thread can have.
  15.134 +     */
  15.135 +    public final static int MIN_PRIORITY = 1;
  15.136 +
  15.137 +   /**
  15.138 +     * The default priority that is assigned to a thread.
  15.139 +     */
  15.140 +    public final static int NORM_PRIORITY = 5;
  15.141 +
  15.142 +    /**
  15.143 +     * The maximum priority that a thread can have.
  15.144 +     */
  15.145 +    public final static int MAX_PRIORITY = 10;
  15.146 +
  15.147 +    private static final Thread ONE = new Thread("main");
  15.148 +    /**
  15.149 +     * Returns a reference to the currently executing thread object.
  15.150 +     *
  15.151 +     * @return  the currently executing thread.
  15.152 +     */
  15.153 +    public static Thread currentThread() {
  15.154 +        return ONE;
  15.155 +    }
  15.156 +
  15.157 +    /**
  15.158 +     * A hint to the scheduler that the current thread is willing to yield
  15.159 +     * its current use of a processor. The scheduler is free to ignore this
  15.160 +     * hint.
  15.161 +     *
  15.162 +     * <p> Yield is a heuristic attempt to improve relative progression
  15.163 +     * between threads that would otherwise over-utilise a CPU. Its use
  15.164 +     * should be combined with detailed profiling and benchmarking to
  15.165 +     * ensure that it actually has the desired effect.
  15.166 +     *
  15.167 +     * <p> It is rarely appropriate to use this method. It may be useful
  15.168 +     * for debugging or testing purposes, where it may help to reproduce
  15.169 +     * bugs due to race conditions. It may also be useful when designing
  15.170 +     * concurrency control constructs such as the ones in the
  15.171 +     * {@link java.util.concurrent.locks} package.
  15.172 +     */
  15.173 +    public static void yield() {
  15.174 +    }
  15.175 +
  15.176 +    /**
  15.177 +     * Causes the currently executing thread to sleep (temporarily cease
  15.178 +     * execution) for the specified number of milliseconds, subject to
  15.179 +     * the precision and accuracy of system timers and schedulers. The thread
  15.180 +     * does not lose ownership of any monitors.
  15.181 +     *
  15.182 +     * @param  millis
  15.183 +     *         the length of time to sleep in milliseconds
  15.184 +     *
  15.185 +     * @throws  IllegalArgumentException
  15.186 +     *          if the value of {@code millis} is negative
  15.187 +     *
  15.188 +     * @throws  InterruptedException
  15.189 +     *          if any thread has interrupted the current thread. The
  15.190 +     *          <i>interrupted status</i> of the current thread is
  15.191 +     *          cleared when this exception is thrown.
  15.192 +     */
  15.193 +    public static native void sleep(long millis) throws InterruptedException;
  15.194 +
  15.195 +    /**
  15.196 +     * Causes the currently executing thread to sleep (temporarily cease
  15.197 +     * execution) for the specified number of milliseconds plus the specified
  15.198 +     * number of nanoseconds, subject to the precision and accuracy of system
  15.199 +     * timers and schedulers. The thread does not lose ownership of any
  15.200 +     * monitors.
  15.201 +     *
  15.202 +     * @param  millis
  15.203 +     *         the length of time to sleep in milliseconds
  15.204 +     *
  15.205 +     * @param  nanos
  15.206 +     *         {@code 0-999999} additional nanoseconds to sleep
  15.207 +     *
  15.208 +     * @throws  IllegalArgumentException
  15.209 +     *          if the value of {@code millis} is negative, or the value of
  15.210 +     *          {@code nanos} is not in the range {@code 0-999999}
  15.211 +     *
  15.212 +     * @throws  InterruptedException
  15.213 +     *          if any thread has interrupted the current thread. The
  15.214 +     *          <i>interrupted status</i> of the current thread is
  15.215 +     *          cleared when this exception is thrown.
  15.216 +     */
  15.217 +    public static void sleep(long millis, int nanos)
  15.218 +    throws InterruptedException {
  15.219 +        if (millis < 0) {
  15.220 +            throw new IllegalArgumentException("timeout value is negative");
  15.221 +        }
  15.222 +
  15.223 +        if (nanos < 0 || nanos > 999999) {
  15.224 +            throw new IllegalArgumentException(
  15.225 +                                "nanosecond timeout value out of range");
  15.226 +        }
  15.227 +
  15.228 +        if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
  15.229 +            millis++;
  15.230 +        }
  15.231 +
  15.232 +        sleep(millis);
  15.233 +    }
  15.234 +    private Runnable target;
  15.235 +    private String name;
  15.236 +
  15.237 +    /**
  15.238 +     * Throws CloneNotSupportedException as a Thread can not be meaningfully
  15.239 +     * cloned. Construct a new Thread instead.
  15.240 +     *
  15.241 +     * @throws  CloneNotSupportedException
  15.242 +     *          always
  15.243 +     */
  15.244 +    @Override
  15.245 +    protected Object clone() throws CloneNotSupportedException {
  15.246 +        throw new CloneNotSupportedException();
  15.247 +    }
  15.248 +
  15.249 +    /**
  15.250 +     * Allocates a new {@code Thread} object. This constructor has the same
  15.251 +     * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
  15.252 +     * {@code (null, null, gname)}, where {@code gname} is a newly generated
  15.253 +     * name. Automatically generated names are of the form
  15.254 +     * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
  15.255 +     */
  15.256 +    public Thread() {
  15.257 +        init(null, null, "Thread-" + nextThreadNum(), 0);
  15.258 +    }
  15.259 +    
  15.260 +    private static int nextThreadNum() {
  15.261 +        return -1;
  15.262 +    }
  15.263 +
  15.264 +    /**
  15.265 +     * Allocates a new {@code Thread} object. This constructor has the same
  15.266 +     * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
  15.267 +     * {@code (null, target, gname)}, where {@code gname} is a newly generated
  15.268 +     * name. Automatically generated names are of the form
  15.269 +     * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
  15.270 +     *
  15.271 +     * @param  target
  15.272 +     *         the object whose {@code run} method is invoked when this thread
  15.273 +     *         is started. If {@code null}, this classes {@code run} method does
  15.274 +     *         nothing.
  15.275 +     */
  15.276 +    public Thread(Runnable target) {
  15.277 +        init(null, target, "Thread-" + nextThreadNum(), 0);
  15.278 +    }
  15.279 +
  15.280 +    /**
  15.281 +     * Allocates a new {@code Thread} object. This constructor has the same
  15.282 +     * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
  15.283 +     * {@code (group, target, gname)} ,where {@code gname} is a newly generated
  15.284 +     * name. Automatically generated names are of the form
  15.285 +     * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
  15.286 +     *
  15.287 +     * @param  group
  15.288 +     *         the thread group. If {@code null} and there is a security
  15.289 +     *         manager, the group is determined by {@linkplain
  15.290 +     *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
  15.291 +     *         If there is not a security manager or {@code
  15.292 +     *         SecurityManager.getThreadGroup()} returns {@code null}, the group
  15.293 +     *         is set to the current thread's thread group.
  15.294 +     *
  15.295 +     * @param  target
  15.296 +     *         the object whose {@code run} method is invoked when this thread
  15.297 +     *         is started. If {@code null}, this thread's run method is invoked.
  15.298 +     *
  15.299 +     * @throws  SecurityException
  15.300 +     *          if the current thread cannot create a thread in the specified
  15.301 +     *          thread group
  15.302 +     */
  15.303 +//    public Thread(ThreadGroup group, Runnable target) {
  15.304 +//        init(group, target, "Thread-" + nextThreadNum(), 0);
  15.305 +//    }
  15.306 +
  15.307 +    /**
  15.308 +     * Allocates a new {@code Thread} object. This constructor has the same
  15.309 +     * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
  15.310 +     * {@code (null, null, name)}.
  15.311 +     *
  15.312 +     * @param   name
  15.313 +     *          the name of the new thread
  15.314 +     */
  15.315 +    public Thread(String name) {
  15.316 +        init(null, null, name, 0);
  15.317 +    }
  15.318 +    
  15.319 +    private void init(Object o1, Runnable trgt, String nm, int i4) {
  15.320 +        this.target = trgt;
  15.321 +        this.name = nm;
  15.322 +    }
  15.323 +
  15.324 +    /**
  15.325 +     * Allocates a new {@code Thread} object. This constructor has the same
  15.326 +     * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
  15.327 +     * {@code (group, null, name)}.
  15.328 +     *
  15.329 +     * @param  group
  15.330 +     *         the thread group. If {@code null} and there is a security
  15.331 +     *         manager, the group is determined by {@linkplain
  15.332 +     *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
  15.333 +     *         If there is not a security manager or {@code
  15.334 +     *         SecurityManager.getThreadGroup()} returns {@code null}, the group
  15.335 +     *         is set to the current thread's thread group.
  15.336 +     *
  15.337 +     * @param  name
  15.338 +     *         the name of the new thread
  15.339 +     *
  15.340 +     * @throws  SecurityException
  15.341 +     *          if the current thread cannot create a thread in the specified
  15.342 +     *          thread group
  15.343 +     */
  15.344 +//    public Thread(ThreadGroup group, String name) {
  15.345 +//        init(group, null, name, 0);
  15.346 +//    }
  15.347 +
  15.348 +    /**
  15.349 +     * Allocates a new {@code Thread} object. This constructor has the same
  15.350 +     * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
  15.351 +     * {@code (null, target, name)}.
  15.352 +     *
  15.353 +     * @param  target
  15.354 +     *         the object whose {@code run} method is invoked when this thread
  15.355 +     *         is started. If {@code null}, this thread's run method is invoked.
  15.356 +     *
  15.357 +     * @param  name
  15.358 +     *         the name of the new thread
  15.359 +     */
  15.360 +    public Thread(Runnable target, String name) {
  15.361 +        init(null, target, name, 0);
  15.362 +    }
  15.363 +
  15.364 +    /**
  15.365 +     * Allocates a new {@code Thread} object so that it has {@code target}
  15.366 +     * as its run object, has the specified {@code name} as its name,
  15.367 +     * and belongs to the thread group referred to by {@code group}.
  15.368 +     *
  15.369 +     * <p>If there is a security manager, its
  15.370 +     * {@link SecurityManager#checkAccess(ThreadGroup) checkAccess}
  15.371 +     * method is invoked with the ThreadGroup as its argument.
  15.372 +     *
  15.373 +     * <p>In addition, its {@code checkPermission} method is invoked with
  15.374 +     * the {@code RuntimePermission("enableContextClassLoaderOverride")}
  15.375 +     * permission when invoked directly or indirectly by the constructor
  15.376 +     * of a subclass which overrides the {@code getContextClassLoader}
  15.377 +     * or {@code setContextClassLoader} methods.
  15.378 +     *
  15.379 +     * <p>The priority of the newly created thread is set equal to the
  15.380 +     * priority of the thread creating it, that is, the currently running
  15.381 +     * thread. The method {@linkplain #setPriority setPriority} may be
  15.382 +     * used to change the priority to a new value.
  15.383 +     *
  15.384 +     * <p>The newly created thread is initially marked as being a daemon
  15.385 +     * thread if and only if the thread creating it is currently marked
  15.386 +     * as a daemon thread. The method {@linkplain #setDaemon setDaemon}
  15.387 +     * may be used to change whether or not a thread is a daemon.
  15.388 +     *
  15.389 +     * @param  group
  15.390 +     *         the thread group. If {@code null} and there is a security
  15.391 +     *         manager, the group is determined by {@linkplain
  15.392 +     *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
  15.393 +     *         If there is not a security manager or {@code
  15.394 +     *         SecurityManager.getThreadGroup()} returns {@code null}, the group
  15.395 +     *         is set to the current thread's thread group.
  15.396 +     *
  15.397 +     * @param  target
  15.398 +     *         the object whose {@code run} method is invoked when this thread
  15.399 +     *         is started. If {@code null}, this thread's run method is invoked.
  15.400 +     *
  15.401 +     * @param  name
  15.402 +     *         the name of the new thread
  15.403 +     *
  15.404 +     * @throws  SecurityException
  15.405 +     *          if the current thread cannot create a thread in the specified
  15.406 +     *          thread group or cannot override the context class loader methods.
  15.407 +     */
  15.408 +//    public Thread(ThreadGroup group, Runnable target, String name) {
  15.409 +//        init(group, target, name, 0);
  15.410 +//    }
  15.411 +
  15.412 +    /**
  15.413 +     * Allocates a new {@code Thread} object so that it has {@code target}
  15.414 +     * as its run object, has the specified {@code name} as its name,
  15.415 +     * and belongs to the thread group referred to by {@code group}, and has
  15.416 +     * the specified <i>stack size</i>.
  15.417 +     *
  15.418 +     * <p>This constructor is identical to {@link
  15.419 +     * #Thread(ThreadGroup,Runnable,String)} with the exception of the fact
  15.420 +     * that it allows the thread stack size to be specified.  The stack size
  15.421 +     * is the approximate number of bytes of address space that the virtual
  15.422 +     * machine is to allocate for this thread's stack.  <b>The effect of the
  15.423 +     * {@code stackSize} parameter, if any, is highly platform dependent.</b>
  15.424 +     *
  15.425 +     * <p>On some platforms, specifying a higher value for the
  15.426 +     * {@code stackSize} parameter may allow a thread to achieve greater
  15.427 +     * recursion depth before throwing a {@link StackOverflowError}.
  15.428 +     * Similarly, specifying a lower value may allow a greater number of
  15.429 +     * threads to exist concurrently without throwing an {@link
  15.430 +     * OutOfMemoryError} (or other internal error).  The details of
  15.431 +     * the relationship between the value of the <tt>stackSize</tt> parameter
  15.432 +     * and the maximum recursion depth and concurrency level are
  15.433 +     * platform-dependent.  <b>On some platforms, the value of the
  15.434 +     * {@code stackSize} parameter may have no effect whatsoever.</b>
  15.435 +     *
  15.436 +     * <p>The virtual machine is free to treat the {@code stackSize}
  15.437 +     * parameter as a suggestion.  If the specified value is unreasonably low
  15.438 +     * for the platform, the virtual machine may instead use some
  15.439 +     * platform-specific minimum value; if the specified value is unreasonably
  15.440 +     * high, the virtual machine may instead use some platform-specific
  15.441 +     * maximum.  Likewise, the virtual machine is free to round the specified
  15.442 +     * value up or down as it sees fit (or to ignore it completely).
  15.443 +     *
  15.444 +     * <p>Specifying a value of zero for the {@code stackSize} parameter will
  15.445 +     * cause this constructor to behave exactly like the
  15.446 +     * {@code Thread(ThreadGroup, Runnable, String)} constructor.
  15.447 +     *
  15.448 +     * <p><i>Due to the platform-dependent nature of the behavior of this
  15.449 +     * constructor, extreme care should be exercised in its use.
  15.450 +     * The thread stack size necessary to perform a given computation will
  15.451 +     * likely vary from one JRE implementation to another.  In light of this
  15.452 +     * variation, careful tuning of the stack size parameter may be required,
  15.453 +     * and the tuning may need to be repeated for each JRE implementation on
  15.454 +     * which an application is to run.</i>
  15.455 +     *
  15.456 +     * <p>Implementation note: Java platform implementers are encouraged to
  15.457 +     * document their implementation's behavior with respect to the
  15.458 +     * {@code stackSize} parameter.
  15.459 +     *
  15.460 +     *
  15.461 +     * @param  group
  15.462 +     *         the thread group. If {@code null} and there is a security
  15.463 +     *         manager, the group is determined by {@linkplain
  15.464 +     *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
  15.465 +     *         If there is not a security manager or {@code
  15.466 +     *         SecurityManager.getThreadGroup()} returns {@code null}, the group
  15.467 +     *         is set to the current thread's thread group.
  15.468 +     *
  15.469 +     * @param  target
  15.470 +     *         the object whose {@code run} method is invoked when this thread
  15.471 +     *         is started. If {@code null}, this thread's run method is invoked.
  15.472 +     *
  15.473 +     * @param  name
  15.474 +     *         the name of the new thread
  15.475 +     *
  15.476 +     * @param  stackSize
  15.477 +     *         the desired stack size for the new thread, or zero to indicate
  15.478 +     *         that this parameter is to be ignored.
  15.479 +     *
  15.480 +     * @throws  SecurityException
  15.481 +     *          if the current thread cannot create a thread in the specified
  15.482 +     *          thread group
  15.483 +     *
  15.484 +     * @since 1.4
  15.485 +     */
  15.486 +//    public Thread(ThreadGroup group, Runnable target, String name,
  15.487 +//                  long stackSize) {
  15.488 +//        init(group, target, name, stackSize);
  15.489 +//    }
  15.490 +
  15.491 +    /**
  15.492 +     * Causes this thread to begin execution; the Java Virtual Machine
  15.493 +     * calls the <code>run</code> method of this thread.
  15.494 +     * <p>
  15.495 +     * The result is that two threads are running concurrently: the
  15.496 +     * current thread (which returns from the call to the
  15.497 +     * <code>start</code> method) and the other thread (which executes its
  15.498 +     * <code>run</code> method).
  15.499 +     * <p>
  15.500 +     * It is never legal to start a thread more than once.
  15.501 +     * In particular, a thread may not be restarted once it has completed
  15.502 +     * execution.
  15.503 +     *
  15.504 +     * @exception  IllegalThreadStateException  if the thread was already
  15.505 +     *               started.
  15.506 +     * @see        #run()
  15.507 +     * @see        #stop()
  15.508 +     */
  15.509 +    public void start() {
  15.510 +        throw new SecurityException();
  15.511 +    }
  15.512 +
  15.513 +    /**
  15.514 +     * If this thread was constructed using a separate
  15.515 +     * <code>Runnable</code> run object, then that
  15.516 +     * <code>Runnable</code> object's <code>run</code> method is called;
  15.517 +     * otherwise, this method does nothing and returns.
  15.518 +     * <p>
  15.519 +     * Subclasses of <code>Thread</code> should override this method.
  15.520 +     *
  15.521 +     * @see     #start()
  15.522 +     * @see     #stop()
  15.523 +     * @see     #Thread(ThreadGroup, Runnable, String)
  15.524 +     */
  15.525 +    @Override
  15.526 +    public void run() {
  15.527 +        if (target != null) {
  15.528 +            target.run();
  15.529 +        }
  15.530 +    }
  15.531 +
  15.532 +    /**
  15.533 +     * Forces the thread to stop executing.
  15.534 +     * <p>
  15.535 +     * If there is a security manager installed, its <code>checkAccess</code>
  15.536 +     * method is called with <code>this</code>
  15.537 +     * as its argument. This may result in a
  15.538 +     * <code>SecurityException</code> being raised (in the current thread).
  15.539 +     * <p>
  15.540 +     * If this thread is different from the current thread (that is, the current
  15.541 +     * thread is trying to stop a thread other than itself), the
  15.542 +     * security manager's <code>checkPermission</code> method (with a
  15.543 +     * <code>RuntimePermission("stopThread")</code> argument) is called in
  15.544 +     * addition.
  15.545 +     * Again, this may result in throwing a
  15.546 +     * <code>SecurityException</code> (in the current thread).
  15.547 +     * <p>
  15.548 +     * The thread represented by this thread is forced to stop whatever
  15.549 +     * it is doing abnormally and to throw a newly created
  15.550 +     * <code>ThreadDeath</code> object as an exception.
  15.551 +     * <p>
  15.552 +     * It is permitted to stop a thread that has not yet been started.
  15.553 +     * If the thread is eventually started, it immediately terminates.
  15.554 +     * <p>
  15.555 +     * An application should not normally try to catch
  15.556 +     * <code>ThreadDeath</code> unless it must do some extraordinary
  15.557 +     * cleanup operation (note that the throwing of
  15.558 +     * <code>ThreadDeath</code> causes <code>finally</code> clauses of
  15.559 +     * <code>try</code> statements to be executed before the thread
  15.560 +     * officially dies).  If a <code>catch</code> clause catches a
  15.561 +     * <code>ThreadDeath</code> object, it is important to rethrow the
  15.562 +     * object so that the thread actually dies.
  15.563 +     * <p>
  15.564 +     * The top-level error handler that reacts to otherwise uncaught
  15.565 +     * exceptions does not print out a message or otherwise notify the
  15.566 +     * application if the uncaught exception is an instance of
  15.567 +     * <code>ThreadDeath</code>.
  15.568 +     *
  15.569 +     * @exception  SecurityException  if the current thread cannot
  15.570 +     *               modify this thread.
  15.571 +     * @see        #interrupt()
  15.572 +     * @see        #checkAccess()
  15.573 +     * @see        #run()
  15.574 +     * @see        #start()
  15.575 +     * @see        ThreadDeath
  15.576 +     * @see        ThreadGroup#uncaughtException(Thread,Throwable)
  15.577 +     * @see        SecurityManager#checkAccess(Thread)
  15.578 +     * @see        SecurityManager#checkPermission
  15.579 +     * @deprecated This method is inherently unsafe.  Stopping a thread with
  15.580 +     *       Thread.stop causes it to unlock all of the monitors that it
  15.581 +     *       has locked (as a natural consequence of the unchecked
  15.582 +     *       <code>ThreadDeath</code> exception propagating up the stack).  If
  15.583 +     *       any of the objects previously protected by these monitors were in
  15.584 +     *       an inconsistent state, the damaged objects become visible to
  15.585 +     *       other threads, potentially resulting in arbitrary behavior.  Many
  15.586 +     *       uses of <code>stop</code> should be replaced by code that simply
  15.587 +     *       modifies some variable to indicate that the target thread should
  15.588 +     *       stop running.  The target thread should check this variable
  15.589 +     *       regularly, and return from its run method in an orderly fashion
  15.590 +     *       if the variable indicates that it is to stop running.  If the
  15.591 +     *       target thread waits for long periods (on a condition variable,
  15.592 +     *       for example), the <code>interrupt</code> method should be used to
  15.593 +     *       interrupt the wait.
  15.594 +     *       For more information, see
  15.595 +     *       <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
  15.596 +     *       are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
  15.597 +     */
  15.598 +    @Deprecated
  15.599 +    public final void stop() {
  15.600 +        stop(null);
  15.601 +    }
  15.602 +
  15.603 +    /**
  15.604 +     * Forces the thread to stop executing.
  15.605 +     * <p>
  15.606 +     * If there is a security manager installed, the <code>checkAccess</code>
  15.607 +     * method of this thread is called, which may result in a
  15.608 +     * <code>SecurityException</code> being raised (in the current thread).
  15.609 +     * <p>
  15.610 +     * If this thread is different from the current thread (that is, the current
  15.611 +     * thread is trying to stop a thread other than itself) or
  15.612 +     * <code>obj</code> is not an instance of <code>ThreadDeath</code>, the
  15.613 +     * security manager's <code>checkPermission</code> method (with the
  15.614 +     * <code>RuntimePermission("stopThread")</code> argument) is called in
  15.615 +     * addition.
  15.616 +     * Again, this may result in throwing a
  15.617 +     * <code>SecurityException</code> (in the current thread).
  15.618 +     * <p>
  15.619 +     * If the argument <code>obj</code> is null, a
  15.620 +     * <code>NullPointerException</code> is thrown (in the current thread).
  15.621 +     * <p>
  15.622 +     * The thread represented by this thread is forced to stop
  15.623 +     * whatever it is doing abnormally and to throw the
  15.624 +     * <code>Throwable</code> object <code>obj</code> as an exception. This
  15.625 +     * is an unusual action to take; normally, the <code>stop</code> method
  15.626 +     * that takes no arguments should be used.
  15.627 +     * <p>
  15.628 +     * It is permitted to stop a thread that has not yet been started.
  15.629 +     * If the thread is eventually started, it immediately terminates.
  15.630 +     *
  15.631 +     * @param      obj   the Throwable object to be thrown.
  15.632 +     * @exception  SecurityException  if the current thread cannot modify
  15.633 +     *               this thread.
  15.634 +     * @throws     NullPointerException if obj is <tt>null</tt>.
  15.635 +     * @see        #interrupt()
  15.636 +     * @see        #checkAccess()
  15.637 +     * @see        #run()
  15.638 +     * @see        #start()
  15.639 +     * @see        #stop()
  15.640 +     * @see        SecurityManager#checkAccess(Thread)
  15.641 +     * @see        SecurityManager#checkPermission
  15.642 +     * @deprecated This method is inherently unsafe.  See {@link #stop()}
  15.643 +     *        for details.  An additional danger of this
  15.644 +     *        method is that it may be used to generate exceptions that the
  15.645 +     *        target thread is unprepared to handle (including checked
  15.646 +     *        exceptions that the thread could not possibly throw, were it
  15.647 +     *        not for this method).
  15.648 +     *        For more information, see
  15.649 +     *        <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
  15.650 +     *        are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
  15.651 +     */
  15.652 +    @Deprecated
  15.653 +    public final synchronized void stop(Throwable obj) {
  15.654 +        throw new SecurityException();
  15.655 +    }
  15.656 +
  15.657 +    /**
  15.658 +     * Interrupts this thread.
  15.659 +     *
  15.660 +     * <p> Unless the current thread is interrupting itself, which is
  15.661 +     * always permitted, the {@link #checkAccess() checkAccess} method
  15.662 +     * of this thread is invoked, which may cause a {@link
  15.663 +     * SecurityException} to be thrown.
  15.664 +     *
  15.665 +     * <p> If this thread is blocked in an invocation of the {@link
  15.666 +     * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
  15.667 +     * Object#wait(long, int) wait(long, int)} methods of the {@link Object}
  15.668 +     * class, or of the {@link #join()}, {@link #join(long)}, {@link
  15.669 +     * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
  15.670 +     * methods of this class, then its interrupt status will be cleared and it
  15.671 +     * will receive an {@link InterruptedException}.
  15.672 +     *
  15.673 +     * <p> If this thread is blocked in an I/O operation upon an {@link
  15.674 +     * java.nio.channels.InterruptibleChannel </code>interruptible
  15.675 +     * channel<code>} then the channel will be closed, the thread's interrupt
  15.676 +     * status will be set, and the thread will receive a {@link
  15.677 +     * java.nio.channels.ClosedByInterruptException}.
  15.678 +     *
  15.679 +     * <p> If this thread is blocked in a {@link java.nio.channels.Selector}
  15.680 +     * then the thread's interrupt status will be set and it will return
  15.681 +     * immediately from the selection operation, possibly with a non-zero
  15.682 +     * value, just as if the selector's {@link
  15.683 +     * java.nio.channels.Selector#wakeup wakeup} method were invoked.
  15.684 +     *
  15.685 +     * <p> If none of the previous conditions hold then this thread's interrupt
  15.686 +     * status will be set. </p>
  15.687 +     *
  15.688 +     * <p> Interrupting a thread that is not alive need not have any effect.
  15.689 +     *
  15.690 +     * @throws  SecurityException
  15.691 +     *          if the current thread cannot modify this thread
  15.692 +     *
  15.693 +     * @revised 6.0
  15.694 +     * @spec JSR-51
  15.695 +     */
  15.696 +    public void interrupt() {
  15.697 +        throw new SecurityException();
  15.698 +    }
  15.699 +
  15.700 +    /**
  15.701 +     * Tests whether the current thread has been interrupted.  The
  15.702 +     * <i>interrupted status</i> of the thread is cleared by this method.  In
  15.703 +     * other words, if this method were to be called twice in succession, the
  15.704 +     * second call would return false (unless the current thread were
  15.705 +     * interrupted again, after the first call had cleared its interrupted
  15.706 +     * status and before the second call had examined it).
  15.707 +     *
  15.708 +     * <p>A thread interruption ignored because a thread was not alive
  15.709 +     * at the time of the interrupt will be reflected by this method
  15.710 +     * returning false.
  15.711 +     *
  15.712 +     * @return  <code>true</code> if the current thread has been interrupted;
  15.713 +     *          <code>false</code> otherwise.
  15.714 +     * @see #isInterrupted()
  15.715 +     * @revised 6.0
  15.716 +     */
  15.717 +    public static boolean interrupted() {
  15.718 +        return currentThread().isInterrupted();
  15.719 +    }
  15.720 +
  15.721 +    /**
  15.722 +     * Tests whether this thread has been interrupted.  The <i>interrupted
  15.723 +     * status</i> of the thread is unaffected by this method.
  15.724 +     *
  15.725 +     * <p>A thread interruption ignored because a thread was not alive
  15.726 +     * at the time of the interrupt will be reflected by this method
  15.727 +     * returning false.
  15.728 +     *
  15.729 +     * @return  <code>true</code> if this thread has been interrupted;
  15.730 +     *          <code>false</code> otherwise.
  15.731 +     * @see     #interrupted()
  15.732 +     * @revised 6.0
  15.733 +     */
  15.734 +    public boolean isInterrupted() {
  15.735 +        return false;
  15.736 +    }
  15.737 +
  15.738 +    /**
  15.739 +     * Throws {@link NoSuchMethodError}.
  15.740 +     *
  15.741 +     * @deprecated This method was originally designed to destroy this
  15.742 +     *     thread without any cleanup. Any monitors it held would have
  15.743 +     *     remained locked. However, the method was never implemented.
  15.744 +     *     If if were to be implemented, it would be deadlock-prone in
  15.745 +     *     much the manner of {@link #suspend}. If the target thread held
  15.746 +     *     a lock protecting a critical system resource when it was
  15.747 +     *     destroyed, no thread could ever access this resource again.
  15.748 +     *     If another thread ever attempted to lock this resource, deadlock
  15.749 +     *     would result. Such deadlocks typically manifest themselves as
  15.750 +     *     "frozen" processes. For more information, see
  15.751 +     *     <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">
  15.752 +     *     Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
  15.753 +     * @throws NoSuchMethodError always
  15.754 +     */
  15.755 +    @Deprecated
  15.756 +    public void destroy() {
  15.757 +        throw new SecurityException();
  15.758 +    }
  15.759 +
  15.760 +    /**
  15.761 +     * Tests if this thread is alive. A thread is alive if it has
  15.762 +     * been started and has not yet died.
  15.763 +     *
  15.764 +     * @return  <code>true</code> if this thread is alive;
  15.765 +     *          <code>false</code> otherwise.
  15.766 +     */
  15.767 +    public final boolean isAlive() {
  15.768 +        return true;
  15.769 +    }
  15.770 +
  15.771 +    /**
  15.772 +     * Suspends this thread.
  15.773 +     * <p>
  15.774 +     * First, the <code>checkAccess</code> method of this thread is called
  15.775 +     * with no arguments. This may result in throwing a
  15.776 +     * <code>SecurityException </code>(in the current thread).
  15.777 +     * <p>
  15.778 +     * If the thread is alive, it is suspended and makes no further
  15.779 +     * progress unless and until it is resumed.
  15.780 +     *
  15.781 +     * @exception  SecurityException  if the current thread cannot modify
  15.782 +     *               this thread.
  15.783 +     * @see #checkAccess
  15.784 +     * @deprecated   This method has been deprecated, as it is
  15.785 +     *   inherently deadlock-prone.  If the target thread holds a lock on the
  15.786 +     *   monitor protecting a critical system resource when it is suspended, no
  15.787 +     *   thread can access this resource until the target thread is resumed. If
  15.788 +     *   the thread that would resume the target thread attempts to lock this
  15.789 +     *   monitor prior to calling <code>resume</code>, deadlock results.  Such
  15.790 +     *   deadlocks typically manifest themselves as "frozen" processes.
  15.791 +     *   For more information, see
  15.792 +     *   <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
  15.793 +     *   are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
  15.794 +     */
  15.795 +    @Deprecated
  15.796 +    public final void suspend() {
  15.797 +        checkAccess();
  15.798 +    }
  15.799 +
  15.800 +    /**
  15.801 +     * Resumes a suspended thread.
  15.802 +     * <p>
  15.803 +     * First, the <code>checkAccess</code> method of this thread is called
  15.804 +     * with no arguments. This may result in throwing a
  15.805 +     * <code>SecurityException</code> (in the current thread).
  15.806 +     * <p>
  15.807 +     * If the thread is alive but suspended, it is resumed and is
  15.808 +     * permitted to make progress in its execution.
  15.809 +     *
  15.810 +     * @exception  SecurityException  if the current thread cannot modify this
  15.811 +     *               thread.
  15.812 +     * @see        #checkAccess
  15.813 +     * @see        #suspend()
  15.814 +     * @deprecated This method exists solely for use with {@link #suspend},
  15.815 +     *     which has been deprecated because it is deadlock-prone.
  15.816 +     *     For more information, see
  15.817 +     *     <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
  15.818 +     *     are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
  15.819 +     */
  15.820 +    @Deprecated
  15.821 +    public final void resume() {
  15.822 +        checkAccess();
  15.823 +    }
  15.824 +
  15.825 +    /**
  15.826 +     * Changes the priority of this thread.
  15.827 +     * <p>
  15.828 +     * First the <code>checkAccess</code> method of this thread is called
  15.829 +     * with no arguments. This may result in throwing a
  15.830 +     * <code>SecurityException</code>.
  15.831 +     * <p>
  15.832 +     * Otherwise, the priority of this thread is set to the smaller of
  15.833 +     * the specified <code>newPriority</code> and the maximum permitted
  15.834 +     * priority of the thread's thread group.
  15.835 +     *
  15.836 +     * @param newPriority priority to set this thread to
  15.837 +     * @exception  IllegalArgumentException  If the priority is not in the
  15.838 +     *               range <code>MIN_PRIORITY</code> to
  15.839 +     *               <code>MAX_PRIORITY</code>.
  15.840 +     * @exception  SecurityException  if the current thread cannot modify
  15.841 +     *               this thread.
  15.842 +     * @see        #getPriority
  15.843 +     * @see        #checkAccess()
  15.844 +     * @see        #getThreadGroup()
  15.845 +     * @see        #MAX_PRIORITY
  15.846 +     * @see        #MIN_PRIORITY
  15.847 +     * @see        ThreadGroup#getMaxPriority()
  15.848 +     */
  15.849 +    public final void setPriority(int newPriority) {
  15.850 +        throw new SecurityException();
  15.851 +    }
  15.852 +
  15.853 +    /**
  15.854 +     * Returns this thread's priority.
  15.855 +     *
  15.856 +     * @return  this thread's priority.
  15.857 +     * @see     #setPriority
  15.858 +     */
  15.859 +    public final int getPriority() {
  15.860 +        return Thread.NORM_PRIORITY;
  15.861 +    }
  15.862 +
  15.863 +    /**
  15.864 +     * Changes the name of this thread to be equal to the argument
  15.865 +     * <code>name</code>.
  15.866 +     * <p>
  15.867 +     * First the <code>checkAccess</code> method of this thread is called
  15.868 +     * with no arguments. This may result in throwing a
  15.869 +     * <code>SecurityException</code>.
  15.870 +     *
  15.871 +     * @param      name   the new name for this thread.
  15.872 +     * @exception  SecurityException  if the current thread cannot modify this
  15.873 +     *               thread.
  15.874 +     * @see        #getName
  15.875 +     * @see        #checkAccess()
  15.876 +     */
  15.877 +    public final void setName(String name) {
  15.878 +        throw new SecurityException();
  15.879 +    }
  15.880 +
  15.881 +    /**
  15.882 +     * Returns this thread's name.
  15.883 +     *
  15.884 +     * @return  this thread's name.
  15.885 +     * @see     #setName(String)
  15.886 +     */
  15.887 +    public final String getName() {
  15.888 +        return String.valueOf(name);
  15.889 +    }
  15.890 +
  15.891 +    /**
  15.892 +     * Returns the thread group to which this thread belongs.
  15.893 +     * This method returns null if this thread has died
  15.894 +     * (been stopped).
  15.895 +     *
  15.896 +     * @return  this thread's thread group.
  15.897 +     */
  15.898 +//    public final ThreadGroup getThreadGroup() {
  15.899 +//        return group;
  15.900 +//    }
  15.901 +
  15.902 +    /**
  15.903 +     * Returns an estimate of the number of active threads in the current
  15.904 +     * thread's {@linkplain java.lang.ThreadGroup thread group} and its
  15.905 +     * subgroups. Recursively iterates over all subgroups in the current
  15.906 +     * thread's thread group.
  15.907 +     *
  15.908 +     * <p> The value returned is only an estimate because the number of
  15.909 +     * threads may change dynamically while this method traverses internal
  15.910 +     * data structures, and might be affected by the presence of certain
  15.911 +     * system threads. This method is intended primarily for debugging
  15.912 +     * and monitoring purposes.
  15.913 +     *
  15.914 +     * @return  an estimate of the number of active threads in the current
  15.915 +     *          thread's thread group and in any other thread group that
  15.916 +     *          has the current thread's thread group as an ancestor
  15.917 +     */
  15.918 +    public static int activeCount() {
  15.919 +        return 1;
  15.920 +    }
  15.921 +
  15.922 +    /**
  15.923 +     * Copies into the specified array every active thread in the current
  15.924 +     * thread's thread group and its subgroups. This method simply
  15.925 +     * invokes the {@link java.lang.ThreadGroup#enumerate(Thread[])}
  15.926 +     * method of the current thread's thread group.
  15.927 +     *
  15.928 +     * <p> An application might use the {@linkplain #activeCount activeCount}
  15.929 +     * method to get an estimate of how big the array should be, however
  15.930 +     * <i>if the array is too short to hold all the threads, the extra threads
  15.931 +     * are silently ignored.</i>  If it is critical to obtain every active
  15.932 +     * thread in the current thread's thread group and its subgroups, the
  15.933 +     * invoker should verify that the returned int value is strictly less
  15.934 +     * than the length of {@code tarray}.
  15.935 +     *
  15.936 +     * <p> Due to the inherent race condition in this method, it is recommended
  15.937 +     * that the method only be used for debugging and monitoring purposes.
  15.938 +     *
  15.939 +     * @param  tarray
  15.940 +     *         an array into which to put the list of threads
  15.941 +     *
  15.942 +     * @return  the number of threads put into the array
  15.943 +     *
  15.944 +     * @throws  SecurityException
  15.945 +     *          if {@link java.lang.ThreadGroup#checkAccess} determines that
  15.946 +     *          the current thread cannot access its thread group
  15.947 +     */
  15.948 +    public static int enumerate(Thread tarray[]) {
  15.949 +        throw new SecurityException();
  15.950 +    }
  15.951 +
  15.952 +    /**
  15.953 +     * Counts the number of stack frames in this thread. The thread must
  15.954 +     * be suspended.
  15.955 +     *
  15.956 +     * @return     the number of stack frames in this thread.
  15.957 +     * @exception  IllegalThreadStateException  if this thread is not
  15.958 +     *             suspended.
  15.959 +     * @deprecated The definition of this call depends on {@link #suspend},
  15.960 +     *             which is deprecated.  Further, the results of this call
  15.961 +     *             were never well-defined.
  15.962 +     */
  15.963 +    @Deprecated
  15.964 +    public native int countStackFrames();
  15.965 +
  15.966 +    /**
  15.967 +     * Waits at most {@code millis} milliseconds for this thread to
  15.968 +     * die. A timeout of {@code 0} means to wait forever.
  15.969 +     *
  15.970 +     * <p> This implementation uses a loop of {@code this.wait} calls
  15.971 +     * conditioned on {@code this.isAlive}. As a thread terminates the
  15.972 +     * {@code this.notifyAll} method is invoked. It is recommended that
  15.973 +     * applications not use {@code wait}, {@code notify}, or
  15.974 +     * {@code notifyAll} on {@code Thread} instances.
  15.975 +     *
  15.976 +     * @param  millis
  15.977 +     *         the time to wait in milliseconds
  15.978 +     *
  15.979 +     * @throws  IllegalArgumentException
  15.980 +     *          if the value of {@code millis} is negative
  15.981 +     *
  15.982 +     * @throws  InterruptedException
  15.983 +     *          if any thread has interrupted the current thread. The
  15.984 +     *          <i>interrupted status</i> of the current thread is
  15.985 +     *          cleared when this exception is thrown.
  15.986 +     */
  15.987 +    public final synchronized void join(long millis)
  15.988 +    throws InterruptedException {
  15.989 +        long base = System.currentTimeMillis();
  15.990 +        long now = 0;
  15.991 +
  15.992 +        if (millis < 0) {
  15.993 +            throw new IllegalArgumentException("timeout value is negative");
  15.994 +        }
  15.995 +
  15.996 +        if (millis == 0) {
  15.997 +            while (isAlive()) {
  15.998 +                wait(0);
  15.999 +            }
 15.1000 +        } else {
 15.1001 +            while (isAlive()) {
 15.1002 +                long delay = millis - now;
 15.1003 +                if (delay <= 0) {
 15.1004 +                    break;
 15.1005 +                }
 15.1006 +                wait(delay);
 15.1007 +                now = System.currentTimeMillis() - base;
 15.1008 +            }
 15.1009 +        }
 15.1010 +    }
 15.1011 +
 15.1012 +    /**
 15.1013 +     * Waits at most {@code millis} milliseconds plus
 15.1014 +     * {@code nanos} nanoseconds for this thread to die.
 15.1015 +     *
 15.1016 +     * <p> This implementation uses a loop of {@code this.wait} calls
 15.1017 +     * conditioned on {@code this.isAlive}. As a thread terminates the
 15.1018 +     * {@code this.notifyAll} method is invoked. It is recommended that
 15.1019 +     * applications not use {@code wait}, {@code notify}, or
 15.1020 +     * {@code notifyAll} on {@code Thread} instances.
 15.1021 +     *
 15.1022 +     * @param  millis
 15.1023 +     *         the time to wait in milliseconds
 15.1024 +     *
 15.1025 +     * @param  nanos
 15.1026 +     *         {@code 0-999999} additional nanoseconds to wait
 15.1027 +     *
 15.1028 +     * @throws  IllegalArgumentException
 15.1029 +     *          if the value of {@code millis} is negative, or the value
 15.1030 +     *          of {@code nanos} is not in the range {@code 0-999999}
 15.1031 +     *
 15.1032 +     * @throws  InterruptedException
 15.1033 +     *          if any thread has interrupted the current thread. The
 15.1034 +     *          <i>interrupted status</i> of the current thread is
 15.1035 +     *          cleared when this exception is thrown.
 15.1036 +     */
 15.1037 +    public final synchronized void join(long millis, int nanos)
 15.1038 +    throws InterruptedException {
 15.1039 +
 15.1040 +        if (millis < 0) {
 15.1041 +            throw new IllegalArgumentException("timeout value is negative");
 15.1042 +        }
 15.1043 +
 15.1044 +        if (nanos < 0 || nanos > 999999) {
 15.1045 +            throw new IllegalArgumentException(
 15.1046 +                                "nanosecond timeout value out of range");
 15.1047 +        }
 15.1048 +
 15.1049 +        if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
 15.1050 +            millis++;
 15.1051 +        }
 15.1052 +
 15.1053 +        join(millis);
 15.1054 +    }
 15.1055 +
 15.1056 +    /**
 15.1057 +     * Waits for this thread to die.
 15.1058 +     *
 15.1059 +     * <p> An invocation of this method behaves in exactly the same
 15.1060 +     * way as the invocation
 15.1061 +     *
 15.1062 +     * <blockquote>
 15.1063 +     * {@linkplain #join(long) join}{@code (0)}
 15.1064 +     * </blockquote>
 15.1065 +     *
 15.1066 +     * @throws  InterruptedException
 15.1067 +     *          if any thread has interrupted the current thread. The
 15.1068 +     *          <i>interrupted status</i> of the current thread is
 15.1069 +     *          cleared when this exception is thrown.
 15.1070 +     */
 15.1071 +    public final void join() throws InterruptedException {
 15.1072 +        join(0);
 15.1073 +    }
 15.1074 +
 15.1075 +    /**
 15.1076 +     * Prints a stack trace of the current thread to the standard error stream.
 15.1077 +     * This method is used only for debugging.
 15.1078 +     *
 15.1079 +     * @see     Throwable#printStackTrace()
 15.1080 +     */
 15.1081 +    public static void dumpStack() {
 15.1082 +        new Exception("Stack trace").printStackTrace();
 15.1083 +    }
 15.1084 +
 15.1085 +    /**
 15.1086 +     * Marks this thread as either a {@linkplain #isDaemon daemon} thread
 15.1087 +     * or a user thread. The Java Virtual Machine exits when the only
 15.1088 +     * threads running are all daemon threads.
 15.1089 +     *
 15.1090 +     * <p> This method must be invoked before the thread is started.
 15.1091 +     *
 15.1092 +     * @param  on
 15.1093 +     *         if {@code true}, marks this thread as a daemon thread
 15.1094 +     *
 15.1095 +     * @throws  IllegalThreadStateException
 15.1096 +     *          if this thread is {@linkplain #isAlive alive}
 15.1097 +     *
 15.1098 +     * @throws  SecurityException
 15.1099 +     *          if {@link #checkAccess} determines that the current
 15.1100 +     *          thread cannot modify this thread
 15.1101 +     */
 15.1102 +    public final void setDaemon(boolean on) {
 15.1103 +        throw new SecurityException();
 15.1104 +    }
 15.1105 +
 15.1106 +    /**
 15.1107 +     * Tests if this thread is a daemon thread.
 15.1108 +     *
 15.1109 +     * @return  <code>true</code> if this thread is a daemon thread;
 15.1110 +     *          <code>false</code> otherwise.
 15.1111 +     * @see     #setDaemon(boolean)
 15.1112 +     */
 15.1113 +    public final boolean isDaemon() {
 15.1114 +        return false;
 15.1115 +    }
 15.1116 +
 15.1117 +    /**
 15.1118 +     * Determines if the currently running thread has permission to
 15.1119 +     * modify this thread.
 15.1120 +     * <p>
 15.1121 +     * If there is a security manager, its <code>checkAccess</code> method
 15.1122 +     * is called with this thread as its argument. This may result in
 15.1123 +     * throwing a <code>SecurityException</code>.
 15.1124 +     *
 15.1125 +     * @exception  SecurityException  if the current thread is not allowed to
 15.1126 +     *               access this thread.
 15.1127 +     * @see        SecurityManager#checkAccess(Thread)
 15.1128 +     */
 15.1129 +    public final void checkAccess() {
 15.1130 +        throw new SecurityException();
 15.1131 +    }
 15.1132 +
 15.1133 +    /**
 15.1134 +     * Returns a string representation of this thread, including the
 15.1135 +     * thread's name, priority, and thread group.
 15.1136 +     *
 15.1137 +     * @return  a string representation of this thread.
 15.1138 +     */
 15.1139 +    public String toString() {
 15.1140 +        return "Thread[" + getName() + "," + getPriority() + "," +
 15.1141 +                        "" + "]";
 15.1142 +    }
 15.1143 +
 15.1144 +    /**
 15.1145 +     * Returns the context ClassLoader for this Thread. The context
 15.1146 +     * ClassLoader is provided by the creator of the thread for use
 15.1147 +     * by code running in this thread when loading classes and resources.
 15.1148 +     * If not {@linkplain #setContextClassLoader set}, the default is the
 15.1149 +     * ClassLoader context of the parent Thread. The context ClassLoader of the
 15.1150 +     * primordial thread is typically set to the class loader used to load the
 15.1151 +     * application.
 15.1152 +     *
 15.1153 +     * <p>If a security manager is present, and the invoker's class loader is not
 15.1154 +     * {@code null} and is not the same as or an ancestor of the context class
 15.1155 +     * loader, then this method invokes the security manager's {@link
 15.1156 +     * SecurityManager#checkPermission(java.security.Permission) checkPermission}
 15.1157 +     * method with a {@link RuntimePermission RuntimePermission}{@code
 15.1158 +     * ("getClassLoader")} permission to verify that retrieval of the context
 15.1159 +     * class loader is permitted.
 15.1160 +     *
 15.1161 +     * @return  the context ClassLoader for this Thread, or {@code null}
 15.1162 +     *          indicating the system class loader (or, failing that, the
 15.1163 +     *          bootstrap class loader)
 15.1164 +     *
 15.1165 +     * @throws  SecurityException
 15.1166 +     *          if the current thread cannot get the context ClassLoader
 15.1167 +     *
 15.1168 +     * @since 1.2
 15.1169 +     */
 15.1170 +    public ClassLoader getContextClassLoader() {
 15.1171 +        return null;
 15.1172 +    }
 15.1173 +
 15.1174 +    /**
 15.1175 +     * Sets the context ClassLoader for this Thread. The context
 15.1176 +     * ClassLoader can be set when a thread is created, and allows
 15.1177 +     * the creator of the thread to provide the appropriate class loader,
 15.1178 +     * through {@code getContextClassLoader}, to code running in the thread
 15.1179 +     * when loading classes and resources.
 15.1180 +     *
 15.1181 +     * <p>If a security manager is present, its {@link
 15.1182 +     * SecurityManager#checkPermission(java.security.Permission) checkPermission}
 15.1183 +     * method is invoked with a {@link RuntimePermission RuntimePermission}{@code
 15.1184 +     * ("setContextClassLoader")} permission to see if setting the context
 15.1185 +     * ClassLoader is permitted.
 15.1186 +     *
 15.1187 +     * @param  cl
 15.1188 +     *         the context ClassLoader for this Thread, or null  indicating the
 15.1189 +     *         system class loader (or, failing that, the bootstrap class loader)
 15.1190 +     *
 15.1191 +     * @throws  SecurityException
 15.1192 +     *          if the current thread cannot set the context ClassLoader
 15.1193 +     *
 15.1194 +     * @since 1.2
 15.1195 +     */
 15.1196 +    public void setContextClassLoader(ClassLoader cl) {
 15.1197 +        throw new SecurityException();
 15.1198 +    }
 15.1199 +
 15.1200 +    /**
 15.1201 +     * Returns <tt>true</tt> if and only if the current thread holds the
 15.1202 +     * monitor lock on the specified object.
 15.1203 +     *
 15.1204 +     * <p>This method is designed to allow a program to assert that
 15.1205 +     * the current thread already holds a specified lock:
 15.1206 +     * <pre>
 15.1207 +     *     assert Thread.holdsLock(obj);
 15.1208 +     * </pre>
 15.1209 +     *
 15.1210 +     * @param  obj the object on which to test lock ownership
 15.1211 +     * @throws NullPointerException if obj is <tt>null</tt>
 15.1212 +     * @return <tt>true</tt> if the current thread holds the monitor lock on
 15.1213 +     *         the specified object.
 15.1214 +     * @since 1.4
 15.1215 +     */
 15.1216 +    public static boolean holdsLock(Object obj) {
 15.1217 +        return true;
 15.1218 +    }
 15.1219 +
 15.1220 +    /**
 15.1221 +     * Returns an array of stack trace elements representing the stack dump
 15.1222 +     * of this thread.  This method will return a zero-length array if
 15.1223 +     * this thread has not started, has started but has not yet been
 15.1224 +     * scheduled to run by the system, or has terminated.
 15.1225 +     * If the returned array is of non-zero length then the first element of
 15.1226 +     * the array represents the top of the stack, which is the most recent
 15.1227 +     * method invocation in the sequence.  The last element of the array
 15.1228 +     * represents the bottom of the stack, which is the least recent method
 15.1229 +     * invocation in the sequence.
 15.1230 +     *
 15.1231 +     * <p>If there is a security manager, and this thread is not
 15.1232 +     * the current thread, then the security manager's
 15.1233 +     * <tt>checkPermission</tt> method is called with a
 15.1234 +     * <tt>RuntimePermission("getStackTrace")</tt> permission
 15.1235 +     * to see if it's ok to get the stack trace.
 15.1236 +     *
 15.1237 +     * <p>Some virtual machines may, under some circumstances, omit one
 15.1238 +     * or more stack frames from the stack trace.  In the extreme case,
 15.1239 +     * a virtual machine that has no stack trace information concerning
 15.1240 +     * this thread is permitted to return a zero-length array from this
 15.1241 +     * method.
 15.1242 +     *
 15.1243 +     * @return an array of <tt>StackTraceElement</tt>,
 15.1244 +     * each represents one stack frame.
 15.1245 +     *
 15.1246 +     * @throws SecurityException
 15.1247 +     *        if a security manager exists and its
 15.1248 +     *        <tt>checkPermission</tt> method doesn't allow
 15.1249 +     *        getting the stack trace of thread.
 15.1250 +     * @see SecurityManager#checkPermission
 15.1251 +     * @see RuntimePermission
 15.1252 +     * @see Throwable#getStackTrace
 15.1253 +     *
 15.1254 +     * @since 1.5
 15.1255 +     */
 15.1256 +    public StackTraceElement[] getStackTrace() {
 15.1257 +        throw new SecurityException();
 15.1258 +    }
 15.1259 +
 15.1260 +    /**
 15.1261 +     * Returns a map of stack traces for all live threads.
 15.1262 +     * The map keys are threads and each map value is an array of
 15.1263 +     * <tt>StackTraceElement</tt> that represents the stack dump
 15.1264 +     * of the corresponding <tt>Thread</tt>.
 15.1265 +     * The returned stack traces are in the format specified for
 15.1266 +     * the {@link #getStackTrace getStackTrace} method.
 15.1267 +     *
 15.1268 +     * <p>The threads may be executing while this method is called.
 15.1269 +     * The stack trace of each thread only represents a snapshot and
 15.1270 +     * each stack trace may be obtained at different time.  A zero-length
 15.1271 +     * array will be returned in the map value if the virtual machine has
 15.1272 +     * no stack trace information about a thread.
 15.1273 +     *
 15.1274 +     * <p>If there is a security manager, then the security manager's
 15.1275 +     * <tt>checkPermission</tt> method is called with a
 15.1276 +     * <tt>RuntimePermission("getStackTrace")</tt> permission as well as
 15.1277 +     * <tt>RuntimePermission("modifyThreadGroup")</tt> permission
 15.1278 +     * to see if it is ok to get the stack trace of all threads.
 15.1279 +     *
 15.1280 +     * @return a <tt>Map</tt> from <tt>Thread</tt> to an array of
 15.1281 +     * <tt>StackTraceElement</tt> that represents the stack trace of
 15.1282 +     * the corresponding thread.
 15.1283 +     *
 15.1284 +     * @throws SecurityException
 15.1285 +     *        if a security manager exists and its
 15.1286 +     *        <tt>checkPermission</tt> method doesn't allow
 15.1287 +     *        getting the stack trace of thread.
 15.1288 +     * @see #getStackTrace
 15.1289 +     * @see SecurityManager#checkPermission
 15.1290 +     * @see RuntimePermission
 15.1291 +     * @see Throwable#getStackTrace
 15.1292 +     *
 15.1293 +     * @since 1.5
 15.1294 +     */
 15.1295 +    public static Map<Thread, StackTraceElement[]> getAllStackTraces() {
 15.1296 +        throw new SecurityException();
 15.1297 +    }
 15.1298 +
 15.1299 +    /**
 15.1300 +     * Returns the identifier of this Thread.  The thread ID is a positive
 15.1301 +     * <tt>long</tt> number generated when this thread was created.
 15.1302 +     * The thread ID is unique and remains unchanged during its lifetime.
 15.1303 +     * When a thread is terminated, this thread ID may be reused.
 15.1304 +     *
 15.1305 +     * @return this thread's ID.
 15.1306 +     * @since 1.5
 15.1307 +     */
 15.1308 +    public long getId() {
 15.1309 +        return 0;
 15.1310 +    }
 15.1311 +
 15.1312 +    /**
 15.1313 +     * A thread state.  A thread can be in one of the following states:
 15.1314 +     * <ul>
 15.1315 +     * <li>{@link #NEW}<br>
 15.1316 +     *     A thread that has not yet started is in this state.
 15.1317 +     *     </li>
 15.1318 +     * <li>{@link #RUNNABLE}<br>
 15.1319 +     *     A thread executing in the Java virtual machine is in this state.
 15.1320 +     *     </li>
 15.1321 +     * <li>{@link #BLOCKED}<br>
 15.1322 +     *     A thread that is blocked waiting for a monitor lock
 15.1323 +     *     is in this state.
 15.1324 +     *     </li>
 15.1325 +     * <li>{@link #WAITING}<br>
 15.1326 +     *     A thread that is waiting indefinitely for another thread to
 15.1327 +     *     perform a particular action is in this state.
 15.1328 +     *     </li>
 15.1329 +     * <li>{@link #TIMED_WAITING}<br>
 15.1330 +     *     A thread that is waiting for another thread to perform an action
 15.1331 +     *     for up to a specified waiting time is in this state.
 15.1332 +     *     </li>
 15.1333 +     * <li>{@link #TERMINATED}<br>
 15.1334 +     *     A thread that has exited is in this state.
 15.1335 +     *     </li>
 15.1336 +     * </ul>
 15.1337 +     *
 15.1338 +     * <p>
 15.1339 +     * A thread can be in only one state at a given point in time.
 15.1340 +     * These states are virtual machine states which do not reflect
 15.1341 +     * any operating system thread states.
 15.1342 +     *
 15.1343 +     * @since   1.5
 15.1344 +     * @see #getState
 15.1345 +     */
 15.1346 +    public enum State {
 15.1347 +        /**
 15.1348 +         * Thread state for a thread which has not yet started.
 15.1349 +         */
 15.1350 +        NEW,
 15.1351 +
 15.1352 +        /**
 15.1353 +         * Thread state for a runnable thread.  A thread in the runnable
 15.1354 +         * state is executing in the Java virtual machine but it may
 15.1355 +         * be waiting for other resources from the operating system
 15.1356 +         * such as processor.
 15.1357 +         */
 15.1358 +        RUNNABLE,
 15.1359 +
 15.1360 +        /**
 15.1361 +         * Thread state for a thread blocked waiting for a monitor lock.
 15.1362 +         * A thread in the blocked state is waiting for a monitor lock
 15.1363 +         * to enter a synchronized block/method or
 15.1364 +         * reenter a synchronized block/method after calling
 15.1365 +         * {@link Object#wait() Object.wait}.
 15.1366 +         */
 15.1367 +        BLOCKED,
 15.1368 +
 15.1369 +        /**
 15.1370 +         * Thread state for a waiting thread.
 15.1371 +         * A thread is in the waiting state due to calling one of the
 15.1372 +         * following methods:
 15.1373 +         * <ul>
 15.1374 +         *   <li>{@link Object#wait() Object.wait} with no timeout</li>
 15.1375 +         *   <li>{@link #join() Thread.join} with no timeout</li>
 15.1376 +         *   <li>{@link LockSupport#park() LockSupport.park}</li>
 15.1377 +         * </ul>
 15.1378 +         *
 15.1379 +         * <p>A thread in the waiting state is waiting for another thread to
 15.1380 +         * perform a particular action.
 15.1381 +         *
 15.1382 +         * For example, a thread that has called <tt>Object.wait()</tt>
 15.1383 +         * on an object is waiting for another thread to call
 15.1384 +         * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
 15.1385 +         * that object. A thread that has called <tt>Thread.join()</tt>
 15.1386 +         * is waiting for a specified thread to terminate.
 15.1387 +         */
 15.1388 +        WAITING,
 15.1389 +
 15.1390 +        /**
 15.1391 +         * Thread state for a waiting thread with a specified waiting time.
 15.1392 +         * A thread is in the timed waiting state due to calling one of
 15.1393 +         * the following methods with a specified positive waiting time:
 15.1394 +         * <ul>
 15.1395 +         *   <li>{@link #sleep Thread.sleep}</li>
 15.1396 +         *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
 15.1397 +         *   <li>{@link #join(long) Thread.join} with timeout</li>
 15.1398 +         *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
 15.1399 +         *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
 15.1400 +         * </ul>
 15.1401 +         */
 15.1402 +        TIMED_WAITING,
 15.1403 +
 15.1404 +        /**
 15.1405 +         * Thread state for a terminated thread.
 15.1406 +         * The thread has completed execution.
 15.1407 +         */
 15.1408 +        TERMINATED;
 15.1409 +    }
 15.1410 +
 15.1411 +    /**
 15.1412 +     * Returns the state of this thread.
 15.1413 +     * This method is designed for use in monitoring of the system state,
 15.1414 +     * not for synchronization control.
 15.1415 +     *
 15.1416 +     * @return this thread's state.
 15.1417 +     * @since 1.5
 15.1418 +     */
 15.1419 +    public State getState() {
 15.1420 +        // get current thread state
 15.1421 +        return State.RUNNABLE;
 15.1422 +    }
 15.1423 +
 15.1424 +    // Added in JSR-166
 15.1425 +
 15.1426 +    /**
 15.1427 +     * Interface for handlers invoked when a <tt>Thread</tt> abruptly
 15.1428 +     * terminates due to an uncaught exception.
 15.1429 +     * <p>When a thread is about to terminate due to an uncaught exception
 15.1430 +     * the Java Virtual Machine will query the thread for its
 15.1431 +     * <tt>UncaughtExceptionHandler</tt> using
 15.1432 +     * {@link #getUncaughtExceptionHandler} and will invoke the handler's
 15.1433 +     * <tt>uncaughtException</tt> method, passing the thread and the
 15.1434 +     * exception as arguments.
 15.1435 +     * If a thread has not had its <tt>UncaughtExceptionHandler</tt>
 15.1436 +     * explicitly set, then its <tt>ThreadGroup</tt> object acts as its
 15.1437 +     * <tt>UncaughtExceptionHandler</tt>. If the <tt>ThreadGroup</tt> object
 15.1438 +     * has no
 15.1439 +     * special requirements for dealing with the exception, it can forward
 15.1440 +     * the invocation to the {@linkplain #getDefaultUncaughtExceptionHandler
 15.1441 +     * default uncaught exception handler}.
 15.1442 +     *
 15.1443 +     * @see #setDefaultUncaughtExceptionHandler
 15.1444 +     * @see #setUncaughtExceptionHandler
 15.1445 +     * @see ThreadGroup#uncaughtException
 15.1446 +     * @since 1.5
 15.1447 +     */
 15.1448 +    public interface UncaughtExceptionHandler {
 15.1449 +        /**
 15.1450 +         * Method invoked when the given thread terminates due to the
 15.1451 +         * given uncaught exception.
 15.1452 +         * <p>Any exception thrown by this method will be ignored by the
 15.1453 +         * Java Virtual Machine.
 15.1454 +         * @param t the thread
 15.1455 +         * @param e the exception
 15.1456 +         */
 15.1457 +        void uncaughtException(Thread t, Throwable e);
 15.1458 +    }
 15.1459 +
 15.1460 +    // null unless explicitly set
 15.1461 +    private volatile UncaughtExceptionHandler uncaughtExceptionHandler;
 15.1462 +
 15.1463 +    // null unless explicitly set
 15.1464 +    private static volatile UncaughtExceptionHandler defaultUncaughtExceptionHandler;
 15.1465 +
 15.1466 +    /**
 15.1467 +     * Set the default handler invoked when a thread abruptly terminates
 15.1468 +     * due to an uncaught exception, and no other handler has been defined
 15.1469 +     * for that thread.
 15.1470 +     *
 15.1471 +     * <p>Uncaught exception handling is controlled first by the thread, then
 15.1472 +     * by the thread's {@link ThreadGroup} object and finally by the default
 15.1473 +     * uncaught exception handler. If the thread does not have an explicit
 15.1474 +     * uncaught exception handler set, and the thread's thread group
 15.1475 +     * (including parent thread groups)  does not specialize its
 15.1476 +     * <tt>uncaughtException</tt> method, then the default handler's
 15.1477 +     * <tt>uncaughtException</tt> method will be invoked.
 15.1478 +     * <p>By setting the default uncaught exception handler, an application
 15.1479 +     * can change the way in which uncaught exceptions are handled (such as
 15.1480 +     * logging to a specific device, or file) for those threads that would
 15.1481 +     * already accept whatever &quot;default&quot; behavior the system
 15.1482 +     * provided.
 15.1483 +     *
 15.1484 +     * <p>Note that the default uncaught exception handler should not usually
 15.1485 +     * defer to the thread's <tt>ThreadGroup</tt> object, as that could cause
 15.1486 +     * infinite recursion.
 15.1487 +     *
 15.1488 +     * @param eh the object to use as the default uncaught exception handler.
 15.1489 +     * If <tt>null</tt> then there is no default handler.
 15.1490 +     *
 15.1491 +     * @throws SecurityException if a security manager is present and it
 15.1492 +     *         denies <tt>{@link RuntimePermission}
 15.1493 +     *         (&quot;setDefaultUncaughtExceptionHandler&quot;)</tt>
 15.1494 +     *
 15.1495 +     * @see #setUncaughtExceptionHandler
 15.1496 +     * @see #getUncaughtExceptionHandler
 15.1497 +     * @see ThreadGroup#uncaughtException
 15.1498 +     * @since 1.5
 15.1499 +     */
 15.1500 +    public static void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh) {
 15.1501 +        throw new SecurityException();
 15.1502 +    }
 15.1503 +
 15.1504 +    /**
 15.1505 +     * Returns the default handler invoked when a thread abruptly terminates
 15.1506 +     * due to an uncaught exception. If the returned value is <tt>null</tt>,
 15.1507 +     * there is no default.
 15.1508 +     * @since 1.5
 15.1509 +     * @see #setDefaultUncaughtExceptionHandler
 15.1510 +     */
 15.1511 +    public static UncaughtExceptionHandler getDefaultUncaughtExceptionHandler(){
 15.1512 +        return defaultUncaughtExceptionHandler;
 15.1513 +    }
 15.1514 +
 15.1515 +    /**
 15.1516 +     * Returns the handler invoked when this thread abruptly terminates
 15.1517 +     * due to an uncaught exception. If this thread has not had an
 15.1518 +     * uncaught exception handler explicitly set then this thread's
 15.1519 +     * <tt>ThreadGroup</tt> object is returned, unless this thread
 15.1520 +     * has terminated, in which case <tt>null</tt> is returned.
 15.1521 +     * @since 1.5
 15.1522 +     */
 15.1523 +    public UncaughtExceptionHandler getUncaughtExceptionHandler() {
 15.1524 +        return uncaughtExceptionHandler != null ?
 15.1525 +            uncaughtExceptionHandler : null;
 15.1526 +    }
 15.1527 +
 15.1528 +    /**
 15.1529 +     * Set the handler invoked when this thread abruptly terminates
 15.1530 +     * due to an uncaught exception.
 15.1531 +     * <p>A thread can take full control of how it responds to uncaught
 15.1532 +     * exceptions by having its uncaught exception handler explicitly set.
 15.1533 +     * If no such handler is set then the thread's <tt>ThreadGroup</tt>
 15.1534 +     * object acts as its handler.
 15.1535 +     * @param eh the object to use as this thread's uncaught exception
 15.1536 +     * handler. If <tt>null</tt> then this thread has no explicit handler.
 15.1537 +     * @throws  SecurityException  if the current thread is not allowed to
 15.1538 +     *          modify this thread.
 15.1539 +     * @see #setDefaultUncaughtExceptionHandler
 15.1540 +     * @see ThreadGroup#uncaughtException
 15.1541 +     * @since 1.5
 15.1542 +     */
 15.1543 +    public void setUncaughtExceptionHandler(UncaughtExceptionHandler eh) {
 15.1544 +        checkAccess();
 15.1545 +        uncaughtExceptionHandler = eh;
 15.1546 +    }
 15.1547 +}
    16.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    16.2 +++ b/rt/emul/compact/src/main/java/java/math/BigDecimal.java	Sun Sep 08 11:22:51 2013 +0200
    16.3 @@ -0,0 +1,3842 @@
    16.4 +/*
    16.5 + * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
    16.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    16.7 + *
    16.8 + * This code is free software; you can redistribute it and/or modify it
    16.9 + * under the terms of the GNU General Public License version 2 only, as
   16.10 + * published by the Free Software Foundation.  Oracle designates this
   16.11 + * particular file as subject to the "Classpath" exception as provided
   16.12 + * by Oracle in the LICENSE file that accompanied this code.
   16.13 + *
   16.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   16.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   16.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   16.17 + * version 2 for more details (a copy is included in the LICENSE file that
   16.18 + * accompanied this code).
   16.19 + *
   16.20 + * You should have received a copy of the GNU General Public License version
   16.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   16.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   16.23 + *
   16.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   16.25 + * or visit www.oracle.com if you need additional information or have any
   16.26 + * questions.
   16.27 + */
   16.28 +
   16.29 +/*
   16.30 + * Portions Copyright IBM Corporation, 2001. All Rights Reserved.
   16.31 + */
   16.32 +
   16.33 +package java.math;
   16.34 +
   16.35 +import java.util.Arrays;
   16.36 +import static java.math.BigInteger.LONG_MASK;
   16.37 +
   16.38 +/**
   16.39 + * Immutable, arbitrary-precision signed decimal numbers.  A
   16.40 + * {@code BigDecimal} consists of an arbitrary precision integer
   16.41 + * <i>unscaled value</i> and a 32-bit integer <i>scale</i>.  If zero
   16.42 + * or positive, the scale is the number of digits to the right of the
   16.43 + * decimal point.  If negative, the unscaled value of the number is
   16.44 + * multiplied by ten to the power of the negation of the scale.  The
   16.45 + * value of the number represented by the {@code BigDecimal} is
   16.46 + * therefore <tt>(unscaledValue &times; 10<sup>-scale</sup>)</tt>.
   16.47 + *
   16.48 + * <p>The {@code BigDecimal} class provides operations for
   16.49 + * arithmetic, scale manipulation, rounding, comparison, hashing, and
   16.50 + * format conversion.  The {@link #toString} method provides a
   16.51 + * canonical representation of a {@code BigDecimal}.
   16.52 + *
   16.53 + * <p>The {@code BigDecimal} class gives its user complete control
   16.54 + * over rounding behavior.  If no rounding mode is specified and the
   16.55 + * exact result cannot be represented, an exception is thrown;
   16.56 + * otherwise, calculations can be carried out to a chosen precision
   16.57 + * and rounding mode by supplying an appropriate {@link MathContext}
   16.58 + * object to the operation.  In either case, eight <em>rounding
   16.59 + * modes</em> are provided for the control of rounding.  Using the
   16.60 + * integer fields in this class (such as {@link #ROUND_HALF_UP}) to
   16.61 + * represent rounding mode is largely obsolete; the enumeration values
   16.62 + * of the {@code RoundingMode} {@code enum}, (such as {@link
   16.63 + * RoundingMode#HALF_UP}) should be used instead.
   16.64 + *
   16.65 + * <p>When a {@code MathContext} object is supplied with a precision
   16.66 + * setting of 0 (for example, {@link MathContext#UNLIMITED}),
   16.67 + * arithmetic operations are exact, as are the arithmetic methods
   16.68 + * which take no {@code MathContext} object.  (This is the only
   16.69 + * behavior that was supported in releases prior to 5.)  As a
   16.70 + * corollary of computing the exact result, the rounding mode setting
   16.71 + * of a {@code MathContext} object with a precision setting of 0 is
   16.72 + * not used and thus irrelevant.  In the case of divide, the exact
   16.73 + * quotient could have an infinitely long decimal expansion; for
   16.74 + * example, 1 divided by 3.  If the quotient has a nonterminating
   16.75 + * decimal expansion and the operation is specified to return an exact
   16.76 + * result, an {@code ArithmeticException} is thrown.  Otherwise, the
   16.77 + * exact result of the division is returned, as done for other
   16.78 + * operations.
   16.79 + *
   16.80 + * <p>When the precision setting is not 0, the rules of
   16.81 + * {@code BigDecimal} arithmetic are broadly compatible with selected
   16.82 + * modes of operation of the arithmetic defined in ANSI X3.274-1996
   16.83 + * and ANSI X3.274-1996/AM 1-2000 (section 7.4).  Unlike those
   16.84 + * standards, {@code BigDecimal} includes many rounding modes, which
   16.85 + * were mandatory for division in {@code BigDecimal} releases prior
   16.86 + * to 5.  Any conflicts between these ANSI standards and the
   16.87 + * {@code BigDecimal} specification are resolved in favor of
   16.88 + * {@code BigDecimal}.
   16.89 + *
   16.90 + * <p>Since the same numerical value can have different
   16.91 + * representations (with different scales), the rules of arithmetic
   16.92 + * and rounding must specify both the numerical result and the scale
   16.93 + * used in the result's representation.
   16.94 + *
   16.95 + *
   16.96 + * <p>In general the rounding modes and precision setting determine
   16.97 + * how operations return results with a limited number of digits when
   16.98 + * the exact result has more digits (perhaps infinitely many in the
   16.99 + * case of division) than the number of digits returned.
  16.100 + *
  16.101 + * First, the
  16.102 + * total number of digits to return is specified by the
  16.103 + * {@code MathContext}'s {@code precision} setting; this determines
  16.104 + * the result's <i>precision</i>.  The digit count starts from the
  16.105 + * leftmost nonzero digit of the exact result.  The rounding mode
  16.106 + * determines how any discarded trailing digits affect the returned
  16.107 + * result.
  16.108 + *
  16.109 + * <p>For all arithmetic operators , the operation is carried out as
  16.110 + * though an exact intermediate result were first calculated and then
  16.111 + * rounded to the number of digits specified by the precision setting
  16.112 + * (if necessary), using the selected rounding mode.  If the exact
  16.113 + * result is not returned, some digit positions of the exact result
  16.114 + * are discarded.  When rounding increases the magnitude of the
  16.115 + * returned result, it is possible for a new digit position to be
  16.116 + * created by a carry propagating to a leading {@literal "9"} digit.
  16.117 + * For example, rounding the value 999.9 to three digits rounding up
  16.118 + * would be numerically equal to one thousand, represented as
  16.119 + * 100&times;10<sup>1</sup>.  In such cases, the new {@literal "1"} is
  16.120 + * the leading digit position of the returned result.
  16.121 + *
  16.122 + * <p>Besides a logical exact result, each arithmetic operation has a
  16.123 + * preferred scale for representing a result.  The preferred
  16.124 + * scale for each operation is listed in the table below.
  16.125 + *
  16.126 + * <table border>
  16.127 + * <caption><b>Preferred Scales for Results of Arithmetic Operations
  16.128 + * </b></caption>
  16.129 + * <tr><th>Operation</th><th>Preferred Scale of Result</th></tr>
  16.130 + * <tr><td>Add</td><td>max(addend.scale(), augend.scale())</td>
  16.131 + * <tr><td>Subtract</td><td>max(minuend.scale(), subtrahend.scale())</td>
  16.132 + * <tr><td>Multiply</td><td>multiplier.scale() + multiplicand.scale()</td>
  16.133 + * <tr><td>Divide</td><td>dividend.scale() - divisor.scale()</td>
  16.134 + * </table>
  16.135 + *
  16.136 + * These scales are the ones used by the methods which return exact
  16.137 + * arithmetic results; except that an exact divide may have to use a
  16.138 + * larger scale since the exact result may have more digits.  For
  16.139 + * example, {@code 1/32} is {@code 0.03125}.
  16.140 + *
  16.141 + * <p>Before rounding, the scale of the logical exact intermediate
  16.142 + * result is the preferred scale for that operation.  If the exact
  16.143 + * numerical result cannot be represented in {@code precision}
  16.144 + * digits, rounding selects the set of digits to return and the scale
  16.145 + * of the result is reduced from the scale of the intermediate result
  16.146 + * to the least scale which can represent the {@code precision}
  16.147 + * digits actually returned.  If the exact result can be represented
  16.148 + * with at most {@code precision} digits, the representation
  16.149 + * of the result with the scale closest to the preferred scale is
  16.150 + * returned.  In particular, an exactly representable quotient may be
  16.151 + * represented in fewer than {@code precision} digits by removing
  16.152 + * trailing zeros and decreasing the scale.  For example, rounding to
  16.153 + * three digits using the {@linkplain RoundingMode#FLOOR floor}
  16.154 + * rounding mode, <br>
  16.155 + *
  16.156 + * {@code 19/100 = 0.19   // integer=19,  scale=2} <br>
  16.157 + *
  16.158 + * but<br>
  16.159 + *
  16.160 + * {@code 21/110 = 0.190  // integer=190, scale=3} <br>
  16.161 + *
  16.162 + * <p>Note that for add, subtract, and multiply, the reduction in
  16.163 + * scale will equal the number of digit positions of the exact result
  16.164 + * which are discarded. If the rounding causes a carry propagation to
  16.165 + * create a new high-order digit position, an additional digit of the
  16.166 + * result is discarded than when no new digit position is created.
  16.167 + *
  16.168 + * <p>Other methods may have slightly different rounding semantics.
  16.169 + * For example, the result of the {@code pow} method using the
  16.170 + * {@linkplain #pow(int, MathContext) specified algorithm} can
  16.171 + * occasionally differ from the rounded mathematical result by more
  16.172 + * than one unit in the last place, one <i>{@linkplain #ulp() ulp}</i>.
  16.173 + *
  16.174 + * <p>Two types of operations are provided for manipulating the scale
  16.175 + * of a {@code BigDecimal}: scaling/rounding operations and decimal
  16.176 + * point motion operations.  Scaling/rounding operations ({@link
  16.177 + * #setScale setScale} and {@link #round round}) return a
  16.178 + * {@code BigDecimal} whose value is approximately (or exactly) equal
  16.179 + * to that of the operand, but whose scale or precision is the
  16.180 + * specified value; that is, they increase or decrease the precision
  16.181 + * of the stored number with minimal effect on its value.  Decimal
  16.182 + * point motion operations ({@link #movePointLeft movePointLeft} and
  16.183 + * {@link #movePointRight movePointRight}) return a
  16.184 + * {@code BigDecimal} created from the operand by moving the decimal
  16.185 + * point a specified distance in the specified direction.
  16.186 + *
  16.187 + * <p>For the sake of brevity and clarity, pseudo-code is used
  16.188 + * throughout the descriptions of {@code BigDecimal} methods.  The
  16.189 + * pseudo-code expression {@code (i + j)} is shorthand for "a
  16.190 + * {@code BigDecimal} whose value is that of the {@code BigDecimal}
  16.191 + * {@code i} added to that of the {@code BigDecimal}
  16.192 + * {@code j}." The pseudo-code expression {@code (i == j)} is
  16.193 + * shorthand for "{@code true} if and only if the
  16.194 + * {@code BigDecimal} {@code i} represents the same value as the
  16.195 + * {@code BigDecimal} {@code j}." Other pseudo-code expressions
  16.196 + * are interpreted similarly.  Square brackets are used to represent
  16.197 + * the particular {@code BigInteger} and scale pair defining a
  16.198 + * {@code BigDecimal} value; for example [19, 2] is the
  16.199 + * {@code BigDecimal} numerically equal to 0.19 having a scale of 2.
  16.200 + *
  16.201 + * <p>Note: care should be exercised if {@code BigDecimal} objects
  16.202 + * are used as keys in a {@link java.util.SortedMap SortedMap} or
  16.203 + * elements in a {@link java.util.SortedSet SortedSet} since
  16.204 + * {@code BigDecimal}'s <i>natural ordering</i> is <i>inconsistent
  16.205 + * with equals</i>.  See {@link Comparable}, {@link
  16.206 + * java.util.SortedMap} or {@link java.util.SortedSet} for more
  16.207 + * information.
  16.208 + *
  16.209 + * <p>All methods and constructors for this class throw
  16.210 + * {@code NullPointerException} when passed a {@code null} object
  16.211 + * reference for any input parameter.
  16.212 + *
  16.213 + * @see     BigInteger
  16.214 + * @see     MathContext
  16.215 + * @see     RoundingMode
  16.216 + * @see     java.util.SortedMap
  16.217 + * @see     java.util.SortedSet
  16.218 + * @author  Josh Bloch
  16.219 + * @author  Mike Cowlishaw
  16.220 + * @author  Joseph D. Darcy
  16.221 + */
  16.222 +public class BigDecimal extends Number implements Comparable<BigDecimal> {
  16.223 +    /**
  16.224 +     * The unscaled value of this BigDecimal, as returned by {@link
  16.225 +     * #unscaledValue}.
  16.226 +     *
  16.227 +     * @serial
  16.228 +     * @see #unscaledValue
  16.229 +     */
  16.230 +    private volatile BigInteger intVal;
  16.231 +
  16.232 +    /**
  16.233 +     * The scale of this BigDecimal, as returned by {@link #scale}.
  16.234 +     *
  16.235 +     * @serial
  16.236 +     * @see #scale
  16.237 +     */
  16.238 +    private int scale;  // Note: this may have any value, so
  16.239 +                        // calculations must be done in longs
  16.240 +    /**
  16.241 +     * The number of decimal digits in this BigDecimal, or 0 if the
  16.242 +     * number of digits are not known (lookaside information).  If
  16.243 +     * nonzero, the value is guaranteed correct.  Use the precision()
  16.244 +     * method to obtain and set the value if it might be 0.  This
  16.245 +     * field is mutable until set nonzero.
  16.246 +     *
  16.247 +     * @since  1.5
  16.248 +     */
  16.249 +    private transient int precision;
  16.250 +
  16.251 +    /**
  16.252 +     * Used to store the canonical string representation, if computed.
  16.253 +     */
  16.254 +    private transient String stringCache;
  16.255 +
  16.256 +    /**
  16.257 +     * Sentinel value for {@link #intCompact} indicating the
  16.258 +     * significand information is only available from {@code intVal}.
  16.259 +     */
  16.260 +    static final long INFLATED = Long.MIN_VALUE;
  16.261 +
  16.262 +    /**
  16.263 +     * If the absolute value of the significand of this BigDecimal is
  16.264 +     * less than or equal to {@code Long.MAX_VALUE}, the value can be
  16.265 +     * compactly stored in this field and used in computations.
  16.266 +     */
  16.267 +    private transient long intCompact;
  16.268 +
  16.269 +    // All 18-digit base ten strings fit into a long; not all 19-digit
  16.270 +    // strings will
  16.271 +    private static final int MAX_COMPACT_DIGITS = 18;
  16.272 +
  16.273 +    private static final int MAX_BIGINT_BITS = 62;
  16.274 +
  16.275 +    /* Appease the serialization gods */
  16.276 +    private static final long serialVersionUID = 6108874887143696463L;
  16.277 +
  16.278 +    // Cache of common small BigDecimal values.
  16.279 +    private static final BigDecimal zeroThroughTen[] = {
  16.280 +        new BigDecimal(BigInteger.ZERO,         0,  0, 1),
  16.281 +        new BigDecimal(BigInteger.ONE,          1,  0, 1),
  16.282 +        new BigDecimal(BigInteger.valueOf(2),   2,  0, 1),
  16.283 +        new BigDecimal(BigInteger.valueOf(3),   3,  0, 1),
  16.284 +        new BigDecimal(BigInteger.valueOf(4),   4,  0, 1),
  16.285 +        new BigDecimal(BigInteger.valueOf(5),   5,  0, 1),
  16.286 +        new BigDecimal(BigInteger.valueOf(6),   6,  0, 1),
  16.287 +        new BigDecimal(BigInteger.valueOf(7),   7,  0, 1),
  16.288 +        new BigDecimal(BigInteger.valueOf(8),   8,  0, 1),
  16.289 +        new BigDecimal(BigInteger.valueOf(9),   9,  0, 1),
  16.290 +        new BigDecimal(BigInteger.TEN,          10, 0, 2),
  16.291 +    };
  16.292 +
  16.293 +    // Cache of zero scaled by 0 - 15
  16.294 +    private static final BigDecimal[] ZERO_SCALED_BY = {
  16.295 +        zeroThroughTen[0],
  16.296 +        new BigDecimal(BigInteger.ZERO, 0, 1, 1),
  16.297 +        new BigDecimal(BigInteger.ZERO, 0, 2, 1),
  16.298 +        new BigDecimal(BigInteger.ZERO, 0, 3, 1),
  16.299 +        new BigDecimal(BigInteger.ZERO, 0, 4, 1),
  16.300 +        new BigDecimal(BigInteger.ZERO, 0, 5, 1),
  16.301 +        new BigDecimal(BigInteger.ZERO, 0, 6, 1),
  16.302 +        new BigDecimal(BigInteger.ZERO, 0, 7, 1),
  16.303 +        new BigDecimal(BigInteger.ZERO, 0, 8, 1),
  16.304 +        new BigDecimal(BigInteger.ZERO, 0, 9, 1),
  16.305 +        new BigDecimal(BigInteger.ZERO, 0, 10, 1),
  16.306 +        new BigDecimal(BigInteger.ZERO, 0, 11, 1),
  16.307 +        new BigDecimal(BigInteger.ZERO, 0, 12, 1),
  16.308 +        new BigDecimal(BigInteger.ZERO, 0, 13, 1),
  16.309 +        new BigDecimal(BigInteger.ZERO, 0, 14, 1),
  16.310 +        new BigDecimal(BigInteger.ZERO, 0, 15, 1),
  16.311 +    };
  16.312 +
  16.313 +    // Half of Long.MIN_VALUE & Long.MAX_VALUE.
  16.314 +    private static final long HALF_LONG_MAX_VALUE = Long.MAX_VALUE / 2;
  16.315 +    private static final long HALF_LONG_MIN_VALUE = Long.MIN_VALUE / 2;
  16.316 +
  16.317 +    // Constants
  16.318 +    /**
  16.319 +     * The value 0, with a scale of 0.
  16.320 +     *
  16.321 +     * @since  1.5
  16.322 +     */
  16.323 +    public static final BigDecimal ZERO =
  16.324 +        zeroThroughTen[0];
  16.325 +
  16.326 +    /**
  16.327 +     * The value 1, with a scale of 0.
  16.328 +     *
  16.329 +     * @since  1.5
  16.330 +     */
  16.331 +    public static final BigDecimal ONE =
  16.332 +        zeroThroughTen[1];
  16.333 +
  16.334 +    /**
  16.335 +     * The value 10, with a scale of 0.
  16.336 +     *
  16.337 +     * @since  1.5
  16.338 +     */
  16.339 +    public static final BigDecimal TEN =
  16.340 +        zeroThroughTen[10];
  16.341 +
  16.342 +    // Constructors
  16.343 +
  16.344 +    /**
  16.345 +     * Trusted package private constructor.
  16.346 +     * Trusted simply means if val is INFLATED, intVal could not be null and
  16.347 +     * if intVal is null, val could not be INFLATED.
  16.348 +     */
  16.349 +    BigDecimal(BigInteger intVal, long val, int scale, int prec) {
  16.350 +        this.scale = scale;
  16.351 +        this.precision = prec;
  16.352 +        this.intCompact = val;
  16.353 +        this.intVal = intVal;
  16.354 +    }
  16.355 +
  16.356 +    /**
  16.357 +     * Translates a character array representation of a
  16.358 +     * {@code BigDecimal} into a {@code BigDecimal}, accepting the
  16.359 +     * same sequence of characters as the {@link #BigDecimal(String)}
  16.360 +     * constructor, while allowing a sub-array to be specified.
  16.361 +     *
  16.362 +     * <p>Note that if the sequence of characters is already available
  16.363 +     * within a character array, using this constructor is faster than
  16.364 +     * converting the {@code char} array to string and using the
  16.365 +     * {@code BigDecimal(String)} constructor .
  16.366 +     *
  16.367 +     * @param  in {@code char} array that is the source of characters.
  16.368 +     * @param  offset first character in the array to inspect.
  16.369 +     * @param  len number of characters to consider.
  16.370 +     * @throws NumberFormatException if {@code in} is not a valid
  16.371 +     *         representation of a {@code BigDecimal} or the defined subarray
  16.372 +     *         is not wholly within {@code in}.
  16.373 +     * @since  1.5
  16.374 +     */
  16.375 +    public BigDecimal(char[] in, int offset, int len) {
  16.376 +        // protect against huge length.
  16.377 +        if (offset+len > in.length || offset < 0)
  16.378 +            throw new NumberFormatException();
  16.379 +        // This is the primary string to BigDecimal constructor; all
  16.380 +        // incoming strings end up here; it uses explicit (inline)
  16.381 +        // parsing for speed and generates at most one intermediate
  16.382 +        // (temporary) object (a char[] array) for non-compact case.
  16.383 +
  16.384 +        // Use locals for all fields values until completion
  16.385 +        int prec = 0;                 // record precision value
  16.386 +        int scl = 0;                  // record scale value
  16.387 +        long rs = 0;                  // the compact value in long
  16.388 +        BigInteger rb = null;         // the inflated value in BigInteger
  16.389 +
  16.390 +        // use array bounds checking to handle too-long, len == 0,
  16.391 +        // bad offset, etc.
  16.392 +        try {
  16.393 +            // handle the sign
  16.394 +            boolean isneg = false;          // assume positive
  16.395 +            if (in[offset] == '-') {
  16.396 +                isneg = true;               // leading minus means negative
  16.397 +                offset++;
  16.398 +                len--;
  16.399 +            } else if (in[offset] == '+') { // leading + allowed
  16.400 +                offset++;
  16.401 +                len--;
  16.402 +            }
  16.403 +
  16.404 +            // should now be at numeric part of the significand
  16.405 +            boolean dot = false;             // true when there is a '.'
  16.406 +            int cfirst = offset;             // record start of integer
  16.407 +            long exp = 0;                    // exponent
  16.408 +            char c;                          // current character
  16.409 +
  16.410 +            boolean isCompact = (len <= MAX_COMPACT_DIGITS);
  16.411 +            // integer significand array & idx is the index to it. The array
  16.412 +            // is ONLY used when we can't use a compact representation.
  16.413 +            char coeff[] = isCompact ? null : new char[len];
  16.414 +            int idx = 0;
  16.415 +
  16.416 +            for (; len > 0; offset++, len--) {
  16.417 +                c = in[offset];
  16.418 +                // have digit
  16.419 +                if ((c >= '0' && c <= '9') || Character.isDigit(c)) {
  16.420 +                    // First compact case, we need not to preserve the character
  16.421 +                    // and we can just compute the value in place.
  16.422 +                    if (isCompact) {
  16.423 +                        int digit = Character.digit(c, 10);
  16.424 +                        if (digit == 0) {
  16.425 +                            if (prec == 0)
  16.426 +                                prec = 1;
  16.427 +                            else if (rs != 0) {
  16.428 +                                rs *= 10;
  16.429 +                                ++prec;
  16.430 +                            } // else digit is a redundant leading zero
  16.431 +                        } else {
  16.432 +                            if (prec != 1 || rs != 0)
  16.433 +                                ++prec; // prec unchanged if preceded by 0s
  16.434 +                            rs = rs * 10 + digit;
  16.435 +                        }
  16.436 +                    } else { // the unscaled value is likely a BigInteger object.
  16.437 +                        if (c == '0' || Character.digit(c, 10) == 0) {
  16.438 +                            if (prec == 0) {
  16.439 +                                coeff[idx] = c;
  16.440 +                                prec = 1;
  16.441 +                            } else if (idx != 0) {
  16.442 +                                coeff[idx++] = c;
  16.443 +                                ++prec;
  16.444 +                            } // else c must be a redundant leading zero
  16.445 +                        } else {
  16.446 +                            if (prec != 1 || idx != 0)
  16.447 +                                ++prec; // prec unchanged if preceded by 0s
  16.448 +                            coeff[idx++] = c;
  16.449 +                        }
  16.450 +                    }
  16.451 +                    if (dot)
  16.452 +                        ++scl;
  16.453 +                    continue;
  16.454 +                }
  16.455 +                // have dot
  16.456 +                if (c == '.') {
  16.457 +                    // have dot
  16.458 +                    if (dot)         // two dots
  16.459 +                        throw new NumberFormatException();
  16.460 +                    dot = true;
  16.461 +                    continue;
  16.462 +                }
  16.463 +                // exponent expected
  16.464 +                if ((c != 'e') && (c != 'E'))
  16.465 +                    throw new NumberFormatException();
  16.466 +                offset++;
  16.467 +                c = in[offset];
  16.468 +                len--;
  16.469 +                boolean negexp = (c == '-');
  16.470 +                // optional sign
  16.471 +                if (negexp || c == '+') {
  16.472 +                    offset++;
  16.473 +                    c = in[offset];
  16.474 +                    len--;
  16.475 +                }
  16.476 +                if (len <= 0)    // no exponent digits
  16.477 +                    throw new NumberFormatException();
  16.478 +                // skip leading zeros in the exponent
  16.479 +                while (len > 10 && Character.digit(c, 10) == 0) {
  16.480 +                    offset++;
  16.481 +                    c = in[offset];
  16.482 +                    len--;
  16.483 +                }
  16.484 +                if (len > 10)  // too many nonzero exponent digits
  16.485 +                    throw new NumberFormatException();
  16.486 +                // c now holds first digit of exponent
  16.487 +                for (;; len--) {
  16.488 +                    int v;
  16.489 +                    if (c >= '0' && c <= '9') {
  16.490 +                        v = c - '0';
  16.491 +                    } else {
  16.492 +                        v = Character.digit(c, 10);
  16.493 +                        if (v < 0)            // not a digit
  16.494 +                            throw new NumberFormatException();
  16.495 +                    }
  16.496 +                    exp = exp * 10 + v;
  16.497 +                    if (len == 1)
  16.498 +                        break;               // that was final character
  16.499 +                    offset++;
  16.500 +                    c = in[offset];
  16.501 +                }
  16.502 +                if (negexp)                  // apply sign
  16.503 +                    exp = -exp;
  16.504 +                // Next test is required for backwards compatibility
  16.505 +                if ((int)exp != exp)         // overflow
  16.506 +                    throw new NumberFormatException();
  16.507 +                break;                       // [saves a test]
  16.508 +            }
  16.509 +            // here when no characters left
  16.510 +            if (prec == 0)              // no digits found
  16.511 +                throw new NumberFormatException();
  16.512 +
  16.513 +            // Adjust scale if exp is not zero.
  16.514 +            if (exp != 0) {                  // had significant exponent
  16.515 +                // Can't call checkScale which relies on proper fields value
  16.516 +                long adjustedScale = scl - exp;
  16.517 +                if (adjustedScale > Integer.MAX_VALUE ||
  16.518 +                    adjustedScale < Integer.MIN_VALUE)
  16.519 +                    throw new NumberFormatException("Scale out of range.");
  16.520 +                scl = (int)adjustedScale;
  16.521 +            }
  16.522 +
  16.523 +            // Remove leading zeros from precision (digits count)
  16.524 +            if (isCompact) {
  16.525 +                rs = isneg ? -rs : rs;
  16.526 +            } else {
  16.527 +                char quick[];
  16.528 +                if (!isneg) {
  16.529 +                    quick = (coeff.length != prec) ?
  16.530 +                        Arrays.copyOf(coeff, prec) : coeff;
  16.531 +                } else {
  16.532 +                    quick = new char[prec + 1];
  16.533 +                    quick[0] = '-';
  16.534 +                    System.arraycopy(coeff, 0, quick, 1, prec);
  16.535 +                }
  16.536 +                rb = new BigInteger(quick);
  16.537 +                rs = compactValFor(rb);
  16.538 +            }
  16.539 +        } catch (ArrayIndexOutOfBoundsException e) {
  16.540 +            throw new NumberFormatException();
  16.541 +        } catch (NegativeArraySizeException e) {
  16.542 +            throw new NumberFormatException();
  16.543 +        }
  16.544 +        this.scale = scl;
  16.545 +        this.precision = prec;
  16.546 +        this.intCompact = rs;
  16.547 +        this.intVal = (rs != INFLATED) ? null : rb;
  16.548 +    }
  16.549 +
  16.550 +    /**
  16.551 +     * Translates a character array representation of a
  16.552 +     * {@code BigDecimal} into a {@code BigDecimal}, accepting the
  16.553 +     * same sequence of characters as the {@link #BigDecimal(String)}
  16.554 +     * constructor, while allowing a sub-array to be specified and
  16.555 +     * with rounding according to the context settings.
  16.556 +     *
  16.557 +     * <p>Note that if the sequence of characters is already available
  16.558 +     * within a character array, using this constructor is faster than
  16.559 +     * converting the {@code char} array to string and using the
  16.560 +     * {@code BigDecimal(String)} constructor .
  16.561 +     *
  16.562 +     * @param  in {@code char} array that is the source of characters.
  16.563 +     * @param  offset first character in the array to inspect.
  16.564 +     * @param  len number of characters to consider..
  16.565 +     * @param  mc the context to use.
  16.566 +     * @throws ArithmeticException if the result is inexact but the
  16.567 +     *         rounding mode is {@code UNNECESSARY}.
  16.568 +     * @throws NumberFormatException if {@code in} is not a valid
  16.569 +     *         representation of a {@code BigDecimal} or the defined subarray
  16.570 +     *         is not wholly within {@code in}.
  16.571 +     * @since  1.5
  16.572 +     */
  16.573 +    public BigDecimal(char[] in, int offset, int len, MathContext mc) {
  16.574 +        this(in, offset, len);
  16.575 +        if (mc.precision > 0)
  16.576 +            roundThis(mc);
  16.577 +    }
  16.578 +
  16.579 +    /**
  16.580 +     * Translates a character array representation of a
  16.581 +     * {@code BigDecimal} into a {@code BigDecimal}, accepting the
  16.582 +     * same sequence of characters as the {@link #BigDecimal(String)}
  16.583 +     * constructor.
  16.584 +     *
  16.585 +     * <p>Note that if the sequence of characters is already available
  16.586 +     * as a character array, using this constructor is faster than
  16.587 +     * converting the {@code char} array to string and using the
  16.588 +     * {@code BigDecimal(String)} constructor .
  16.589 +     *
  16.590 +     * @param in {@code char} array that is the source of characters.
  16.591 +     * @throws NumberFormatException if {@code in} is not a valid
  16.592 +     *         representation of a {@code BigDecimal}.
  16.593 +     * @since  1.5
  16.594 +     */
  16.595 +    public BigDecimal(char[] in) {
  16.596 +        this(in, 0, in.length);
  16.597 +    }
  16.598 +
  16.599 +    /**
  16.600 +     * Translates a character array representation of a
  16.601 +     * {@code BigDecimal} into a {@code BigDecimal}, accepting the
  16.602 +     * same sequence of characters as the {@link #BigDecimal(String)}
  16.603 +     * constructor and with rounding according to the context
  16.604 +     * settings.
  16.605 +     *
  16.606 +     * <p>Note that if the sequence of characters is already available
  16.607 +     * as a character array, using this constructor is faster than
  16.608 +     * converting the {@code char} array to string and using the
  16.609 +     * {@code BigDecimal(String)} constructor .
  16.610 +     *
  16.611 +     * @param  in {@code char} array that is the source of characters.
  16.612 +     * @param  mc the context to use.
  16.613 +     * @throws ArithmeticException if the result is inexact but the
  16.614 +     *         rounding mode is {@code UNNECESSARY}.
  16.615 +     * @throws NumberFormatException if {@code in} is not a valid
  16.616 +     *         representation of a {@code BigDecimal}.
  16.617 +     * @since  1.5
  16.618 +     */
  16.619 +    public BigDecimal(char[] in, MathContext mc) {
  16.620 +        this(in, 0, in.length, mc);
  16.621 +    }
  16.622 +
  16.623 +    /**
  16.624 +     * Translates the string representation of a {@code BigDecimal}
  16.625 +     * into a {@code BigDecimal}.  The string representation consists
  16.626 +     * of an optional sign, {@code '+'} (<tt> '&#92;u002B'</tt>) or
  16.627 +     * {@code '-'} (<tt>'&#92;u002D'</tt>), followed by a sequence of
  16.628 +     * zero or more decimal digits ("the integer"), optionally
  16.629 +     * followed by a fraction, optionally followed by an exponent.
  16.630 +     *
  16.631 +     * <p>The fraction consists of a decimal point followed by zero
  16.632 +     * or more decimal digits.  The string must contain at least one
  16.633 +     * digit in either the integer or the fraction.  The number formed
  16.634 +     * by the sign, the integer and the fraction is referred to as the
  16.635 +     * <i>significand</i>.
  16.636 +     *
  16.637 +     * <p>The exponent consists of the character {@code 'e'}
  16.638 +     * (<tt>'&#92;u0065'</tt>) or {@code 'E'} (<tt>'&#92;u0045'</tt>)
  16.639 +     * followed by one or more decimal digits.  The value of the
  16.640 +     * exponent must lie between -{@link Integer#MAX_VALUE} ({@link
  16.641 +     * Integer#MIN_VALUE}+1) and {@link Integer#MAX_VALUE}, inclusive.
  16.642 +     *
  16.643 +     * <p>More formally, the strings this constructor accepts are
  16.644 +     * described by the following grammar:
  16.645 +     * <blockquote>
  16.646 +     * <dl>
  16.647 +     * <dt><i>BigDecimalString:</i>
  16.648 +     * <dd><i>Sign<sub>opt</sub> Significand Exponent<sub>opt</sub></i>
  16.649 +     * <p>
  16.650 +     * <dt><i>Sign:</i>
  16.651 +     * <dd>{@code +}
  16.652 +     * <dd>{@code -}
  16.653 +     * <p>
  16.654 +     * <dt><i>Significand:</i>
  16.655 +     * <dd><i>IntegerPart</i> {@code .} <i>FractionPart<sub>opt</sub></i>
  16.656 +     * <dd>{@code .} <i>FractionPart</i>
  16.657 +     * <dd><i>IntegerPart</i>
  16.658 +     * <p>
  16.659 +     * <dt><i>IntegerPart:</i>
  16.660 +     * <dd><i>Digits</i>
  16.661 +     * <p>
  16.662 +     * <dt><i>FractionPart:</i>
  16.663 +     * <dd><i>Digits</i>
  16.664 +     * <p>
  16.665 +     * <dt><i>Exponent:</i>
  16.666 +     * <dd><i>ExponentIndicator SignedInteger</i>
  16.667 +     * <p>
  16.668 +     * <dt><i>ExponentIndicator:</i>
  16.669 +     * <dd>{@code e}
  16.670 +     * <dd>{@code E}
  16.671 +     * <p>
  16.672 +     * <dt><i>SignedInteger:</i>
  16.673 +     * <dd><i>Sign<sub>opt</sub> Digits</i>
  16.674 +     * <p>
  16.675 +     * <dt><i>Digits:</i>
  16.676 +     * <dd><i>Digit</i>
  16.677 +     * <dd><i>Digits Digit</i>
  16.678 +     * <p>
  16.679 +     * <dt><i>Digit:</i>
  16.680 +     * <dd>any character for which {@link Character#isDigit}
  16.681 +     * returns {@code true}, including 0, 1, 2 ...
  16.682 +     * </dl>
  16.683 +     * </blockquote>
  16.684 +     *
  16.685 +     * <p>The scale of the returned {@code BigDecimal} will be the
  16.686 +     * number of digits in the fraction, or zero if the string
  16.687 +     * contains no decimal point, subject to adjustment for any
  16.688 +     * exponent; if the string contains an exponent, the exponent is
  16.689 +     * subtracted from the scale.  The value of the resulting scale
  16.690 +     * must lie between {@code Integer.MIN_VALUE} and
  16.691 +     * {@code Integer.MAX_VALUE}, inclusive.
  16.692 +     *
  16.693 +     * <p>The character-to-digit mapping is provided by {@link
  16.694 +     * java.lang.Character#digit} set to convert to radix 10.  The
  16.695 +     * String may not contain any extraneous characters (whitespace,
  16.696 +     * for example).
  16.697 +     *
  16.698 +     * <p><b>Examples:</b><br>
  16.699 +     * The value of the returned {@code BigDecimal} is equal to
  16.700 +     * <i>significand</i> &times; 10<sup>&nbsp;<i>exponent</i></sup>.
  16.701 +     * For each string on the left, the resulting representation
  16.702 +     * [{@code BigInteger}, {@code scale}] is shown on the right.
  16.703 +     * <pre>
  16.704 +     * "0"            [0,0]
  16.705 +     * "0.00"         [0,2]
  16.706 +     * "123"          [123,0]
  16.707 +     * "-123"         [-123,0]
  16.708 +     * "1.23E3"       [123,-1]
  16.709 +     * "1.23E+3"      [123,-1]
  16.710 +     * "12.3E+7"      [123,-6]
  16.711 +     * "12.0"         [120,1]
  16.712 +     * "12.3"         [123,1]
  16.713 +     * "0.00123"      [123,5]
  16.714 +     * "-1.23E-12"    [-123,14]
  16.715 +     * "1234.5E-4"    [12345,5]
  16.716 +     * "0E+7"         [0,-7]
  16.717 +     * "-0"           [0,0]
  16.718 +     * </pre>
  16.719 +     *
  16.720 +     * <p>Note: For values other than {@code float} and
  16.721 +     * {@code double} NaN and &plusmn;Infinity, this constructor is
  16.722 +     * compatible with the values returned by {@link Float#toString}
  16.723 +     * and {@link Double#toString}.  This is generally the preferred
  16.724 +     * way to convert a {@code float} or {@code double} into a
  16.725 +     * BigDecimal, as it doesn't suffer from the unpredictability of
  16.726 +     * the {@link #BigDecimal(double)} constructor.
  16.727 +     *
  16.728 +     * @param val String representation of {@code BigDecimal}.
  16.729 +     *
  16.730 +     * @throws NumberFormatException if {@code val} is not a valid
  16.731 +     *         representation of a {@code BigDecimal}.
  16.732 +     */
  16.733 +    public BigDecimal(String val) {
  16.734 +        this(val.toCharArray(), 0, val.length());
  16.735 +    }
  16.736 +
  16.737 +    /**
  16.738 +     * Translates the string representation of a {@code BigDecimal}
  16.739 +     * into a {@code BigDecimal}, accepting the same strings as the
  16.740 +     * {@link #BigDecimal(String)} constructor, with rounding
  16.741 +     * according to the context settings.
  16.742 +     *
  16.743 +     * @param  val string representation of a {@code BigDecimal}.
  16.744 +     * @param  mc the context to use.
  16.745 +     * @throws ArithmeticException if the result is inexact but the
  16.746 +     *         rounding mode is {@code UNNECESSARY}.
  16.747 +     * @throws NumberFormatException if {@code val} is not a valid
  16.748 +     *         representation of a BigDecimal.
  16.749 +     * @since  1.5
  16.750 +     */
  16.751 +    public BigDecimal(String val, MathContext mc) {
  16.752 +        this(val.toCharArray(), 0, val.length());
  16.753 +        if (mc.precision > 0)
  16.754 +            roundThis(mc);
  16.755 +    }
  16.756 +
  16.757 +    /**
  16.758 +     * Translates a {@code double} into a {@code BigDecimal} which
  16.759 +     * is the exact decimal representation of the {@code double}'s
  16.760 +     * binary floating-point value.  The scale of the returned
  16.761 +     * {@code BigDecimal} is the smallest value such that
  16.762 +     * <tt>(10<sup>scale</sup> &times; val)</tt> is an integer.
  16.763 +     * <p>
  16.764 +     * <b>Notes:</b>
  16.765 +     * <ol>
  16.766 +     * <li>
  16.767 +     * The results of this constructor can be somewhat unpredictable.
  16.768 +     * One might assume that writing {@code new BigDecimal(0.1)} in
  16.769 +     * Java creates a {@code BigDecimal} which is exactly equal to
  16.770 +     * 0.1 (an unscaled value of 1, with a scale of 1), but it is
  16.771 +     * actually equal to
  16.772 +     * 0.1000000000000000055511151231257827021181583404541015625.
  16.773 +     * This is because 0.1 cannot be represented exactly as a
  16.774 +     * {@code double} (or, for that matter, as a binary fraction of
  16.775 +     * any finite length).  Thus, the value that is being passed
  16.776 +     * <i>in</i> to the constructor is not exactly equal to 0.1,
  16.777 +     * appearances notwithstanding.
  16.778 +     *
  16.779 +     * <li>
  16.780 +     * The {@code String} constructor, on the other hand, is
  16.781 +     * perfectly predictable: writing {@code new BigDecimal("0.1")}
  16.782 +     * creates a {@code BigDecimal} which is <i>exactly</i> equal to
  16.783 +     * 0.1, as one would expect.  Therefore, it is generally
  16.784 +     * recommended that the {@linkplain #BigDecimal(String)
  16.785 +     * <tt>String</tt> constructor} be used in preference to this one.
  16.786 +     *
  16.787 +     * <li>
  16.788 +     * When a {@code double} must be used as a source for a
  16.789 +     * {@code BigDecimal}, note that this constructor provides an
  16.790 +     * exact conversion; it does not give the same result as
  16.791 +     * converting the {@code double} to a {@code String} using the
  16.792 +     * {@link Double#toString(double)} method and then using the
  16.793 +     * {@link #BigDecimal(String)} constructor.  To get that result,
  16.794 +     * use the {@code static} {@link #valueOf(double)} method.
  16.795 +     * </ol>
  16.796 +     *
  16.797 +     * @param val {@code double} value to be converted to
  16.798 +     *        {@code BigDecimal}.
  16.799 +     * @throws NumberFormatException if {@code val} is infinite or NaN.
  16.800 +     */
  16.801 +    public BigDecimal(double val) {
  16.802 +        if (Double.isInfinite(val) || Double.isNaN(val))
  16.803 +            throw new NumberFormatException("Infinite or NaN");
  16.804 +
  16.805 +        // Translate the double into sign, exponent and significand, according
  16.806 +        // to the formulae in JLS, Section 20.10.22.
  16.807 +        long valBits = Double.doubleToLongBits(val);
  16.808 +        int sign = ((valBits >> 63)==0 ? 1 : -1);
  16.809 +        int exponent = (int) ((valBits >> 52) & 0x7ffL);
  16.810 +        long significand = (exponent==0 ? (valBits & ((1L<<52) - 1)) << 1
  16.811 +                            : (valBits & ((1L<<52) - 1)) | (1L<<52));
  16.812 +        exponent -= 1075;
  16.813 +        // At this point, val == sign * significand * 2**exponent.
  16.814 +
  16.815 +        /*
  16.816 +         * Special case zero to supress nonterminating normalization
  16.817 +         * and bogus scale calculation.
  16.818 +         */
  16.819 +        if (significand == 0) {
  16.820 +            intVal = BigInteger.ZERO;
  16.821 +            intCompact = 0;
  16.822 +            precision = 1;
  16.823 +            return;
  16.824 +        }
  16.825 +
  16.826 +        // Normalize
  16.827 +        while((significand & 1) == 0) {    //  i.e., significand is even
  16.828 +            significand >>= 1;
  16.829 +            exponent++;
  16.830 +        }
  16.831 +
  16.832 +        // Calculate intVal and scale
  16.833 +        long s = sign * significand;
  16.834 +        BigInteger b;
  16.835 +        if (exponent < 0) {
  16.836 +            b = BigInteger.valueOf(5).pow(-exponent).multiply(s);
  16.837 +            scale = -exponent;
  16.838 +        } else if (exponent > 0) {
  16.839 +            b = BigInteger.valueOf(2).pow(exponent).multiply(s);
  16.840 +        } else {
  16.841 +            b = BigInteger.valueOf(s);
  16.842 +        }
  16.843 +        intCompact = compactValFor(b);
  16.844 +        intVal = (intCompact != INFLATED) ? null : b;
  16.845 +    }
  16.846 +
  16.847 +    /**
  16.848 +     * Translates a {@code double} into a {@code BigDecimal}, with
  16.849 +     * rounding according to the context settings.  The scale of the
  16.850 +     * {@code BigDecimal} is the smallest value such that
  16.851 +     * <tt>(10<sup>scale</sup> &times; val)</tt> is an integer.
  16.852 +     *
  16.853 +     * <p>The results of this constructor can be somewhat unpredictable
  16.854 +     * and its use is generally not recommended; see the notes under
  16.855 +     * the {@link #BigDecimal(double)} constructor.
  16.856 +     *
  16.857 +     * @param  val {@code double} value to be converted to
  16.858 +     *         {@code BigDecimal}.
  16.859 +     * @param  mc the context to use.
  16.860 +     * @throws ArithmeticException if the result is inexact but the
  16.861 +     *         RoundingMode is UNNECESSARY.
  16.862 +     * @throws NumberFormatException if {@code val} is infinite or NaN.
  16.863 +     * @since  1.5
  16.864 +     */
  16.865 +    public BigDecimal(double val, MathContext mc) {
  16.866 +        this(val);
  16.867 +        if (mc.precision > 0)
  16.868 +            roundThis(mc);
  16.869 +    }
  16.870 +
  16.871 +    /**
  16.872 +     * Translates a {@code BigInteger} into a {@code BigDecimal}.
  16.873 +     * The scale of the {@code BigDecimal} is zero.
  16.874 +     *
  16.875 +     * @param val {@code BigInteger} value to be converted to
  16.876 +     *            {@code BigDecimal}.
  16.877 +     */
  16.878 +    public BigDecimal(BigInteger val) {
  16.879 +        intCompact = compactValFor(val);
  16.880 +        intVal = (intCompact != INFLATED) ? null : val;
  16.881 +    }
  16.882 +
  16.883 +    /**
  16.884 +     * Translates a {@code BigInteger} into a {@code BigDecimal}
  16.885 +     * rounding according to the context settings.  The scale of the
  16.886 +     * {@code BigDecimal} is zero.
  16.887 +     *
  16.888 +     * @param val {@code BigInteger} value to be converted to
  16.889 +     *            {@code BigDecimal}.
  16.890 +     * @param  mc the context to use.
  16.891 +     * @throws ArithmeticException if the result is inexact but the
  16.892 +     *         rounding mode is {@code UNNECESSARY}.
  16.893 +     * @since  1.5
  16.894 +     */
  16.895 +    public BigDecimal(BigInteger val, MathContext mc) {
  16.896 +        this(val);
  16.897 +        if (mc.precision > 0)
  16.898 +            roundThis(mc);
  16.899 +    }
  16.900 +
  16.901 +    /**
  16.902 +     * Translates a {@code BigInteger} unscaled value and an
  16.903 +     * {@code int} scale into a {@code BigDecimal}.  The value of
  16.904 +     * the {@code BigDecimal} is
  16.905 +     * <tt>(unscaledVal &times; 10<sup>-scale</sup>)</tt>.
  16.906 +     *
  16.907 +     * @param unscaledVal unscaled value of the {@code BigDecimal}.
  16.908 +     * @param scale scale of the {@code BigDecimal}.
  16.909 +     */
  16.910 +    public BigDecimal(BigInteger unscaledVal, int scale) {
  16.911 +        // Negative scales are now allowed
  16.912 +        this(unscaledVal);
  16.913 +        this.scale = scale;
  16.914 +    }
  16.915 +
  16.916 +    /**
  16.917 +     * Translates a {@code BigInteger} unscaled value and an
  16.918 +     * {@code int} scale into a {@code BigDecimal}, with rounding
  16.919 +     * according to the context settings.  The value of the
  16.920 +     * {@code BigDecimal} is <tt>(unscaledVal &times;
  16.921 +     * 10<sup>-scale</sup>)</tt>, rounded according to the
  16.922 +     * {@code precision} and rounding mode settings.
  16.923 +     *
  16.924 +     * @param  unscaledVal unscaled value of the {@code BigDecimal}.
  16.925 +     * @param  scale scale of the {@code BigDecimal}.
  16.926 +     * @param  mc the context to use.
  16.927 +     * @throws ArithmeticException if the result is inexact but the
  16.928 +     *         rounding mode is {@code UNNECESSARY}.
  16.929 +     * @since  1.5
  16.930 +     */
  16.931 +    public BigDecimal(BigInteger unscaledVal, int scale, MathContext mc) {
  16.932 +        this(unscaledVal);
  16.933 +        this.scale = scale;
  16.934 +        if (mc.precision > 0)
  16.935 +            roundThis(mc);
  16.936 +    }
  16.937 +
  16.938 +    /**
  16.939 +     * Translates an {@code int} into a {@code BigDecimal}.  The
  16.940 +     * scale of the {@code BigDecimal} is zero.
  16.941 +     *
  16.942 +     * @param val {@code int} value to be converted to
  16.943 +     *            {@code BigDecimal}.
  16.944 +     * @since  1.5
  16.945 +     */
  16.946 +    public BigDecimal(int val) {
  16.947 +        intCompact = val;
  16.948 +    }
  16.949 +
  16.950 +    /**
  16.951 +     * Translates an {@code int} into a {@code BigDecimal}, with
  16.952 +     * rounding according to the context settings.  The scale of the
  16.953 +     * {@code BigDecimal}, before any rounding, is zero.
  16.954 +     *
  16.955 +     * @param  val {@code int} value to be converted to {@code BigDecimal}.
  16.956 +     * @param  mc the context to use.
  16.957 +     * @throws ArithmeticException if the result is inexact but the
  16.958 +     *         rounding mode is {@code UNNECESSARY}.
  16.959 +     * @since  1.5
  16.960 +     */
  16.961 +    public BigDecimal(int val, MathContext mc) {
  16.962 +        intCompact = val;
  16.963 +        if (mc.precision > 0)
  16.964 +            roundThis(mc);
  16.965 +    }
  16.966 +
  16.967 +    /**
  16.968 +     * Translates a {@code long} into a {@code BigDecimal}.  The
  16.969 +     * scale of the {@code BigDecimal} is zero.
  16.970 +     *
  16.971 +     * @param val {@code long} value to be converted to {@code BigDecimal}.
  16.972 +     * @since  1.5
  16.973 +     */
  16.974 +    public BigDecimal(long val) {
  16.975 +        this.intCompact = val;
  16.976 +        this.intVal = (val == INFLATED) ? BigInteger.valueOf(val) : null;
  16.977 +    }
  16.978 +
  16.979 +    /**
  16.980 +     * Translates a {@code long} into a {@code BigDecimal}, with
  16.981 +     * rounding according to the context settings.  The scale of the
  16.982 +     * {@code BigDecimal}, before any rounding, is zero.
  16.983 +     *
  16.984 +     * @param  val {@code long} value to be converted to {@code BigDecimal}.
  16.985 +     * @param  mc the context to use.
  16.986 +     * @throws ArithmeticException if the result is inexact but the
  16.987 +     *         rounding mode is {@code UNNECESSARY}.
  16.988 +     * @since  1.5
  16.989 +     */
  16.990 +    public BigDecimal(long val, MathContext mc) {
  16.991 +        this(val);
  16.992 +        if (mc.precision > 0)
  16.993 +            roundThis(mc);
  16.994 +    }
  16.995 +
  16.996 +    // Static Factory Methods
  16.997 +
  16.998 +    /**
  16.999 +     * Translates a {@code long} unscaled value and an
 16.1000 +     * {@code int} scale into a {@code BigDecimal}.  This
 16.1001 +     * {@literal "static factory method"} is provided in preference to
 16.1002 +     * a ({@code long}, {@code int}) constructor because it
 16.1003 +     * allows for reuse of frequently used {@code BigDecimal} values..
 16.1004 +     *
 16.1005 +     * @param unscaledVal unscaled value of the {@code BigDecimal}.
 16.1006 +     * @param scale scale of the {@code BigDecimal}.
 16.1007 +     * @return a {@code BigDecimal} whose value is
 16.1008 +     *         <tt>(unscaledVal &times; 10<sup>-scale</sup>)</tt>.
 16.1009 +     */
 16.1010 +    public static BigDecimal valueOf(long unscaledVal, int scale) {
 16.1011 +        if (scale == 0)
 16.1012 +            return valueOf(unscaledVal);
 16.1013 +        else if (unscaledVal == 0) {
 16.1014 +            if (scale > 0 && scale < ZERO_SCALED_BY.length)
 16.1015 +                return ZERO_SCALED_BY[scale];
 16.1016 +            else
 16.1017 +                return new BigDecimal(BigInteger.ZERO, 0, scale, 1);
 16.1018 +        }
 16.1019 +        return new BigDecimal(unscaledVal == INFLATED ?
 16.1020 +                              BigInteger.valueOf(unscaledVal) : null,
 16.1021 +                              unscaledVal, scale, 0);
 16.1022 +    }
 16.1023 +
 16.1024 +    /**
 16.1025 +     * Translates a {@code long} value into a {@code BigDecimal}
 16.1026 +     * with a scale of zero.  This {@literal "static factory method"}
 16.1027 +     * is provided in preference to a ({@code long}) constructor
 16.1028 +     * because it allows for reuse of frequently used
 16.1029 +     * {@code BigDecimal} values.
 16.1030 +     *
 16.1031 +     * @param val value of the {@code BigDecimal}.
 16.1032 +     * @return a {@code BigDecimal} whose value is {@code val}.
 16.1033 +     */
 16.1034 +    public static BigDecimal valueOf(long val) {
 16.1035 +        if (val >= 0 && val < zeroThroughTen.length)
 16.1036 +            return zeroThroughTen[(int)val];
 16.1037 +        else if (val != INFLATED)
 16.1038 +            return new BigDecimal(null, val, 0, 0);
 16.1039 +        return new BigDecimal(BigInteger.valueOf(val), val, 0, 0);
 16.1040 +    }
 16.1041 +
 16.1042 +    /**
 16.1043 +     * Translates a {@code double} into a {@code BigDecimal}, using
 16.1044 +     * the {@code double}'s canonical string representation provided
 16.1045 +     * by the {@link Double#toString(double)} method.
 16.1046 +     *
 16.1047 +     * <p><b>Note:</b> This is generally the preferred way to convert
 16.1048 +     * a {@code double} (or {@code float}) into a
 16.1049 +     * {@code BigDecimal}, as the value returned is equal to that
 16.1050 +     * resulting from constructing a {@code BigDecimal} from the
 16.1051 +     * result of using {@link Double#toString(double)}.
 16.1052 +     *
 16.1053 +     * @param  val {@code double} to convert to a {@code BigDecimal}.
 16.1054 +     * @return a {@code BigDecimal} whose value is equal to or approximately
 16.1055 +     *         equal to the value of {@code val}.
 16.1056 +     * @throws NumberFormatException if {@code val} is infinite or NaN.
 16.1057 +     * @since  1.5
 16.1058 +     */
 16.1059 +    public static BigDecimal valueOf(double val) {
 16.1060 +        // Reminder: a zero double returns '0.0', so we cannot fastpath
 16.1061 +        // to use the constant ZERO.  This might be important enough to
 16.1062 +        // justify a factory approach, a cache, or a few private
 16.1063 +        // constants, later.
 16.1064 +        return new BigDecimal(Double.toString(val));
 16.1065 +    }
 16.1066 +
 16.1067 +    // Arithmetic Operations
 16.1068 +    /**
 16.1069 +     * Returns a {@code BigDecimal} whose value is {@code (this +
 16.1070 +     * augend)}, and whose scale is {@code max(this.scale(),
 16.1071 +     * augend.scale())}.
 16.1072 +     *
 16.1073 +     * @param  augend value to be added to this {@code BigDecimal}.
 16.1074 +     * @return {@code this + augend}
 16.1075 +     */
 16.1076 +    public BigDecimal add(BigDecimal augend) {
 16.1077 +        long xs = this.intCompact;
 16.1078 +        long ys = augend.intCompact;
 16.1079 +        BigInteger fst = (xs != INFLATED) ? null : this.intVal;
 16.1080 +        BigInteger snd = (ys != INFLATED) ? null : augend.intVal;
 16.1081 +        int rscale = this.scale;
 16.1082 +
 16.1083 +        long sdiff = (long)rscale - augend.scale;
 16.1084 +        if (sdiff != 0) {
 16.1085 +            if (sdiff < 0) {
 16.1086 +                int raise = checkScale(-sdiff);
 16.1087 +                rscale = augend.scale;
 16.1088 +                if (xs == INFLATED ||
 16.1089 +                    (xs = longMultiplyPowerTen(xs, raise)) == INFLATED)
 16.1090 +                    fst = bigMultiplyPowerTen(raise);
 16.1091 +            } else {
 16.1092 +                int raise = augend.checkScale(sdiff);
 16.1093 +                if (ys == INFLATED ||
 16.1094 +                    (ys = longMultiplyPowerTen(ys, raise)) == INFLATED)
 16.1095 +                    snd = augend.bigMultiplyPowerTen(raise);
 16.1096 +            }
 16.1097 +        }
 16.1098 +        if (xs != INFLATED && ys != INFLATED) {
 16.1099 +            long sum = xs + ys;
 16.1100 +            // See "Hacker's Delight" section 2-12 for explanation of
 16.1101 +            // the overflow test.
 16.1102 +            if ( (((sum ^ xs) & (sum ^ ys))) >= 0L) // not overflowed
 16.1103 +                return BigDecimal.valueOf(sum, rscale);
 16.1104 +        }
 16.1105 +        if (fst == null)
 16.1106 +            fst = BigInteger.valueOf(xs);
 16.1107 +        if (snd == null)
 16.1108 +            snd = BigInteger.valueOf(ys);
 16.1109 +        BigInteger sum = fst.add(snd);
 16.1110 +        return (fst.signum == snd.signum) ?
 16.1111 +            new BigDecimal(sum, INFLATED, rscale, 0) :
 16.1112 +            new BigDecimal(sum, rscale);
 16.1113 +    }
 16.1114 +
 16.1115 +    /**
 16.1116 +     * Returns a {@code BigDecimal} whose value is {@code (this + augend)},
 16.1117 +     * with rounding according to the context settings.
 16.1118 +     *
 16.1119 +     * If either number is zero and the precision setting is nonzero then
 16.1120 +     * the other number, rounded if necessary, is used as the result.
 16.1121 +     *
 16.1122 +     * @param  augend value to be added to this {@code BigDecimal}.
 16.1123 +     * @param  mc the context to use.
 16.1124 +     * @return {@code this + augend}, rounded as necessary.
 16.1125 +     * @throws ArithmeticException if the result is inexact but the
 16.1126 +     *         rounding mode is {@code UNNECESSARY}.
 16.1127 +     * @since  1.5
 16.1128 +     */
 16.1129 +    public BigDecimal add(BigDecimal augend, MathContext mc) {
 16.1130 +        if (mc.precision == 0)
 16.1131 +            return add(augend);
 16.1132 +        BigDecimal lhs = this;
 16.1133 +
 16.1134 +        // Could optimize if values are compact
 16.1135 +        this.inflate();
 16.1136 +        augend.inflate();
 16.1137 +
 16.1138 +        // If either number is zero then the other number, rounded and
 16.1139 +        // scaled if necessary, is used as the result.
 16.1140 +        {
 16.1141 +            boolean lhsIsZero = lhs.signum() == 0;
 16.1142 +            boolean augendIsZero = augend.signum() == 0;
 16.1143 +
 16.1144 +            if (lhsIsZero || augendIsZero) {
 16.1145 +                int preferredScale = Math.max(lhs.scale(), augend.scale());
 16.1146 +                BigDecimal result;
 16.1147 +
 16.1148 +                // Could use a factory for zero instead of a new object
 16.1149 +                if (lhsIsZero && augendIsZero)
 16.1150 +                    return new BigDecimal(BigInteger.ZERO, 0, preferredScale, 0);
 16.1151 +
 16.1152 +                result = lhsIsZero ? doRound(augend, mc) : doRound(lhs, mc);
 16.1153 +
 16.1154 +                if (result.scale() == preferredScale)
 16.1155 +                    return result;
 16.1156 +                else if (result.scale() > preferredScale) {
 16.1157 +                    BigDecimal scaledResult =
 16.1158 +                        new BigDecimal(result.intVal, result.intCompact,
 16.1159 +                                       result.scale, 0);
 16.1160 +                    scaledResult.stripZerosToMatchScale(preferredScale);
 16.1161 +                    return scaledResult;
 16.1162 +                } else { // result.scale < preferredScale
 16.1163 +                    int precisionDiff = mc.precision - result.precision();
 16.1164 +                    int scaleDiff     = preferredScale - result.scale();
 16.1165 +
 16.1166 +                    if (precisionDiff >= scaleDiff)
 16.1167 +                        return result.setScale(preferredScale); // can achieve target scale
 16.1168 +                    else
 16.1169 +                        return result.setScale(result.scale() + precisionDiff);
 16.1170 +                }
 16.1171 +            }
 16.1172 +        }
 16.1173 +
 16.1174 +        long padding = (long)lhs.scale - augend.scale;
 16.1175 +        if (padding != 0) {        // scales differ; alignment needed
 16.1176 +            BigDecimal arg[] = preAlign(lhs, augend, padding, mc);
 16.1177 +            matchScale(arg);
 16.1178 +            lhs    = arg[0];
 16.1179 +            augend = arg[1];
 16.1180 +        }
 16.1181 +
 16.1182 +        BigDecimal d = new BigDecimal(lhs.inflate().add(augend.inflate()),
 16.1183 +                                      lhs.scale);
 16.1184 +        return doRound(d, mc);
 16.1185 +    }
 16.1186 +
 16.1187 +    /**
 16.1188 +     * Returns an array of length two, the sum of whose entries is
 16.1189 +     * equal to the rounded sum of the {@code BigDecimal} arguments.
 16.1190 +     *
 16.1191 +     * <p>If the digit positions of the arguments have a sufficient
 16.1192 +     * gap between them, the value smaller in magnitude can be
 16.1193 +     * condensed into a {@literal "sticky bit"} and the end result will
 16.1194 +     * round the same way <em>if</em> the precision of the final
 16.1195 +     * result does not include the high order digit of the small
 16.1196 +     * magnitude operand.
 16.1197 +     *
 16.1198 +     * <p>Note that while strictly speaking this is an optimization,
 16.1199 +     * it makes a much wider range of additions practical.
 16.1200 +     *
 16.1201 +     * <p>This corresponds to a pre-shift operation in a fixed
 16.1202 +     * precision floating-point adder; this method is complicated by
 16.1203 +     * variable precision of the result as determined by the
 16.1204 +     * MathContext.  A more nuanced operation could implement a
 16.1205 +     * {@literal "right shift"} on the smaller magnitude operand so
 16.1206 +     * that the number of digits of the smaller operand could be
 16.1207 +     * reduced even though the significands partially overlapped.
 16.1208 +     */
 16.1209 +    private BigDecimal[] preAlign(BigDecimal lhs, BigDecimal augend,
 16.1210 +                                  long padding, MathContext mc) {
 16.1211 +        assert padding != 0;
 16.1212 +        BigDecimal big;
 16.1213 +        BigDecimal small;
 16.1214 +
 16.1215 +        if (padding < 0) {     // lhs is big;   augend is small
 16.1216 +            big   = lhs;
 16.1217 +            small = augend;
 16.1218 +        } else {               // lhs is small; augend is big
 16.1219 +            big   = augend;
 16.1220 +            small = lhs;
 16.1221 +        }
 16.1222 +
 16.1223 +        /*
 16.1224 +         * This is the estimated scale of an ulp of the result; it
 16.1225 +         * assumes that the result doesn't have a carry-out on a true
 16.1226 +         * add (e.g. 999 + 1 => 1000) or any subtractive cancellation
 16.1227 +         * on borrowing (e.g. 100 - 1.2 => 98.8)
 16.1228 +         */
 16.1229 +        long estResultUlpScale = (long)big.scale - big.precision() + mc.precision;
 16.1230 +
 16.1231 +        /*
 16.1232 +         * The low-order digit position of big is big.scale().  This
 16.1233 +         * is true regardless of whether big has a positive or
 16.1234 +         * negative scale.  The high-order digit position of small is
 16.1235 +         * small.scale - (small.precision() - 1).  To do the full
 16.1236 +         * condensation, the digit positions of big and small must be
 16.1237 +         * disjoint *and* the digit positions of small should not be
 16.1238 +         * directly visible in the result.
 16.1239 +         */
 16.1240 +        long smallHighDigitPos = (long)small.scale - small.precision() + 1;
 16.1241 +        if (smallHighDigitPos > big.scale + 2 &&         // big and small disjoint
 16.1242 +            smallHighDigitPos > estResultUlpScale + 2) { // small digits not visible
 16.1243 +            small = BigDecimal.valueOf(small.signum(),
 16.1244 +                                       this.checkScale(Math.max(big.scale, estResultUlpScale) + 3));
 16.1245 +        }
 16.1246 +
 16.1247 +        // Since addition is symmetric, preserving input order in
 16.1248 +        // returned operands doesn't matter
 16.1249 +        BigDecimal[] result = {big, small};
 16.1250 +        return result;
 16.1251 +    }
 16.1252 +
 16.1253 +    /**
 16.1254 +     * Returns a {@code BigDecimal} whose value is {@code (this -
 16.1255 +     * subtrahend)}, and whose scale is {@code max(this.scale(),
 16.1256 +     * subtrahend.scale())}.
 16.1257 +     *
 16.1258 +     * @param  subtrahend value to be subtracted from this {@code BigDecimal}.
 16.1259 +     * @return {@code this - subtrahend}
 16.1260 +     */
 16.1261 +    public BigDecimal subtract(BigDecimal subtrahend) {
 16.1262 +        return add(subtrahend.negate());
 16.1263 +    }
 16.1264 +
 16.1265 +    /**
 16.1266 +     * Returns a {@code BigDecimal} whose value is {@code (this - subtrahend)},
 16.1267 +     * with rounding according to the context settings.
 16.1268 +     *
 16.1269 +     * If {@code subtrahend} is zero then this, rounded if necessary, is used as the
 16.1270 +     * result.  If this is zero then the result is {@code subtrahend.negate(mc)}.
 16.1271 +     *
 16.1272 +     * @param  subtrahend value to be subtracted from this {@code BigDecimal}.
 16.1273 +     * @param  mc the context to use.
 16.1274 +     * @return {@code this - subtrahend}, rounded as necessary.
 16.1275 +     * @throws ArithmeticException if the result is inexact but the
 16.1276 +     *         rounding mode is {@code UNNECESSARY}.
 16.1277 +     * @since  1.5
 16.1278 +     */
 16.1279 +    public BigDecimal subtract(BigDecimal subtrahend, MathContext mc) {
 16.1280 +        BigDecimal nsubtrahend = subtrahend.negate();
 16.1281 +        if (mc.precision == 0)
 16.1282 +            return add(nsubtrahend);
 16.1283 +        // share the special rounding code in add()
 16.1284 +        return add(nsubtrahend, mc);
 16.1285 +    }
 16.1286 +
 16.1287 +    /**
 16.1288 +     * Returns a {@code BigDecimal} whose value is <tt>(this &times;
 16.1289 +     * multiplicand)</tt>, and whose scale is {@code (this.scale() +
 16.1290 +     * multiplicand.scale())}.
 16.1291 +     *
 16.1292 +     * @param  multiplicand value to be multiplied by this {@code BigDecimal}.
 16.1293 +     * @return {@code this * multiplicand}
 16.1294 +     */
 16.1295 +    public BigDecimal multiply(BigDecimal multiplicand) {
 16.1296 +        long x = this.intCompact;
 16.1297 +        long y = multiplicand.intCompact;
 16.1298 +        int productScale = checkScale((long)scale + multiplicand.scale);
 16.1299 +
 16.1300 +        // Might be able to do a more clever check incorporating the
 16.1301 +        // inflated check into the overflow computation.
 16.1302 +        if (x != INFLATED && y != INFLATED) {
 16.1303 +            /*
 16.1304 +             * If the product is not an overflowed value, continue
 16.1305 +             * to use the compact representation.  if either of x or y
 16.1306 +             * is INFLATED, the product should also be regarded as
 16.1307 +             * an overflow. Before using the overflow test suggested in
 16.1308 +             * "Hacker's Delight" section 2-12, we perform quick checks
 16.1309 +             * using the precision information to see whether the overflow
 16.1310 +             * would occur since division is expensive on most CPUs.
 16.1311 +             */
 16.1312 +            long product = x * y;
 16.1313 +            long prec = this.precision() + multiplicand.precision();
 16.1314 +            if (prec < 19 || (prec < 21 && (y == 0 || product / y == x)))
 16.1315 +                return BigDecimal.valueOf(product, productScale);
 16.1316 +            return new BigDecimal(BigInteger.valueOf(x).multiply(y), INFLATED,
 16.1317 +                                  productScale, 0);
 16.1318 +        }
 16.1319 +        BigInteger rb;
 16.1320 +        if (x == INFLATED && y == INFLATED)
 16.1321 +            rb = this.intVal.multiply(multiplicand.intVal);
 16.1322 +        else if (x != INFLATED)
 16.1323 +            rb = multiplicand.intVal.multiply(x);
 16.1324 +        else
 16.1325 +            rb = this.intVal.multiply(y);
 16.1326 +        return new BigDecimal(rb, INFLATED, productScale, 0);
 16.1327 +    }
 16.1328 +
 16.1329 +    /**
 16.1330 +     * Returns a {@code BigDecimal} whose value is <tt>(this &times;
 16.1331 +     * multiplicand)</tt>, with rounding according to the context settings.
 16.1332 +     *
 16.1333 +     * @param  multiplicand value to be multiplied by this {@code BigDecimal}.
 16.1334 +     * @param  mc the context to use.
 16.1335 +     * @return {@code this * multiplicand}, rounded as necessary.
 16.1336 +     * @throws ArithmeticException if the result is inexact but the
 16.1337 +     *         rounding mode is {@code UNNECESSARY}.
 16.1338 +     * @since  1.5
 16.1339 +     */
 16.1340 +    public BigDecimal multiply(BigDecimal multiplicand, MathContext mc) {
 16.1341 +        if (mc.precision == 0)
 16.1342 +            return multiply(multiplicand);
 16.1343 +        return doRound(this.multiply(multiplicand), mc);
 16.1344 +    }
 16.1345 +
 16.1346 +    /**
 16.1347 +     * Returns a {@code BigDecimal} whose value is {@code (this /
 16.1348 +     * divisor)}, and whose scale is as specified.  If rounding must
 16.1349 +     * be performed to generate a result with the specified scale, the
 16.1350 +     * specified rounding mode is applied.
 16.1351 +     *
 16.1352 +     * <p>The new {@link #divide(BigDecimal, int, RoundingMode)} method
 16.1353 +     * should be used in preference to this legacy method.
 16.1354 +     *
 16.1355 +     * @param  divisor value by which this {@code BigDecimal} is to be divided.
 16.1356 +     * @param  scale scale of the {@code BigDecimal} quotient to be returned.
 16.1357 +     * @param  roundingMode rounding mode to apply.
 16.1358 +     * @return {@code this / divisor}
 16.1359 +     * @throws ArithmeticException if {@code divisor} is zero,
 16.1360 +     *         {@code roundingMode==ROUND_UNNECESSARY} and
 16.1361 +     *         the specified scale is insufficient to represent the result
 16.1362 +     *         of the division exactly.
 16.1363 +     * @throws IllegalArgumentException if {@code roundingMode} does not
 16.1364 +     *         represent a valid rounding mode.
 16.1365 +     * @see    #ROUND_UP
 16.1366 +     * @see    #ROUND_DOWN
 16.1367 +     * @see    #ROUND_CEILING
 16.1368 +     * @see    #ROUND_FLOOR
 16.1369 +     * @see    #ROUND_HALF_UP
 16.1370 +     * @see    #ROUND_HALF_DOWN
 16.1371 +     * @see    #ROUND_HALF_EVEN
 16.1372 +     * @see    #ROUND_UNNECESSARY
 16.1373 +     */
 16.1374 +    public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) {
 16.1375 +        /*
 16.1376 +         * IMPLEMENTATION NOTE: This method *must* return a new object
 16.1377 +         * since divideAndRound uses divide to generate a value whose
 16.1378 +         * scale is then modified.
 16.1379 +         */
 16.1380 +        if (roundingMode < ROUND_UP || roundingMode > ROUND_UNNECESSARY)
 16.1381 +            throw new IllegalArgumentException("Invalid rounding mode");
 16.1382 +        /*
 16.1383 +         * Rescale dividend or divisor (whichever can be "upscaled" to
 16.1384 +         * produce correctly scaled quotient).
 16.1385 +         * Take care to detect out-of-range scales
 16.1386 +         */
 16.1387 +        BigDecimal dividend = this;
 16.1388 +        if (checkScale((long)scale + divisor.scale) > this.scale)
 16.1389 +            dividend = this.setScale(scale + divisor.scale, ROUND_UNNECESSARY);
 16.1390 +        else
 16.1391 +            divisor = divisor.setScale(checkScale((long)this.scale - scale),
 16.1392 +                                       ROUND_UNNECESSARY);
 16.1393 +        return divideAndRound(dividend.intCompact, dividend.intVal,
 16.1394 +                              divisor.intCompact, divisor.intVal,
 16.1395 +                              scale, roundingMode, scale);
 16.1396 +    }
 16.1397 +
 16.1398 +    /**
 16.1399 +     * Internally used for division operation. The dividend and divisor are
 16.1400 +     * passed both in {@code long} format and {@code BigInteger} format. The
 16.1401 +     * returned {@code BigDecimal} object is the quotient whose scale is set to
 16.1402 +     * the passed in scale. If the remainder is not zero, it will be rounded
 16.1403 +     * based on the passed in roundingMode. Also, if the remainder is zero and
 16.1404 +     * the last parameter, i.e. preferredScale is NOT equal to scale, the
 16.1405 +     * trailing zeros of the result is stripped to match the preferredScale.
 16.1406 +     */
 16.1407 +    private static BigDecimal divideAndRound(long ldividend, BigInteger bdividend,
 16.1408 +                                             long ldivisor,  BigInteger bdivisor,
 16.1409 +                                             int scale, int roundingMode,
 16.1410 +                                             int preferredScale) {
 16.1411 +        boolean isRemainderZero;       // record remainder is zero or not
 16.1412 +        int qsign;                     // quotient sign
 16.1413 +        long q = 0, r = 0;             // store quotient & remainder in long
 16.1414 +        MutableBigInteger mq = null;   // store quotient
 16.1415 +        MutableBigInteger mr = null;   // store remainder
 16.1416 +        MutableBigInteger mdivisor = null;
 16.1417 +        boolean isLongDivision = (ldividend != INFLATED && ldivisor != INFLATED);
 16.1418 +        if (isLongDivision) {
 16.1419 +            q = ldividend / ldivisor;
 16.1420 +            if (roundingMode == ROUND_DOWN && scale == preferredScale)
 16.1421 +                return new BigDecimal(null, q, scale, 0);
 16.1422 +            r = ldividend % ldivisor;
 16.1423 +            isRemainderZero = (r == 0);
 16.1424 +            qsign = ((ldividend < 0) == (ldivisor < 0)) ? 1 : -1;
 16.1425 +        } else {
 16.1426 +            if (bdividend == null)
 16.1427 +                bdividend = BigInteger.valueOf(ldividend);
 16.1428 +            // Descend into mutables for faster remainder checks
 16.1429 +            MutableBigInteger mdividend = new MutableBigInteger(bdividend.mag);
 16.1430 +            mq = new MutableBigInteger();
 16.1431 +            if (ldivisor != INFLATED) {
 16.1432 +                r = mdividend.divide(ldivisor, mq);
 16.1433 +                isRemainderZero = (r == 0);
 16.1434 +                qsign = (ldivisor < 0) ? -bdividend.signum : bdividend.signum;
 16.1435 +            } else {
 16.1436 +                mdivisor = new MutableBigInteger(bdivisor.mag);
 16.1437 +                mr = mdividend.divide(mdivisor, mq);
 16.1438 +                isRemainderZero = mr.isZero();
 16.1439 +                qsign = (bdividend.signum != bdivisor.signum) ? -1 : 1;
 16.1440 +            }
 16.1441 +        }
 16.1442 +        boolean increment = false;
 16.1443 +        if (!isRemainderZero) {
 16.1444 +            int cmpFracHalf;
 16.1445 +            /* Round as appropriate */
 16.1446 +            if (roundingMode == ROUND_UNNECESSARY) {  // Rounding prohibited
 16.1447 +                throw new ArithmeticException("Rounding necessary");
 16.1448 +            } else if (roundingMode == ROUND_UP) {      // Away from zero
 16.1449 +                increment = true;
 16.1450 +            } else if (roundingMode == ROUND_DOWN) {    // Towards zero
 16.1451 +                increment = false;
 16.1452 +            } else if (roundingMode == ROUND_CEILING) { // Towards +infinity
 16.1453 +                increment = (qsign > 0);
 16.1454 +            } else if (roundingMode == ROUND_FLOOR) {   // Towards -infinity
 16.1455 +                increment = (qsign < 0);
 16.1456 +            } else {
 16.1457 +                if (isLongDivision || ldivisor != INFLATED) {
 16.1458 +                    if (r <= HALF_LONG_MIN_VALUE || r > HALF_LONG_MAX_VALUE) {
 16.1459 +                        cmpFracHalf = 1;    // 2 * r can't fit into long
 16.1460 +                    } else {
 16.1461 +                        cmpFracHalf = longCompareMagnitude(2 * r, ldivisor);
 16.1462 +                    }
 16.1463 +                } else {
 16.1464 +                    cmpFracHalf = mr.compareHalf(mdivisor);
 16.1465 +                }
 16.1466 +                if (cmpFracHalf < 0)
 16.1467 +                    increment = false;     // We're closer to higher digit
 16.1468 +                else if (cmpFracHalf > 0)  // We're closer to lower digit
 16.1469 +                    increment = true;
 16.1470 +                else if (roundingMode == ROUND_HALF_UP)
 16.1471 +                    increment = true;
 16.1472 +                else if (roundingMode == ROUND_HALF_DOWN)
 16.1473 +                    increment = false;
 16.1474 +                else  // roundingMode == ROUND_HALF_EVEN, true iff quotient is odd
 16.1475 +                    increment = isLongDivision ? (q & 1L) != 0L : mq.isOdd();
 16.1476 +            }
 16.1477 +        }
 16.1478 +        BigDecimal res;
 16.1479 +        if (isLongDivision)
 16.1480 +            res = new BigDecimal(null, (increment ? q + qsign : q), scale, 0);
 16.1481 +        else {
 16.1482 +            if (increment)
 16.1483 +                mq.add(MutableBigInteger.ONE);
 16.1484 +            res = mq.toBigDecimal(qsign, scale);
 16.1485 +        }
 16.1486 +        if (isRemainderZero && preferredScale != scale)
 16.1487 +            res.stripZerosToMatchScale(preferredScale);
 16.1488 +        return res;
 16.1489 +    }
 16.1490 +
 16.1491 +    /**
 16.1492 +     * Returns a {@code BigDecimal} whose value is {@code (this /
 16.1493 +     * divisor)}, and whose scale is as specified.  If rounding must
 16.1494 +     * be performed to generate a result with the specified scale, the
 16.1495 +     * specified rounding mode is applied.
 16.1496 +     *
 16.1497 +     * @param  divisor value by which this {@code BigDecimal} is to be divided.
 16.1498 +     * @param  scale scale of the {@code BigDecimal} quotient to be returned.
 16.1499 +     * @param  roundingMode rounding mode to apply.
 16.1500 +     * @return {@code this / divisor}
 16.1501 +     * @throws ArithmeticException if {@code divisor} is zero,
 16.1502 +     *         {@code roundingMode==RoundingMode.UNNECESSARY} and
 16.1503 +     *         the specified scale is insufficient to represent the result
 16.1504 +     *         of the division exactly.
 16.1505 +     * @since 1.5
 16.1506 +     */
 16.1507 +    public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) {
 16.1508 +        return divide(divisor, scale, roundingMode.oldMode);
 16.1509 +    }
 16.1510 +
 16.1511 +    /**
 16.1512 +     * Returns a {@code BigDecimal} whose value is {@code (this /
 16.1513 +     * divisor)}, and whose scale is {@code this.scale()}.  If
 16.1514 +     * rounding must be performed to generate a result with the given
 16.1515 +     * scale, the specified rounding mode is applied.
 16.1516 +     *
 16.1517 +     * <p>The new {@link #divide(BigDecimal, RoundingMode)} method
 16.1518 +     * should be used in preference to this legacy method.
 16.1519 +     *
 16.1520 +     * @param  divisor value by which this {@code BigDecimal} is to be divided.
 16.1521 +     * @param  roundingMode rounding mode to apply.
 16.1522 +     * @return {@code this / divisor}
 16.1523 +     * @throws ArithmeticException if {@code divisor==0}, or
 16.1524 +     *         {@code roundingMode==ROUND_UNNECESSARY} and
 16.1525 +     *         {@code this.scale()} is insufficient to represent the result
 16.1526 +     *         of the division exactly.
 16.1527 +     * @throws IllegalArgumentException if {@code roundingMode} does not
 16.1528 +     *         represent a valid rounding mode.
 16.1529 +     * @see    #ROUND_UP
 16.1530 +     * @see    #ROUND_DOWN
 16.1531 +     * @see    #ROUND_CEILING
 16.1532 +     * @see    #ROUND_FLOOR
 16.1533 +     * @see    #ROUND_HALF_UP
 16.1534 +     * @see    #ROUND_HALF_DOWN
 16.1535 +     * @see    #ROUND_HALF_EVEN
 16.1536 +     * @see    #ROUND_UNNECESSARY
 16.1537 +     */
 16.1538 +    public BigDecimal divide(BigDecimal divisor, int roundingMode) {
 16.1539 +            return this.divide(divisor, scale, roundingMode);
 16.1540 +    }
 16.1541 +
 16.1542 +    /**
 16.1543 +     * Returns a {@code BigDecimal} whose value is {@code (this /
 16.1544 +     * divisor)}, and whose scale is {@code this.scale()}.  If
 16.1545 +     * rounding must be performed to generate a result with the given
 16.1546 +     * scale, the specified rounding mode is applied.
 16.1547 +     *
 16.1548 +     * @param  divisor value by which this {@code BigDecimal} is to be divided.
 16.1549 +     * @param  roundingMode rounding mode to apply.
 16.1550 +     * @return {@code this / divisor}
 16.1551 +     * @throws ArithmeticException if {@code divisor==0}, or
 16.1552 +     *         {@code roundingMode==RoundingMode.UNNECESSARY} and
 16.1553 +     *         {@code this.scale()} is insufficient to represent the result
 16.1554 +     *         of the division exactly.
 16.1555 +     * @since 1.5
 16.1556 +     */
 16.1557 +    public BigDecimal divide(BigDecimal divisor, RoundingMode roundingMode) {
 16.1558 +        return this.divide(divisor, scale, roundingMode.oldMode);
 16.1559 +    }
 16.1560 +
 16.1561 +    /**
 16.1562 +     * Returns a {@code BigDecimal} whose value is {@code (this /
 16.1563 +     * divisor)}, and whose preferred scale is {@code (this.scale() -
 16.1564 +     * divisor.scale())}; if the exact quotient cannot be
 16.1565 +     * represented (because it has a non-terminating decimal
 16.1566 +     * expansion) an {@code ArithmeticException} is thrown.
 16.1567 +     *
 16.1568 +     * @param  divisor value by which this {@code BigDecimal} is to be divided.
 16.1569 +     * @throws ArithmeticException if the exact quotient does not have a
 16.1570 +     *         terminating decimal expansion
 16.1571 +     * @return {@code this / divisor}
 16.1572 +     * @since 1.5
 16.1573 +     * @author Joseph D. Darcy
 16.1574 +     */
 16.1575 +    public BigDecimal divide(BigDecimal divisor) {
 16.1576 +        /*
 16.1577 +         * Handle zero cases first.
 16.1578 +         */
 16.1579 +        if (divisor.signum() == 0) {   // x/0
 16.1580 +            if (this.signum() == 0)    // 0/0
 16.1581 +                throw new ArithmeticException("Division undefined");  // NaN
 16.1582 +            throw new ArithmeticException("Division by zero");
 16.1583 +        }
 16.1584 +
 16.1585 +        // Calculate preferred scale
 16.1586 +        int preferredScale = saturateLong((long)this.scale - divisor.scale);
 16.1587 +        if (this.signum() == 0)        // 0/y
 16.1588 +            return (preferredScale >= 0 &&
 16.1589 +                    preferredScale < ZERO_SCALED_BY.length) ?
 16.1590 +                ZERO_SCALED_BY[preferredScale] :
 16.1591 +                BigDecimal.valueOf(0, preferredScale);
 16.1592 +        else {
 16.1593 +            this.inflate();
 16.1594 +            divisor.inflate();
 16.1595 +            /*
 16.1596 +             * If the quotient this/divisor has a terminating decimal
 16.1597 +             * expansion, the expansion can have no more than
 16.1598 +             * (a.precision() + ceil(10*b.precision)/3) digits.
 16.1599 +             * Therefore, create a MathContext object with this
 16.1600 +             * precision and do a divide with the UNNECESSARY rounding
 16.1601 +             * mode.
 16.1602 +             */
 16.1603 +            MathContext mc = new MathContext( (int)Math.min(this.precision() +
 16.1604 +                                                            (long)Math.ceil(10.0*divisor.precision()/3.0),
 16.1605 +                                                            Integer.MAX_VALUE),
 16.1606 +                                              RoundingMode.UNNECESSARY);
 16.1607 +            BigDecimal quotient;
 16.1608 +            try {
 16.1609 +                quotient = this.divide(divisor, mc);
 16.1610 +            } catch (ArithmeticException e) {
 16.1611 +                throw new ArithmeticException("Non-terminating decimal expansion; " +
 16.1612 +                                              "no exact representable decimal result.");
 16.1613 +            }
 16.1614 +
 16.1615 +            int quotientScale = quotient.scale();
 16.1616 +
 16.1617 +            // divide(BigDecimal, mc) tries to adjust the quotient to
 16.1618 +            // the desired one by removing trailing zeros; since the
 16.1619 +            // exact divide method does not have an explicit digit
 16.1620 +            // limit, we can add zeros too.
 16.1621 +
 16.1622 +            if (preferredScale > quotientScale)
 16.1623 +                return quotient.setScale(preferredScale, ROUND_UNNECESSARY);
 16.1624 +
 16.1625 +            return quotient;
 16.1626 +        }
 16.1627 +    }
 16.1628 +
 16.1629 +    /**
 16.1630 +     * Returns a {@code BigDecimal} whose value is {@code (this /
 16.1631 +     * divisor)}, with rounding according to the context settings.
 16.1632 +     *
 16.1633 +     * @param  divisor value by which this {@code BigDecimal} is to be divided.
 16.1634 +     * @param  mc the context to use.
 16.1635 +     * @return {@code this / divisor}, rounded as necessary.
 16.1636 +     * @throws ArithmeticException if the result is inexact but the
 16.1637 +     *         rounding mode is {@code UNNECESSARY} or
 16.1638 +     *         {@code mc.precision == 0} and the quotient has a
 16.1639 +     *         non-terminating decimal expansion.
 16.1640 +     * @since  1.5
 16.1641 +     */
 16.1642 +    public BigDecimal divide(BigDecimal divisor, MathContext mc) {
 16.1643 +        int mcp = mc.precision;
 16.1644 +        if (mcp == 0)
 16.1645 +            return divide(divisor);
 16.1646 +
 16.1647 +        BigDecimal dividend = this;
 16.1648 +        long preferredScale = (long)dividend.scale - divisor.scale;
 16.1649 +        // Now calculate the answer.  We use the existing
 16.1650 +        // divide-and-round method, but as this rounds to scale we have
 16.1651 +        // to normalize the values here to achieve the desired result.
 16.1652 +        // For x/y we first handle y=0 and x=0, and then normalize x and
 16.1653 +        // y to give x' and y' with the following constraints:
 16.1654 +        //   (a) 0.1 <= x' < 1
 16.1655 +        //   (b)  x' <= y' < 10*x'
 16.1656 +        // Dividing x'/y' with the required scale set to mc.precision then
 16.1657 +        // will give a result in the range 0.1 to 1 rounded to exactly
 16.1658 +        // the right number of digits (except in the case of a result of
 16.1659 +        // 1.000... which can arise when x=y, or when rounding overflows
 16.1660 +        // The 1.000... case will reduce properly to 1.
 16.1661 +        if (divisor.signum() == 0) {      // x/0
 16.1662 +            if (dividend.signum() == 0)    // 0/0
 16.1663 +                throw new ArithmeticException("Division undefined");  // NaN
 16.1664 +            throw new ArithmeticException("Division by zero");
 16.1665 +        }
 16.1666 +        if (dividend.signum() == 0)        // 0/y
 16.1667 +            return new BigDecimal(BigInteger.ZERO, 0,
 16.1668 +                                  saturateLong(preferredScale), 1);
 16.1669 +
 16.1670 +        // Normalize dividend & divisor so that both fall into [0.1, 0.999...]
 16.1671 +        int xscale = dividend.precision();
 16.1672 +        int yscale = divisor.precision();
 16.1673 +        dividend = new BigDecimal(dividend.intVal, dividend.intCompact,
 16.1674 +                                  xscale, xscale);
 16.1675 +        divisor = new BigDecimal(divisor.intVal, divisor.intCompact,
 16.1676 +                                 yscale, yscale);
 16.1677 +        if (dividend.compareMagnitude(divisor) > 0) // satisfy constraint (b)
 16.1678 +            yscale = divisor.scale -= 1;            // [that is, divisor *= 10]
 16.1679 +
 16.1680 +        // In order to find out whether the divide generates the exact result,
 16.1681 +        // we avoid calling the above divide method. 'quotient' holds the
 16.1682 +        // return BigDecimal object whose scale will be set to 'scl'.
 16.1683 +        BigDecimal quotient;
 16.1684 +        int scl = checkScale(preferredScale + yscale - xscale + mcp);
 16.1685 +        if (checkScale((long)mcp + yscale) > xscale)
 16.1686 +            dividend = dividend.setScale(mcp + yscale, ROUND_UNNECESSARY);
 16.1687 +        else
 16.1688 +            divisor = divisor.setScale(checkScale((long)xscale - mcp),
 16.1689 +                                       ROUND_UNNECESSARY);
 16.1690 +        quotient = divideAndRound(dividend.intCompact, dividend.intVal,
 16.1691 +                                  divisor.intCompact, divisor.intVal,
 16.1692 +                                  scl, mc.roundingMode.oldMode,
 16.1693 +                                  checkScale(preferredScale));
 16.1694 +        // doRound, here, only affects 1000000000 case.
 16.1695 +        quotient = doRound(quotient, mc);
 16.1696 +
 16.1697 +        return quotient;
 16.1698 +    }
 16.1699 +
 16.1700 +    /**
 16.1701 +     * Returns a {@code BigDecimal} whose value is the integer part
 16.1702 +     * of the quotient {@code (this / divisor)} rounded down.  The
 16.1703 +     * preferred scale of the result is {@code (this.scale() -
 16.1704 +     * divisor.scale())}.
 16.1705 +     *
 16.1706 +     * @param  divisor value by which this {@code BigDecimal} is to be divided.
 16.1707 +     * @return The integer part of {@code this / divisor}.
 16.1708 +     * @throws ArithmeticException if {@code divisor==0}
 16.1709 +     * @since  1.5
 16.1710 +     */
 16.1711 +    public BigDecimal divideToIntegralValue(BigDecimal divisor) {
 16.1712 +        // Calculate preferred scale
 16.1713 +        int preferredScale = saturateLong((long)this.scale - divisor.scale);
 16.1714 +        if (this.compareMagnitude(divisor) < 0) {
 16.1715 +            // much faster when this << divisor
 16.1716 +            return BigDecimal.valueOf(0, preferredScale);
 16.1717 +        }
 16.1718 +
 16.1719 +        if(this.signum() == 0 && divisor.signum() != 0)
 16.1720 +            return this.setScale(preferredScale, ROUND_UNNECESSARY);
 16.1721 +
 16.1722 +        // Perform a divide with enough digits to round to a correct
 16.1723 +        // integer value; then remove any fractional digits
 16.1724 +
 16.1725 +        int maxDigits = (int)Math.min(this.precision() +
 16.1726 +                                      (long)Math.ceil(10.0*divisor.precision()/3.0) +
 16.1727 +                                      Math.abs((long)this.scale() - divisor.scale()) + 2,
 16.1728 +                                      Integer.MAX_VALUE);
 16.1729 +        BigDecimal quotient = this.divide(divisor, new MathContext(maxDigits,
 16.1730 +                                                                   RoundingMode.DOWN));
 16.1731 +        if (quotient.scale > 0) {
 16.1732 +            quotient = quotient.setScale(0, RoundingMode.DOWN);
 16.1733 +            quotient.stripZerosToMatchScale(preferredScale);
 16.1734 +        }
 16.1735 +
 16.1736 +        if (quotient.scale < preferredScale) {
 16.1737 +            // pad with zeros if necessary
 16.1738 +            quotient = quotient.setScale(preferredScale, ROUND_UNNECESSARY);
 16.1739 +        }
 16.1740 +        return quotient;
 16.1741 +    }
 16.1742 +
 16.1743 +    /**
 16.1744 +     * Returns a {@code BigDecimal} whose value is the integer part
 16.1745 +     * of {@code (this / divisor)}.  Since the integer part of the
 16.1746 +     * exact quotient does not depend on the rounding mode, the
 16.1747 +     * rounding mode does not affect the values returned by this
 16.1748 +     * method.  The preferred scale of the result is
 16.1749 +     * {@code (this.scale() - divisor.scale())}.  An
 16.1750 +     * {@code ArithmeticException} is thrown if the integer part of
 16.1751 +     * the exact quotient needs more than {@code mc.precision}
 16.1752 +     * digits.
 16.1753 +     *
 16.1754 +     * @param  divisor value by which this {@code BigDecimal} is to be divided.
 16.1755 +     * @param  mc the context to use.
 16.1756 +     * @return The integer part of {@code this / divisor}.
 16.1757 +     * @throws ArithmeticException if {@code divisor==0}
 16.1758 +     * @throws ArithmeticException if {@code mc.precision} {@literal >} 0 and the result
 16.1759 +     *         requires a precision of more than {@code mc.precision} digits.
 16.1760 +     * @since  1.5
 16.1761 +     * @author Joseph D. Darcy
 16.1762 +     */
 16.1763 +    public BigDecimal divideToIntegralValue(BigDecimal divisor, MathContext mc) {
 16.1764 +        if (mc.precision == 0 ||                        // exact result
 16.1765 +            (this.compareMagnitude(divisor) < 0) )      // zero result
 16.1766 +            return divideToIntegralValue(divisor);
 16.1767 +
 16.1768 +        // Calculate preferred scale
 16.1769 +        int preferredScale = saturateLong((long)this.scale - divisor.scale);
 16.1770 +
 16.1771 +        /*
 16.1772 +         * Perform a normal divide to mc.precision digits.  If the
 16.1773 +         * remainder has absolute value less than the divisor, the
 16.1774 +         * integer portion of the quotient fits into mc.precision
 16.1775 +         * digits.  Next, remove any fractional digits from the
 16.1776 +         * quotient and adjust the scale to the preferred value.
 16.1777 +         */
 16.1778 +        BigDecimal result = this.
 16.1779 +            divide(divisor, new MathContext(mc.precision, RoundingMode.DOWN));
 16.1780 +
 16.1781 +        if (result.scale() < 0) {
 16.1782 +            /*
 16.1783 +             * Result is an integer. See if quotient represents the
 16.1784 +             * full integer portion of the exact quotient; if it does,
 16.1785 +             * the computed remainder will be less than the divisor.
 16.1786 +             */
 16.1787 +            BigDecimal product = result.multiply(divisor);
 16.1788 +            // If the quotient is the full integer value,
 16.1789 +            // |dividend-product| < |divisor|.
 16.1790 +            if (this.subtract(product).compareMagnitude(divisor) >= 0) {
 16.1791 +                throw new ArithmeticException("Division impossible");
 16.1792 +            }
 16.1793 +        } else if (result.scale() > 0) {
 16.1794 +            /*
 16.1795 +             * Integer portion of quotient will fit into precision
 16.1796 +             * digits; recompute quotient to scale 0 to avoid double
 16.1797 +             * rounding and then try to adjust, if necessary.
 16.1798 +             */
 16.1799 +            result = result.setScale(0, RoundingMode.DOWN);
 16.1800 +        }
 16.1801 +        // else result.scale() == 0;
 16.1802 +
 16.1803 +        int precisionDiff;
 16.1804 +        if ((preferredScale > result.scale()) &&
 16.1805 +            (precisionDiff = mc.precision - result.precision()) > 0) {
 16.1806 +            return result.setScale(result.scale() +
 16.1807 +                                   Math.min(precisionDiff, preferredScale - result.scale) );
 16.1808 +        } else {
 16.1809 +            result.stripZerosToMatchScale(preferredScale);
 16.1810 +            return result;
 16.1811 +        }
 16.1812 +    }
 16.1813 +
 16.1814 +    /**
 16.1815 +     * Returns a {@code BigDecimal} whose value is {@code (this % divisor)}.
 16.1816 +     *
 16.1817 +     * <p>The remainder is given by
 16.1818 +     * {@code this.subtract(this.divideToIntegralValue(divisor).multiply(divisor))}.
 16.1819 +     * Note that this is not the modulo operation (the result can be
 16.1820 +     * negative).
 16.1821 +     *
 16.1822 +     * @param  divisor value by which this {@code BigDecimal} is to be divided.
 16.1823 +     * @return {@code this % divisor}.
 16.1824 +     * @throws ArithmeticException if {@code divisor==0}
 16.1825 +     * @since  1.5
 16.1826 +     */
 16.1827 +    public BigDecimal remainder(BigDecimal divisor) {
 16.1828 +        BigDecimal divrem[] = this.divideAndRemainder(divisor);
 16.1829 +        return divrem[1];
 16.1830 +    }
 16.1831 +
 16.1832 +
 16.1833 +    /**
 16.1834 +     * Returns a {@code BigDecimal} whose value is {@code (this %
 16.1835 +     * divisor)}, with rounding according to the context settings.
 16.1836 +     * The {@code MathContext} settings affect the implicit divide
 16.1837 +     * used to compute the remainder.  The remainder computation
 16.1838 +     * itself is by definition exact.  Therefore, the remainder may
 16.1839 +     * contain more than {@code mc.getPrecision()} digits.
 16.1840 +     *
 16.1841 +     * <p>The remainder is given by
 16.1842 +     * {@code this.subtract(this.divideToIntegralValue(divisor,
 16.1843 +     * mc).multiply(divisor))}.  Note that this is not the modulo
 16.1844 +     * operation (the result can be negative).
 16.1845 +     *
 16.1846 +     * @param  divisor value by which this {@code BigDecimal} is to be divided.
 16.1847 +     * @param  mc the context to use.
 16.1848 +     * @return {@code this % divisor}, rounded as necessary.
 16.1849 +     * @throws ArithmeticException if {@code divisor==0}
 16.1850 +     * @throws ArithmeticException if the result is inexact but the
 16.1851 +     *         rounding mode is {@code UNNECESSARY}, or {@code mc.precision}
 16.1852 +     *         {@literal >} 0 and the result of {@code this.divideToIntgralValue(divisor)} would
 16.1853 +     *         require a precision of more than {@code mc.precision} digits.
 16.1854 +     * @see    #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
 16.1855 +     * @since  1.5
 16.1856 +     */
 16.1857 +    public BigDecimal remainder(BigDecimal divisor, MathContext mc) {
 16.1858 +        BigDecimal divrem[] = this.divideAndRemainder(divisor, mc);
 16.1859 +        return divrem[1];
 16.1860 +    }
 16.1861 +
 16.1862 +    /**
 16.1863 +     * Returns a two-element {@code BigDecimal} array containing the
 16.1864 +     * result of {@code divideToIntegralValue} followed by the result of
 16.1865 +     * {@code remainder} on the two operands.
 16.1866 +     *
 16.1867 +     * <p>Note that if both the integer quotient and remainder are
 16.1868 +     * needed, this method is faster than using the
 16.1869 +     * {@code divideToIntegralValue} and {@code remainder} methods
 16.1870 +     * separately because the division need only be carried out once.
 16.1871 +     *
 16.1872 +     * @param  divisor value by which this {@code BigDecimal} is to be divided,
 16.1873 +     *         and the remainder computed.
 16.1874 +     * @return a two element {@code BigDecimal} array: the quotient
 16.1875 +     *         (the result of {@code divideToIntegralValue}) is the initial element
 16.1876 +     *         and the remainder is the final element.
 16.1877 +     * @throws ArithmeticException if {@code divisor==0}
 16.1878 +     * @see    #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
 16.1879 +     * @see    #remainder(java.math.BigDecimal, java.math.MathContext)
 16.1880 +     * @since  1.5
 16.1881 +     */
 16.1882 +    public BigDecimal[] divideAndRemainder(BigDecimal divisor) {
 16.1883 +        // we use the identity  x = i * y + r to determine r
 16.1884 +        BigDecimal[] result = new BigDecimal[2];
 16.1885 +
 16.1886 +        result[0] = this.divideToIntegralValue(divisor);
 16.1887 +        result[1] = this.subtract(result[0].multiply(divisor));
 16.1888 +        return result;
 16.1889 +    }
 16.1890 +
 16.1891 +    /**
 16.1892 +     * Returns a two-element {@code BigDecimal} array containing the
 16.1893 +     * result of {@code divideToIntegralValue} followed by the result of
 16.1894 +     * {@code remainder} on the two operands calculated with rounding
 16.1895 +     * according to the context settings.
 16.1896 +     *
 16.1897 +     * <p>Note that if both the integer quotient and remainder are
 16.1898 +     * needed, this method is faster than using the
 16.1899 +     * {@code divideToIntegralValue} and {@code remainder} methods
 16.1900 +     * separately because the division need only be carried out once.
 16.1901 +     *
 16.1902 +     * @param  divisor value by which this {@code BigDecimal} is to be divided,
 16.1903 +     *         and the remainder computed.
 16.1904 +     * @param  mc the context to use.
 16.1905 +     * @return a two element {@code BigDecimal} array: the quotient
 16.1906 +     *         (the result of {@code divideToIntegralValue}) is the
 16.1907 +     *         initial element and the remainder is the final element.
 16.1908 +     * @throws ArithmeticException if {@code divisor==0}
 16.1909 +     * @throws ArithmeticException if the result is inexact but the
 16.1910 +     *         rounding mode is {@code UNNECESSARY}, or {@code mc.precision}
 16.1911 +     *         {@literal >} 0 and the result of {@code this.divideToIntgralValue(divisor)} would
 16.1912 +     *         require a precision of more than {@code mc.precision} digits.
 16.1913 +     * @see    #divideToIntegralValue(java.math.BigDecimal, java.math.MathContext)
 16.1914 +     * @see    #remainder(java.math.BigDecimal, java.math.MathContext)
 16.1915 +     * @since  1.5
 16.1916 +     */
 16.1917 +    public BigDecimal[] divideAndRemainder(BigDecimal divisor, MathContext mc) {
 16.1918 +        if (mc.precision == 0)
 16.1919 +            return divideAndRemainder(divisor);
 16.1920 +
 16.1921 +        BigDecimal[] result = new BigDecimal[2];
 16.1922 +        BigDecimal lhs = this;
 16.1923 +
 16.1924 +        result[0] = lhs.divideToIntegralValue(divisor, mc);
 16.1925 +        result[1] = lhs.subtract(result[0].multiply(divisor));
 16.1926 +        return result;
 16.1927 +    }
 16.1928 +
 16.1929 +    /**
 16.1930 +     * Returns a {@code BigDecimal} whose value is
 16.1931 +     * <tt>(this<sup>n</sup>)</tt>, The power is computed exactly, to
 16.1932 +     * unlimited precision.
 16.1933 +     *
 16.1934 +     * <p>The parameter {@code n} must be in the range 0 through
 16.1935 +     * 999999999, inclusive.  {@code ZERO.pow(0)} returns {@link
 16.1936 +     * #ONE}.
 16.1937 +     *
 16.1938 +     * Note that future releases may expand the allowable exponent
 16.1939 +     * range of this method.
 16.1940 +     *
 16.1941 +     * @param  n power to raise this {@code BigDecimal} to.
 16.1942 +     * @return <tt>this<sup>n</sup></tt>
 16.1943 +     * @throws ArithmeticException if {@code n} is out of range.
 16.1944 +     * @since  1.5
 16.1945 +     */
 16.1946 +    public BigDecimal pow(int n) {
 16.1947 +        if (n < 0 || n > 999999999)
 16.1948 +            throw new ArithmeticException("Invalid operation");
 16.1949 +        // No need to calculate pow(n) if result will over/underflow.
 16.1950 +        // Don't attempt to support "supernormal" numbers.
 16.1951 +        int newScale = checkScale((long)scale * n);
 16.1952 +        this.inflate();
 16.1953 +        return new BigDecimal(intVal.pow(n), newScale);
 16.1954 +    }
 16.1955 +
 16.1956 +
 16.1957 +    /**
 16.1958 +     * Returns a {@code BigDecimal} whose value is
 16.1959 +     * <tt>(this<sup>n</sup>)</tt>.  The current implementation uses
 16.1960 +     * the core algorithm defined in ANSI standard X3.274-1996 with
 16.1961 +     * rounding according to the context settings.  In general, the
 16.1962 +     * returned numerical value is within two ulps of the exact
 16.1963 +     * numerical value for the chosen precision.  Note that future
 16.1964 +     * releases may use a different algorithm with a decreased
 16.1965 +     * allowable error bound and increased allowable exponent range.
 16.1966 +     *
 16.1967 +     * <p>The X3.274-1996 algorithm is:
 16.1968 +     *
 16.1969 +     * <ul>
 16.1970 +     * <li> An {@code ArithmeticException} exception is thrown if
 16.1971 +     *  <ul>
 16.1972 +     *    <li>{@code abs(n) > 999999999}
 16.1973 +     *    <li>{@code mc.precision == 0} and {@code n < 0}
 16.1974 +     *    <li>{@code mc.precision > 0} and {@code n} has more than
 16.1975 +     *    {@code mc.precision} decimal digits
 16.1976 +     *  </ul>
 16.1977 +     *
 16.1978 +     * <li> if {@code n} is zero, {@link #ONE} is returned even if
 16.1979 +     * {@code this} is zero, otherwise
 16.1980 +     * <ul>
 16.1981 +     *   <li> if {@code n} is positive, the result is calculated via
 16.1982 +     *   the repeated squaring technique into a single accumulator.
 16.1983 +     *   The individual multiplications with the accumulator use the
 16.1984 +     *   same math context settings as in {@code mc} except for a
 16.1985 +     *   precision increased to {@code mc.precision + elength + 1}
 16.1986 +     *   where {@code elength} is the number of decimal digits in
 16.1987 +     *   {@code n}.
 16.1988 +     *
 16.1989 +     *   <li> if {@code n} is negative, the result is calculated as if
 16.1990 +     *   {@code n} were positive; this value is then divided into one
 16.1991 +     *   using the working precision specified above.
 16.1992 +     *
 16.1993 +     *   <li> The final value from either the positive or negative case
 16.1994 +     *   is then rounded to the destination precision.
 16.1995 +     *   </ul>
 16.1996 +     * </ul>
 16.1997 +     *
 16.1998 +     * @param  n power to raise this {@code BigDecimal} to.
 16.1999 +     * @param  mc the context to use.
 16.2000 +     * @return <tt>this<sup>n</sup></tt> using the ANSI standard X3.274-1996
 16.2001 +     *         algorithm
 16.2002 +     * @throws ArithmeticException if the result is inexact but the
 16.2003 +     *         rounding mode is {@code UNNECESSARY}, or {@code n} is out
 16.2004 +     *         of range.
 16.2005 +     * @since  1.5
 16.2006 +     */
 16.2007 +    public BigDecimal pow(int n, MathContext mc) {
 16.2008 +        if (mc.precision == 0)
 16.2009 +            return pow(n);
 16.2010 +        if (n < -999999999 || n > 999999999)
 16.2011 +            throw new ArithmeticException("Invalid operation");
 16.2012 +        if (n == 0)
 16.2013 +            return ONE;                      // x**0 == 1 in X3.274
 16.2014 +        this.inflate();
 16.2015 +        BigDecimal lhs = this;
 16.2016 +        MathContext workmc = mc;           // working settings
 16.2017 +        int mag = Math.abs(n);               // magnitude of n
 16.2018 +        if (mc.precision > 0) {
 16.2019 +
 16.2020 +            int elength = longDigitLength(mag); // length of n in digits
 16.2021 +            if (elength > mc.precision)        // X3.274 rule
 16.2022 +                throw new ArithmeticException("Invalid operation");
 16.2023 +            workmc = new MathContext(mc.precision + elength + 1,
 16.2024 +                                      mc.roundingMode);
 16.2025 +        }
 16.2026 +        // ready to carry out power calculation...
 16.2027 +        BigDecimal acc = ONE;           // accumulator
 16.2028 +        boolean seenbit = false;        // set once we've seen a 1-bit
 16.2029 +        for (int i=1;;i++) {            // for each bit [top bit ignored]
 16.2030 +            mag += mag;                 // shift left 1 bit
 16.2031 +            if (mag < 0) {              // top bit is set
 16.2032 +                seenbit = true;         // OK, we're off
 16.2033 +                acc = acc.multiply(lhs, workmc); // acc=acc*x
 16.2034 +            }
 16.2035 +            if (i == 31)
 16.2036 +                break;                  // that was the last bit
 16.2037 +            if (seenbit)
 16.2038 +                acc=acc.multiply(acc, workmc);   // acc=acc*acc [square]
 16.2039 +                // else (!seenbit) no point in squaring ONE
 16.2040 +        }
 16.2041 +        // if negative n, calculate the reciprocal using working precision
 16.2042 +        if (n<0)                          // [hence mc.precision>0]
 16.2043 +            acc=ONE.divide(acc, workmc);
 16.2044 +        // round to final precision and strip zeros
 16.2045 +        return doRound(acc, mc);
 16.2046 +    }
 16.2047 +
 16.2048 +    /**
 16.2049 +     * Returns a {@code BigDecimal} whose value is the absolute value
 16.2050 +     * of this {@code BigDecimal}, and whose scale is
 16.2051 +     * {@code this.scale()}.
 16.2052 +     *
 16.2053 +     * @return {@code abs(this)}
 16.2054 +     */
 16.2055 +    public BigDecimal abs() {
 16.2056 +        return (signum() < 0 ? negate() : this);
 16.2057 +    }
 16.2058 +
 16.2059 +    /**
 16.2060 +     * Returns a {@code BigDecimal} whose value is the absolute value
 16.2061 +     * of this {@code BigDecimal}, with rounding according to the
 16.2062 +     * context settings.
 16.2063 +     *
 16.2064 +     * @param mc the context to use.
 16.2065 +     * @return {@code abs(this)}, rounded as necessary.
 16.2066 +     * @throws ArithmeticException if the result is inexact but the
 16.2067 +     *         rounding mode is {@code UNNECESSARY}.
 16.2068 +     * @since 1.5
 16.2069 +     */
 16.2070 +    public BigDecimal abs(MathContext mc) {
 16.2071 +        return (signum() < 0 ? negate(mc) : plus(mc));
 16.2072 +    }
 16.2073 +
 16.2074 +    /**
 16.2075 +     * Returns a {@code BigDecimal} whose value is {@code (-this)},
 16.2076 +     * and whose scale is {@code this.scale()}.
 16.2077 +     *
 16.2078 +     * @return {@code -this}.
 16.2079 +     */
 16.2080 +    public BigDecimal negate() {
 16.2081 +        BigDecimal result;
 16.2082 +        if (intCompact != INFLATED)
 16.2083 +            result = BigDecimal.valueOf(-intCompact, scale);
 16.2084 +        else {
 16.2085 +            result = new BigDecimal(intVal.negate(), scale);
 16.2086 +            result.precision = precision;
 16.2087 +        }
 16.2088 +        return result;
 16.2089 +    }
 16.2090 +
 16.2091 +    /**
 16.2092 +     * Returns a {@code BigDecimal} whose value is {@code (-this)},
 16.2093 +     * with rounding according to the context settings.
 16.2094 +     *
 16.2095 +     * @param mc the context to use.
 16.2096 +     * @return {@code -this}, rounded as necessary.
 16.2097 +     * @throws ArithmeticException if the result is inexact but the
 16.2098 +     *         rounding mode is {@code UNNECESSARY}.
 16.2099 +     * @since  1.5
 16.2100 +     */
 16.2101 +    public BigDecimal negate(MathContext mc) {
 16.2102 +        return negate().plus(mc);
 16.2103 +    }
 16.2104 +
 16.2105 +    /**
 16.2106 +     * Returns a {@code BigDecimal} whose value is {@code (+this)}, and whose
 16.2107 +     * scale is {@code this.scale()}.
 16.2108 +     *
 16.2109 +     * <p>This method, which simply returns this {@code BigDecimal}
 16.2110 +     * is included for symmetry with the unary minus method {@link
 16.2111 +     * #negate()}.
 16.2112 +     *
 16.2113 +     * @return {@code this}.
 16.2114 +     * @see #negate()
 16.2115 +     * @since  1.5
 16.2116 +     */
 16.2117 +    public BigDecimal plus() {
 16.2118 +        return this;
 16.2119 +    }
 16.2120 +
 16.2121 +    /**
 16.2122 +     * Returns a {@code BigDecimal} whose value is {@code (+this)},
 16.2123 +     * with rounding according to the context settings.
 16.2124 +     *
 16.2125 +     * <p>The effect of this method is identical to that of the {@link
 16.2126 +     * #round(MathContext)} method.
 16.2127 +     *
 16.2128 +     * @param mc the context to use.
 16.2129 +     * @return {@code this}, rounded as necessary.  A zero result will
 16.2130 +     *         have a scale of 0.
 16.2131 +     * @throws ArithmeticException if the result is inexact but the
 16.2132 +     *         rounding mode is {@code UNNECESSARY}.
 16.2133 +     * @see    #round(MathContext)
 16.2134 +     * @since  1.5
 16.2135 +     */
 16.2136 +    public BigDecimal plus(MathContext mc) {
 16.2137 +        if (mc.precision == 0)                 // no rounding please
 16.2138 +            return this;
 16.2139 +        return doRound(this, mc);
 16.2140 +    }
 16.2141 +
 16.2142 +    /**
 16.2143 +     * Returns the signum function of this {@code BigDecimal}.
 16.2144 +     *
 16.2145 +     * @return -1, 0, or 1 as the value of this {@code BigDecimal}
 16.2146 +     *         is negative, zero, or positive.
 16.2147 +     */
 16.2148 +    public int signum() {
 16.2149 +        return (intCompact != INFLATED)?
 16.2150 +            Long.signum(intCompact):
 16.2151 +            intVal.signum();
 16.2152 +    }
 16.2153 +
 16.2154 +    /**
 16.2155 +     * Returns the <i>scale</i> of this {@code BigDecimal}.  If zero
 16.2156 +     * or positive, the scale is the number of digits to the right of
 16.2157 +     * the decimal point.  If negative, the unscaled value of the
 16.2158 +     * number is multiplied by ten to the power of the negation of the
 16.2159 +     * scale.  For example, a scale of {@code -3} means the unscaled
 16.2160 +     * value is multiplied by 1000.
 16.2161 +     *
 16.2162 +     * @return the scale of this {@code BigDecimal}.
 16.2163 +     */
 16.2164 +    public int scale() {
 16.2165 +        return scale;
 16.2166 +    }
 16.2167 +
 16.2168 +    /**
 16.2169 +     * Returns the <i>precision</i> of this {@code BigDecimal}.  (The
 16.2170 +     * precision is the number of digits in the unscaled value.)
 16.2171 +     *
 16.2172 +     * <p>The precision of a zero value is 1.
 16.2173 +     *
 16.2174 +     * @return the precision of this {@code BigDecimal}.
 16.2175 +     * @since  1.5
 16.2176 +     */
 16.2177 +    public int precision() {
 16.2178 +        int result = precision;
 16.2179 +        if (result == 0) {
 16.2180 +            long s = intCompact;
 16.2181 +            if (s != INFLATED)
 16.2182 +                result = longDigitLength(s);
 16.2183 +            else
 16.2184 +                result = bigDigitLength(inflate());
 16.2185 +            precision = result;
 16.2186 +        }
 16.2187 +        return result;
 16.2188 +    }
 16.2189 +
 16.2190 +
 16.2191 +    /**
 16.2192 +     * Returns a {@code BigInteger} whose value is the <i>unscaled
 16.2193 +     * value</i> of this {@code BigDecimal}.  (Computes <tt>(this *
 16.2194 +     * 10<sup>this.scale()</sup>)</tt>.)
 16.2195 +     *
 16.2196 +     * @return the unscaled value of this {@code BigDecimal}.
 16.2197 +     * @since  1.2
 16.2198 +     */
 16.2199 +    public BigInteger unscaledValue() {
 16.2200 +        return this.inflate();
 16.2201 +    }
 16.2202 +
 16.2203 +    // Rounding Modes
 16.2204 +
 16.2205 +    /**
 16.2206 +     * Rounding mode to round away from zero.  Always increments the
 16.2207 +     * digit prior to a nonzero discarded fraction.  Note that this rounding
 16.2208 +     * mode never decreases the magnitude of the calculated value.
 16.2209 +     */
 16.2210 +    public final static int ROUND_UP =           0;
 16.2211 +
 16.2212 +    /**
 16.2213 +     * Rounding mode to round towards zero.  Never increments the digit
 16.2214 +     * prior to a discarded fraction (i.e., truncates).  Note that this
 16.2215 +     * rounding mode never increases the magnitude of the calculated value.
 16.2216 +     */
 16.2217 +    public final static int ROUND_DOWN =         1;
 16.2218 +
 16.2219 +    /**
 16.2220 +     * Rounding mode to round towards positive infinity.  If the
 16.2221 +     * {@code BigDecimal} is positive, behaves as for
 16.2222 +     * {@code ROUND_UP}; if negative, behaves as for
 16.2223 +     * {@code ROUND_DOWN}.  Note that this rounding mode never
 16.2224 +     * decreases the calculated value.
 16.2225 +     */
 16.2226 +    public final static int ROUND_CEILING =      2;
 16.2227 +
 16.2228 +    /**
 16.2229 +     * Rounding mode to round towards negative infinity.  If the
 16.2230 +     * {@code BigDecimal} is positive, behave as for
 16.2231 +     * {@code ROUND_DOWN}; if negative, behave as for
 16.2232 +     * {@code ROUND_UP}.  Note that this rounding mode never
 16.2233 +     * increases the calculated value.
 16.2234 +     */
 16.2235 +    public final static int ROUND_FLOOR =        3;
 16.2236 +
 16.2237 +    /**
 16.2238 +     * Rounding mode to round towards {@literal "nearest neighbor"}
 16.2239 +     * unless both neighbors are equidistant, in which case round up.
 16.2240 +     * Behaves as for {@code ROUND_UP} if the discarded fraction is
 16.2241 +     * &ge; 0.5; otherwise, behaves as for {@code ROUND_DOWN}.  Note
 16.2242 +     * that this is the rounding mode that most of us were taught in
 16.2243 +     * grade school.
 16.2244 +     */
 16.2245 +    public final static int ROUND_HALF_UP =      4;
 16.2246 +
 16.2247 +    /**
 16.2248 +     * Rounding mode to round towards {@literal "nearest neighbor"}
 16.2249 +     * unless both neighbors are equidistant, in which case round
 16.2250 +     * down.  Behaves as for {@code ROUND_UP} if the discarded
 16.2251 +     * fraction is {@literal >} 0.5; otherwise, behaves as for
 16.2252 +     * {@code ROUND_DOWN}.
 16.2253 +     */
 16.2254 +    public final static int ROUND_HALF_DOWN =    5;
 16.2255 +
 16.2256 +    /**
 16.2257 +     * Rounding mode to round towards the {@literal "nearest neighbor"}
 16.2258 +     * unless both neighbors are equidistant, in which case, round
 16.2259 +     * towards the even neighbor.  Behaves as for
 16.2260 +     * {@code ROUND_HALF_UP} if the digit to the left of the
 16.2261 +     * discarded fraction is odd; behaves as for
 16.2262 +     * {@code ROUND_HALF_DOWN} if it's even.  Note that this is the
 16.2263 +     * rounding mode that minimizes cumulative error when applied
 16.2264 +     * repeatedly over a sequence of calculations.
 16.2265 +     */
 16.2266 +    public final static int ROUND_HALF_EVEN =    6;
 16.2267 +
 16.2268 +    /**
 16.2269 +     * Rounding mode to assert that the requested operation has an exact
 16.2270 +     * result, hence no rounding is necessary.  If this rounding mode is
 16.2271 +     * specified on an operation that yields an inexact result, an
 16.2272 +     * {@code ArithmeticException} is thrown.
 16.2273 +     */
 16.2274 +    public final static int ROUND_UNNECESSARY =  7;
 16.2275 +
 16.2276 +
 16.2277 +    // Scaling/Rounding Operations
 16.2278 +
 16.2279 +    /**
 16.2280 +     * Returns a {@code BigDecimal} rounded according to the
 16.2281 +     * {@code MathContext} settings.  If the precision setting is 0 then
 16.2282 +     * no rounding takes place.
 16.2283 +     *
 16.2284 +     * <p>The effect of this method is identical to that of the
 16.2285 +     * {@link #plus(MathContext)} method.
 16.2286 +     *
 16.2287 +     * @param mc the context to use.
 16.2288 +     * @return a {@code BigDecimal} rounded according to the
 16.2289 +     *         {@code MathContext} settings.
 16.2290 +     * @throws ArithmeticException if the rounding mode is
 16.2291 +     *         {@code UNNECESSARY} and the
 16.2292 +     *         {@code BigDecimal}  operation would require rounding.
 16.2293 +     * @see    #plus(MathContext)
 16.2294 +     * @since  1.5
 16.2295 +     */
 16.2296 +    public BigDecimal round(MathContext mc) {
 16.2297 +        return plus(mc);
 16.2298 +    }
 16.2299 +
 16.2300 +    /**
 16.2301 +     * Returns a {@code BigDecimal} whose scale is the specified
 16.2302 +     * value, and whose unscaled value is determined by multiplying or
 16.2303 +     * dividing this {@code BigDecimal}'s unscaled value by the
 16.2304 +     * appropriate power of ten to maintain its overall value.  If the
 16.2305 +     * scale is reduced by the operation, the unscaled value must be
 16.2306 +     * divided (rather than multiplied), and the value may be changed;
 16.2307 +     * in this case, the specified rounding mode is applied to the
 16.2308 +     * division.
 16.2309 +     *
 16.2310 +     * <p>Note that since BigDecimal objects are immutable, calls of
 16.2311 +     * this method do <i>not</i> result in the original object being
 16.2312 +     * modified, contrary to the usual convention of having methods
 16.2313 +     * named <tt>set<i>X</i></tt> mutate field <i>{@code X}</i>.
 16.2314 +     * Instead, {@code setScale} returns an object with the proper
 16.2315 +     * scale; the returned object may or may not be newly allocated.
 16.2316 +     *
 16.2317 +     * @param  newScale scale of the {@code BigDecimal} value to be returned.
 16.2318 +     * @param  roundingMode The rounding mode to apply.
 16.2319 +     * @return a {@code BigDecimal} whose scale is the specified value,
 16.2320 +     *         and whose unscaled value is determined by multiplying or
 16.2321 +     *         dividing this {@code BigDecimal}'s unscaled value by the
 16.2322 +     *         appropriate power of ten to maintain its overall value.
 16.2323 +     * @throws ArithmeticException if {@code roundingMode==UNNECESSARY}
 16.2324 +     *         and the specified scaling operation would require
 16.2325 +     *         rounding.
 16.2326 +     * @see    RoundingMode
 16.2327 +     * @since  1.5
 16.2328 +     */
 16.2329 +    public BigDecimal setScale(int newScale, RoundingMode roundingMode) {
 16.2330 +        return setScale(newScale, roundingMode.oldMode);
 16.2331 +    }
 16.2332 +
 16.2333 +    /**
 16.2334 +     * Returns a {@code BigDecimal} whose scale is the specified
 16.2335 +     * value, and whose unscaled value is determined by multiplying or
 16.2336 +     * dividing this {@code BigDecimal}'s unscaled value by the
 16.2337 +     * appropriate power of ten to maintain its overall value.  If the
 16.2338 +     * scale is reduced by the operation, the unscaled value must be
 16.2339 +     * divided (rather than multiplied), and the value may be changed;
 16.2340 +     * in this case, the specified rounding mode is applied to the
 16.2341 +     * division.
 16.2342 +     *
 16.2343 +     * <p>Note that since BigDecimal objects are immutable, calls of
 16.2344 +     * this method do <i>not</i> result in the original object being
 16.2345 +     * modified, contrary to the usual convention of having methods
 16.2346 +     * named <tt>set<i>X</i></tt> mutate field <i>{@code X}</i>.
 16.2347 +     * Instead, {@code setScale} returns an object with the proper
 16.2348 +     * scale; the returned object may or may not be newly allocated.
 16.2349 +     *
 16.2350 +     * <p>The new {@link #setScale(int, RoundingMode)} method should
 16.2351 +     * be used in preference to this legacy method.
 16.2352 +     *
 16.2353 +     * @param  newScale scale of the {@code BigDecimal} value to be returned.
 16.2354 +     * @param  roundingMode The rounding mode to apply.
 16.2355 +     * @return a {@code BigDecimal} whose scale is the specified value,
 16.2356 +     *         and whose unscaled value is determined by multiplying or
 16.2357 +     *         dividing this {@code BigDecimal}'s unscaled value by the
 16.2358 +     *         appropriate power of ten to maintain its overall value.
 16.2359 +     * @throws ArithmeticException if {@code roundingMode==ROUND_UNNECESSARY}
 16.2360 +     *         and the specified scaling operation would require
 16.2361 +     *         rounding.
 16.2362 +     * @throws IllegalArgumentException if {@code roundingMode} does not
 16.2363 +     *         represent a valid rounding mode.
 16.2364 +     * @see    #ROUND_UP
 16.2365 +     * @see    #ROUND_DOWN
 16.2366 +     * @see    #ROUND_CEILING
 16.2367 +     * @see    #ROUND_FLOOR
 16.2368 +     * @see    #ROUND_HALF_UP
 16.2369 +     * @see    #ROUND_HALF_DOWN
 16.2370 +     * @see    #ROUND_HALF_EVEN
 16.2371 +     * @see    #ROUND_UNNECESSARY
 16.2372 +     */
 16.2373 +    public BigDecimal setScale(int newScale, int roundingMode) {
 16.2374 +        if (roundingMode < ROUND_UP || roundingMode > ROUND_UNNECESSARY)
 16.2375 +            throw new IllegalArgumentException("Invalid rounding mode");
 16.2376 +
 16.2377 +        int oldScale = this.scale;
 16.2378 +        if (newScale == oldScale)        // easy case
 16.2379 +            return this;
 16.2380 +        if (this.signum() == 0)            // zero can have any scale
 16.2381 +            return BigDecimal.valueOf(0, newScale);
 16.2382 +
 16.2383 +        long rs = this.intCompact;
 16.2384 +        if (newScale > oldScale) {
 16.2385 +            int raise = checkScale((long)newScale - oldScale);
 16.2386 +            BigInteger rb = null;
 16.2387 +            if (rs == INFLATED ||
 16.2388 +                (rs = longMultiplyPowerTen(rs, raise)) == INFLATED)
 16.2389 +                rb = bigMultiplyPowerTen(raise);
 16.2390 +            return new BigDecimal(rb, rs, newScale,
 16.2391 +                                  (precision > 0) ? precision + raise : 0);
 16.2392 +        } else {
 16.2393 +            // newScale < oldScale -- drop some digits
 16.2394 +            // Can't predict the precision due to the effect of rounding.
 16.2395 +            int drop = checkScale((long)oldScale - newScale);
 16.2396 +            if (drop < LONG_TEN_POWERS_TABLE.length)
 16.2397 +                return divideAndRound(rs, this.intVal,
 16.2398 +                                      LONG_TEN_POWERS_TABLE[drop], null,
 16.2399 +                                      newScale, roundingMode, newScale);
 16.2400 +            else
 16.2401 +                return divideAndRound(rs, this.intVal,
 16.2402 +                                      INFLATED, bigTenToThe(drop),
 16.2403 +                                      newScale, roundingMode, newScale);
 16.2404 +        }
 16.2405 +    }
 16.2406 +
 16.2407 +    /**
 16.2408 +     * Returns a {@code BigDecimal} whose scale is the specified
 16.2409 +     * value, and whose value is numerically equal to this
 16.2410 +     * {@code BigDecimal}'s.  Throws an {@code ArithmeticException}
 16.2411 +     * if this is not possible.
 16.2412 +     *
 16.2413 +     * <p>This call is typically used to increase the scale, in which
 16.2414 +     * case it is guaranteed that there exists a {@code BigDecimal}
 16.2415 +     * of the specified scale and the correct value.  The call can
 16.2416 +     * also be used to reduce the scale if the caller knows that the
 16.2417 +     * {@code BigDecimal} has sufficiently many zeros at the end of
 16.2418 +     * its fractional part (i.e., factors of ten in its integer value)
 16.2419 +     * to allow for the rescaling without changing its value.
 16.2420 +     *
 16.2421 +     * <p>This method returns the same result as the two-argument
 16.2422 +     * versions of {@code setScale}, but saves the caller the trouble
 16.2423 +     * of specifying a rounding mode in cases where it is irrelevant.
 16.2424 +     *
 16.2425 +     * <p>Note that since {@code BigDecimal} objects are immutable,
 16.2426 +     * calls of this method do <i>not</i> result in the original
 16.2427 +     * object being modified, contrary to the usual convention of
 16.2428 +     * having methods named <tt>set<i>X</i></tt> mutate field
 16.2429 +     * <i>{@code X}</i>.  Instead, {@code setScale} returns an
 16.2430 +     * object with the proper scale; the returned object may or may
 16.2431 +     * not be newly allocated.
 16.2432 +     *
 16.2433 +     * @param  newScale scale of the {@code BigDecimal} value to be returned.
 16.2434 +     * @return a {@code BigDecimal} whose scale is the specified value, and
 16.2435 +     *         whose unscaled value is determined by multiplying or dividing
 16.2436 +     *         this {@code BigDecimal}'s unscaled value by the appropriate
 16.2437 +     *         power of ten to maintain its overall value.
 16.2438 +     * @throws ArithmeticException if the specified scaling operation would
 16.2439 +     *         require rounding.
 16.2440 +     * @see    #setScale(int, int)
 16.2441 +     * @see    #setScale(int, RoundingMode)
 16.2442 +     */
 16.2443 +    public BigDecimal setScale(int newScale) {
 16.2444 +        return setScale(newScale, ROUND_UNNECESSARY);
 16.2445 +    }
 16.2446 +
 16.2447 +    // Decimal Point Motion Operations
 16.2448 +
 16.2449 +    /**
 16.2450 +     * Returns a {@code BigDecimal} which is equivalent to this one
 16.2451 +     * with the decimal point moved {@code n} places to the left.  If
 16.2452 +     * {@code n} is non-negative, the call merely adds {@code n} to
 16.2453 +     * the scale.  If {@code n} is negative, the call is equivalent
 16.2454 +     * to {@code movePointRight(-n)}.  The {@code BigDecimal}
 16.2455 +     * returned by this call has value <tt>(this &times;
 16.2456 +     * 10<sup>-n</sup>)</tt> and scale {@code max(this.scale()+n,
 16.2457 +     * 0)}.
 16.2458 +     *
 16.2459 +     * @param  n number of places to move the decimal point to the left.
 16.2460 +     * @return a {@code BigDecimal} which is equivalent to this one with the
 16.2461 +     *         decimal point moved {@code n} places to the left.
 16.2462 +     * @throws ArithmeticException if scale overflows.
 16.2463 +     */
 16.2464 +    public BigDecimal movePointLeft(int n) {
 16.2465 +        // Cannot use movePointRight(-n) in case of n==Integer.MIN_VALUE
 16.2466 +        int newScale = checkScale((long)scale + n);
 16.2467 +        BigDecimal num = new BigDecimal(intVal, intCompact, newScale, 0);
 16.2468 +        return num.scale < 0 ? num.setScale(0, ROUND_UNNECESSARY) : num;
 16.2469 +    }
 16.2470 +
 16.2471 +    /**
 16.2472 +     * Returns a {@code BigDecimal} which is equivalent to this one
 16.2473 +     * with the decimal point moved {@code n} places to the right.
 16.2474 +     * If {@code n} is non-negative, the call merely subtracts
 16.2475 +     * {@code n} from the scale.  If {@code n} is negative, the call
 16.2476 +     * is equivalent to {@code movePointLeft(-n)}.  The
 16.2477 +     * {@code BigDecimal} returned by this call has value <tt>(this
 16.2478 +     * &times; 10<sup>n</sup>)</tt> and scale {@code max(this.scale()-n,
 16.2479 +     * 0)}.
 16.2480 +     *
 16.2481 +     * @param  n number of places to move the decimal point to the right.
 16.2482 +     * @return a {@code BigDecimal} which is equivalent to this one
 16.2483 +     *         with the decimal point moved {@code n} places to the right.
 16.2484 +     * @throws ArithmeticException if scale overflows.
 16.2485 +     */
 16.2486 +    public BigDecimal movePointRight(int n) {
 16.2487 +        // Cannot use movePointLeft(-n) in case of n==Integer.MIN_VALUE
 16.2488 +        int newScale = checkScale((long)scale - n);
 16.2489 +        BigDecimal num = new BigDecimal(intVal, intCompact, newScale, 0);
 16.2490 +        return num.scale < 0 ? num.setScale(0, ROUND_UNNECESSARY) : num;
 16.2491 +    }
 16.2492 +
 16.2493 +    /**
 16.2494 +     * Returns a BigDecimal whose numerical value is equal to
 16.2495 +     * ({@code this} * 10<sup>n</sup>).  The scale of
 16.2496 +     * the result is {@code (this.scale() - n)}.
 16.2497 +     *
 16.2498 +     * @throws ArithmeticException if the scale would be
 16.2499 +     *         outside the range of a 32-bit integer.
 16.2500 +     *
 16.2501 +     * @since 1.5
 16.2502 +     */
 16.2503 +    public BigDecimal scaleByPowerOfTen(int n) {
 16.2504 +        return new BigDecimal(intVal, intCompact,
 16.2505 +                              checkScale((long)scale - n), precision);
 16.2506 +    }
 16.2507 +
 16.2508 +    /**
 16.2509 +     * Returns a {@code BigDecimal} which is numerically equal to
 16.2510 +     * this one but with any trailing zeros removed from the
 16.2511 +     * representation.  For example, stripping the trailing zeros from
 16.2512 +     * the {@code BigDecimal} value {@code 600.0}, which has
 16.2513 +     * [{@code BigInteger}, {@code scale}] components equals to
 16.2514 +     * [6000, 1], yields {@code 6E2} with [{@code BigInteger},
 16.2515 +     * {@code scale}] components equals to [6, -2]
 16.2516 +     *
 16.2517 +     * @return a numerically equal {@code BigDecimal} with any
 16.2518 +     * trailing zeros removed.
 16.2519 +     * @since 1.5
 16.2520 +     */
 16.2521 +    public BigDecimal stripTrailingZeros() {
 16.2522 +        this.inflate();
 16.2523 +        BigDecimal result = new BigDecimal(intVal, scale);
 16.2524 +        result.stripZerosToMatchScale(Long.MIN_VALUE);
 16.2525 +        return result;
 16.2526 +    }
 16.2527 +
 16.2528 +    // Comparison Operations
 16.2529 +
 16.2530 +    /**
 16.2531 +     * Compares this {@code BigDecimal} with the specified
 16.2532 +     * {@code BigDecimal}.  Two {@code BigDecimal} objects that are
 16.2533 +     * equal in value but have a different scale (like 2.0 and 2.00)
 16.2534 +     * are considered equal by this method.  This method is provided
 16.2535 +     * in preference to individual methods for each of the six boolean
 16.2536 +     * comparison operators ({@literal <}, ==,
 16.2537 +     * {@literal >}, {@literal >=}, !=, {@literal <=}).  The
 16.2538 +     * suggested idiom for performing these comparisons is:
 16.2539 +     * {@code (x.compareTo(y)} &lt;<i>op</i>&gt; {@code 0)}, where
 16.2540 +     * &lt;<i>op</i>&gt; is one of the six comparison operators.
 16.2541 +     *
 16.2542 +     * @param  val {@code BigDecimal} to which this {@code BigDecimal} is
 16.2543 +     *         to be compared.
 16.2544 +     * @return -1, 0, or 1 as this {@code BigDecimal} is numerically
 16.2545 +     *          less than, equal to, or greater than {@code val}.
 16.2546 +     */
 16.2547 +    public int compareTo(BigDecimal val) {
 16.2548 +        // Quick path for equal scale and non-inflated case.
 16.2549 +        if (scale == val.scale) {
 16.2550 +            long xs = intCompact;
 16.2551 +            long ys = val.intCompact;
 16.2552 +            if (xs != INFLATED && ys != INFLATED)
 16.2553 +                return xs != ys ? ((xs > ys) ? 1 : -1) : 0;
 16.2554 +        }
 16.2555 +        int xsign = this.signum();
 16.2556 +        int ysign = val.signum();
 16.2557 +        if (xsign != ysign)
 16.2558 +            return (xsign > ysign) ? 1 : -1;
 16.2559 +        if (xsign == 0)
 16.2560 +            return 0;
 16.2561 +        int cmp = compareMagnitude(val);
 16.2562 +        return (xsign > 0) ? cmp : -cmp;
 16.2563 +    }
 16.2564 +
 16.2565 +    /**
 16.2566 +     * Version of compareTo that ignores sign.
 16.2567 +     */
 16.2568 +    private int compareMagnitude(BigDecimal val) {
 16.2569 +        // Match scales, avoid unnecessary inflation
 16.2570 +        long ys = val.intCompact;
 16.2571 +        long xs = this.intCompact;
 16.2572 +        if (xs == 0)
 16.2573 +            return (ys == 0) ? 0 : -1;
 16.2574 +        if (ys == 0)
 16.2575 +            return 1;
 16.2576 +
 16.2577 +        int sdiff = this.scale - val.scale;
 16.2578 +        if (sdiff != 0) {
 16.2579 +            // Avoid matching scales if the (adjusted) exponents differ
 16.2580 +            int xae = this.precision() - this.scale;   // [-1]
 16.2581 +            int yae = val.precision() - val.scale;     // [-1]
 16.2582 +            if (xae < yae)
 16.2583 +                return -1;
 16.2584 +            if (xae > yae)
 16.2585 +                return 1;
 16.2586 +            BigInteger rb = null;
 16.2587 +            if (sdiff < 0) {
 16.2588 +                if ( (xs == INFLATED ||
 16.2589 +                      (xs = longMultiplyPowerTen(xs, -sdiff)) == INFLATED) &&
 16.2590 +                     ys == INFLATED) {
 16.2591 +                    rb = bigMultiplyPowerTen(-sdiff);
 16.2592 +                    return rb.compareMagnitude(val.intVal);
 16.2593 +                }
 16.2594 +            } else { // sdiff > 0
 16.2595 +                if ( (ys == INFLATED ||
 16.2596 +                      (ys = longMultiplyPowerTen(ys, sdiff)) == INFLATED) &&
 16.2597 +                     xs == INFLATED) {
 16.2598 +                    rb = val.bigMultiplyPowerTen(sdiff);
 16.2599 +                    return this.intVal.compareMagnitude(rb);
 16.2600 +                }
 16.2601 +            }
 16.2602 +        }
 16.2603 +        if (xs != INFLATED)
 16.2604 +            return (ys != INFLATED) ? longCompareMagnitude(xs, ys) : -1;
 16.2605 +        else if (ys != INFLATED)
 16.2606 +            return 1;
 16.2607 +        else
 16.2608 +            return this.intVal.compareMagnitude(val.intVal);
 16.2609 +    }
 16.2610 +
 16.2611 +    /**
 16.2612 +     * Compares this {@code BigDecimal} with the specified
 16.2613 +     * {@code Object} for equality.  Unlike {@link
 16.2614 +     * #compareTo(BigDecimal) compareTo}, this method considers two
 16.2615 +     * {@code BigDecimal} objects equal only if they are equal in
 16.2616 +     * value and scale (thus 2.0 is not equal to 2.00 when compared by
 16.2617 +     * this method).
 16.2618 +     *
 16.2619 +     * @param  x {@code Object} to which this {@code BigDecimal} is
 16.2620 +     *         to be compared.
 16.2621 +     * @return {@code true} if and only if the specified {@code Object} is a
 16.2622 +     *         {@code BigDecimal} whose value and scale are equal to this
 16.2623 +     *         {@code BigDecimal}'s.
 16.2624 +     * @see    #compareTo(java.math.BigDecimal)
 16.2625 +     * @see    #hashCode
 16.2626 +     */
 16.2627 +    @Override
 16.2628 +    public boolean equals(Object x) {
 16.2629 +        if (!(x instanceof BigDecimal))
 16.2630 +            return false;
 16.2631 +        BigDecimal xDec = (BigDecimal) x;
 16.2632 +        if (x == this)
 16.2633 +            return true;
 16.2634 +        if (scale != xDec.scale)
 16.2635 +            return false;
 16.2636 +        long s = this.intCompact;
 16.2637 +        long xs = xDec.intCompact;
 16.2638 +        if (s != INFLATED) {
 16.2639 +            if (xs == INFLATED)
 16.2640 +                xs = compactValFor(xDec.intVal);
 16.2641 +            return xs == s;
 16.2642 +        } else if (xs != INFLATED)
 16.2643 +            return xs == compactValFor(this.intVal);
 16.2644 +
 16.2645 +        return this.inflate().equals(xDec.inflate());
 16.2646 +    }
 16.2647 +
 16.2648 +    /**
 16.2649 +     * Returns the minimum of this {@code BigDecimal} and
 16.2650 +     * {@code val}.
 16.2651 +     *
 16.2652 +     * @param  val value with which the minimum is to be computed.
 16.2653 +     * @return the {@code BigDecimal} whose value is the lesser of this
 16.2654 +     *         {@code BigDecimal} and {@code val}.  If they are equal,
 16.2655 +     *         as defined by the {@link #compareTo(BigDecimal) compareTo}
 16.2656 +     *         method, {@code this} is returned.
 16.2657 +     * @see    #compareTo(java.math.BigDecimal)
 16.2658 +     */
 16.2659 +    public BigDecimal min(BigDecimal val) {
 16.2660 +        return (compareTo(val) <= 0 ? this : val);
 16.2661 +    }
 16.2662 +
 16.2663 +    /**
 16.2664 +     * Returns the maximum of this {@code BigDecimal} and {@code val}.
 16.2665 +     *
 16.2666 +     * @param  val value with which the maximum is to be computed.
 16.2667 +     * @return the {@code BigDecimal} whose value is the greater of this
 16.2668 +     *         {@code BigDecimal} and {@code val}.  If they are equal,
 16.2669 +     *         as defined by the {@link #compareTo(BigDecimal) compareTo}
 16.2670 +     *         method, {@code this} is returned.
 16.2671 +     * @see    #compareTo(java.math.BigDecimal)
 16.2672 +     */
 16.2673 +    public BigDecimal max(BigDecimal val) {
 16.2674 +        return (compareTo(val) >= 0 ? this : val);
 16.2675 +    }
 16.2676 +
 16.2677 +    // Hash Function
 16.2678 +
 16.2679 +    /**
 16.2680 +     * Returns the hash code for this {@code BigDecimal}.  Note that
 16.2681 +     * two {@code BigDecimal} objects that are numerically equal but
 16.2682 +     * differ in scale (like 2.0 and 2.00) will generally <i>not</i>
 16.2683 +     * have the same hash code.
 16.2684 +     *
 16.2685 +     * @return hash code for this {@code BigDecimal}.
 16.2686 +     * @see #equals(Object)
 16.2687 +     */
 16.2688 +    @Override
 16.2689 +    public int hashCode() {
 16.2690 +        if (intCompact != INFLATED) {
 16.2691 +            long val2 = (intCompact < 0)? -intCompact : intCompact;
 16.2692 +            int temp = (int)( ((int)(val2 >>> 32)) * 31  +
 16.2693 +                              (val2 & LONG_MASK));
 16.2694 +            return 31*((intCompact < 0) ?-temp:temp) + scale;
 16.2695 +        } else
 16.2696 +            return 31*intVal.hashCode() + scale;
 16.2697 +    }
 16.2698 +
 16.2699 +    // Format Converters
 16.2700 +
 16.2701 +    /**
 16.2702 +     * Returns the string representation of this {@code BigDecimal},
 16.2703 +     * using scientific notation if an exponent is needed.
 16.2704 +     *
 16.2705 +     * <p>A standard canonical string form of the {@code BigDecimal}
 16.2706 +     * is created as though by the following steps: first, the
 16.2707 +     * absolute value of the unscaled value of the {@code BigDecimal}
 16.2708 +     * is converted to a string in base ten using the characters
 16.2709 +     * {@code '0'} through {@code '9'} with no leading zeros (except
 16.2710 +     * if its value is zero, in which case a single {@code '0'}
 16.2711 +     * character is used).
 16.2712 +     *
 16.2713 +     * <p>Next, an <i>adjusted exponent</i> is calculated; this is the
 16.2714 +     * negated scale, plus the number of characters in the converted
 16.2715 +     * unscaled value, less one.  That is,
 16.2716 +     * {@code -scale+(ulength-1)}, where {@code ulength} is the
 16.2717 +     * length of the absolute value of the unscaled value in decimal
 16.2718 +     * digits (its <i>precision</i>).
 16.2719 +     *
 16.2720 +     * <p>If the scale is greater than or equal to zero and the
 16.2721 +     * adjusted exponent is greater than or equal to {@code -6}, the
 16.2722 +     * number will be converted to a character form without using
 16.2723 +     * exponential notation.  In this case, if the scale is zero then
 16.2724 +     * no decimal point is added and if the scale is positive a
 16.2725 +     * decimal point will be inserted with the scale specifying the
 16.2726 +     * number of characters to the right of the decimal point.
 16.2727 +     * {@code '0'} characters are added to the left of the converted
 16.2728 +     * unscaled value as necessary.  If no character precedes the
 16.2729 +     * decimal point after this insertion then a conventional
 16.2730 +     * {@code '0'} character is prefixed.
 16.2731 +     *
 16.2732 +     * <p>Otherwise (that is, if the scale is negative, or the
 16.2733 +     * adjusted exponent is less than {@code -6}), the number will be
 16.2734 +     * converted to a character form using exponential notation.  In
 16.2735 +     * this case, if the converted {@code BigInteger} has more than
 16.2736 +     * one digit a decimal point is inserted after the first digit.
 16.2737 +     * An exponent in character form is then suffixed to the converted
 16.2738 +     * unscaled value (perhaps with inserted decimal point); this
 16.2739 +     * comprises the letter {@code 'E'} followed immediately by the
 16.2740 +     * adjusted exponent converted to a character form.  The latter is
 16.2741 +     * in base ten, using the characters {@code '0'} through
 16.2742 +     * {@code '9'} with no leading zeros, and is always prefixed by a
 16.2743 +     * sign character {@code '-'} (<tt>'&#92;u002D'</tt>) if the
 16.2744 +     * adjusted exponent is negative, {@code '+'}
 16.2745 +     * (<tt>'&#92;u002B'</tt>) otherwise).
 16.2746 +     *
 16.2747 +     * <p>Finally, the entire string is prefixed by a minus sign
 16.2748 +     * character {@code '-'} (<tt>'&#92;u002D'</tt>) if the unscaled
 16.2749 +     * value is less than zero.  No sign character is prefixed if the
 16.2750 +     * unscaled value is zero or positive.
 16.2751 +     *
 16.2752 +     * <p><b>Examples:</b>
 16.2753 +     * <p>For each representation [<i>unscaled value</i>, <i>scale</i>]
 16.2754 +     * on the left, the resulting string is shown on the right.
 16.2755 +     * <pre>
 16.2756 +     * [123,0]      "123"
 16.2757 +     * [-123,0]     "-123"
 16.2758 +     * [123,-1]     "1.23E+3"
 16.2759 +     * [123,-3]     "1.23E+5"
 16.2760 +     * [123,1]      "12.3"
 16.2761 +     * [123,5]      "0.00123"
 16.2762 +     * [123,10]     "1.23E-8"
 16.2763 +     * [-123,12]    "-1.23E-10"
 16.2764 +     * </pre>
 16.2765 +     *
 16.2766 +     * <b>Notes:</b>
 16.2767 +     * <ol>
 16.2768 +     *
 16.2769 +     * <li>There is a one-to-one mapping between the distinguishable
 16.2770 +     * {@code BigDecimal} values and the result of this conversion.
 16.2771 +     * That is, every distinguishable {@code BigDecimal} value
 16.2772 +     * (unscaled value and scale) has a unique string representation
 16.2773 +     * as a result of using {@code toString}.  If that string
 16.2774 +     * representation is converted back to a {@code BigDecimal} using
 16.2775 +     * the {@link #BigDecimal(String)} constructor, then the original
 16.2776 +     * value will be recovered.
 16.2777 +     *
 16.2778 +     * <li>The string produced for a given number is always the same;
 16.2779 +     * it is not affected by locale.  This means that it can be used
 16.2780 +     * as a canonical string representation for exchanging decimal
 16.2781 +     * data, or as a key for a Hashtable, etc.  Locale-sensitive
 16.2782 +     * number formatting and parsing is handled by the {@link
 16.2783 +     * java.text.NumberFormat} class and its subclasses.
 16.2784 +     *
 16.2785 +     * <li>The {@link #toEngineeringString} method may be used for
 16.2786 +     * presenting numbers with exponents in engineering notation, and the
 16.2787 +     * {@link #setScale(int,RoundingMode) setScale} method may be used for
 16.2788 +     * rounding a {@code BigDecimal} so it has a known number of digits after
 16.2789 +     * the decimal point.
 16.2790 +     *
 16.2791 +     * <li>The digit-to-character mapping provided by
 16.2792 +     * {@code Character.forDigit} is used.
 16.2793 +     *
 16.2794 +     * </ol>
 16.2795 +     *
 16.2796 +     * @return string representation of this {@code BigDecimal}.
 16.2797 +     * @see    Character#forDigit
 16.2798 +     * @see    #BigDecimal(java.lang.String)
 16.2799 +     */
 16.2800 +    @Override
 16.2801 +    public String toString() {
 16.2802 +        String sc = stringCache;
 16.2803 +        if (sc == null)
 16.2804 +            stringCache = sc = layoutChars(true);
 16.2805 +        return sc;
 16.2806 +    }
 16.2807 +
 16.2808 +    /**
 16.2809 +     * Returns a string representation of this {@code BigDecimal},
 16.2810 +     * using engineering notation if an exponent is needed.
 16.2811 +     *
 16.2812 +     * <p>Returns a string that represents the {@code BigDecimal} as
 16.2813 +     * described in the {@link #toString()} method, except that if
 16.2814 +     * exponential notation is used, the power of ten is adjusted to
 16.2815 +     * be a multiple of three (engineering notation) such that the
 16.2816 +     * integer part of nonzero values will be in the range 1 through
 16.2817 +     * 999.  If exponential notation is used for zero values, a
 16.2818 +     * decimal point and one or two fractional zero digits are used so
 16.2819 +     * that the scale of the zero value is preserved.  Note that
 16.2820 +     * unlike the output of {@link #toString()}, the output of this
 16.2821 +     * method is <em>not</em> guaranteed to recover the same [integer,
 16.2822 +     * scale] pair of this {@code BigDecimal} if the output string is
 16.2823 +     * converting back to a {@code BigDecimal} using the {@linkplain
 16.2824 +     * #BigDecimal(String) string constructor}.  The result of this method meets
 16.2825 +     * the weaker constraint of always producing a numerically equal
 16.2826 +     * result from applying the string constructor to the method's output.
 16.2827 +     *
 16.2828 +     * @return string representation of this {@code BigDecimal}, using
 16.2829 +     *         engineering notation if an exponent is needed.
 16.2830 +     * @since  1.5
 16.2831 +     */
 16.2832 +    public String toEngineeringString() {
 16.2833 +        return layoutChars(false);
 16.2834 +    }
 16.2835 +
 16.2836 +    /**
 16.2837 +     * Returns a string representation of this {@code BigDecimal}
 16.2838 +     * without an exponent field.  For values with a positive scale,
 16.2839 +     * the number of digits to the right of the decimal point is used
 16.2840 +     * to indicate scale.  For values with a zero or negative scale,
 16.2841 +     * the resulting string is generated as if the value were
 16.2842 +     * converted to a numerically equal value with zero scale and as
 16.2843 +     * if all the trailing zeros of the zero scale value were present
 16.2844 +     * in the result.
 16.2845 +     *
 16.2846 +     * The entire string is prefixed by a minus sign character '-'
 16.2847 +     * (<tt>'&#92;u002D'</tt>) if the unscaled value is less than
 16.2848 +     * zero. No sign character is prefixed if the unscaled value is
 16.2849 +     * zero or positive.
 16.2850 +     *
 16.2851 +     * Note that if the result of this method is passed to the
 16.2852 +     * {@linkplain #BigDecimal(String) string constructor}, only the
 16.2853 +     * numerical value of this {@code BigDecimal} will necessarily be
 16.2854 +     * recovered; the representation of the new {@code BigDecimal}
 16.2855 +     * may have a different scale.  In particular, if this
 16.2856 +     * {@code BigDecimal} has a negative scale, the string resulting
 16.2857 +     * from this method will have a scale of zero when processed by
 16.2858 +     * the string constructor.
 16.2859 +     *
 16.2860 +     * (This method behaves analogously to the {@code toString}
 16.2861 +     * method in 1.4 and earlier releases.)
 16.2862 +     *
 16.2863 +     * @return a string representation of this {@code BigDecimal}
 16.2864 +     * without an exponent field.
 16.2865 +     * @since 1.5
 16.2866 +     * @see #toString()
 16.2867 +     * @see #toEngineeringString()
 16.2868 +     */
 16.2869 +    public String toPlainString() {
 16.2870 +        BigDecimal bd = this;
 16.2871 +        if (bd.scale < 0)
 16.2872 +            bd = bd.setScale(0);
 16.2873 +        bd.inflate();
 16.2874 +        if (bd.scale == 0)      // No decimal point
 16.2875 +            return bd.intVal.toString();
 16.2876 +        return bd.getValueString(bd.signum(), bd.intVal.abs().toString(), bd.scale);
 16.2877 +    }
 16.2878 +
 16.2879 +    /* Returns a digit.digit string */
 16.2880 +    private String getValueString(int signum, String intString, int scale) {
 16.2881 +        /* Insert decimal point */
 16.2882 +        StringBuilder buf;
 16.2883 +        int insertionPoint = intString.length() - scale;
 16.2884 +        if (insertionPoint == 0) {  /* Point goes right before intVal */
 16.2885 +            return (signum<0 ? "-0." : "0.") + intString;
 16.2886 +        } else if (insertionPoint > 0) { /* Point goes inside intVal */
 16.2887 +            buf = new StringBuilder(intString);
 16.2888 +            buf.insert(insertionPoint, '.');
 16.2889 +            if (signum < 0)
 16.2890 +                buf.insert(0, '-');
 16.2891 +        } else { /* We must insert zeros between point and intVal */
 16.2892 +            buf = new StringBuilder(3-insertionPoint + intString.length());
 16.2893 +            buf.append(signum<0 ? "-0." : "0.");
 16.2894 +            for (int i=0; i<-insertionPoint; i++)
 16.2895 +                buf.append('0');
 16.2896 +            buf.append(intString);
 16.2897 +        }
 16.2898 +        return buf.toString();
 16.2899 +    }
 16.2900 +
 16.2901 +    /**
 16.2902 +     * Converts this {@code BigDecimal} to a {@code BigInteger}.
 16.2903 +     * This conversion is analogous to the
 16.2904 +     * <i>narrowing primitive conversion</i> from {@code double} to
 16.2905 +     * {@code long} as defined in section 5.1.3 of
 16.2906 +     * <cite>The Java&trade; Language Specification</cite>:
 16.2907 +     * any fractional part of this
 16.2908 +     * {@code BigDecimal} will be discarded.  Note that this
 16.2909 +     * conversion can lose information about the precision of the
 16.2910 +     * {@code BigDecimal} value.
 16.2911 +     * <p>
 16.2912 +     * To have an exception thrown if the conversion is inexact (in
 16.2913 +     * other words if a nonzero fractional part is discarded), use the
 16.2914 +     * {@link #toBigIntegerExact()} method.
 16.2915 +     *
 16.2916 +     * @return this {@code BigDecimal} converted to a {@code BigInteger}.
 16.2917 +     */
 16.2918 +    public BigInteger toBigInteger() {
 16.2919 +        // force to an integer, quietly
 16.2920 +        return this.setScale(0, ROUND_DOWN).inflate();
 16.2921 +    }
 16.2922 +
 16.2923 +    /**
 16.2924 +     * Converts this {@code BigDecimal} to a {@code BigInteger},
 16.2925 +     * checking for lost information.  An exception is thrown if this
 16.2926 +     * {@code BigDecimal} has a nonzero fractional part.
 16.2927 +     *
 16.2928 +     * @return this {@code BigDecimal} converted to a {@code BigInteger}.
 16.2929 +     * @throws ArithmeticException if {@code this} has a nonzero
 16.2930 +     *         fractional part.
 16.2931 +     * @since  1.5
 16.2932 +     */
 16.2933 +    public BigInteger toBigIntegerExact() {
 16.2934 +        // round to an integer, with Exception if decimal part non-0
 16.2935 +        return this.setScale(0, ROUND_UNNECESSARY).inflate();
 16.2936 +    }
 16.2937 +
 16.2938 +    /**
 16.2939 +     * Converts this {@code BigDecimal} to a {@code long}.
 16.2940 +     * This conversion is analogous to the
 16.2941 +     * <i>narrowing primitive conversion</i> from {@code double} to
 16.2942 +     * {@code short} as defined in section 5.1.3 of
 16.2943 +     * <cite>The Java&trade; Language Specification</cite>:
 16.2944 +     * any fractional part of this
 16.2945 +     * {@code BigDecimal} will be discarded, and if the resulting
 16.2946 +     * "{@code BigInteger}" is too big to fit in a
 16.2947 +     * {@code long}, only the low-order 64 bits are returned.
 16.2948 +     * Note that this conversion can lose information about the
 16.2949 +     * overall magnitude and precision of this {@code BigDecimal} value as well
 16.2950 +     * as return a result with the opposite sign.
 16.2951 +     *
 16.2952 +     * @return this {@code BigDecimal} converted to a {@code long}.
 16.2953 +     */
 16.2954 +    public long longValue(){
 16.2955 +        return (intCompact != INFLATED && scale == 0) ?
 16.2956 +            intCompact:
 16.2957 +            toBigInteger().longValue();
 16.2958 +    }
 16.2959 +
 16.2960 +    /**
 16.2961 +     * Converts this {@code BigDecimal} to a {@code long}, checking
 16.2962 +     * for lost information.  If this {@code BigDecimal} has a
 16.2963 +     * nonzero fractional part or is out of the possible range for a
 16.2964 +     * {@code long} result then an {@code ArithmeticException} is
 16.2965 +     * thrown.
 16.2966 +     *
 16.2967 +     * @return this {@code BigDecimal} converted to a {@code long}.
 16.2968 +     * @throws ArithmeticException if {@code this} has a nonzero
 16.2969 +     *         fractional part, or will not fit in a {@code long}.
 16.2970 +     * @since  1.5
 16.2971 +     */
 16.2972 +    public long longValueExact() {
 16.2973 +        if (intCompact != INFLATED && scale == 0)
 16.2974 +            return intCompact;
 16.2975 +        // If more than 19 digits in integer part it cannot possibly fit
 16.2976 +        if ((precision() - scale) > 19) // [OK for negative scale too]
 16.2977 +            throw new java.lang.ArithmeticException("Overflow");
 16.2978 +        // Fastpath zero and < 1.0 numbers (the latter can be very slow
 16.2979 +        // to round if very small)
 16.2980 +        if (this.signum() == 0)
 16.2981 +            return 0;
 16.2982 +        if ((this.precision() - this.scale) <= 0)
 16.2983 +            throw new ArithmeticException("Rounding necessary");
 16.2984 +        // round to an integer, with Exception if decimal part non-0
 16.2985 +        BigDecimal num = this.setScale(0, ROUND_UNNECESSARY);
 16.2986 +        if (num.precision() >= 19) // need to check carefully
 16.2987 +            LongOverflow.check(num);
 16.2988 +        return num.inflate().longValue();
 16.2989 +    }
 16.2990 +
 16.2991 +    private static class LongOverflow {
 16.2992 +        /** BigInteger equal to Long.MIN_VALUE. */
 16.2993 +        private static final BigInteger LONGMIN = BigInteger.valueOf(Long.MIN_VALUE);
 16.2994 +
 16.2995 +        /** BigInteger equal to Long.MAX_VALUE. */
 16.2996 +        private static final BigInteger LONGMAX = BigInteger.valueOf(Long.MAX_VALUE);
 16.2997 +
 16.2998 +        public static void check(BigDecimal num) {
 16.2999 +            num.inflate();
 16.3000 +            if ((num.intVal.compareTo(LONGMIN) < 0) ||
 16.3001 +                (num.intVal.compareTo(LONGMAX) > 0))
 16.3002 +                throw new java.lang.ArithmeticException("Overflow");
 16.3003 +        }
 16.3004 +    }
 16.3005 +
 16.3006 +    /**
 16.3007 +     * Converts this {@code BigDecimal} to an {@code int}.
 16.3008 +     * This conversion is analogous to the
 16.3009 +     * <i>narrowing primitive conversion</i> from {@code double} to
 16.3010 +     * {@code short} as defined in section 5.1.3 of
 16.3011 +     * <cite>The Java&trade; Language Specification</cite>:
 16.3012 +     * any fractional part of this
 16.3013 +     * {@code BigDecimal} will be discarded, and if the resulting
 16.3014 +     * "{@code BigInteger}" is too big to fit in an
 16.3015 +     * {@code int}, only the low-order 32 bits are returned.
 16.3016 +     * Note that this conversion can lose information about the
 16.3017 +     * overall magnitude and precision of this {@code BigDecimal}
 16.3018 +     * value as well as return a result with the opposite sign.
 16.3019 +     *
 16.3020 +     * @return this {@code BigDecimal} converted to an {@code int}.
 16.3021 +     */
 16.3022 +    public int intValue() {
 16.3023 +        return  (intCompact != INFLATED && scale == 0) ?
 16.3024 +            (int)intCompact :
 16.3025 +            toBigInteger().intValue();
 16.3026 +    }
 16.3027 +
 16.3028 +    /**
 16.3029 +     * Converts this {@code BigDecimal} to an {@code int}, checking
 16.3030 +     * for lost information.  If this {@code BigDecimal} has a
 16.3031 +     * nonzero fractional part or is out of the possible range for an
 16.3032 +     * {@code int} result then an {@code ArithmeticException} is
 16.3033 +     * thrown.
 16.3034 +     *
 16.3035 +     * @return this {@code BigDecimal} converted to an {@code int}.
 16.3036 +     * @throws ArithmeticException if {@code this} has a nonzero
 16.3037 +     *         fractional part, or will not fit in an {@code int}.
 16.3038 +     * @since  1.5
 16.3039 +     */
 16.3040 +    public int intValueExact() {
 16.3041 +       long num;
 16.3042 +       num = this.longValueExact();     // will check decimal part
 16.3043 +       if ((int)num != num)
 16.3044 +           throw new java.lang.ArithmeticException("Overflow");
 16.3045 +       return (int)num;
 16.3046 +    }
 16.3047 +
 16.3048 +    /**
 16.3049 +     * Converts this {@code BigDecimal} to a {@code short}, checking
 16.3050 +     * for lost information.  If this {@code BigDecimal} has a
 16.3051 +     * nonzero fractional part or is out of the possible range for a
 16.3052 +     * {@code short} result then an {@code ArithmeticException} is
 16.3053 +     * thrown.
 16.3054 +     *
 16.3055 +     * @return this {@code BigDecimal} converted to a {@code short}.
 16.3056 +     * @throws ArithmeticException if {@code this} has a nonzero
 16.3057 +     *         fractional part, or will not fit in a {@code short}.
 16.3058 +     * @since  1.5
 16.3059 +     */
 16.3060 +    public short shortValueExact() {
 16.3061 +       long num;
 16.3062 +       num = this.longValueExact();     // will check decimal part
 16.3063 +       if ((short)num != num)
 16.3064 +           throw new java.lang.ArithmeticException("Overflow");
 16.3065 +       return (short)num;
 16.3066 +    }
 16.3067 +
 16.3068 +    /**
 16.3069 +     * Converts this {@code BigDecimal} to a {@code byte}, checking
 16.3070 +     * for lost information.  If this {@code BigDecimal} has a
 16.3071 +     * nonzero fractional part or is out of the possible range for a
 16.3072 +     * {@code byte} result then an {@code ArithmeticException} is
 16.3073 +     * thrown.
 16.3074 +     *
 16.3075 +     * @return this {@code BigDecimal} converted to a {@code byte}.
 16.3076 +     * @throws ArithmeticException if {@code this} has a nonzero
 16.3077 +     *         fractional part, or will not fit in a {@code byte}.
 16.3078 +     * @since  1.5
 16.3079 +     */
 16.3080 +    public byte byteValueExact() {
 16.3081 +       long num;
 16.3082 +       num = this.longValueExact();     // will check decimal part
 16.3083 +       if ((byte)num != num)
 16.3084 +           throw new java.lang.ArithmeticException("Overflow");
 16.3085 +       return (byte)num;
 16.3086 +    }
 16.3087 +
 16.3088 +    /**
 16.3089 +     * Converts this {@code BigDecimal} to a {@code float}.
 16.3090 +     * This conversion is similar to the
 16.3091 +     * <i>narrowing primitive conversion</i> from {@code double} to
 16.3092 +     * {@code float} as defined in section 5.1.3 of
 16.3093 +     * <cite>The Java&trade; Language Specification</cite>:
 16.3094 +     * if this {@code BigDecimal} has too great a
 16.3095 +     * magnitude to represent as a {@code float}, it will be
 16.3096 +     * converted to {@link Float#NEGATIVE_INFINITY} or {@link
 16.3097 +     * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 16.3098 +     * the return value is finite, this conversion can lose
 16.3099 +     * information about the precision of the {@code BigDecimal}
 16.3100 +     * value.
 16.3101 +     *
 16.3102 +     * @return this {@code BigDecimal} converted to a {@code float}.
 16.3103 +     */
 16.3104 +    public float floatValue(){
 16.3105 +        if (scale == 0 && intCompact != INFLATED)
 16.3106 +                return (float)intCompact;
 16.3107 +        // Somewhat inefficient, but guaranteed to work.
 16.3108 +        return Float.parseFloat(this.toString());
 16.3109 +    }
 16.3110 +
 16.3111 +    /**
 16.3112 +     * Converts this {@code BigDecimal} to a {@code double}.
 16.3113 +     * This conversion is similar to the
 16.3114 +     * <i>narrowing primitive conversion</i> from {@code double} to
 16.3115 +     * {@code float} as defined in section 5.1.3 of
 16.3116 +     * <cite>The Java&trade; Language Specification</cite>:
 16.3117 +     * if this {@code BigDecimal} has too great a
 16.3118 +     * magnitude represent as a {@code double}, it will be
 16.3119 +     * converted to {@link Double#NEGATIVE_INFINITY} or {@link
 16.3120 +     * Double#POSITIVE_INFINITY} as appropriate.  Note that even when
 16.3121 +     * the return value is finite, this conversion can lose
 16.3122 +     * information about the precision of the {@code BigDecimal}
 16.3123 +     * value.
 16.3124 +     *
 16.3125 +     * @return this {@code BigDecimal} converted to a {@code double}.
 16.3126 +     */
 16.3127 +    public double doubleValue(){
 16.3128 +        if (scale == 0 && intCompact != INFLATED)
 16.3129 +            return (double)intCompact;
 16.3130 +        // Somewhat inefficient, but guaranteed to work.
 16.3131 +        return Double.parseDouble(this.toString());
 16.3132 +    }
 16.3133 +
 16.3134 +    /**
 16.3135 +     * Returns the size of an ulp, a unit in the last place, of this
 16.3136 +     * {@code BigDecimal}.  An ulp of a nonzero {@code BigDecimal}
 16.3137 +     * value is the positive distance between this value and the
 16.3138 +     * {@code BigDecimal} value next larger in magnitude with the
 16.3139 +     * same number of digits.  An ulp of a zero value is numerically
 16.3140 +     * equal to 1 with the scale of {@code this}.  The result is
 16.3141 +     * stored with the same scale as {@code this} so the result
 16.3142 +     * for zero and nonzero values is equal to {@code [1,
 16.3143 +     * this.scale()]}.
 16.3144 +     *
 16.3145 +     * @return the size of an ulp of {@code this}
 16.3146 +     * @since 1.5
 16.3147 +     */
 16.3148 +    public BigDecimal ulp() {
 16.3149 +        return BigDecimal.valueOf(1, this.scale());
 16.3150 +    }
 16.3151 +
 16.3152 +
 16.3153 +    // Private class to build a string representation for BigDecimal object.
 16.3154 +    // "StringBuilderHelper" is constructed as a thread local variable so it is
 16.3155 +    // thread safe. The StringBuilder field acts as a buffer to hold the temporary
 16.3156 +    // representation of BigDecimal. The cmpCharArray holds all the characters for
 16.3157 +    // the compact representation of BigDecimal (except for '-' sign' if it is
 16.3158 +    // negative) if its intCompact field is not INFLATED. It is shared by all
 16.3159 +    // calls to toString() and its variants in that particular thread.
 16.3160 +    static class StringBuilderHelper {
 16.3161 +        private static StringBuilderHelper INSTANCE = new StringBuilderHelper();
 16.3162 +        final StringBuilder sb;    // Placeholder for BigDecimal string
 16.3163 +        final char[] cmpCharArray; // character array to place the intCompact
 16.3164 +
 16.3165 +        StringBuilderHelper() {
 16.3166 +            sb = new StringBuilder();
 16.3167 +            // All non negative longs can be made to fit into 19 character array.
 16.3168 +            cmpCharArray = new char[19];
 16.3169 +        }
 16.3170 +
 16.3171 +        // Accessors.
 16.3172 +        StringBuilder getStringBuilder() {
 16.3173 +            sb.setLength(0);
 16.3174 +            return sb;
 16.3175 +        }
 16.3176 +
 16.3177 +        char[] getCompactCharArray() {
 16.3178 +            return cmpCharArray;
 16.3179 +        }
 16.3180 +
 16.3181 +        /**
 16.3182 +         * Places characters representing the intCompact in {@code long} into
 16.3183 +         * cmpCharArray and returns the offset to the array where the
 16.3184 +         * representation starts.
 16.3185 +         *
 16.3186 +         * @param intCompact the number to put into the cmpCharArray.
 16.3187 +         * @return offset to the array where the representation starts.
 16.3188 +         * Note: intCompact must be greater or equal to zero.
 16.3189 +         */
 16.3190 +        int putIntCompact(long intCompact) {
 16.3191 +            assert intCompact >= 0;
 16.3192 +
 16.3193 +            long q;
 16.3194 +            int r;
 16.3195 +            // since we start from the least significant digit, charPos points to
 16.3196 +            // the last character in cmpCharArray.
 16.3197 +            int charPos = cmpCharArray.length;
 16.3198 +
 16.3199 +            // Get 2 digits/iteration using longs until quotient fits into an int
 16.3200 +            while (intCompact > Integer.MAX_VALUE) {
 16.3201 +                q = intCompact / 100;
 16.3202 +                r = (int)(intCompact - q * 100);
 16.3203 +                intCompact = q;
 16.3204 +                cmpCharArray[--charPos] = DIGIT_ONES[r];
 16.3205 +                cmpCharArray[--charPos] = DIGIT_TENS[r];
 16.3206 +            }
 16.3207 +
 16.3208 +            // Get 2 digits/iteration using ints when i2 >= 100
 16.3209 +            int q2;
 16.3210 +            int i2 = (int)intCompact;
 16.3211 +            while (i2 >= 100) {
 16.3212 +                q2 = i2 / 100;
 16.3213 +                r  = i2 - q2 * 100;
 16.3214 +                i2 = q2;
 16.3215 +                cmpCharArray[--charPos] = DIGIT_ONES[r];
 16.3216 +                cmpCharArray[--charPos] = DIGIT_TENS[r];
 16.3217 +            }
 16.3218 +
 16.3219 +            cmpCharArray[--charPos] = DIGIT_ONES[i2];
 16.3220 +            if (i2 >= 10)
 16.3221 +                cmpCharArray[--charPos] = DIGIT_TENS[i2];
 16.3222 +
 16.3223 +            return charPos;
 16.3224 +        }
 16.3225 +
 16.3226 +        final static char[] DIGIT_TENS = {
 16.3227 +            '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
 16.3228 +            '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
 16.3229 +            '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
 16.3230 +            '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
 16.3231 +            '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
 16.3232 +            '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
 16.3233 +            '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
 16.3234 +            '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
 16.3235 +            '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
 16.3236 +            '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
 16.3237 +        };
 16.3238 +
 16.3239 +        final static char[] DIGIT_ONES = {
 16.3240 +            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 16.3241 +            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 16.3242 +            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 16.3243 +            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 16.3244 +            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 16.3245 +            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 16.3246 +            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 16.3247 +            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 16.3248 +            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 16.3249 +            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
 16.3250 +        };
 16.3251 +    }
 16.3252 +
 16.3253 +    /**
 16.3254 +     * Lay out this {@code BigDecimal} into a {@code char[]} array.
 16.3255 +     * The Java 1.2 equivalent to this was called {@code getValueString}.
 16.3256 +     *
 16.3257 +     * @param  sci {@code true} for Scientific exponential notation;
 16.3258 +     *          {@code false} for Engineering
 16.3259 +     * @return string with canonical string representation of this
 16.3260 +     *         {@code BigDecimal}
 16.3261 +     */
 16.3262 +    private String layoutChars(boolean sci) {
 16.3263 +        if (scale == 0)                      // zero scale is trivial
 16.3264 +            return (intCompact != INFLATED) ?
 16.3265 +                Long.toString(intCompact):
 16.3266 +                intVal.toString();
 16.3267 +
 16.3268 +        StringBuilderHelper sbHelper = StringBuilderHelper.INSTANCE;
 16.3269 +        char[] coeff;
 16.3270 +        int offset;  // offset is the starting index for coeff array
 16.3271 +        // Get the significand as an absolute value
 16.3272 +        if (intCompact != INFLATED) {
 16.3273 +            offset = sbHelper.putIntCompact(Math.abs(intCompact));
 16.3274 +            coeff  = sbHelper.getCompactCharArray();
 16.3275 +        } else {
 16.3276 +            offset = 0;
 16.3277 +            coeff  = intVal.abs().toString().toCharArray();
 16.3278 +        }
 16.3279 +
 16.3280 +        // Construct a buffer, with sufficient capacity for all cases.
 16.3281 +        // If E-notation is needed, length will be: +1 if negative, +1
 16.3282 +        // if '.' needed, +2 for "E+", + up to 10 for adjusted exponent.
 16.3283 +        // Otherwise it could have +1 if negative, plus leading "0.00000"
 16.3284 +        StringBuilder buf = sbHelper.getStringBuilder();
 16.3285 +        if (signum() < 0)             // prefix '-' if negative
 16.3286 +            buf.append('-');
 16.3287 +        int coeffLen = coeff.length - offset;
 16.3288 +        long adjusted = -(long)scale + (coeffLen -1);
 16.3289 +        if ((scale >= 0) && (adjusted >= -6)) { // plain number
 16.3290 +            int pad = scale - coeffLen;         // count of padding zeros
 16.3291 +            if (pad >= 0) {                     // 0.xxx form
 16.3292 +                buf.append('0');
 16.3293 +                buf.append('.');
 16.3294 +                for (; pad>0; pad--) {
 16.3295 +                    buf.append('0');
 16.3296 +                }
 16.3297 +                buf.append(coeff, offset, coeffLen);
 16.3298 +            } else {                         // xx.xx form
 16.3299 +                buf.append(coeff, offset, -pad);
 16.3300 +                buf.append('.');
 16.3301 +                buf.append(coeff, -pad + offset, scale);
 16.3302 +            }
 16.3303 +        } else { // E-notation is needed
 16.3304 +            if (sci) {                       // Scientific notation
 16.3305 +                buf.append(coeff[offset]);   // first character
 16.3306 +                if (coeffLen > 1) {          // more to come
 16.3307 +                    buf.append('.');
 16.3308 +                    buf.append(coeff, offset + 1, coeffLen - 1);
 16.3309 +                }
 16.3310 +            } else {                         // Engineering notation
 16.3311 +                int sig = (int)(adjusted % 3);
 16.3312 +                if (sig < 0)
 16.3313 +                    sig += 3;                // [adjusted was negative]
 16.3314 +                adjusted -= sig;             // now a multiple of 3
 16.3315 +                sig++;
 16.3316 +                if (signum() == 0) {
 16.3317 +                    switch (sig) {
 16.3318 +                    case 1:
 16.3319 +                        buf.append('0'); // exponent is a multiple of three
 16.3320 +                        break;
 16.3321 +                    case 2:
 16.3322 +                        buf.append("0.00");
 16.3323 +                        adjusted += 3;
 16.3324 +                        break;
 16.3325 +                    case 3:
 16.3326 +                        buf.append("0.0");
 16.3327 +                        adjusted += 3;
 16.3328 +                        break;
 16.3329 +                    default:
 16.3330 +                        throw new AssertionError("Unexpected sig value " + sig);
 16.3331 +                    }
 16.3332 +                } else if (sig >= coeffLen) {   // significand all in integer
 16.3333 +                    buf.append(coeff, offset, coeffLen);
 16.3334 +                    // may need some zeros, too
 16.3335 +                    for (int i = sig - coeffLen; i > 0; i--)
 16.3336 +                        buf.append('0');
 16.3337 +                } else {                     // xx.xxE form
 16.3338 +                    buf.append(coeff, offset, sig);
 16.3339 +                    buf.append('.');
 16.3340 +                    buf.append(coeff, offset + sig, coeffLen - sig);
 16.3341 +                }
 16.3342 +            }
 16.3343 +            if (adjusted != 0) {             // [!sci could have made 0]
 16.3344 +                buf.append('E');
 16.3345 +                if (adjusted > 0)            // force sign for positive
 16.3346 +                    buf.append('+');
 16.3347 +                buf.append(adjusted);
 16.3348 +            }
 16.3349 +        }
 16.3350 +        return buf.toString();
 16.3351 +    }
 16.3352 +
 16.3353 +    /**
 16.3354 +     * Return 10 to the power n, as a {@code BigInteger}.
 16.3355 +     *
 16.3356 +     * @param  n the power of ten to be returned (>=0)
 16.3357 +     * @return a {@code BigInteger} with the value (10<sup>n</sup>)
 16.3358 +     */
 16.3359 +    private static BigInteger bigTenToThe(int n) {
 16.3360 +        if (n < 0)
 16.3361 +            return BigInteger.ZERO;
 16.3362 +
 16.3363 +        if (n < BIG_TEN_POWERS_TABLE_MAX) {
 16.3364 +            BigInteger[] pows = BIG_TEN_POWERS_TABLE;
 16.3365 +            if (n < pows.length)
 16.3366 +                return pows[n];
 16.3367 +            else
 16.3368 +                return expandBigIntegerTenPowers(n);
 16.3369 +        }
 16.3370 +        // BigInteger.pow is slow, so make 10**n by constructing a
 16.3371 +        // BigInteger from a character string (still not very fast)
 16.3372 +        char tenpow[] = new char[n + 1];
 16.3373 +        tenpow[0] = '1';
 16.3374 +        for (int i = 1; i <= n; i++)
 16.3375 +            tenpow[i] = '0';
 16.3376 +        return new BigInteger(tenpow);
 16.3377 +    }
 16.3378 +
 16.3379 +    /**
 16.3380 +     * Expand the BIG_TEN_POWERS_TABLE array to contain at least 10**n.
 16.3381 +     *
 16.3382 +     * @param n the power of ten to be returned (>=0)
 16.3383 +     * @return a {@code BigDecimal} with the value (10<sup>n</sup>) and
 16.3384 +     *         in the meantime, the BIG_TEN_POWERS_TABLE array gets
 16.3385 +     *         expanded to the size greater than n.
 16.3386 +     */
 16.3387 +    private static BigInteger expandBigIntegerTenPowers(int n) {
 16.3388 +        synchronized(BigDecimal.class) {
 16.3389 +            BigInteger[] pows = BIG_TEN_POWERS_TABLE;
 16.3390 +            int curLen = pows.length;
 16.3391 +            // The following comparison and the above synchronized statement is
 16.3392 +            // to prevent multiple threads from expanding the same array.
 16.3393 +            if (curLen <= n) {
 16.3394 +                int newLen = curLen << 1;
 16.3395 +                while (newLen <= n)
 16.3396 +                    newLen <<= 1;
 16.3397 +                pows = Arrays.copyOf(pows, newLen);
 16.3398 +                for (int i = curLen; i < newLen; i++)
 16.3399 +                    pows[i] = pows[i - 1].multiply(BigInteger.TEN);
 16.3400 +                // Based on the following facts:
 16.3401 +                // 1. pows is a private local varible;
 16.3402 +                // 2. the following store is a volatile store.
 16.3403 +                // the newly created array elements can be safely published.
 16.3404 +                BIG_TEN_POWERS_TABLE = pows;
 16.3405 +            }
 16.3406 +            return pows[n];
 16.3407 +        }
 16.3408 +    }
 16.3409 +
 16.3410 +    private static final long[] LONG_TEN_POWERS_TABLE = {
 16.3411 +        1,                     // 0 / 10^0
 16.3412 +        10,                    // 1 / 10^1
 16.3413 +        100,                   // 2 / 10^2
 16.3414 +        1000,                  // 3 / 10^3
 16.3415 +        10000,                 // 4 / 10^4
 16.3416 +        100000,                // 5 / 10^5
 16.3417 +        1000000,               // 6 / 10^6
 16.3418 +        10000000,              // 7 / 10^7
 16.3419 +        100000000,             // 8 / 10^8
 16.3420 +        1000000000,            // 9 / 10^9
 16.3421 +        10000000000L,          // 10 / 10^10
 16.3422 +        100000000000L,         // 11 / 10^11
 16.3423 +        1000000000000L,        // 12 / 10^12
 16.3424 +        10000000000000L,       // 13 / 10^13
 16.3425 +        100000000000000L,      // 14 / 10^14
 16.3426 +        1000000000000000L,     // 15 / 10^15
 16.3427 +        10000000000000000L,    // 16 / 10^16
 16.3428 +        100000000000000000L,   // 17 / 10^17
 16.3429 +        1000000000000000000L   // 18 / 10^18
 16.3430 +    };
 16.3431 +
 16.3432 +    private static volatile BigInteger BIG_TEN_POWERS_TABLE[] = {BigInteger.ONE,
 16.3433 +        BigInteger.valueOf(10),       BigInteger.valueOf(100),
 16.3434 +        BigInteger.valueOf(1000),     BigInteger.valueOf(10000),
 16.3435 +        BigInteger.valueOf(100000),   BigInteger.valueOf(1000000),
 16.3436 +        BigInteger.valueOf(10000000), BigInteger.valueOf(100000000),
 16.3437 +        BigInteger.valueOf(1000000000),
 16.3438 +        BigInteger.valueOf(10000000000L),
 16.3439 +        BigInteger.valueOf(100000000000L),
 16.3440 +        BigInteger.valueOf(1000000000000L),
 16.3441 +        BigInteger.valueOf(10000000000000L),
 16.3442 +        BigInteger.valueOf(100000000000000L),
 16.3443 +        BigInteger.valueOf(1000000000000000L),
 16.3444 +        BigInteger.valueOf(10000000000000000L),
 16.3445 +        BigInteger.valueOf(100000000000000000L),
 16.3446 +        BigInteger.valueOf(1000000000000000000L)
 16.3447 +    };
 16.3448 +
 16.3449 +    private static final int BIG_TEN_POWERS_TABLE_INITLEN =
 16.3450 +        BIG_TEN_POWERS_TABLE.length;
 16.3451 +    private static final int BIG_TEN_POWERS_TABLE_MAX =
 16.3452 +        16 * BIG_TEN_POWERS_TABLE_INITLEN;
 16.3453 +
 16.3454 +    private static final long THRESHOLDS_TABLE[] = {
 16.3455 +        Long.MAX_VALUE,                     // 0
 16.3456 +        Long.MAX_VALUE/10L,                 // 1
 16.3457 +        Long.MAX_VALUE/100L,                // 2
 16.3458 +        Long.MAX_VALUE/1000L,               // 3
 16.3459 +        Long.MAX_VALUE/10000L,              // 4
 16.3460 +        Long.MAX_VALUE/100000L,             // 5
 16.3461 +        Long.MAX_VALUE/1000000L,            // 6
 16.3462 +        Long.MAX_VALUE/10000000L,           // 7
 16.3463 +        Long.MAX_VALUE/100000000L,          // 8
 16.3464 +        Long.MAX_VALUE/1000000000L,         // 9
 16.3465 +        Long.MAX_VALUE/10000000000L,        // 10
 16.3466 +        Long.MAX_VALUE/100000000000L,       // 11
 16.3467 +        Long.MAX_VALUE/1000000000000L,      // 12
 16.3468 +        Long.MAX_VALUE/10000000000000L,     // 13
 16.3469 +        Long.MAX_VALUE/100000000000000L,    // 14
 16.3470 +        Long.MAX_VALUE/1000000000000000L,   // 15
 16.3471 +        Long.MAX_VALUE/10000000000000000L,  // 16
 16.3472 +        Long.MAX_VALUE/100000000000000000L, // 17
 16.3473 +        Long.MAX_VALUE/1000000000000000000L // 18
 16.3474 +    };
 16.3475 +
 16.3476 +    /**
 16.3477 +     * Compute val * 10 ^ n; return this product if it is
 16.3478 +     * representable as a long, INFLATED otherwise.
 16.3479 +     */
 16.3480 +    private static long longMultiplyPowerTen(long val, int n) {
 16.3481 +        if (val == 0 || n <= 0)
 16.3482 +            return val;
 16.3483 +        long[] tab = LONG_TEN_POWERS_TABLE;
 16.3484 +        long[] bounds = THRESHOLDS_TABLE;
 16.3485 +        if (n < tab.length && n < bounds.length) {
 16.3486 +            long tenpower = tab[n];
 16.3487 +            if (val == 1)
 16.3488 +                return tenpower;
 16.3489 +            if (Math.abs(val) <= bounds[n])
 16.3490 +                return val * tenpower;
 16.3491 +        }
 16.3492 +        return INFLATED;
 16.3493 +    }
 16.3494 +
 16.3495 +    /**
 16.3496 +     * Compute this * 10 ^ n.
 16.3497 +     * Needed mainly to allow special casing to trap zero value
 16.3498 +     */
 16.3499 +    private BigInteger bigMultiplyPowerTen(int n) {
 16.3500 +        if (n <= 0)
 16.3501 +            return this.inflate();
 16.3502 +
 16.3503 +        if (intCompact != INFLATED)
 16.3504 +            return bigTenToThe(n).multiply(intCompact);
 16.3505 +        else
 16.3506 +            return intVal.multiply(bigTenToThe(n));
 16.3507 +    }
 16.3508 +
 16.3509 +    /**
 16.3510 +     * Assign appropriate BigInteger to intVal field if intVal is
 16.3511 +     * null, i.e. the compact representation is in use.
 16.3512 +     */
 16.3513 +    private BigInteger inflate() {
 16.3514 +        if (intVal == null)
 16.3515 +            intVal = BigInteger.valueOf(intCompact);
 16.3516 +        return intVal;
 16.3517 +    }
 16.3518 +
 16.3519 +    /**
 16.3520 +     * Match the scales of two {@code BigDecimal}s to align their
 16.3521 +     * least significant digits.
 16.3522 +     *
 16.3523 +     * <p>If the scales of val[0] and val[1] differ, rescale
 16.3524 +     * (non-destructively) the lower-scaled {@code BigDecimal} so
 16.3525 +     * they match.  That is, the lower-scaled reference will be
 16.3526 +     * replaced by a reference to a new object with the same scale as
 16.3527 +     * the other {@code BigDecimal}.
 16.3528 +     *
 16.3529 +     * @param  val array of two elements referring to the two
 16.3530 +     *         {@code BigDecimal}s to be aligned.
 16.3531 +     */
 16.3532 +    private static void matchScale(BigDecimal[] val) {
 16.3533 +        if (val[0].scale == val[1].scale) {
 16.3534 +            return;
 16.3535 +        } else if (val[0].scale < val[1].scale) {
 16.3536 +            val[0] = val[0].setScale(val[1].scale, ROUND_UNNECESSARY);
 16.3537 +        } else if (val[1].scale < val[0].scale) {
 16.3538 +            val[1] = val[1].setScale(val[0].scale, ROUND_UNNECESSARY);
 16.3539 +        }
 16.3540 +    }
 16.3541 +
 16.3542 +    /**
 16.3543 +     * Reconstitute the {@code BigDecimal} instance from a stream (that is,
 16.3544 +     * deserialize it).
 16.3545 +     *
 16.3546 +     * @param s the stream being read.
 16.3547 +     */
 16.3548 +    private void readObject(java.io.ObjectInputStream s)
 16.3549 +        throws java.io.IOException, ClassNotFoundException {
 16.3550 +        // Read in all fields
 16.3551 +        s.defaultReadObject();
 16.3552 +        // validate possibly bad fields
 16.3553 +        if (intVal == null) {
 16.3554 +            String message = "BigDecimal: null intVal in stream";
 16.3555 +            throw new java.io.StreamCorruptedException(message);
 16.3556 +        // [all values of scale are now allowed]
 16.3557 +        }
 16.3558 +        intCompact = compactValFor(intVal);
 16.3559 +    }
 16.3560 +
 16.3561 +   /**
 16.3562 +    * Serialize this {@code BigDecimal} to the stream in question
 16.3563 +    *
 16.3564 +    * @param s the stream to serialize to.
 16.3565 +    */
 16.3566 +   private void writeObject(java.io.ObjectOutputStream s)
 16.3567 +       throws java.io.IOException {
 16.3568 +       // Must inflate to maintain compatible serial form.
 16.3569 +       this.inflate();
 16.3570 +
 16.3571 +       // Write proper fields
 16.3572 +       s.defaultWriteObject();
 16.3573 +   }
 16.3574 +
 16.3575 +
 16.3576 +    /**
 16.3577 +     * Returns the length of the absolute value of a {@code long}, in decimal
 16.3578 +     * digits.
 16.3579 +     *
 16.3580 +     * @param x the {@code long}
 16.3581 +     * @return the length of the unscaled value, in deciaml digits.
 16.3582 +     */
 16.3583 +    private static int longDigitLength(long x) {
 16.3584 +        /*
 16.3585 +         * As described in "Bit Twiddling Hacks" by Sean Anderson,
 16.3586 +         * (http://graphics.stanford.edu/~seander/bithacks.html)
 16.3587 +         * integer log 10 of x is within 1 of
 16.3588 +         * (1233/4096)* (1 + integer log 2 of x).
 16.3589 +         * The fraction 1233/4096 approximates log10(2). So we first
 16.3590 +         * do a version of log2 (a variant of Long class with
 16.3591 +         * pre-checks and opposite directionality) and then scale and
 16.3592 +         * check against powers table. This is a little simpler in
 16.3593 +         * present context than the version in Hacker's Delight sec
 16.3594 +         * 11-4.  Adding one to bit length allows comparing downward
 16.3595 +         * from the LONG_TEN_POWERS_TABLE that we need anyway.
 16.3596 +         */
 16.3597 +        assert x != INFLATED;
 16.3598 +        if (x < 0)
 16.3599 +            x = -x;
 16.3600 +        if (x < 10) // must screen for 0, might as well 10
 16.3601 +            return 1;
 16.3602 +        int n = 64; // not 63, to avoid needing to add 1 later
 16.3603 +        int y = (int)(x >>> 32);
 16.3604 +        if (y == 0) { n -= 32; y = (int)x; }
 16.3605 +        if (y >>> 16 == 0) { n -= 16; y <<= 16; }
 16.3606 +        if (y >>> 24 == 0) { n -=  8; y <<=  8; }
 16.3607 +        if (y >>> 28 == 0) { n -=  4; y <<=  4; }
 16.3608 +        if (y >>> 30 == 0) { n -=  2; y <<=  2; }
 16.3609 +        int r = (((y >>> 31) + n) * 1233) >>> 12;
 16.3610 +        long[] tab = LONG_TEN_POWERS_TABLE;
 16.3611 +        // if r >= length, must have max possible digits for long
 16.3612 +        return (r >= tab.length || x < tab[r])? r : r+1;
 16.3613 +    }
 16.3614 +
 16.3615 +    /**
 16.3616 +     * Returns the length of the absolute value of a BigInteger, in
 16.3617 +     * decimal digits.
 16.3618 +     *
 16.3619 +     * @param b the BigInteger
 16.3620 +     * @return the length of the unscaled value, in decimal digits
 16.3621 +     */
 16.3622 +    private static int bigDigitLength(BigInteger b) {
 16.3623 +        /*
 16.3624 +         * Same idea as the long version, but we need a better
 16.3625 +         * approximation of log10(2). Using 646456993/2^31
 16.3626 +         * is accurate up to max possible reported bitLength.
 16.3627 +         */
 16.3628 +        if (b.signum == 0)
 16.3629 +            return 1;
 16.3630 +        int r = (int)((((long)b.bitLength() + 1) * 646456993) >>> 31);
 16.3631 +        return b.compareMagnitude(bigTenToThe(r)) < 0? r : r+1;
 16.3632 +    }
 16.3633 +
 16.3634 +
 16.3635 +    /**
 16.3636 +     * Remove insignificant trailing zeros from this
 16.3637 +     * {@code BigDecimal} until the preferred scale is reached or no
 16.3638 +     * more zeros can be removed.  If the preferred scale is less than
 16.3639 +     * Integer.MIN_VALUE, all the trailing zeros will be removed.
 16.3640 +     *
 16.3641 +     * {@code BigInteger} assistance could help, here?
 16.3642 +     *
 16.3643 +     * <p>WARNING: This method should only be called on new objects as
 16.3644 +     * it mutates the value fields.
 16.3645 +     *
 16.3646 +     * @return this {@code BigDecimal} with a scale possibly reduced
 16.3647 +     * to be closed to the preferred scale.
 16.3648 +     */
 16.3649 +    private BigDecimal stripZerosToMatchScale(long preferredScale) {
 16.3650 +        this.inflate();
 16.3651 +        BigInteger qr[];                // quotient-remainder pair
 16.3652 +        while ( intVal.compareMagnitude(BigInteger.TEN) >= 0 &&
 16.3653 +                scale > preferredScale) {
 16.3654 +            if (intVal.testBit(0))
 16.3655 +                break;                  // odd number cannot end in 0
 16.3656 +            qr = intVal.divideAndRemainder(BigInteger.TEN);
 16.3657 +            if (qr[1].signum() != 0)
 16.3658 +                break;                  // non-0 remainder
 16.3659 +            intVal=qr[0];
 16.3660 +            scale = checkScale((long)scale-1);  // could Overflow
 16.3661 +            if (precision > 0)          // adjust precision if known
 16.3662 +              precision--;
 16.3663 +        }
 16.3664 +        if (intVal != null)
 16.3665 +            intCompact = compactValFor(intVal);
 16.3666 +        return this;
 16.3667 +    }
 16.3668 +
 16.3669 +    /**
 16.3670 +     * Check a scale for Underflow or Overflow.  If this BigDecimal is
 16.3671 +     * nonzero, throw an exception if the scale is outof range. If this
 16.3672 +     * is zero, saturate the scale to the extreme value of the right
 16.3673 +     * sign if the scale is out of range.
 16.3674 +     *
 16.3675 +     * @param val The new scale.
 16.3676 +     * @throws ArithmeticException (overflow or underflow) if the new
 16.3677 +     *         scale is out of range.
 16.3678 +     * @return validated scale as an int.
 16.3679 +     */
 16.3680 +    private int checkScale(long val) {
 16.3681 +        int asInt = (int)val;
 16.3682 +        if (asInt != val) {
 16.3683 +            asInt = val>Integer.MAX_VALUE ? Integer.MAX_VALUE : Integer.MIN_VALUE;
 16.3684 +            BigInteger b;
 16.3685 +            if (intCompact != 0 &&
 16.3686 +                ((b = intVal) == null || b.signum() != 0))
 16.3687 +                throw new ArithmeticException(asInt>0 ? "Underflow":"Overflow");
 16.3688 +        }
 16.3689 +        return asInt;
 16.3690 +    }
 16.3691 +
 16.3692 +    /**
 16.3693 +     * Round an operand; used only if digits &gt; 0.  Does not change
 16.3694 +     * {@code this}; if rounding is needed a new {@code BigDecimal}
 16.3695 +     * is created and returned.
 16.3696 +     *
 16.3697 +     * @param mc the context to use.
 16.3698 +     * @throws ArithmeticException if the result is inexact but the
 16.3699 +     *         rounding mode is {@code UNNECESSARY}.
 16.3700 +     */
 16.3701 +    private BigDecimal roundOp(MathContext mc) {
 16.3702 +        BigDecimal rounded = doRound(this, mc);
 16.3703 +        return rounded;
 16.3704 +    }
 16.3705 +
 16.3706 +    /** Round this BigDecimal according to the MathContext settings;
 16.3707 +     *  used only if precision {@literal >} 0.
 16.3708 +     *
 16.3709 +     * <p>WARNING: This method should only be called on new objects as
 16.3710 +     * it mutates the value fields.
 16.3711 +     *
 16.3712 +     * @param mc the context to use.
 16.3713 +     * @throws ArithmeticException if the rounding mode is
 16.3714 +     *         {@code RoundingMode.UNNECESSARY} and the
 16.3715 +     *         {@code BigDecimal} operation would require rounding.
 16.3716 +     */
 16.3717 +    private void roundThis(MathContext mc) {
 16.3718 +        BigDecimal rounded = doRound(this, mc);
 16.3719 +        if (rounded == this)                 // wasn't rounded
 16.3720 +            return;
 16.3721 +        this.intVal     = rounded.intVal;
 16.3722 +        this.intCompact = rounded.intCompact;
 16.3723 +        this.scale      = rounded.scale;
 16.3724 +        this.precision  = rounded.precision;
 16.3725 +    }
 16.3726 +
 16.3727 +    /**
 16.3728 +     * Returns a {@code BigDecimal} rounded according to the
 16.3729 +     * MathContext settings; used only if {@code mc.precision > 0}.
 16.3730 +     * Does not change {@code this}; if rounding is needed a new
 16.3731 +     * {@code BigDecimal} is created and returned.
 16.3732 +     *
 16.3733 +     * @param mc the context to use.
 16.3734 +     * @return a {@code BigDecimal} rounded according to the MathContext
 16.3735 +     *         settings.  May return this, if no rounding needed.
 16.3736 +     * @throws ArithmeticException if the rounding mode is
 16.3737 +     *         {@code RoundingMode.UNNECESSARY} and the
 16.3738 +     *         result is inexact.
 16.3739 +     */
 16.3740 +    private static BigDecimal doRound(BigDecimal d, MathContext mc) {
 16.3741 +        int mcp = mc.precision;
 16.3742 +        int drop;
 16.3743 +        // This might (rarely) iterate to cover the 999=>1000 case
 16.3744 +        while ((drop = d.precision() - mcp) > 0) {
 16.3745 +            int newScale = d.checkScale((long)d.scale - drop);
 16.3746 +            int mode = mc.roundingMode.oldMode;
 16.3747 +            if (drop < LONG_TEN_POWERS_TABLE.length)
 16.3748 +                d = divideAndRound(d.intCompact, d.intVal,
 16.3749 +                                   LONG_TEN_POWERS_TABLE[drop], null,
 16.3750 +                                   newScale, mode, newScale);
 16.3751 +            else
 16.3752 +                d = divideAndRound(d.intCompact, d.intVal,
 16.3753 +                                   INFLATED, bigTenToThe(drop),
 16.3754 +                                   newScale, mode, newScale);
 16.3755 +        }
 16.3756 +        return d;
 16.3757 +    }
 16.3758 +
 16.3759 +    /**
 16.3760 +     * Returns the compact value for given {@code BigInteger}, or
 16.3761 +     * INFLATED if too big. Relies on internal representation of
 16.3762 +     * {@code BigInteger}.
 16.3763 +     */
 16.3764 +    private static long compactValFor(BigInteger b) {
 16.3765 +        int[] m = b.mag;
 16.3766 +        int len = m.length;
 16.3767 +        if (len == 0)
 16.3768 +            return 0;
 16.3769 +        int d = m[0];
 16.3770 +        if (len > 2 || (len == 2 && d < 0))
 16.3771 +            return INFLATED;
 16.3772 +
 16.3773 +        long u = (len == 2)?
 16.3774 +            (((long) m[1] & LONG_MASK) + (((long)d) << 32)) :
 16.3775 +            (((long)d)   & LONG_MASK);
 16.3776 +        return (b.signum < 0)? -u : u;
 16.3777 +    }
 16.3778 +
 16.3779 +    private static int longCompareMagnitude(long x, long y) {
 16.3780 +        if (x < 0)
 16.3781 +            x = -x;
 16.3782 +        if (y < 0)
 16.3783 +            y = -y;
 16.3784 +        return (x < y) ? -1 : ((x == y) ? 0 : 1);
 16.3785 +    }
 16.3786 +
 16.3787 +    private static int saturateLong(long s) {
 16.3788 +        int i = (int)s;
 16.3789 +        return (s == i) ? i : (s < 0 ? Integer.MIN_VALUE : Integer.MAX_VALUE);
 16.3790 +    }
 16.3791 +
 16.3792 +    /*
 16.3793 +     * Internal printing routine
 16.3794 +     */
 16.3795 +    private static void print(String name, BigDecimal bd) {
 16.3796 +    }
 16.3797 +
 16.3798 +    /**
 16.3799 +     * Check internal invariants of this BigDecimal.  These invariants
 16.3800 +     * include:
 16.3801 +     *
 16.3802 +     * <ul>
 16.3803 +     *
 16.3804 +     * <li>The object must be initialized; either intCompact must not be
 16.3805 +     * INFLATED or intVal is non-null.  Both of these conditions may
 16.3806 +     * be true.
 16.3807 +     *
 16.3808 +     * <li>If both intCompact and intVal and set, their values must be
 16.3809 +     * consistent.
 16.3810 +     *
 16.3811 +     * <li>If precision is nonzero, it must have the right value.
 16.3812 +     * </ul>
 16.3813 +     *
 16.3814 +     * Note: Since this is an audit method, we are not supposed to change the
 16.3815 +     * state of this BigDecimal object.
 16.3816 +     */
 16.3817 +    private BigDecimal audit() {
 16.3818 +        if (intCompact == INFLATED) {
 16.3819 +            if (intVal == null) {
 16.3820 +                print("audit", this);
 16.3821 +                throw new AssertionError("null intVal");
 16.3822 +            }
 16.3823 +            // Check precision
 16.3824 +            if (precision > 0 && precision != bigDigitLength(intVal)) {
 16.3825 +                print("audit", this);
 16.3826 +                throw new AssertionError("precision mismatch");
 16.3827 +            }
 16.3828 +        } else {
 16.3829 +            if (intVal != null) {
 16.3830 +                long val = intVal.longValue();
 16.3831 +                if (val != intCompact) {
 16.3832 +                    print("audit", this);
 16.3833 +                    throw new AssertionError("Inconsistent state, intCompact=" +
 16.3834 +                                             intCompact + "\t intVal=" + val);
 16.3835 +                }
 16.3836 +            }
 16.3837 +            // Check precision
 16.3838 +            if (precision > 0 && precision != longDigitLength(intCompact)) {
 16.3839 +                print("audit", this);
 16.3840 +                throw new AssertionError("precision mismatch");
 16.3841 +            }
 16.3842 +        }
 16.3843 +        return this;
 16.3844 +    }
 16.3845 +}
    17.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.2 +++ b/rt/emul/compact/src/main/java/java/math/BigInteger.java	Sun Sep 08 11:22:51 2013 +0200
    17.3 @@ -0,0 +1,3122 @@
    17.4 +/*
    17.5 + * Copyright (c) 1996, 2007, Oracle and/or its affiliates. All rights reserved.
    17.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    17.7 + *
    17.8 + * This code is free software; you can redistribute it and/or modify it
    17.9 + * under the terms of the GNU General Public License version 2 only, as
   17.10 + * published by the Free Software Foundation.  Oracle designates this
   17.11 + * particular file as subject to the "Classpath" exception as provided
   17.12 + * by Oracle in the LICENSE file that accompanied this code.
   17.13 + *
   17.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   17.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   17.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   17.17 + * version 2 for more details (a copy is included in the LICENSE file that
   17.18 + * accompanied this code).
   17.19 + *
   17.20 + * You should have received a copy of the GNU General Public License version
   17.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   17.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   17.23 + *
   17.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   17.25 + * or visit www.oracle.com if you need additional information or have any
   17.26 + * questions.
   17.27 + */
   17.28 +
   17.29 +/*
   17.30 + * Portions Copyright (c) 1995  Colin Plumb.  All rights reserved.
   17.31 + */
   17.32 +
   17.33 +package java.math;
   17.34 +
   17.35 +import java.util.Random;
   17.36 +import java.io.*;
   17.37 +
   17.38 +/**
   17.39 + * Immutable arbitrary-precision integers.  All operations behave as if
   17.40 + * BigIntegers were represented in two's-complement notation (like Java's
   17.41 + * primitive integer types).  BigInteger provides analogues to all of Java's
   17.42 + * primitive integer operators, and all relevant methods from java.lang.Math.
   17.43 + * Additionally, BigInteger provides operations for modular arithmetic, GCD
   17.44 + * calculation, primality testing, prime generation, bit manipulation,
   17.45 + * and a few other miscellaneous operations.
   17.46 + *
   17.47 + * <p>Semantics of arithmetic operations exactly mimic those of Java's integer
   17.48 + * arithmetic operators, as defined in <i>The Java Language Specification</i>.
   17.49 + * For example, division by zero throws an {@code ArithmeticException}, and
   17.50 + * division of a negative by a positive yields a negative (or zero) remainder.
   17.51 + * All of the details in the Spec concerning overflow are ignored, as
   17.52 + * BigIntegers are made as large as necessary to accommodate the results of an
   17.53 + * operation.
   17.54 + *
   17.55 + * <p>Semantics of shift operations extend those of Java's shift operators
   17.56 + * to allow for negative shift distances.  A right-shift with a negative
   17.57 + * shift distance results in a left shift, and vice-versa.  The unsigned
   17.58 + * right shift operator ({@code >>>}) is omitted, as this operation makes
   17.59 + * little sense in combination with the "infinite word size" abstraction
   17.60 + * provided by this class.
   17.61 + *
   17.62 + * <p>Semantics of bitwise logical operations exactly mimic those of Java's
   17.63 + * bitwise integer operators.  The binary operators ({@code and},
   17.64 + * {@code or}, {@code xor}) implicitly perform sign extension on the shorter
   17.65 + * of the two operands prior to performing the operation.
   17.66 + *
   17.67 + * <p>Comparison operations perform signed integer comparisons, analogous to
   17.68 + * those performed by Java's relational and equality operators.
   17.69 + *
   17.70 + * <p>Modular arithmetic operations are provided to compute residues, perform
   17.71 + * exponentiation, and compute multiplicative inverses.  These methods always
   17.72 + * return a non-negative result, between {@code 0} and {@code (modulus - 1)},
   17.73 + * inclusive.
   17.74 + *
   17.75 + * <p>Bit operations operate on a single bit of the two's-complement
   17.76 + * representation of their operand.  If necessary, the operand is sign-
   17.77 + * extended so that it contains the designated bit.  None of the single-bit
   17.78 + * operations can produce a BigInteger with a different sign from the
   17.79 + * BigInteger being operated on, as they affect only a single bit, and the
   17.80 + * "infinite word size" abstraction provided by this class ensures that there
   17.81 + * are infinitely many "virtual sign bits" preceding each BigInteger.
   17.82 + *
   17.83 + * <p>For the sake of brevity and clarity, pseudo-code is used throughout the
   17.84 + * descriptions of BigInteger methods.  The pseudo-code expression
   17.85 + * {@code (i + j)} is shorthand for "a BigInteger whose value is
   17.86 + * that of the BigInteger {@code i} plus that of the BigInteger {@code j}."
   17.87 + * The pseudo-code expression {@code (i == j)} is shorthand for
   17.88 + * "{@code true} if and only if the BigInteger {@code i} represents the same
   17.89 + * value as the BigInteger {@code j}."  Other pseudo-code expressions are
   17.90 + * interpreted similarly.
   17.91 + *
   17.92 + * <p>All methods and constructors in this class throw
   17.93 + * {@code NullPointerException} when passed
   17.94 + * a null object reference for any input parameter.
   17.95 + *
   17.96 + * @see     BigDecimal
   17.97 + * @author  Josh Bloch
   17.98 + * @author  Michael McCloskey
   17.99 + * @since JDK1.1
  17.100 + */
  17.101 +
  17.102 +public class BigInteger extends Number implements Comparable<BigInteger> {
  17.103 +    /**
  17.104 +     * The signum of this BigInteger: -1 for negative, 0 for zero, or
  17.105 +     * 1 for positive.  Note that the BigInteger zero <i>must</i> have
  17.106 +     * a signum of 0.  This is necessary to ensures that there is exactly one
  17.107 +     * representation for each BigInteger value.
  17.108 +     *
  17.109 +     * @serial
  17.110 +     */
  17.111 +    final int signum;
  17.112 +
  17.113 +    /**
  17.114 +     * The magnitude of this BigInteger, in <i>big-endian</i> order: the
  17.115 +     * zeroth element of this array is the most-significant int of the
  17.116 +     * magnitude.  The magnitude must be "minimal" in that the most-significant
  17.117 +     * int ({@code mag[0]}) must be non-zero.  This is necessary to
  17.118 +     * ensure that there is exactly one representation for each BigInteger
  17.119 +     * value.  Note that this implies that the BigInteger zero has a
  17.120 +     * zero-length mag array.
  17.121 +     */
  17.122 +    final int[] mag;
  17.123 +
  17.124 +    // These "redundant fields" are initialized with recognizable nonsense
  17.125 +    // values, and cached the first time they are needed (or never, if they
  17.126 +    // aren't needed).
  17.127 +
  17.128 +     /**
  17.129 +     * One plus the bitCount of this BigInteger. Zeros means unitialized.
  17.130 +     *
  17.131 +     * @serial
  17.132 +     * @see #bitCount
  17.133 +     * @deprecated Deprecated since logical value is offset from stored
  17.134 +     * value and correction factor is applied in accessor method.
  17.135 +     */
  17.136 +    @Deprecated
  17.137 +    private int bitCount;
  17.138 +
  17.139 +    /**
  17.140 +     * One plus the bitLength of this BigInteger. Zeros means unitialized.
  17.141 +     * (either value is acceptable).
  17.142 +     *
  17.143 +     * @serial
  17.144 +     * @see #bitLength()
  17.145 +     * @deprecated Deprecated since logical value is offset from stored
  17.146 +     * value and correction factor is applied in accessor method.
  17.147 +     */
  17.148 +    @Deprecated
  17.149 +    private int bitLength;
  17.150 +
  17.151 +    /**
  17.152 +     * Two plus the lowest set bit of this BigInteger, as returned by
  17.153 +     * getLowestSetBit().
  17.154 +     *
  17.155 +     * @serial
  17.156 +     * @see #getLowestSetBit
  17.157 +     * @deprecated Deprecated since logical value is offset from stored
  17.158 +     * value and correction factor is applied in accessor method.
  17.159 +     */
  17.160 +    @Deprecated
  17.161 +    private int lowestSetBit;
  17.162 +
  17.163 +    /**
  17.164 +     * Two plus the index of the lowest-order int in the magnitude of this
  17.165 +     * BigInteger that contains a nonzero int, or -2 (either value is acceptable).
  17.166 +     * The least significant int has int-number 0, the next int in order of
  17.167 +     * increasing significance has int-number 1, and so forth.
  17.168 +     * @deprecated Deprecated since logical value is offset from stored
  17.169 +     * value and correction factor is applied in accessor method.
  17.170 +     */
  17.171 +    @Deprecated
  17.172 +    private int firstNonzeroIntNum;
  17.173 +
  17.174 +    /**
  17.175 +     * This mask is used to obtain the value of an int as if it were unsigned.
  17.176 +     */
  17.177 +    final static long LONG_MASK = 0xffffffffL;
  17.178 +
  17.179 +    //Constructors
  17.180 +
  17.181 +    /**
  17.182 +     * Translates a byte array containing the two's-complement binary
  17.183 +     * representation of a BigInteger into a BigInteger.  The input array is
  17.184 +     * assumed to be in <i>big-endian</i> byte-order: the most significant
  17.185 +     * byte is in the zeroth element.
  17.186 +     *
  17.187 +     * @param  val big-endian two's-complement binary representation of
  17.188 +     *         BigInteger.
  17.189 +     * @throws NumberFormatException {@code val} is zero bytes long.
  17.190 +     */
  17.191 +    public BigInteger(byte[] val) {
  17.192 +        if (val.length == 0)
  17.193 +            throw new NumberFormatException("Zero length BigInteger");
  17.194 +
  17.195 +        if (val[0] < 0) {
  17.196 +            mag = makePositive(val);
  17.197 +            signum = -1;
  17.198 +        } else {
  17.199 +            mag = stripLeadingZeroBytes(val);
  17.200 +            signum = (mag.length == 0 ? 0 : 1);
  17.201 +        }
  17.202 +    }
  17.203 +
  17.204 +    /**
  17.205 +     * This private constructor translates an int array containing the
  17.206 +     * two's-complement binary representation of a BigInteger into a
  17.207 +     * BigInteger. The input array is assumed to be in <i>big-endian</i>
  17.208 +     * int-order: the most significant int is in the zeroth element.
  17.209 +     */
  17.210 +    private BigInteger(int[] val) {
  17.211 +        if (val.length == 0)
  17.212 +            throw new NumberFormatException("Zero length BigInteger");
  17.213 +
  17.214 +        if (val[0] < 0) {
  17.215 +            mag = makePositive(val);
  17.216 +            signum = -1;
  17.217 +        } else {
  17.218 +            mag = trustedStripLeadingZeroInts(val);
  17.219 +            signum = (mag.length == 0 ? 0 : 1);
  17.220 +        }
  17.221 +    }
  17.222 +
  17.223 +    /**
  17.224 +     * Translates the sign-magnitude representation of a BigInteger into a
  17.225 +     * BigInteger.  The sign is represented as an integer signum value: -1 for
  17.226 +     * negative, 0 for zero, or 1 for positive.  The magnitude is a byte array
  17.227 +     * in <i>big-endian</i> byte-order: the most significant byte is in the
  17.228 +     * zeroth element.  A zero-length magnitude array is permissible, and will
  17.229 +     * result in a BigInteger value of 0, whether signum is -1, 0 or 1.
  17.230 +     *
  17.231 +     * @param  signum signum of the number (-1 for negative, 0 for zero, 1
  17.232 +     *         for positive).
  17.233 +     * @param  magnitude big-endian binary representation of the magnitude of
  17.234 +     *         the number.
  17.235 +     * @throws NumberFormatException {@code signum} is not one of the three
  17.236 +     *         legal values (-1, 0, and 1), or {@code signum} is 0 and
  17.237 +     *         {@code magnitude} contains one or more non-zero bytes.
  17.238 +     */
  17.239 +    public BigInteger(int signum, byte[] magnitude) {
  17.240 +        this.mag = stripLeadingZeroBytes(magnitude);
  17.241 +
  17.242 +        if (signum < -1 || signum > 1)
  17.243 +            throw(new NumberFormatException("Invalid signum value"));
  17.244 +
  17.245 +        if (this.mag.length==0) {
  17.246 +            this.signum = 0;
  17.247 +        } else {
  17.248 +            if (signum == 0)
  17.249 +                throw(new NumberFormatException("signum-magnitude mismatch"));
  17.250 +            this.signum = signum;
  17.251 +        }
  17.252 +    }
  17.253 +
  17.254 +    /**
  17.255 +     * A constructor for internal use that translates the sign-magnitude
  17.256 +     * representation of a BigInteger into a BigInteger. It checks the
  17.257 +     * arguments and copies the magnitude so this constructor would be
  17.258 +     * safe for external use.
  17.259 +     */
  17.260 +    private BigInteger(int signum, int[] magnitude) {
  17.261 +        this.mag = stripLeadingZeroInts(magnitude);
  17.262 +
  17.263 +        if (signum < -1 || signum > 1)
  17.264 +            throw(new NumberFormatException("Invalid signum value"));
  17.265 +
  17.266 +        if (this.mag.length==0) {
  17.267 +            this.signum = 0;
  17.268 +        } else {
  17.269 +            if (signum == 0)
  17.270 +                throw(new NumberFormatException("signum-magnitude mismatch"));
  17.271 +            this.signum = signum;
  17.272 +        }
  17.273 +    }
  17.274 +
  17.275 +    /**
  17.276 +     * Translates the String representation of a BigInteger in the
  17.277 +     * specified radix into a BigInteger.  The String representation
  17.278 +     * consists of an optional minus or plus sign followed by a
  17.279 +     * sequence of one or more digits in the specified radix.  The
  17.280 +     * character-to-digit mapping is provided by {@code
  17.281 +     * Character.digit}.  The String may not contain any extraneous
  17.282 +     * characters (whitespace, for example).
  17.283 +     *
  17.284 +     * @param val String representation of BigInteger.
  17.285 +     * @param radix radix to be used in interpreting {@code val}.
  17.286 +     * @throws NumberFormatException {@code val} is not a valid representation
  17.287 +     *         of a BigInteger in the specified radix, or {@code radix} is
  17.288 +     *         outside the range from {@link Character#MIN_RADIX} to
  17.289 +     *         {@link Character#MAX_RADIX}, inclusive.
  17.290 +     * @see    Character#digit
  17.291 +     */
  17.292 +    public BigInteger(String val, int radix) {
  17.293 +        int cursor = 0, numDigits;
  17.294 +        final int len = val.length();
  17.295 +
  17.296 +        if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
  17.297 +            throw new NumberFormatException("Radix out of range");
  17.298 +        if (len == 0)
  17.299 +            throw new NumberFormatException("Zero length BigInteger");
  17.300 +
  17.301 +        // Check for at most one leading sign
  17.302 +        int sign = 1;
  17.303 +        int index1 = val.lastIndexOf('-');
  17.304 +        int index2 = val.lastIndexOf('+');
  17.305 +        if ((index1 + index2) <= -1) {
  17.306 +            // No leading sign character or at most one leading sign character
  17.307 +            if (index1 == 0 || index2 == 0) {
  17.308 +                cursor = 1;
  17.309 +                if (len == 1)
  17.310 +                    throw new NumberFormatException("Zero length BigInteger");
  17.311 +            }
  17.312 +            if (index1 == 0)
  17.313 +                sign = -1;
  17.314 +        } else
  17.315 +            throw new NumberFormatException("Illegal embedded sign character");
  17.316 +
  17.317 +        // Skip leading zeros and compute number of digits in magnitude
  17.318 +        while (cursor < len &&
  17.319 +               Character.digit(val.charAt(cursor), radix) == 0)
  17.320 +            cursor++;
  17.321 +        if (cursor == len) {
  17.322 +            signum = 0;
  17.323 +            mag = ZERO.mag;
  17.324 +            return;
  17.325 +        }
  17.326 +
  17.327 +        numDigits = len - cursor;
  17.328 +        signum = sign;
  17.329 +
  17.330 +        // Pre-allocate array of expected size. May be too large but can
  17.331 +        // never be too small. Typically exact.
  17.332 +        int numBits = (int)(((numDigits * bitsPerDigit[radix]) >>> 10) + 1);
  17.333 +        int numWords = (numBits + 31) >>> 5;
  17.334 +        int[] magnitude = new int[numWords];
  17.335 +
  17.336 +        // Process first (potentially short) digit group
  17.337 +        int firstGroupLen = numDigits % digitsPerInt[radix];
  17.338 +        if (firstGroupLen == 0)
  17.339 +            firstGroupLen = digitsPerInt[radix];
  17.340 +        String group = val.substring(cursor, cursor += firstGroupLen);
  17.341 +        magnitude[numWords - 1] = Integer.parseInt(group, radix);
  17.342 +        if (magnitude[numWords - 1] < 0)
  17.343 +            throw new NumberFormatException("Illegal digit");
  17.344 +
  17.345 +        // Process remaining digit groups
  17.346 +        int superRadix = intRadix[radix];
  17.347 +        int groupVal = 0;
  17.348 +        while (cursor < len) {
  17.349 +            group = val.substring(cursor, cursor += digitsPerInt[radix]);
  17.350 +            groupVal = Integer.parseInt(group, radix);
  17.351 +            if (groupVal < 0)
  17.352 +                throw new NumberFormatException("Illegal digit");
  17.353 +            destructiveMulAdd(magnitude, superRadix, groupVal);
  17.354 +        }
  17.355 +        // Required for cases where the array was overallocated.
  17.356 +        mag = trustedStripLeadingZeroInts(magnitude);
  17.357 +    }
  17.358 +
  17.359 +    // Constructs a new BigInteger using a char array with radix=10
  17.360 +    BigInteger(char[] val) {
  17.361 +        int cursor = 0, numDigits;
  17.362 +        int len = val.length;
  17.363 +
  17.364 +        // Check for leading minus sign
  17.365 +        int sign = 1;
  17.366 +        if (val[0] == '-') {
  17.367 +            if (len == 1)
  17.368 +                throw new NumberFormatException("Zero length BigInteger");
  17.369 +            sign = -1;
  17.370 +            cursor = 1;
  17.371 +        } else if (val[0] == '+') {
  17.372 +            if (len == 1)
  17.373 +                throw new NumberFormatException("Zero length BigInteger");
  17.374 +            cursor = 1;
  17.375 +        }
  17.376 +
  17.377 +        // Skip leading zeros and compute number of digits in magnitude
  17.378 +        while (cursor < len && Character.digit(val[cursor], 10) == 0)
  17.379 +            cursor++;
  17.380 +        if (cursor == len) {
  17.381 +            signum = 0;
  17.382 +            mag = ZERO.mag;
  17.383 +            return;
  17.384 +        }
  17.385 +
  17.386 +        numDigits = len - cursor;
  17.387 +        signum = sign;
  17.388 +
  17.389 +        // Pre-allocate array of expected size
  17.390 +        int numWords;
  17.391 +        if (len < 10) {
  17.392 +            numWords = 1;
  17.393 +        } else {
  17.394 +            int numBits = (int)(((numDigits * bitsPerDigit[10]) >>> 10) + 1);
  17.395 +            numWords = (numBits + 31) >>> 5;
  17.396 +        }
  17.397 +        int[] magnitude = new int[numWords];
  17.398 +
  17.399 +        // Process first (potentially short) digit group
  17.400 +        int firstGroupLen = numDigits % digitsPerInt[10];
  17.401 +        if (firstGroupLen == 0)
  17.402 +            firstGroupLen = digitsPerInt[10];
  17.403 +        magnitude[numWords - 1] = parseInt(val, cursor,  cursor += firstGroupLen);
  17.404 +
  17.405 +        // Process remaining digit groups
  17.406 +        while (cursor < len) {
  17.407 +            int groupVal = parseInt(val, cursor, cursor += digitsPerInt[10]);
  17.408 +            destructiveMulAdd(magnitude, intRadix[10], groupVal);
  17.409 +        }
  17.410 +        mag = trustedStripLeadingZeroInts(magnitude);
  17.411 +    }
  17.412 +
  17.413 +    // Create an integer with the digits between the two indexes
  17.414 +    // Assumes start < end. The result may be negative, but it
  17.415 +    // is to be treated as an unsigned value.
  17.416 +    private int parseInt(char[] source, int start, int end) {
  17.417 +        int result = Character.digit(source[start++], 10);
  17.418 +        if (result == -1)
  17.419 +            throw new NumberFormatException(new String(source));
  17.420 +
  17.421 +        for (int index = start; index<end; index++) {
  17.422 +            int nextVal = Character.digit(source[index], 10);
  17.423 +            if (nextVal == -1)
  17.424 +                throw new NumberFormatException(new String(source));
  17.425 +            result = 10*result + nextVal;
  17.426 +        }
  17.427 +
  17.428 +        return result;
  17.429 +    }
  17.430 +
  17.431 +    // bitsPerDigit in the given radix times 1024
  17.432 +    // Rounded up to avoid underallocation.
  17.433 +    private static long bitsPerDigit[] = { 0, 0,
  17.434 +        1024, 1624, 2048, 2378, 2648, 2875, 3072, 3247, 3402, 3543, 3672,
  17.435 +        3790, 3899, 4001, 4096, 4186, 4271, 4350, 4426, 4498, 4567, 4633,
  17.436 +        4696, 4756, 4814, 4870, 4923, 4975, 5025, 5074, 5120, 5166, 5210,
  17.437 +                                           5253, 5295};
  17.438 +
  17.439 +    // Multiply x array times word y in place, and add word z
  17.440 +    private static void destructiveMulAdd(int[] x, int y, int z) {
  17.441 +        // Perform the multiplication word by word
  17.442 +        long ylong = y & LONG_MASK;
  17.443 +        long zlong = z & LONG_MASK;
  17.444 +        int len = x.length;
  17.445 +
  17.446 +        long product = 0;
  17.447 +        long carry = 0;
  17.448 +        for (int i = len-1; i >= 0; i--) {
  17.449 +            product = ylong * (x[i] & LONG_MASK) + carry;
  17.450 +            x[i] = (int)product;
  17.451 +            carry = product >>> 32;
  17.452 +        }
  17.453 +
  17.454 +        // Perform the addition
  17.455 +        long sum = (x[len-1] & LONG_MASK) + zlong;
  17.456 +        x[len-1] = (int)sum;
  17.457 +        carry = sum >>> 32;
  17.458 +        for (int i = len-2; i >= 0; i--) {
  17.459 +            sum = (x[i] & LONG_MASK) + carry;
  17.460 +            x[i] = (int)sum;
  17.461 +            carry = sum >>> 32;
  17.462 +        }
  17.463 +    }
  17.464 +
  17.465 +    /**
  17.466 +     * Translates the decimal String representation of a BigInteger into a
  17.467 +     * BigInteger.  The String representation consists of an optional minus
  17.468 +     * sign followed by a sequence of one or more decimal digits.  The
  17.469 +     * character-to-digit mapping is provided by {@code Character.digit}.
  17.470 +     * The String may not contain any extraneous characters (whitespace, for
  17.471 +     * example).
  17.472 +     *
  17.473 +     * @param val decimal String representation of BigInteger.
  17.474 +     * @throws NumberFormatException {@code val} is not a valid representation
  17.475 +     *         of a BigInteger.
  17.476 +     * @see    Character#digit
  17.477 +     */
  17.478 +    public BigInteger(String val) {
  17.479 +        this(val, 10);
  17.480 +    }
  17.481 +
  17.482 +    /**
  17.483 +     * Constructs a randomly generated BigInteger, uniformly distributed over
  17.484 +     * the range 0 to (2<sup>{@code numBits}</sup> - 1), inclusive.
  17.485 +     * The uniformity of the distribution assumes that a fair source of random
  17.486 +     * bits is provided in {@code rnd}.  Note that this constructor always
  17.487 +     * constructs a non-negative BigInteger.
  17.488 +     *
  17.489 +     * @param  numBits maximum bitLength of the new BigInteger.
  17.490 +     * @param  rnd source of randomness to be used in computing the new
  17.491 +     *         BigInteger.
  17.492 +     * @throws IllegalArgumentException {@code numBits} is negative.
  17.493 +     * @see #bitLength()
  17.494 +     */
  17.495 +    public BigInteger(int numBits, Random rnd) {
  17.496 +        this(1, randomBits(numBits, rnd));
  17.497 +    }
  17.498 +
  17.499 +    private static byte[] randomBits(int numBits, Random rnd) {
  17.500 +        if (numBits < 0)
  17.501 +            throw new IllegalArgumentException("numBits must be non-negative");
  17.502 +        int numBytes = (int)(((long)numBits+7)/8); // avoid overflow
  17.503 +        byte[] randomBits = new byte[numBytes];
  17.504 +
  17.505 +        // Generate random bytes and mask out any excess bits
  17.506 +        if (numBytes > 0) {
  17.507 +            rnd.nextBytes(randomBits);
  17.508 +            int excessBits = 8*numBytes - numBits;
  17.509 +            randomBits[0] &= (1 << (8-excessBits)) - 1;
  17.510 +        }
  17.511 +        return randomBits;
  17.512 +    }
  17.513 +
  17.514 +    /**
  17.515 +     * Constructs a randomly generated positive BigInteger that is probably
  17.516 +     * prime, with the specified bitLength.
  17.517 +     *
  17.518 +     * <p>It is recommended that the {@link #probablePrime probablePrime}
  17.519 +     * method be used in preference to this constructor unless there
  17.520 +     * is a compelling need to specify a certainty.
  17.521 +     *
  17.522 +     * @param  bitLength bitLength of the returned BigInteger.
  17.523 +     * @param  certainty a measure of the uncertainty that the caller is
  17.524 +     *         willing to tolerate.  The probability that the new BigInteger
  17.525 +     *         represents a prime number will exceed
  17.526 +     *         (1 - 1/2<sup>{@code certainty}</sup>).  The execution time of
  17.527 +     *         this constructor is proportional to the value of this parameter.
  17.528 +     * @param  rnd source of random bits used to select candidates to be
  17.529 +     *         tested for primality.
  17.530 +     * @throws ArithmeticException {@code bitLength < 2}.
  17.531 +     * @see    #bitLength()
  17.532 +     */
  17.533 +    public BigInteger(int bitLength, int certainty, Random rnd) {
  17.534 +        BigInteger prime;
  17.535 +
  17.536 +        if (bitLength < 2)
  17.537 +            throw new ArithmeticException("bitLength < 2");
  17.538 +        // The cutoff of 95 was chosen empirically for best performance
  17.539 +        prime = (bitLength < 95 ? smallPrime(bitLength, certainty, rnd)
  17.540 +                                : largePrime(bitLength, certainty, rnd));
  17.541 +        signum = 1;
  17.542 +        mag = prime.mag;
  17.543 +    }
  17.544 +
  17.545 +    // Minimum size in bits that the requested prime number has
  17.546 +    // before we use the large prime number generating algorithms
  17.547 +    private static final int SMALL_PRIME_THRESHOLD = 95;
  17.548 +
  17.549 +    // Certainty required to meet the spec of probablePrime
  17.550 +    private static final int DEFAULT_PRIME_CERTAINTY = 100;
  17.551 +
  17.552 +    /**
  17.553 +     * Returns a positive BigInteger that is probably prime, with the
  17.554 +     * specified bitLength. The probability that a BigInteger returned
  17.555 +     * by this method is composite does not exceed 2<sup>-100</sup>.
  17.556 +     *
  17.557 +     * @param  bitLength bitLength of the returned BigInteger.
  17.558 +     * @param  rnd source of random bits used to select candidates to be
  17.559 +     *         tested for primality.
  17.560 +     * @return a BigInteger of {@code bitLength} bits that is probably prime
  17.561 +     * @throws ArithmeticException {@code bitLength < 2}.
  17.562 +     * @see    #bitLength()
  17.563 +     * @since 1.4
  17.564 +     */
  17.565 +    public static BigInteger probablePrime(int bitLength, Random rnd) {
  17.566 +        if (bitLength < 2)
  17.567 +            throw new ArithmeticException("bitLength < 2");
  17.568 +
  17.569 +        // The cutoff of 95 was chosen empirically for best performance
  17.570 +        return (bitLength < SMALL_PRIME_THRESHOLD ?
  17.571 +                smallPrime(bitLength, DEFAULT_PRIME_CERTAINTY, rnd) :
  17.572 +                largePrime(bitLength, DEFAULT_PRIME_CERTAINTY, rnd));
  17.573 +    }
  17.574 +
  17.575 +    /**
  17.576 +     * Find a random number of the specified bitLength that is probably prime.
  17.577 +     * This method is used for smaller primes, its performance degrades on
  17.578 +     * larger bitlengths.
  17.579 +     *
  17.580 +     * This method assumes bitLength > 1.
  17.581 +     */
  17.582 +    private static BigInteger smallPrime(int bitLength, int certainty, Random rnd) {
  17.583 +        int magLen = (bitLength + 31) >>> 5;
  17.584 +        int temp[] = new int[magLen];
  17.585 +        int highBit = 1 << ((bitLength+31) & 0x1f);  // High bit of high int
  17.586 +        int highMask = (highBit << 1) - 1;  // Bits to keep in high int
  17.587 +
  17.588 +        while(true) {
  17.589 +            // Construct a candidate
  17.590 +            for (int i=0; i<magLen; i++)
  17.591 +                temp[i] = rnd.nextInt();
  17.592 +            temp[0] = (temp[0] & highMask) | highBit;  // Ensure exact length
  17.593 +            if (bitLength > 2)
  17.594 +                temp[magLen-1] |= 1;  // Make odd if bitlen > 2
  17.595 +
  17.596 +            BigInteger p = new BigInteger(temp, 1);
  17.597 +
  17.598 +            // Do cheap "pre-test" if applicable
  17.599 +            if (bitLength > 6) {
  17.600 +                long r = p.remainder(SMALL_PRIME_PRODUCT).longValue();
  17.601 +                if ((r%3==0)  || (r%5==0)  || (r%7==0)  || (r%11==0) ||
  17.602 +                    (r%13==0) || (r%17==0) || (r%19==0) || (r%23==0) ||
  17.603 +                    (r%29==0) || (r%31==0) || (r%37==0) || (r%41==0))
  17.604 +                    continue; // Candidate is composite; try another
  17.605 +            }
  17.606 +
  17.607 +            // All candidates of bitLength 2 and 3 are prime by this point
  17.608 +            if (bitLength < 4)
  17.609 +                return p;
  17.610 +
  17.611 +            // Do expensive test if we survive pre-test (or it's inapplicable)
  17.612 +            if (p.primeToCertainty(certainty, rnd))
  17.613 +                return p;
  17.614 +        }
  17.615 +    }
  17.616 +
  17.617 +    private static final BigInteger SMALL_PRIME_PRODUCT
  17.618 +                       = valueOf(3L*5*7*11*13*17*19*23*29*31*37*41);
  17.619 +
  17.620 +    /**
  17.621 +     * Find a random number of the specified bitLength that is probably prime.
  17.622 +     * This method is more appropriate for larger bitlengths since it uses
  17.623 +     * a sieve to eliminate most composites before using a more expensive
  17.624 +     * test.
  17.625 +     */
  17.626 +    private static BigInteger largePrime(int bitLength, int certainty, Random rnd) {
  17.627 +        BigInteger p;
  17.628 +        p = new BigInteger(bitLength, rnd).setBit(bitLength-1);
  17.629 +        p.mag[p.mag.length-1] &= 0xfffffffe;
  17.630 +
  17.631 +        // Use a sieve length likely to contain the next prime number
  17.632 +        int searchLen = (bitLength / 20) * 64;
  17.633 +        BitSieve searchSieve = new BitSieve(p, searchLen);
  17.634 +        BigInteger candidate = searchSieve.retrieve(p, certainty, rnd);
  17.635 +
  17.636 +        while ((candidate == null) || (candidate.bitLength() != bitLength)) {
  17.637 +            p = p.add(BigInteger.valueOf(2*searchLen));
  17.638 +            if (p.bitLength() != bitLength)
  17.639 +                p = new BigInteger(bitLength, rnd).setBit(bitLength-1);
  17.640 +            p.mag[p.mag.length-1] &= 0xfffffffe;
  17.641 +            searchSieve = new BitSieve(p, searchLen);
  17.642 +            candidate = searchSieve.retrieve(p, certainty, rnd);
  17.643 +        }
  17.644 +        return candidate;
  17.645 +    }
  17.646 +
  17.647 +   /**
  17.648 +    * Returns the first integer greater than this {@code BigInteger} that
  17.649 +    * is probably prime.  The probability that the number returned by this
  17.650 +    * method is composite does not exceed 2<sup>-100</sup>. This method will
  17.651 +    * never skip over a prime when searching: if it returns {@code p}, there
  17.652 +    * is no prime {@code q} such that {@code this < q < p}.
  17.653 +    *
  17.654 +    * @return the first integer greater than this {@code BigInteger} that
  17.655 +    *         is probably prime.
  17.656 +    * @throws ArithmeticException {@code this < 0}.
  17.657 +    * @since 1.5
  17.658 +    */
  17.659 +    public BigInteger nextProbablePrime() {
  17.660 +        if (this.signum < 0)
  17.661 +            throw new ArithmeticException("start < 0: " + this);
  17.662 +
  17.663 +        // Handle trivial cases
  17.664 +        if ((this.signum == 0) || this.equals(ONE))
  17.665 +            return TWO;
  17.666 +
  17.667 +        BigInteger result = this.add(ONE);
  17.668 +
  17.669 +        // Fastpath for small numbers
  17.670 +        if (result.bitLength() < SMALL_PRIME_THRESHOLD) {
  17.671 +
  17.672 +            // Ensure an odd number
  17.673 +            if (!result.testBit(0))
  17.674 +                result = result.add(ONE);
  17.675 +
  17.676 +            while(true) {
  17.677 +                // Do cheap "pre-test" if applicable
  17.678 +                if (result.bitLength() > 6) {
  17.679 +                    long r = result.remainder(SMALL_PRIME_PRODUCT).longValue();
  17.680 +                    if ((r%3==0)  || (r%5==0)  || (r%7==0)  || (r%11==0) ||
  17.681 +                        (r%13==0) || (r%17==0) || (r%19==0) || (r%23==0) ||
  17.682 +                        (r%29==0) || (r%31==0) || (r%37==0) || (r%41==0)) {
  17.683 +                        result = result.add(TWO);
  17.684 +                        continue; // Candidate is composite; try another
  17.685 +                    }
  17.686 +                }
  17.687 +
  17.688 +                // All candidates of bitLength 2 and 3 are prime by this point
  17.689 +                if (result.bitLength() < 4)
  17.690 +                    return result;
  17.691 +
  17.692 +                // The expensive test
  17.693 +                if (result.primeToCertainty(DEFAULT_PRIME_CERTAINTY, null))
  17.694 +                    return result;
  17.695 +
  17.696 +                result = result.add(TWO);
  17.697 +            }
  17.698 +        }
  17.699 +
  17.700 +        // Start at previous even number
  17.701 +        if (result.testBit(0))
  17.702 +            result = result.subtract(ONE);
  17.703 +
  17.704 +        // Looking for the next large prime
  17.705 +        int searchLen = (result.bitLength() / 20) * 64;
  17.706 +
  17.707 +        while(true) {
  17.708 +           BitSieve searchSieve = new BitSieve(result, searchLen);
  17.709 +           BigInteger candidate = searchSieve.retrieve(result,
  17.710 +                                                 DEFAULT_PRIME_CERTAINTY, null);
  17.711 +           if (candidate != null)
  17.712 +               return candidate;
  17.713 +           result = result.add(BigInteger.valueOf(2 * searchLen));
  17.714 +        }
  17.715 +    }
  17.716 +
  17.717 +    /**
  17.718 +     * Returns {@code true} if this BigInteger is probably prime,
  17.719 +     * {@code false} if it's definitely composite.
  17.720 +     *
  17.721 +     * This method assumes bitLength > 2.
  17.722 +     *
  17.723 +     * @param  certainty a measure of the uncertainty that the caller is
  17.724 +     *         willing to tolerate: if the call returns {@code true}
  17.725 +     *         the probability that this BigInteger is prime exceeds
  17.726 +     *         {@code (1 - 1/2<sup>certainty</sup>)}.  The execution time of
  17.727 +     *         this method is proportional to the value of this parameter.
  17.728 +     * @return {@code true} if this BigInteger is probably prime,
  17.729 +     *         {@code false} if it's definitely composite.
  17.730 +     */
  17.731 +    boolean primeToCertainty(int certainty, Random random) {
  17.732 +        int rounds = 0;
  17.733 +        int n = (Math.min(certainty, Integer.MAX_VALUE-1)+1)/2;
  17.734 +
  17.735 +        // The relationship between the certainty and the number of rounds
  17.736 +        // we perform is given in the draft standard ANSI X9.80, "PRIME
  17.737 +        // NUMBER GENERATION, PRIMALITY TESTING, AND PRIMALITY CERTIFICATES".
  17.738 +        int sizeInBits = this.bitLength();
  17.739 +        if (sizeInBits < 100) {
  17.740 +            rounds = 50;
  17.741 +            rounds = n < rounds ? n : rounds;
  17.742 +            return passesMillerRabin(rounds, random);
  17.743 +        }
  17.744 +
  17.745 +        if (sizeInBits < 256) {
  17.746 +            rounds = 27;
  17.747 +        } else if (sizeInBits < 512) {
  17.748 +            rounds = 15;
  17.749 +        } else if (sizeInBits < 768) {
  17.750 +            rounds = 8;
  17.751 +        } else if (sizeInBits < 1024) {
  17.752 +            rounds = 4;
  17.753 +        } else {
  17.754 +            rounds = 2;
  17.755 +        }
  17.756 +        rounds = n < rounds ? n : rounds;
  17.757 +
  17.758 +        return passesMillerRabin(rounds, random) && passesLucasLehmer();
  17.759 +    }
  17.760 +
  17.761 +    /**
  17.762 +     * Returns true iff this BigInteger is a Lucas-Lehmer probable prime.
  17.763 +     *
  17.764 +     * The following assumptions are made:
  17.765 +     * This BigInteger is a positive, odd number.
  17.766 +     */
  17.767 +    private boolean passesLucasLehmer() {
  17.768 +        BigInteger thisPlusOne = this.add(ONE);
  17.769 +
  17.770 +        // Step 1
  17.771 +        int d = 5;
  17.772 +        while (jacobiSymbol(d, this) != -1) {
  17.773 +            // 5, -7, 9, -11, ...
  17.774 +            d = (d<0) ? Math.abs(d)+2 : -(d+2);
  17.775 +        }
  17.776 +
  17.777 +        // Step 2
  17.778 +        BigInteger u = lucasLehmerSequence(d, thisPlusOne, this);
  17.779 +
  17.780 +        // Step 3
  17.781 +        return u.mod(this).equals(ZERO);
  17.782 +    }
  17.783 +
  17.784 +    /**
  17.785 +     * Computes Jacobi(p,n).
  17.786 +     * Assumes n positive, odd, n>=3.
  17.787 +     */
  17.788 +    private static int jacobiSymbol(int p, BigInteger n) {
  17.789 +        if (p == 0)
  17.790 +            return 0;
  17.791 +
  17.792 +        // Algorithm and comments adapted from Colin Plumb's C library.
  17.793 +        int j = 1;
  17.794 +        int u = n.mag[n.mag.length-1];
  17.795 +
  17.796 +        // Make p positive
  17.797 +        if (p < 0) {
  17.798 +            p = -p;
  17.799 +            int n8 = u & 7;
  17.800 +            if ((n8 == 3) || (n8 == 7))
  17.801 +                j = -j; // 3 (011) or 7 (111) mod 8
  17.802 +        }
  17.803 +
  17.804 +        // Get rid of factors of 2 in p
  17.805 +        while ((p & 3) == 0)
  17.806 +            p >>= 2;
  17.807 +        if ((p & 1) == 0) {
  17.808 +            p >>= 1;
  17.809 +            if (((u ^ (u>>1)) & 2) != 0)
  17.810 +                j = -j; // 3 (011) or 5 (101) mod 8
  17.811 +        }
  17.812 +        if (p == 1)
  17.813 +            return j;
  17.814 +        // Then, apply quadratic reciprocity
  17.815 +        if ((p & u & 2) != 0)   // p = u = 3 (mod 4)?
  17.816 +            j = -j;
  17.817 +        // And reduce u mod p
  17.818 +        u = n.mod(BigInteger.valueOf(p)).intValue();
  17.819 +
  17.820 +        // Now compute Jacobi(u,p), u < p
  17.821 +        while (u != 0) {
  17.822 +            while ((u & 3) == 0)
  17.823 +                u >>= 2;
  17.824 +            if ((u & 1) == 0) {
  17.825 +                u >>= 1;
  17.826 +                if (((p ^ (p>>1)) & 2) != 0)
  17.827 +                    j = -j;     // 3 (011) or 5 (101) mod 8
  17.828 +            }
  17.829 +            if (u == 1)
  17.830 +                return j;
  17.831 +            // Now both u and p are odd, so use quadratic reciprocity
  17.832 +            assert (u < p);
  17.833 +            int t = u; u = p; p = t;
  17.834 +            if ((u & p & 2) != 0) // u = p = 3 (mod 4)?
  17.835 +                j = -j;
  17.836 +            // Now u >= p, so it can be reduced
  17.837 +            u %= p;
  17.838 +        }
  17.839 +        return 0;
  17.840 +    }
  17.841 +
  17.842 +    private static BigInteger lucasLehmerSequence(int z, BigInteger k, BigInteger n) {
  17.843 +        BigInteger d = BigInteger.valueOf(z);
  17.844 +        BigInteger u = ONE; BigInteger u2;
  17.845 +        BigInteger v = ONE; BigInteger v2;
  17.846 +
  17.847 +        for (int i=k.bitLength()-2; i>=0; i--) {
  17.848 +            u2 = u.multiply(v).mod(n);
  17.849 +
  17.850 +            v2 = v.square().add(d.multiply(u.square())).mod(n);
  17.851 +            if (v2.testBit(0))
  17.852 +                v2 = v2.subtract(n);
  17.853 +
  17.854 +            v2 = v2.shiftRight(1);
  17.855 +
  17.856 +            u = u2; v = v2;
  17.857 +            if (k.testBit(i)) {
  17.858 +                u2 = u.add(v).mod(n);
  17.859 +                if (u2.testBit(0))
  17.860 +                    u2 = u2.subtract(n);
  17.861 +
  17.862 +                u2 = u2.shiftRight(1);
  17.863 +                v2 = v.add(d.multiply(u)).mod(n);
  17.864 +                if (v2.testBit(0))
  17.865 +                    v2 = v2.subtract(n);
  17.866 +                v2 = v2.shiftRight(1);
  17.867 +
  17.868 +                u = u2; v = v2;
  17.869 +            }
  17.870 +        }
  17.871 +        return u;
  17.872 +    }
  17.873 +
  17.874 +    private static volatile Random staticRandom;
  17.875 +
  17.876 +    private static Random getSecureRandom() {
  17.877 +        if (staticRandom == null) {
  17.878 +            staticRandom = new Random();
  17.879 +        }
  17.880 +        return staticRandom;
  17.881 +    }
  17.882 +
  17.883 +    /**
  17.884 +     * Returns true iff this BigInteger passes the specified number of
  17.885 +     * Miller-Rabin tests. This test is taken from the DSA spec (NIST FIPS
  17.886 +     * 186-2).
  17.887 +     *
  17.888 +     * The following assumptions are made:
  17.889 +     * This BigInteger is a positive, odd number greater than 2.
  17.890 +     * iterations<=50.
  17.891 +     */
  17.892 +    private boolean passesMillerRabin(int iterations, Random rnd) {
  17.893 +        // Find a and m such that m is odd and this == 1 + 2**a * m
  17.894 +        BigInteger thisMinusOne = this.subtract(ONE);
  17.895 +        BigInteger m = thisMinusOne;
  17.896 +        int a = m.getLowestSetBit();
  17.897 +        m = m.shiftRight(a);
  17.898 +
  17.899 +        // Do the tests
  17.900 +        if (rnd == null) {
  17.901 +            rnd = getSecureRandom();
  17.902 +        }
  17.903 +        for (int i=0; i<iterations; i++) {
  17.904 +            // Generate a uniform random on (1, this)
  17.905 +            BigInteger b;
  17.906 +            do {
  17.907 +                b = new BigInteger(this.bitLength(), rnd);
  17.908 +            } while (b.compareTo(ONE) <= 0 || b.compareTo(this) >= 0);
  17.909 +
  17.910 +            int j = 0;
  17.911 +            BigInteger z = b.modPow(m, this);
  17.912 +            while(!((j==0 && z.equals(ONE)) || z.equals(thisMinusOne))) {
  17.913 +                if (j>0 && z.equals(ONE) || ++j==a)
  17.914 +                    return false;
  17.915 +                z = z.modPow(TWO, this);
  17.916 +            }
  17.917 +        }
  17.918 +        return true;
  17.919 +    }
  17.920 +
  17.921 +    /**
  17.922 +     * This internal constructor differs from its public cousin
  17.923 +     * with the arguments reversed in two ways: it assumes that its
  17.924 +     * arguments are correct, and it doesn't copy the magnitude array.
  17.925 +     */
  17.926 +    BigInteger(int[] magnitude, int signum) {
  17.927 +        this.signum = (magnitude.length==0 ? 0 : signum);
  17.928 +        this.mag = magnitude;
  17.929 +    }
  17.930 +
  17.931 +    /**
  17.932 +     * This private constructor is for internal use and assumes that its
  17.933 +     * arguments are correct.
  17.934 +     */
  17.935 +    private BigInteger(byte[] magnitude, int signum) {
  17.936 +        this.signum = (magnitude.length==0 ? 0 : signum);
  17.937 +        this.mag = stripLeadingZeroBytes(magnitude);
  17.938 +    }
  17.939 +
  17.940 +    //Static Factory Methods
  17.941 +
  17.942 +    /**
  17.943 +     * Returns a BigInteger whose value is equal to that of the
  17.944 +     * specified {@code long}.  This "static factory method" is
  17.945 +     * provided in preference to a ({@code long}) constructor
  17.946 +     * because it allows for reuse of frequently used BigIntegers.
  17.947 +     *
  17.948 +     * @param  val value of the BigInteger to return.
  17.949 +     * @return a BigInteger with the specified value.
  17.950 +     */
  17.951 +    public static BigInteger valueOf(long val) {
  17.952 +        // If -MAX_CONSTANT < val < MAX_CONSTANT, return stashed constant
  17.953 +        if (val == 0)
  17.954 +            return ZERO;
  17.955 +        if (val > 0 && val <= MAX_CONSTANT)
  17.956 +            return posConst[(int) val];
  17.957 +        else if (val < 0 && val >= -MAX_CONSTANT)
  17.958 +            return negConst[(int) -val];
  17.959 +
  17.960 +        return new BigInteger(val);
  17.961 +    }
  17.962 +
  17.963 +    /**
  17.964 +     * Constructs a BigInteger with the specified value, which may not be zero.
  17.965 +     */
  17.966 +    private BigInteger(long val) {
  17.967 +        if (val < 0) {
  17.968 +            val = -val;
  17.969 +            signum = -1;
  17.970 +        } else {
  17.971 +            signum = 1;
  17.972 +        }
  17.973 +
  17.974 +        int highWord = (int)(val >>> 32);
  17.975 +        if (highWord==0) {
  17.976 +            mag = new int[1];
  17.977 +            mag[0] = (int)val;
  17.978 +        } else {
  17.979 +            mag = new int[2];
  17.980 +            mag[0] = highWord;
  17.981 +            mag[1] = (int)val;
  17.982 +        }
  17.983 +    }
  17.984 +
  17.985 +    /**
  17.986 +     * Returns a BigInteger with the given two's complement representation.
  17.987 +     * Assumes that the input array will not be modified (the returned
  17.988 +     * BigInteger will reference the input array if feasible).
  17.989 +     */
  17.990 +    private static BigInteger valueOf(int val[]) {
  17.991 +        return (val[0]>0 ? new BigInteger(val, 1) : new BigInteger(val));
  17.992 +    }
  17.993 +
  17.994 +    // Constants
  17.995 +
  17.996 +    /**
  17.997 +     * Initialize static constant array when class is loaded.
  17.998 +     */
  17.999 +    private final static int MAX_CONSTANT = 16;
 17.1000 +    private static BigInteger posConst[] = new BigInteger[MAX_CONSTANT+1];
 17.1001 +    private static BigInteger negConst[] = new BigInteger[MAX_CONSTANT+1];
 17.1002 +    static {
 17.1003 +        for (int i = 1; i <= MAX_CONSTANT; i++) {
 17.1004 +            int[] magnitude = new int[1];
 17.1005 +            magnitude[0] = i;
 17.1006 +            posConst[i] = new BigInteger(magnitude,  1);
 17.1007 +            negConst[i] = new BigInteger(magnitude, -1);
 17.1008 +        }
 17.1009 +    }
 17.1010 +
 17.1011 +    /**
 17.1012 +     * The BigInteger constant zero.
 17.1013 +     *
 17.1014 +     * @since   1.2
 17.1015 +     */
 17.1016 +    public static final BigInteger ZERO = new BigInteger(new int[0], 0);
 17.1017 +
 17.1018 +    /**
 17.1019 +     * The BigInteger constant one.
 17.1020 +     *
 17.1021 +     * @since   1.2
 17.1022 +     */
 17.1023 +    public static final BigInteger ONE = valueOf(1);
 17.1024 +
 17.1025 +    /**
 17.1026 +     * The BigInteger constant two.  (Not exported.)
 17.1027 +     */
 17.1028 +    private static final BigInteger TWO = valueOf(2);
 17.1029 +
 17.1030 +    /**
 17.1031 +     * The BigInteger constant ten.
 17.1032 +     *
 17.1033 +     * @since   1.5
 17.1034 +     */
 17.1035 +    public static final BigInteger TEN = valueOf(10);
 17.1036 +
 17.1037 +    // Arithmetic Operations
 17.1038 +
 17.1039 +    /**
 17.1040 +     * Returns a BigInteger whose value is {@code (this + val)}.
 17.1041 +     *
 17.1042 +     * @param  val value to be added to this BigInteger.
 17.1043 +     * @return {@code this + val}
 17.1044 +     */
 17.1045 +    public BigInteger add(BigInteger val) {
 17.1046 +        if (val.signum == 0)
 17.1047 +            return this;
 17.1048 +        if (signum == 0)
 17.1049 +            return val;
 17.1050 +        if (val.signum == signum)
 17.1051 +            return new BigInteger(add(mag, val.mag), signum);
 17.1052 +
 17.1053 +        int cmp = compareMagnitude(val);
 17.1054 +        if (cmp == 0)
 17.1055 +            return ZERO;
 17.1056 +        int[] resultMag = (cmp > 0 ? subtract(mag, val.mag)
 17.1057 +                           : subtract(val.mag, mag));
 17.1058 +        resultMag = trustedStripLeadingZeroInts(resultMag);
 17.1059 +
 17.1060 +        return new BigInteger(resultMag, cmp == signum ? 1 : -1);
 17.1061 +    }
 17.1062 +
 17.1063 +    /**
 17.1064 +     * Adds the contents of the int arrays x and y. This method allocates
 17.1065 +     * a new int array to hold the answer and returns a reference to that
 17.1066 +     * array.
 17.1067 +     */
 17.1068 +    private static int[] add(int[] x, int[] y) {
 17.1069 +        // If x is shorter, swap the two arrays
 17.1070 +        if (x.length < y.length) {
 17.1071 +            int[] tmp = x;
 17.1072 +            x = y;
 17.1073 +            y = tmp;
 17.1074 +        }
 17.1075 +
 17.1076 +        int xIndex = x.length;
 17.1077 +        int yIndex = y.length;
 17.1078 +        int result[] = new int[xIndex];
 17.1079 +        long sum = 0;
 17.1080 +
 17.1081 +        // Add common parts of both numbers
 17.1082 +        while(yIndex > 0) {
 17.1083 +            sum = (x[--xIndex] & LONG_MASK) +
 17.1084 +                  (y[--yIndex] & LONG_MASK) + (sum >>> 32);
 17.1085 +            result[xIndex] = (int)sum;
 17.1086 +        }
 17.1087 +
 17.1088 +        // Copy remainder of longer number while carry propagation is required
 17.1089 +        boolean carry = (sum >>> 32 != 0);
 17.1090 +        while (xIndex > 0 && carry)
 17.1091 +            carry = ((result[--xIndex] = x[xIndex] + 1) == 0);
 17.1092 +
 17.1093 +        // Copy remainder of longer number
 17.1094 +        while (xIndex > 0)
 17.1095 +            result[--xIndex] = x[xIndex];
 17.1096 +
 17.1097 +        // Grow result if necessary
 17.1098 +        if (carry) {
 17.1099 +            int bigger[] = new int[result.length + 1];
 17.1100 +            System.arraycopy(result, 0, bigger, 1, result.length);
 17.1101 +            bigger[0] = 0x01;
 17.1102 +            return bigger;
 17.1103 +        }
 17.1104 +        return result;
 17.1105 +    }
 17.1106 +
 17.1107 +    /**
 17.1108 +     * Returns a BigInteger whose value is {@code (this - val)}.
 17.1109 +     *
 17.1110 +     * @param  val value to be subtracted from this BigInteger.
 17.1111 +     * @return {@code this - val}
 17.1112 +     */
 17.1113 +    public BigInteger subtract(BigInteger val) {
 17.1114 +        if (val.signum == 0)
 17.1115 +            return this;
 17.1116 +        if (signum == 0)
 17.1117 +            return val.negate();
 17.1118 +        if (val.signum != signum)
 17.1119 +            return new BigInteger(add(mag, val.mag), signum);
 17.1120 +
 17.1121 +        int cmp = compareMagnitude(val);
 17.1122 +        if (cmp == 0)
 17.1123 +            return ZERO;
 17.1124 +        int[] resultMag = (cmp > 0 ? subtract(mag, val.mag)
 17.1125 +                           : subtract(val.mag, mag));
 17.1126 +        resultMag = trustedStripLeadingZeroInts(resultMag);
 17.1127 +        return new BigInteger(resultMag, cmp == signum ? 1 : -1);
 17.1128 +    }
 17.1129 +
 17.1130 +    /**
 17.1131 +     * Subtracts the contents of the second int arrays (little) from the
 17.1132 +     * first (big).  The first int array (big) must represent a larger number
 17.1133 +     * than the second.  This method allocates the space necessary to hold the
 17.1134 +     * answer.
 17.1135 +     */
 17.1136 +    private static int[] subtract(int[] big, int[] little) {
 17.1137 +        int bigIndex = big.length;
 17.1138 +        int result[] = new int[bigIndex];
 17.1139 +        int littleIndex = little.length;
 17.1140 +        long difference = 0;
 17.1141 +
 17.1142 +        // Subtract common parts of both numbers
 17.1143 +        while(littleIndex > 0) {
 17.1144 +            difference = (big[--bigIndex] & LONG_MASK) -
 17.1145 +                         (little[--littleIndex] & LONG_MASK) +
 17.1146 +                         (difference >> 32);
 17.1147 +            result[bigIndex] = (int)difference;
 17.1148 +        }
 17.1149 +
 17.1150 +        // Subtract remainder of longer number while borrow propagates
 17.1151 +        boolean borrow = (difference >> 32 != 0);
 17.1152 +        while (bigIndex > 0 && borrow)
 17.1153 +            borrow = ((result[--bigIndex] = big[bigIndex] - 1) == -1);
 17.1154 +
 17.1155 +        // Copy remainder of longer number
 17.1156 +        while (bigIndex > 0)
 17.1157 +            result[--bigIndex] = big[bigIndex];
 17.1158 +
 17.1159 +        return result;
 17.1160 +    }
 17.1161 +
 17.1162 +    /**
 17.1163 +     * Returns a BigInteger whose value is {@code (this * val)}.
 17.1164 +     *
 17.1165 +     * @param  val value to be multiplied by this BigInteger.
 17.1166 +     * @return {@code this * val}
 17.1167 +     */
 17.1168 +    public BigInteger multiply(BigInteger val) {
 17.1169 +        if (val.signum == 0 || signum == 0)
 17.1170 +            return ZERO;
 17.1171 +
 17.1172 +        int[] result = multiplyToLen(mag, mag.length,
 17.1173 +                                     val.mag, val.mag.length, null);
 17.1174 +        result = trustedStripLeadingZeroInts(result);
 17.1175 +        return new BigInteger(result, signum == val.signum ? 1 : -1);
 17.1176 +    }
 17.1177 +
 17.1178 +    /**
 17.1179 +     * Package private methods used by BigDecimal code to multiply a BigInteger
 17.1180 +     * with a long. Assumes v is not equal to INFLATED.
 17.1181 +     */
 17.1182 +    BigInteger multiply(long v) {
 17.1183 +        if (v == 0 || signum == 0)
 17.1184 +          return ZERO;
 17.1185 +        if (v == BigDecimal.INFLATED)
 17.1186 +            return multiply(BigInteger.valueOf(v));
 17.1187 +        int rsign = (v > 0 ? signum : -signum);
 17.1188 +        if (v < 0)
 17.1189 +            v = -v;
 17.1190 +        long dh = v >>> 32;      // higher order bits
 17.1191 +        long dl = v & LONG_MASK; // lower order bits
 17.1192 +
 17.1193 +        int xlen = mag.length;
 17.1194 +        int[] value = mag;
 17.1195 +        int[] rmag = (dh == 0L) ? (new int[xlen + 1]) : (new int[xlen + 2]);
 17.1196 +        long carry = 0;
 17.1197 +        int rstart = rmag.length - 1;
 17.1198 +        for (int i = xlen - 1; i >= 0; i--) {
 17.1199 +            long product = (value[i] & LONG_MASK) * dl + carry;
 17.1200 +            rmag[rstart--] = (int)product;
 17.1201 +            carry = product >>> 32;
 17.1202 +        }
 17.1203 +        rmag[rstart] = (int)carry;
 17.1204 +        if (dh != 0L) {
 17.1205 +            carry = 0;
 17.1206 +            rstart = rmag.length - 2;
 17.1207 +            for (int i = xlen - 1; i >= 0; i--) {
 17.1208 +                long product = (value[i] & LONG_MASK) * dh +
 17.1209 +                    (rmag[rstart] & LONG_MASK) + carry;
 17.1210 +                rmag[rstart--] = (int)product;
 17.1211 +                carry = product >>> 32;
 17.1212 +            }
 17.1213 +            rmag[0] = (int)carry;
 17.1214 +        }
 17.1215 +        if (carry == 0L)
 17.1216 +            rmag = java.util.Arrays.copyOfRange(rmag, 1, rmag.length);
 17.1217 +        return new BigInteger(rmag, rsign);
 17.1218 +    }
 17.1219 +
 17.1220 +    /**
 17.1221 +     * Multiplies int arrays x and y to the specified lengths and places
 17.1222 +     * the result into z. There will be no leading zeros in the resultant array.
 17.1223 +     */
 17.1224 +    private int[] multiplyToLen(int[] x, int xlen, int[] y, int ylen, int[] z) {
 17.1225 +        int xstart = xlen - 1;
 17.1226 +        int ystart = ylen - 1;
 17.1227 +
 17.1228 +        if (z == null || z.length < (xlen+ ylen))
 17.1229 +            z = new int[xlen+ylen];
 17.1230 +
 17.1231 +        long carry = 0;
 17.1232 +        for (int j=ystart, k=ystart+1+xstart; j>=0; j--, k--) {
 17.1233 +            long product = (y[j] & LONG_MASK) *
 17.1234 +                           (x[xstart] & LONG_MASK) + carry;
 17.1235 +            z[k] = (int)product;
 17.1236 +            carry = product >>> 32;
 17.1237 +        }
 17.1238 +        z[xstart] = (int)carry;
 17.1239 +
 17.1240 +        for (int i = xstart-1; i >= 0; i--) {
 17.1241 +            carry = 0;
 17.1242 +            for (int j=ystart, k=ystart+1+i; j>=0; j--, k--) {
 17.1243 +                long product = (y[j] & LONG_MASK) *
 17.1244 +                               (x[i] & LONG_MASK) +
 17.1245 +                               (z[k] & LONG_MASK) + carry;
 17.1246 +                z[k] = (int)product;
 17.1247 +                carry = product >>> 32;
 17.1248 +            }
 17.1249 +            z[i] = (int)carry;
 17.1250 +        }
 17.1251 +        return z;
 17.1252 +    }
 17.1253 +
 17.1254 +    /**
 17.1255 +     * Returns a BigInteger whose value is {@code (this<sup>2</sup>)}.
 17.1256 +     *
 17.1257 +     * @return {@code this<sup>2</sup>}
 17.1258 +     */
 17.1259 +    private BigInteger square() {
 17.1260 +        if (signum == 0)
 17.1261 +            return ZERO;
 17.1262 +        int[] z = squareToLen(mag, mag.length, null);
 17.1263 +        return new BigInteger(trustedStripLeadingZeroInts(z), 1);
 17.1264 +    }
 17.1265 +
 17.1266 +    /**
 17.1267 +     * Squares the contents of the int array x. The result is placed into the
 17.1268 +     * int array z.  The contents of x are not changed.
 17.1269 +     */
 17.1270 +    private static final int[] squareToLen(int[] x, int len, int[] z) {
 17.1271 +        /*
 17.1272 +         * The algorithm used here is adapted from Colin Plumb's C library.
 17.1273 +         * Technique: Consider the partial products in the multiplication
 17.1274 +         * of "abcde" by itself:
 17.1275 +         *
 17.1276 +         *               a  b  c  d  e
 17.1277 +         *            *  a  b  c  d  e
 17.1278 +         *          ==================
 17.1279 +         *              ae be ce de ee
 17.1280 +         *           ad bd cd dd de
 17.1281 +         *        ac bc cc cd ce
 17.1282 +         *     ab bb bc bd be
 17.1283 +         *  aa ab ac ad ae
 17.1284 +         *
 17.1285 +         * Note that everything above the main diagonal:
 17.1286 +         *              ae be ce de = (abcd) * e
 17.1287 +         *           ad bd cd       = (abc) * d
 17.1288 +         *        ac bc             = (ab) * c
 17.1289 +         *     ab                   = (a) * b
 17.1290 +         *
 17.1291 +         * is a copy of everything below the main diagonal:
 17.1292 +         *                       de
 17.1293 +         *                 cd ce
 17.1294 +         *           bc bd be
 17.1295 +         *     ab ac ad ae
 17.1296 +         *
 17.1297 +         * Thus, the sum is 2 * (off the diagonal) + diagonal.
 17.1298 +         *
 17.1299 +         * This is accumulated beginning with the diagonal (which
 17.1300 +         * consist of the squares of the digits of the input), which is then
 17.1301 +         * divided by two, the off-diagonal added, and multiplied by two
 17.1302 +         * again.  The low bit is simply a copy of the low bit of the
 17.1303 +         * input, so it doesn't need special care.
 17.1304 +         */
 17.1305 +        int zlen = len << 1;
 17.1306 +        if (z == null || z.length < zlen)
 17.1307 +            z = new int[zlen];
 17.1308 +
 17.1309 +        // Store the squares, right shifted one bit (i.e., divided by 2)
 17.1310 +        int lastProductLowWord = 0;
 17.1311 +        for (int j=0, i=0; j<len; j++) {
 17.1312 +            long piece = (x[j] & LONG_MASK);
 17.1313 +            long product = piece * piece;
 17.1314 +            z[i++] = (lastProductLowWord << 31) | (int)(product >>> 33);
 17.1315 +            z[i++] = (int)(product >>> 1);
 17.1316 +            lastProductLowWord = (int)product;
 17.1317 +        }
 17.1318 +
 17.1319 +        // Add in off-diagonal sums
 17.1320 +        for (int i=len, offset=1; i>0; i--, offset+=2) {
 17.1321 +            int t = x[i-1];
 17.1322 +            t = mulAdd(z, x, offset, i-1, t);
 17.1323 +            addOne(z, offset-1, i, t);
 17.1324 +        }
 17.1325 +
 17.1326 +        // Shift back up and set low bit
 17.1327 +        primitiveLeftShift(z, zlen, 1);
 17.1328 +        z[zlen-1] |= x[len-1] & 1;
 17.1329 +
 17.1330 +        return z;
 17.1331 +    }
 17.1332 +
 17.1333 +    /**
 17.1334 +     * Returns a BigInteger whose value is {@code (this / val)}.
 17.1335 +     *
 17.1336 +     * @param  val value by which this BigInteger is to be divided.
 17.1337 +     * @return {@code this / val}
 17.1338 +     * @throws ArithmeticException if {@code val} is zero.
 17.1339 +     */
 17.1340 +    public BigInteger divide(BigInteger val) {
 17.1341 +        MutableBigInteger q = new MutableBigInteger(),
 17.1342 +                          a = new MutableBigInteger(this.mag),
 17.1343 +                          b = new MutableBigInteger(val.mag);
 17.1344 +
 17.1345 +        a.divide(b, q);
 17.1346 +        return q.toBigInteger(this.signum == val.signum ? 1 : -1);
 17.1347 +    }
 17.1348 +
 17.1349 +    /**
 17.1350 +     * Returns an array of two BigIntegers containing {@code (this / val)}
 17.1351 +     * followed by {@code (this % val)}.
 17.1352 +     *
 17.1353 +     * @param  val value by which this BigInteger is to be divided, and the
 17.1354 +     *         remainder computed.
 17.1355 +     * @return an array of two BigIntegers: the quotient {@code (this / val)}
 17.1356 +     *         is the initial element, and the remainder {@code (this % val)}
 17.1357 +     *         is the final element.
 17.1358 +     * @throws ArithmeticException if {@code val} is zero.
 17.1359 +     */
 17.1360 +    public BigInteger[] divideAndRemainder(BigInteger val) {
 17.1361 +        BigInteger[] result = new BigInteger[2];
 17.1362 +        MutableBigInteger q = new MutableBigInteger(),
 17.1363 +                          a = new MutableBigInteger(this.mag),
 17.1364 +                          b = new MutableBigInteger(val.mag);
 17.1365 +        MutableBigInteger r = a.divide(b, q);
 17.1366 +        result[0] = q.toBigInteger(this.signum == val.signum ? 1 : -1);
 17.1367 +        result[1] = r.toBigInteger(this.signum);
 17.1368 +        return result;
 17.1369 +    }
 17.1370 +
 17.1371 +    /**
 17.1372 +     * Returns a BigInteger whose value is {@code (this % val)}.
 17.1373 +     *
 17.1374 +     * @param  val value by which this BigInteger is to be divided, and the
 17.1375 +     *         remainder computed.
 17.1376 +     * @return {@code this % val}
 17.1377 +     * @throws ArithmeticException if {@code val} is zero.
 17.1378 +     */
 17.1379 +    public BigInteger remainder(BigInteger val) {
 17.1380 +        MutableBigInteger q = new MutableBigInteger(),
 17.1381 +                          a = new MutableBigInteger(this.mag),
 17.1382 +                          b = new MutableBigInteger(val.mag);
 17.1383 +
 17.1384 +        return a.divide(b, q).toBigInteger(this.signum);
 17.1385 +    }
 17.1386 +
 17.1387 +    /**
 17.1388 +     * Returns a BigInteger whose value is <tt>(this<sup>exponent</sup>)</tt>.
 17.1389 +     * Note that {@code exponent} is an integer rather than a BigInteger.
 17.1390 +     *
 17.1391 +     * @param  exponent exponent to which this BigInteger is to be raised.
 17.1392 +     * @return <tt>this<sup>exponent</sup></tt>
 17.1393 +     * @throws ArithmeticException {@code exponent} is negative.  (This would
 17.1394 +     *         cause the operation to yield a non-integer value.)
 17.1395 +     */
 17.1396 +    public BigInteger pow(int exponent) {
 17.1397 +        if (exponent < 0)
 17.1398 +            throw new ArithmeticException("Negative exponent");
 17.1399 +        if (signum==0)
 17.1400 +            return (exponent==0 ? ONE : this);
 17.1401 +
 17.1402 +        // Perform exponentiation using repeated squaring trick
 17.1403 +        int newSign = (signum<0 && (exponent&1)==1 ? -1 : 1);
 17.1404 +        int[] baseToPow2 = this.mag;
 17.1405 +        int[] result = {1};
 17.1406 +
 17.1407 +        while (exponent != 0) {
 17.1408 +            if ((exponent & 1)==1) {
 17.1409 +                result = multiplyToLen(result, result.length,
 17.1410 +                                       baseToPow2, baseToPow2.length, null);
 17.1411 +                result = trustedStripLeadingZeroInts(result);
 17.1412 +            }
 17.1413 +            if ((exponent >>>= 1) != 0) {
 17.1414 +                baseToPow2 = squareToLen(baseToPow2, baseToPow2.length, null);
 17.1415 +                baseToPow2 = trustedStripLeadingZeroInts(baseToPow2);
 17.1416 +            }
 17.1417 +        }
 17.1418 +        return new BigInteger(result, newSign);
 17.1419 +    }
 17.1420 +
 17.1421 +    /**
 17.1422 +     * Returns a BigInteger whose value is the greatest common divisor of
 17.1423 +     * {@code abs(this)} and {@code abs(val)}.  Returns 0 if
 17.1424 +     * {@code this==0 && val==0}.
 17.1425 +     *
 17.1426 +     * @param  val value with which the GCD is to be computed.
 17.1427 +     * @return {@code GCD(abs(this), abs(val))}
 17.1428 +     */
 17.1429 +    public BigInteger gcd(BigInteger val) {
 17.1430 +        if (val.signum == 0)
 17.1431 +            return this.abs();
 17.1432 +        else if (this.signum == 0)
 17.1433 +            return val.abs();
 17.1434 +
 17.1435 +        MutableBigInteger a = new MutableBigInteger(this);
 17.1436 +        MutableBigInteger b = new MutableBigInteger(val);
 17.1437 +
 17.1438 +        MutableBigInteger result = a.hybridGCD(b);
 17.1439 +
 17.1440 +        return result.toBigInteger(1);
 17.1441 +    }
 17.1442 +
 17.1443 +    /**
 17.1444 +     * Package private method to return bit length for an integer.
 17.1445 +     */
 17.1446 +    static int bitLengthForInt(int n) {
 17.1447 +        return 32 - Integer.numberOfLeadingZeros(n);
 17.1448 +    }
 17.1449 +
 17.1450 +    /**
 17.1451 +     * Left shift int array a up to len by n bits. Returns the array that
 17.1452 +     * results from the shift since space may have to be reallocated.
 17.1453 +     */
 17.1454 +    private static int[] leftShift(int[] a, int len, int n) {
 17.1455 +        int nInts = n >>> 5;
 17.1456 +        int nBits = n&0x1F;
 17.1457 +        int bitsInHighWord = bitLengthForInt(a[0]);
 17.1458 +
 17.1459 +        // If shift can be done without recopy, do so
 17.1460 +        if (n <= (32-bitsInHighWord)) {
 17.1461 +            primitiveLeftShift(a, len, nBits);
 17.1462 +            return a;
 17.1463 +        } else { // Array must be resized
 17.1464 +            if (nBits <= (32-bitsInHighWord)) {
 17.1465 +                int result[] = new int[nInts+len];
 17.1466 +                for (int i=0; i<len; i++)
 17.1467 +                    result[i] = a[i];
 17.1468 +                primitiveLeftShift(result, result.length, nBits);
 17.1469 +                return result;
 17.1470 +            } else {
 17.1471 +                int result[] = new int[nInts+len+1];
 17.1472 +                for (int i=0; i<len; i++)
 17.1473 +                    result[i] = a[i];
 17.1474 +                primitiveRightShift(result, result.length, 32 - nBits);
 17.1475 +                return result;
 17.1476 +            }
 17.1477 +        }
 17.1478 +    }
 17.1479 +
 17.1480 +    // shifts a up to len right n bits assumes no leading zeros, 0<n<32
 17.1481 +    static void primitiveRightShift(int[] a, int len, int n) {
 17.1482 +        int n2 = 32 - n;
 17.1483 +        for (int i=len-1, c=a[i]; i>0; i--) {
 17.1484 +            int b = c;
 17.1485 +            c = a[i-1];
 17.1486 +            a[i] = (c << n2) | (b >>> n);
 17.1487 +        }
 17.1488 +        a[0] >>>= n;
 17.1489 +    }
 17.1490 +
 17.1491 +    // shifts a up to len left n bits assumes no leading zeros, 0<=n<32
 17.1492 +    static void primitiveLeftShift(int[] a, int len, int n) {
 17.1493 +        if (len == 0 || n == 0)
 17.1494 +            return;
 17.1495 +
 17.1496 +        int n2 = 32 - n;
 17.1497 +        for (int i=0, c=a[i], m=i+len-1; i<m; i++) {
 17.1498 +            int b = c;
 17.1499 +            c = a[i+1];
 17.1500 +            a[i] = (b << n) | (c >>> n2);
 17.1501 +        }
 17.1502 +        a[len-1] <<= n;
 17.1503 +    }
 17.1504 +
 17.1505 +    /**
 17.1506 +     * Calculate bitlength of contents of the first len elements an int array,
 17.1507 +     * assuming there are no leading zero ints.
 17.1508 +     */
 17.1509 +    private static int bitLength(int[] val, int len) {
 17.1510 +        if (len == 0)
 17.1511 +            return 0;
 17.1512 +        return ((len - 1) << 5) + bitLengthForInt(val[0]);
 17.1513 +    }
 17.1514 +
 17.1515 +    /**
 17.1516 +     * Returns a BigInteger whose value is the absolute value of this
 17.1517 +     * BigInteger.
 17.1518 +     *
 17.1519 +     * @return {@code abs(this)}
 17.1520 +     */
 17.1521 +    public BigInteger abs() {
 17.1522 +        return (signum >= 0 ? this : this.negate());
 17.1523 +    }
 17.1524 +
 17.1525 +    /**
 17.1526 +     * Returns a BigInteger whose value is {@code (-this)}.
 17.1527 +     *
 17.1528 +     * @return {@code -this}
 17.1529 +     */
 17.1530 +    public BigInteger negate() {
 17.1531 +        return new BigInteger(this.mag, -this.signum);
 17.1532 +    }
 17.1533 +
 17.1534 +    /**
 17.1535 +     * Returns the signum function of this BigInteger.
 17.1536 +     *
 17.1537 +     * @return -1, 0 or 1 as the value of this BigInteger is negative, zero or
 17.1538 +     *         positive.
 17.1539 +     */
 17.1540 +    public int signum() {
 17.1541 +        return this.signum;
 17.1542 +    }
 17.1543 +
 17.1544 +    // Modular Arithmetic Operations
 17.1545 +
 17.1546 +    /**
 17.1547 +     * Returns a BigInteger whose value is {@code (this mod m}).  This method
 17.1548 +     * differs from {@code remainder} in that it always returns a
 17.1549 +     * <i>non-negative</i> BigInteger.
 17.1550 +     *
 17.1551 +     * @param  m the modulus.
 17.1552 +     * @return {@code this mod m}
 17.1553 +     * @throws ArithmeticException {@code m} &le; 0
 17.1554 +     * @see    #remainder
 17.1555 +     */
 17.1556 +    public BigInteger mod(BigInteger m) {
 17.1557 +        if (m.signum <= 0)
 17.1558 +            throw new ArithmeticException("BigInteger: modulus not positive");
 17.1559 +
 17.1560 +        BigInteger result = this.remainder(m);
 17.1561 +        return (result.signum >= 0 ? result : result.add(m));
 17.1562 +    }
 17.1563 +
 17.1564 +    /**
 17.1565 +     * Returns a BigInteger whose value is
 17.1566 +     * <tt>(this<sup>exponent</sup> mod m)</tt>.  (Unlike {@code pow}, this
 17.1567 +     * method permits negative exponents.)
 17.1568 +     *
 17.1569 +     * @param  exponent the exponent.
 17.1570 +     * @param  m the modulus.
 17.1571 +     * @return <tt>this<sup>exponent</sup> mod m</tt>
 17.1572 +     * @throws ArithmeticException {@code m} &le; 0 or the exponent is
 17.1573 +     *         negative and this BigInteger is not <i>relatively
 17.1574 +     *         prime</i> to {@code m}.
 17.1575 +     * @see    #modInverse
 17.1576 +     */
 17.1577 +    public BigInteger modPow(BigInteger exponent, BigInteger m) {
 17.1578 +        if (m.signum <= 0)
 17.1579 +            throw new ArithmeticException("BigInteger: modulus not positive");
 17.1580 +
 17.1581 +        // Trivial cases
 17.1582 +        if (exponent.signum == 0)
 17.1583 +            return (m.equals(ONE) ? ZERO : ONE);
 17.1584 +
 17.1585 +        if (this.equals(ONE))
 17.1586 +            return (m.equals(ONE) ? ZERO : ONE);
 17.1587 +
 17.1588 +        if (this.equals(ZERO) && exponent.signum >= 0)
 17.1589 +            return ZERO;
 17.1590 +
 17.1591 +        if (this.equals(negConst[1]) && (!exponent.testBit(0)))
 17.1592 +            return (m.equals(ONE) ? ZERO : ONE);
 17.1593 +
 17.1594 +        boolean invertResult;
 17.1595 +        if ((invertResult = (exponent.signum < 0)))
 17.1596 +            exponent = exponent.negate();
 17.1597 +
 17.1598 +        BigInteger base = (this.signum < 0 || this.compareTo(m) >= 0
 17.1599 +                           ? this.mod(m) : this);
 17.1600 +        BigInteger result;
 17.1601 +        if (m.testBit(0)) { // odd modulus
 17.1602 +            result = base.oddModPow(exponent, m);
 17.1603 +        } else {
 17.1604 +            /*
 17.1605 +             * Even modulus.  Tear it into an "odd part" (m1) and power of two
 17.1606 +             * (m2), exponentiate mod m1, manually exponentiate mod m2, and
 17.1607 +             * use Chinese Remainder Theorem to combine results.
 17.1608 +             */
 17.1609 +
 17.1610 +            // Tear m apart into odd part (m1) and power of 2 (m2)
 17.1611 +            int p = m.getLowestSetBit();   // Max pow of 2 that divides m
 17.1612 +
 17.1613 +            BigInteger m1 = m.shiftRight(p);  // m/2**p
 17.1614 +            BigInteger m2 = ONE.shiftLeft(p); // 2**p
 17.1615 +
 17.1616 +            // Calculate new base from m1
 17.1617 +            BigInteger base2 = (this.signum < 0 || this.compareTo(m1) >= 0
 17.1618 +                                ? this.mod(m1) : this);
 17.1619 +
 17.1620 +            // Caculate (base ** exponent) mod m1.
 17.1621 +            BigInteger a1 = (m1.equals(ONE) ? ZERO :
 17.1622 +                             base2.oddModPow(exponent, m1));
 17.1623 +
 17.1624 +            // Calculate (this ** exponent) mod m2
 17.1625 +            BigInteger a2 = base.modPow2(exponent, p);
 17.1626 +
 17.1627 +            // Combine results using Chinese Remainder Theorem
 17.1628 +            BigInteger y1 = m2.modInverse(m1);
 17.1629 +            BigInteger y2 = m1.modInverse(m2);
 17.1630 +
 17.1631 +            result = a1.multiply(m2).multiply(y1).add
 17.1632 +                     (a2.multiply(m1).multiply(y2)).mod(m);
 17.1633 +        }
 17.1634 +
 17.1635 +        return (invertResult ? result.modInverse(m) : result);
 17.1636 +    }
 17.1637 +
 17.1638 +    static int[] bnExpModThreshTable = {7, 25, 81, 241, 673, 1793,
 17.1639 +                                                Integer.MAX_VALUE}; // Sentinel
 17.1640 +
 17.1641 +    /**
 17.1642 +     * Returns a BigInteger whose value is x to the power of y mod z.
 17.1643 +     * Assumes: z is odd && x < z.
 17.1644 +     */
 17.1645 +    private BigInteger oddModPow(BigInteger y, BigInteger z) {
 17.1646 +    /*
 17.1647 +     * The algorithm is adapted from Colin Plumb's C library.
 17.1648 +     *
 17.1649 +     * The window algorithm:
 17.1650 +     * The idea is to keep a running product of b1 = n^(high-order bits of exp)
 17.1651 +     * and then keep appending exponent bits to it.  The following patterns
 17.1652 +     * apply to a 3-bit window (k = 3):
 17.1653 +     * To append   0: square
 17.1654 +     * To append   1: square, multiply by n^1
 17.1655 +     * To append  10: square, multiply by n^1, square
 17.1656 +     * To append  11: square, square, multiply by n^3
 17.1657 +     * To append 100: square, multiply by n^1, square, square
 17.1658 +     * To append 101: square, square, square, multiply by n^5
 17.1659 +     * To append 110: square, square, multiply by n^3, square
 17.1660 +     * To append 111: square, square, square, multiply by n^7
 17.1661 +     *
 17.1662 +     * Since each pattern involves only one multiply, the longer the pattern
 17.1663 +     * the better, except that a 0 (no multiplies) can be appended directly.
 17.1664 +     * We precompute a table of odd powers of n, up to 2^k, and can then
 17.1665 +     * multiply k bits of exponent at a time.  Actually, assuming random
 17.1666 +     * exponents, there is on average one zero bit between needs to
 17.1667 +     * multiply (1/2 of the time there's none, 1/4 of the time there's 1,
 17.1668 +     * 1/8 of the time, there's 2, 1/32 of the time, there's 3, etc.), so
 17.1669 +     * you have to do one multiply per k+1 bits of exponent.
 17.1670 +     *
 17.1671 +     * The loop walks down the exponent, squaring the result buffer as
 17.1672 +     * it goes.  There is a wbits+1 bit lookahead buffer, buf, that is
 17.1673 +     * filled with the upcoming exponent bits.  (What is read after the
 17.1674 +     * end of the exponent is unimportant, but it is filled with zero here.)
 17.1675 +     * When the most-significant bit of this buffer becomes set, i.e.
 17.1676 +     * (buf & tblmask) != 0, we have to decide what pattern to multiply
 17.1677 +     * by, and when to do it.  We decide, remember to do it in future
 17.1678 +     * after a suitable number of squarings have passed (e.g. a pattern
 17.1679 +     * of "100" in the buffer requires that we multiply by n^1 immediately;
 17.1680 +     * a pattern of "110" calls for multiplying by n^3 after one more
 17.1681 +     * squaring), clear the buffer, and continue.
 17.1682 +     *
 17.1683 +     * When we start, there is one more optimization: the result buffer
 17.1684 +     * is implcitly one, so squaring it or multiplying by it can be
 17.1685 +     * optimized away.  Further, if we start with a pattern like "100"
 17.1686 +     * in the lookahead window, rather than placing n into the buffer
 17.1687 +     * and then starting to square it, we have already computed n^2
 17.1688 +     * to compute the odd-powers table, so we can place that into
 17.1689 +     * the buffer and save a squaring.
 17.1690 +     *
 17.1691 +     * This means that if you have a k-bit window, to compute n^z,
 17.1692 +     * where z is the high k bits of the exponent, 1/2 of the time
 17.1693 +     * it requires no squarings.  1/4 of the time, it requires 1
 17.1694 +     * squaring, ... 1/2^(k-1) of the time, it reqires k-2 squarings.
 17.1695 +     * And the remaining 1/2^(k-1) of the time, the top k bits are a
 17.1696 +     * 1 followed by k-1 0 bits, so it again only requires k-2
 17.1697 +     * squarings, not k-1.  The average of these is 1.  Add that
 17.1698 +     * to the one squaring we have to do to compute the table,
 17.1699 +     * and you'll see that a k-bit window saves k-2 squarings
 17.1700 +     * as well as reducing the multiplies.  (It actually doesn't
 17.1701 +     * hurt in the case k = 1, either.)
 17.1702 +     */
 17.1703 +        // Special case for exponent of one
 17.1704 +        if (y.equals(ONE))
 17.1705 +            return this;
 17.1706 +
 17.1707 +        // Special case for base of zero
 17.1708 +        if (signum==0)
 17.1709 +            return ZERO;
 17.1710 +
 17.1711 +        int[] base = mag.clone();
 17.1712 +        int[] exp = y.mag;
 17.1713 +        int[] mod = z.mag;
 17.1714 +        int modLen = mod.length;
 17.1715 +
 17.1716 +        // Select an appropriate window size
 17.1717 +        int wbits = 0;
 17.1718 +        int ebits = bitLength(exp, exp.length);
 17.1719 +        // if exponent is 65537 (0x10001), use minimum window size
 17.1720 +        if ((ebits != 17) || (exp[0] != 65537)) {
 17.1721 +            while (ebits > bnExpModThreshTable[wbits]) {
 17.1722 +                wbits++;
 17.1723 +            }
 17.1724 +        }
 17.1725 +
 17.1726 +        // Calculate appropriate table size
 17.1727 +        int tblmask = 1 << wbits;
 17.1728 +
 17.1729 +        // Allocate table for precomputed odd powers of base in Montgomery form
 17.1730 +        int[][] table = new int[tblmask][];
 17.1731 +        for (int i=0; i<tblmask; i++)
 17.1732 +            table[i] = new int[modLen];
 17.1733 +
 17.1734 +        // Compute the modular inverse
 17.1735 +        int inv = -MutableBigInteger.inverseMod32(mod[modLen-1]);
 17.1736 +
 17.1737 +        // Convert base to Montgomery form
 17.1738 +        int[] a = leftShift(base, base.length, modLen << 5);
 17.1739 +
 17.1740 +        MutableBigInteger q = new MutableBigInteger(),
 17.1741 +                          a2 = new MutableBigInteger(a),
 17.1742 +                          b2 = new MutableBigInteger(mod);
 17.1743 +
 17.1744 +        MutableBigInteger r= a2.divide(b2, q);
 17.1745 +        table[0] = r.toIntArray();
 17.1746 +
 17.1747 +        // Pad table[0] with leading zeros so its length is at least modLen
 17.1748 +        if (table[0].length < modLen) {
 17.1749 +           int offset = modLen - table[0].length;
 17.1750 +           int[] t2 = new int[modLen];
 17.1751 +           for (int i=0; i<table[0].length; i++)
 17.1752 +               t2[i+offset] = table[0][i];
 17.1753 +           table[0] = t2;
 17.1754 +        }
 17.1755 +
 17.1756 +        // Set b to the square of the base
 17.1757 +        int[] b = squareToLen(table[0], modLen, null);
 17.1758 +        b = montReduce(b, mod, modLen, inv);
 17.1759 +
 17.1760 +        // Set t to high half of b
 17.1761 +        int[] t = new int[modLen];
 17.1762 +        for(int i=0; i<modLen; i++)
 17.1763 +            t[i] = b[i];
 17.1764 +
 17.1765 +        // Fill in the table with odd powers of the base
 17.1766 +        for (int i=1; i<tblmask; i++) {
 17.1767 +            int[] prod = multiplyToLen(t, modLen, table[i-1], modLen, null);
 17.1768 +            table[i] = montReduce(prod, mod, modLen, inv);
 17.1769 +        }
 17.1770 +
 17.1771 +        // Pre load the window that slides over the exponent
 17.1772 +        int bitpos = 1 << ((ebits-1) & (32-1));
 17.1773 +
 17.1774 +        int buf = 0;
 17.1775 +        int elen = exp.length;
 17.1776 +        int eIndex = 0;
 17.1777 +        for (int i = 0; i <= wbits; i++) {
 17.1778 +            buf = (buf << 1) | (((exp[eIndex] & bitpos) != 0)?1:0);
 17.1779 +            bitpos >>>= 1;
 17.1780 +            if (bitpos == 0) {
 17.1781 +                eIndex++;
 17.1782 +                bitpos = 1 << (32-1);
 17.1783 +                elen--;
 17.1784 +            }
 17.1785 +        }
 17.1786 +
 17.1787 +        int multpos = ebits;
 17.1788 +
 17.1789 +        // The first iteration, which is hoisted out of the main loop
 17.1790 +        ebits--;
 17.1791 +        boolean isone = true;
 17.1792 +
 17.1793 +        multpos = ebits - wbits;
 17.1794 +        while ((buf & 1) == 0) {
 17.1795 +            buf >>>= 1;
 17.1796 +            multpos++;
 17.1797 +        }
 17.1798 +
 17.1799 +        int[] mult = table[buf >>> 1];
 17.1800 +
 17.1801 +        buf = 0;
 17.1802 +        if (multpos == ebits)
 17.1803 +            isone = false;
 17.1804 +
 17.1805 +        // The main loop
 17.1806 +        while(true) {
 17.1807 +            ebits--;
 17.1808 +            // Advance the window
 17.1809 +            buf <<= 1;
 17.1810 +
 17.1811 +            if (elen != 0) {
 17.1812 +                buf |= ((exp[eIndex] & bitpos) != 0) ? 1 : 0;
 17.1813 +                bitpos >>>= 1;
 17.1814 +                if (bitpos == 0) {
 17.1815 +                    eIndex++;
 17.1816 +                    bitpos = 1 << (32-1);
 17.1817 +                    elen--;
 17.1818 +                }
 17.1819 +            }
 17.1820 +
 17.1821 +            // Examine the window for pending multiplies
 17.1822 +            if ((buf & tblmask) != 0) {
 17.1823 +                multpos = ebits - wbits;
 17.1824 +                while ((buf & 1) == 0) {
 17.1825 +                    buf >>>= 1;
 17.1826 +                    multpos++;
 17.1827 +                }
 17.1828 +                mult = table[buf >>> 1];
 17.1829 +                buf = 0;
 17.1830 +            }
 17.1831 +
 17.1832 +            // Perform multiply
 17.1833 +            if (ebits == multpos) {
 17.1834 +                if (isone) {
 17.1835 +                    b = mult.clone();
 17.1836 +                    isone = false;
 17.1837 +                } else {
 17.1838 +                    t = b;
 17.1839 +                    a = multiplyToLen(t, modLen, mult, modLen, a);
 17.1840 +                    a = montReduce(a, mod, modLen, inv);
 17.1841 +                    t = a; a = b; b = t;
 17.1842 +                }
 17.1843 +            }
 17.1844 +
 17.1845 +            // Check if done
 17.1846 +            if (ebits == 0)
 17.1847 +                break;
 17.1848 +
 17.1849 +            // Square the input
 17.1850 +            if (!isone) {
 17.1851 +                t = b;
 17.1852 +                a = squareToLen(t, modLen, a);
 17.1853 +                a = montReduce(a, mod, modLen, inv);
 17.1854 +                t = a; a = b; b = t;
 17.1855 +            }
 17.1856 +        }
 17.1857 +
 17.1858 +        // Convert result out of Montgomery form and return
 17.1859 +        int[] t2 = new int[2*modLen];
 17.1860 +        for(int i=0; i<modLen; i++)
 17.1861 +            t2[i+modLen] = b[i];
 17.1862 +
 17.1863 +        b = montReduce(t2, mod, modLen, inv);
 17.1864 +
 17.1865 +        t2 = new int[modLen];
 17.1866 +        for(int i=0; i<modLen; i++)
 17.1867 +            t2[i] = b[i];
 17.1868 +
 17.1869 +        return new BigInteger(1, t2);
 17.1870 +    }
 17.1871 +
 17.1872 +    /**
 17.1873 +     * Montgomery reduce n, modulo mod.  This reduces modulo mod and divides
 17.1874 +     * by 2^(32*mlen). Adapted from Colin Plumb's C library.
 17.1875 +     */
 17.1876 +    private static int[] montReduce(int[] n, int[] mod, int mlen, int inv) {
 17.1877 +        int c=0;
 17.1878 +        int len = mlen;
 17.1879 +        int offset=0;
 17.1880 +
 17.1881 +        do {
 17.1882 +            int nEnd = n[n.length-1-offset];
 17.1883 +            int carry = mulAdd(n, mod, offset, mlen, inv * nEnd);
 17.1884 +            c += addOne(n, offset, mlen, carry);
 17.1885 +            offset++;
 17.1886 +        } while(--len > 0);
 17.1887 +
 17.1888 +        while(c>0)
 17.1889 +            c += subN(n, mod, mlen);
 17.1890 +
 17.1891 +        while (intArrayCmpToLen(n, mod, mlen) >= 0)
 17.1892 +            subN(n, mod, mlen);
 17.1893 +
 17.1894 +        return n;
 17.1895 +    }
 17.1896 +
 17.1897 +
 17.1898 +    /*
 17.1899 +     * Returns -1, 0 or +1 as big-endian unsigned int array arg1 is less than,
 17.1900 +     * equal to, or greater than arg2 up to length len.
 17.1901 +     */
 17.1902 +    private static int intArrayCmpToLen(int[] arg1, int[] arg2, int len) {
 17.1903 +        for (int i=0; i<len; i++) {
 17.1904 +            long b1 = arg1[i] & LONG_MASK;
 17.1905 +            long b2 = arg2[i] & LONG_MASK;
 17.1906 +            if (b1 < b2)
 17.1907 +                return -1;
 17.1908 +            if (b1 > b2)
 17.1909 +                return 1;
 17.1910 +        }
 17.1911 +        return 0;
 17.1912 +    }
 17.1913 +
 17.1914 +    /**
 17.1915 +     * Subtracts two numbers of same length, returning borrow.
 17.1916 +     */
 17.1917 +    private static int subN(int[] a, int[] b, int len) {
 17.1918 +        long sum = 0;
 17.1919 +
 17.1920 +        while(--len >= 0) {
 17.1921 +            sum = (a[len] & LONG_MASK) -
 17.1922 +                 (b[len] & LONG_MASK) + (sum >> 32);
 17.1923 +            a[len] = (int)sum;
 17.1924 +        }
 17.1925 +
 17.1926 +        return (int)(sum >> 32);
 17.1927 +    }
 17.1928 +
 17.1929 +    /**
 17.1930 +     * Multiply an array by one word k and add to result, return the carry
 17.1931 +     */
 17.1932 +    static int mulAdd(int[] out, int[] in, int offset, int len, int k) {
 17.1933 +        long kLong = k & LONG_MASK;
 17.1934 +        long carry = 0;
 17.1935 +
 17.1936 +        offset = out.length-offset - 1;
 17.1937 +        for (int j=len-1; j >= 0; j--) {
 17.1938 +            long product = (in[j] & LONG_MASK) * kLong +
 17.1939 +                           (out[offset] & LONG_MASK) + carry;
 17.1940 +            out[offset--] = (int)product;
 17.1941 +            carry = product >>> 32;
 17.1942 +        }
 17.1943 +        return (int)carry;
 17.1944 +    }
 17.1945 +
 17.1946 +    /**
 17.1947 +     * Add one word to the number a mlen words into a. Return the resulting
 17.1948 +     * carry.
 17.1949 +     */
 17.1950 +    static int addOne(int[] a, int offset, int mlen, int carry) {
 17.1951 +        offset = a.length-1-mlen-offset;
 17.1952 +        long t = (a[offset] & LONG_MASK) + (carry & LONG_MASK);
 17.1953 +
 17.1954 +        a[offset] = (int)t;
 17.1955 +        if ((t >>> 32) == 0)
 17.1956 +            return 0;
 17.1957 +        while (--mlen >= 0) {
 17.1958 +            if (--offset < 0) { // Carry out of number
 17.1959 +                return 1;
 17.1960 +            } else {
 17.1961 +                a[offset]++;
 17.1962 +                if (a[offset] != 0)
 17.1963 +                    return 0;
 17.1964 +            }
 17.1965 +        }
 17.1966 +        return 1;
 17.1967 +    }
 17.1968 +
 17.1969 +    /**
 17.1970 +     * Returns a BigInteger whose value is (this ** exponent) mod (2**p)
 17.1971 +     */
 17.1972 +    private BigInteger modPow2(BigInteger exponent, int p) {
 17.1973 +        /*
 17.1974 +         * Perform exponentiation using repeated squaring trick, chopping off
 17.1975 +         * high order bits as indicated by modulus.
 17.1976 +         */
 17.1977 +        BigInteger result = valueOf(1);
 17.1978 +        BigInteger baseToPow2 = this.mod2(p);
 17.1979 +        int expOffset = 0;
 17.1980 +
 17.1981 +        int limit = exponent.bitLength();
 17.1982 +
 17.1983 +        if (this.testBit(0))
 17.1984 +           limit = (p-1) < limit ? (p-1) : limit;
 17.1985 +
 17.1986 +        while (expOffset < limit) {
 17.1987 +            if (exponent.testBit(expOffset))
 17.1988 +                result = result.multiply(baseToPow2).mod2(p);
 17.1989 +            expOffset++;
 17.1990 +            if (expOffset < limit)
 17.1991 +                baseToPow2 = baseToPow2.square().mod2(p);
 17.1992 +        }
 17.1993 +
 17.1994 +        return result;
 17.1995 +    }
 17.1996 +
 17.1997 +    /**
 17.1998 +     * Returns a BigInteger whose value is this mod(2**p).
 17.1999 +     * Assumes that this {@code BigInteger >= 0} and {@code p > 0}.
 17.2000 +     */
 17.2001 +    private BigInteger mod2(int p) {
 17.2002 +        if (bitLength() <= p)
 17.2003 +            return this;
 17.2004 +
 17.2005 +        // Copy remaining ints of mag
 17.2006 +        int numInts = (p + 31) >>> 5;
 17.2007 +        int[] mag = new int[numInts];
 17.2008 +        for (int i=0; i<numInts; i++)
 17.2009 +            mag[i] = this.mag[i + (this.mag.length - numInts)];
 17.2010 +
 17.2011 +        // Mask out any excess bits
 17.2012 +        int excessBits = (numInts << 5) - p;
 17.2013 +        mag[0] &= (1L << (32-excessBits)) - 1;
 17.2014 +
 17.2015 +        return (mag[0]==0 ? new BigInteger(1, mag) : new BigInteger(mag, 1));
 17.2016 +    }
 17.2017 +
 17.2018 +    /**
 17.2019 +     * Returns a BigInteger whose value is {@code (this}<sup>-1</sup> {@code mod m)}.
 17.2020 +     *
 17.2021 +     * @param  m the modulus.
 17.2022 +     * @return {@code this}<sup>-1</sup> {@code mod m}.
 17.2023 +     * @throws ArithmeticException {@code  m} &le; 0, or this BigInteger
 17.2024 +     *         has no multiplicative inverse mod m (that is, this BigInteger
 17.2025 +     *         is not <i>relatively prime</i> to m).
 17.2026 +     */
 17.2027 +    public BigInteger modInverse(BigInteger m) {
 17.2028 +        if (m.signum != 1)
 17.2029 +            throw new ArithmeticException("BigInteger: modulus not positive");
 17.2030 +
 17.2031 +        if (m.equals(ONE))
 17.2032 +            return ZERO;
 17.2033 +
 17.2034 +        // Calculate (this mod m)
 17.2035 +        BigInteger modVal = this;
 17.2036 +        if (signum < 0 || (this.compareMagnitude(m) >= 0))
 17.2037 +            modVal = this.mod(m);
 17.2038 +
 17.2039 +        if (modVal.equals(ONE))
 17.2040 +            return ONE;
 17.2041 +
 17.2042 +        MutableBigInteger a = new MutableBigInteger(modVal);
 17.2043 +        MutableBigInteger b = new MutableBigInteger(m);
 17.2044 +
 17.2045 +        MutableBigInteger result = a.mutableModInverse(b);
 17.2046 +        return result.toBigInteger(1);
 17.2047 +    }
 17.2048 +
 17.2049 +    // Shift Operations
 17.2050 +
 17.2051 +    /**
 17.2052 +     * Returns a BigInteger whose value is {@code (this << n)}.
 17.2053 +     * The shift distance, {@code n}, may be negative, in which case
 17.2054 +     * this method performs a right shift.
 17.2055 +     * (Computes <tt>floor(this * 2<sup>n</sup>)</tt>.)
 17.2056 +     *
 17.2057 +     * @param  n shift distance, in bits.
 17.2058 +     * @return {@code this << n}
 17.2059 +     * @throws ArithmeticException if the shift distance is {@code
 17.2060 +     *         Integer.MIN_VALUE}.
 17.2061 +     * @see #shiftRight
 17.2062 +     */
 17.2063 +    public BigInteger shiftLeft(int n) {
 17.2064 +        if (signum == 0)
 17.2065 +            return ZERO;
 17.2066 +        if (n==0)
 17.2067 +            return this;
 17.2068 +        if (n<0) {
 17.2069 +            if (n == Integer.MIN_VALUE) {
 17.2070 +                throw new ArithmeticException("Shift distance of Integer.MIN_VALUE not supported.");
 17.2071 +            } else {
 17.2072 +                return shiftRight(-n);
 17.2073 +            }
 17.2074 +        }
 17.2075 +
 17.2076 +        int nInts = n >>> 5;
 17.2077 +        int nBits = n & 0x1f;
 17.2078 +        int magLen = mag.length;
 17.2079 +        int newMag[] = null;
 17.2080 +
 17.2081 +        if (nBits == 0) {
 17.2082 +            newMag = new int[magLen + nInts];
 17.2083 +            for (int i=0; i<magLen; i++)
 17.2084 +                newMag[i] = mag[i];
 17.2085 +        } else {
 17.2086 +            int i = 0;
 17.2087 +            int nBits2 = 32 - nBits;
 17.2088 +            int highBits = mag[0] >>> nBits2;
 17.2089 +            if (highBits != 0) {
 17.2090 +                newMag = new int[magLen + nInts + 1];
 17.2091 +                newMag[i++] = highBits;
 17.2092 +            } else {
 17.2093 +                newMag = new int[magLen + nInts];
 17.2094 +            }
 17.2095 +            int j=0;
 17.2096 +            while (j < magLen-1)
 17.2097 +                newMag[i++] = mag[j++] << nBits | mag[j] >>> nBits2;
 17.2098 +            newMag[i] = mag[j] << nBits;
 17.2099 +        }
 17.2100 +
 17.2101 +        return new BigInteger(newMag, signum);
 17.2102 +    }
 17.2103 +
 17.2104 +    /**
 17.2105 +     * Returns a BigInteger whose value is {@code (this >> n)}.  Sign
 17.2106 +     * extension is performed.  The shift distance, {@code n}, may be
 17.2107 +     * negative, in which case this method performs a left shift.
 17.2108 +     * (Computes <tt>floor(this / 2<sup>n</sup>)</tt>.)
 17.2109 +     *
 17.2110 +     * @param  n shift distance, in bits.
 17.2111 +     * @return {@code this >> n}
 17.2112 +     * @throws ArithmeticException if the shift distance is {@code
 17.2113 +     *         Integer.MIN_VALUE}.
 17.2114 +     * @see #shiftLeft
 17.2115 +     */
 17.2116 +    public BigInteger shiftRight(int n) {
 17.2117 +        if (n==0)
 17.2118 +            return this;
 17.2119 +        if (n<0) {
 17.2120 +            if (n == Integer.MIN_VALUE) {
 17.2121 +                throw new ArithmeticException("Shift distance of Integer.MIN_VALUE not supported.");
 17.2122 +            } else {
 17.2123 +                return shiftLeft(-n);
 17.2124 +            }
 17.2125 +        }
 17.2126 +
 17.2127 +        int nInts = n >>> 5;
 17.2128 +        int nBits = n & 0x1f;
 17.2129 +        int magLen = mag.length;
 17.2130 +        int newMag[] = null;
 17.2131 +
 17.2132 +        // Special case: entire contents shifted off the end
 17.2133 +        if (nInts >= magLen)
 17.2134 +            return (signum >= 0 ? ZERO : negConst[1]);
 17.2135 +
 17.2136 +        if (nBits == 0) {
 17.2137 +            int newMagLen = magLen - nInts;
 17.2138 +            newMag = new int[newMagLen];
 17.2139 +            for (int i=0; i<newMagLen; i++)
 17.2140 +                newMag[i] = mag[i];
 17.2141 +        } else {
 17.2142 +            int i = 0;
 17.2143 +            int highBits = mag[0] >>> nBits;
 17.2144 +            if (highBits != 0) {
 17.2145 +                newMag = new int[magLen - nInts];
 17.2146 +                newMag[i++] = highBits;
 17.2147 +            } else {
 17.2148 +                newMag = new int[magLen - nInts -1];
 17.2149 +            }
 17.2150 +
 17.2151 +            int nBits2 = 32 - nBits;
 17.2152 +            int j=0;
 17.2153 +            while (j < magLen - nInts - 1)
 17.2154 +                newMag[i++] = (mag[j++] << nBits2) | (mag[j] >>> nBits);
 17.2155 +        }
 17.2156 +
 17.2157 +        if (signum < 0) {
 17.2158 +            // Find out whether any one-bits were shifted off the end.
 17.2159 +            boolean onesLost = false;
 17.2160 +            for (int i=magLen-1, j=magLen-nInts; i>=j && !onesLost; i--)
 17.2161 +                onesLost = (mag[i] != 0);
 17.2162 +            if (!onesLost && nBits != 0)
 17.2163 +                onesLost = (mag[magLen - nInts - 1] << (32 - nBits) != 0);
 17.2164 +
 17.2165 +            if (onesLost)
 17.2166 +                newMag = javaIncrement(newMag);
 17.2167 +        }
 17.2168 +
 17.2169 +        return new BigInteger(newMag, signum);
 17.2170 +    }
 17.2171 +
 17.2172 +    int[] javaIncrement(int[] val) {
 17.2173 +        int lastSum = 0;
 17.2174 +        for (int i=val.length-1;  i >= 0 && lastSum == 0; i--)
 17.2175 +            lastSum = (val[i] += 1);
 17.2176 +        if (lastSum == 0) {
 17.2177 +            val = new int[val.length+1];
 17.2178 +            val[0] = 1;
 17.2179 +        }
 17.2180 +        return val;
 17.2181 +    }
 17.2182 +
 17.2183 +    // Bitwise Operations
 17.2184 +
 17.2185 +    /**
 17.2186 +     * Returns a BigInteger whose value is {@code (this & val)}.  (This
 17.2187 +     * method returns a negative BigInteger if and only if this and val are
 17.2188 +     * both negative.)
 17.2189 +     *
 17.2190 +     * @param val value to be AND'ed with this BigInteger.
 17.2191 +     * @return {@code this & val}
 17.2192 +     */
 17.2193 +    public BigInteger and(BigInteger val) {
 17.2194 +        int[] result = new int[Math.max(intLength(), val.intLength())];
 17.2195 +        for (int i=0; i<result.length; i++)
 17.2196 +            result[i] = (getInt(result.length-i-1)
 17.2197 +                         & val.getInt(result.length-i-1));
 17.2198 +
 17.2199 +        return valueOf(result);
 17.2200 +    }
 17.2201 +
 17.2202 +    /**
 17.2203 +     * Returns a BigInteger whose value is {@code (this | val)}.  (This method
 17.2204 +     * returns a negative BigInteger if and only if either this or val is
 17.2205 +     * negative.)
 17.2206 +     *
 17.2207 +     * @param val value to be OR'ed with this BigInteger.
 17.2208 +     * @return {@code this | val}
 17.2209 +     */
 17.2210 +    public BigInteger or(BigInteger val) {
 17.2211 +        int[] result = new int[Math.max(intLength(), val.intLength())];
 17.2212 +        for (int i=0; i<result.length; i++)
 17.2213 +            result[i] = (getInt(result.length-i-1)
 17.2214 +                         | val.getInt(result.length-i-1));
 17.2215 +
 17.2216 +        return valueOf(result);
 17.2217 +    }
 17.2218 +
 17.2219 +    /**
 17.2220 +     * Returns a BigInteger whose value is {@code (this ^ val)}.  (This method
 17.2221 +     * returns a negative BigInteger if and only if exactly one of this and
 17.2222 +     * val are negative.)
 17.2223 +     *
 17.2224 +     * @param val value to be XOR'ed with this BigInteger.
 17.2225 +     * @return {@code this ^ val}
 17.2226 +     */
 17.2227 +    public BigInteger xor(BigInteger val) {
 17.2228 +        int[] result = new int[Math.max(intLength(), val.intLength())];
 17.2229 +        for (int i=0; i<result.length; i++)
 17.2230 +            result[i] = (getInt(result.length-i-1)
 17.2231 +                         ^ val.getInt(result.length-i-1));
 17.2232 +
 17.2233 +        return valueOf(result);
 17.2234 +    }
 17.2235 +
 17.2236 +    /**
 17.2237 +     * Returns a BigInteger whose value is {@code (~this)}.  (This method
 17.2238 +     * returns a negative value if and only if this BigInteger is
 17.2239 +     * non-negative.)
 17.2240 +     *
 17.2241 +     * @return {@code ~this}
 17.2242 +     */
 17.2243 +    public BigInteger not() {
 17.2244 +        int[] result = new int[intLength()];
 17.2245 +        for (int i=0; i<result.length; i++)
 17.2246 +            result[i] = ~getInt(result.length-i-1);
 17.2247 +
 17.2248 +        return valueOf(result);
 17.2249 +    }
 17.2250 +
 17.2251 +    /**
 17.2252 +     * Returns a BigInteger whose value is {@code (this & ~val)}.  This
 17.2253 +     * method, which is equivalent to {@code and(val.not())}, is provided as
 17.2254 +     * a convenience for masking operations.  (This method returns a negative
 17.2255 +     * BigInteger if and only if {@code this} is negative and {@code val} is
 17.2256 +     * positive.)
 17.2257 +     *
 17.2258 +     * @param val value to be complemented and AND'ed with this BigInteger.
 17.2259 +     * @return {@code this & ~val}
 17.2260 +     */
 17.2261 +    public BigInteger andNot(BigInteger val) {
 17.2262 +        int[] result = new int[Math.max(intLength(), val.intLength())];
 17.2263 +        for (int i=0; i<result.length; i++)
 17.2264 +            result[i] = (getInt(result.length-i-1)
 17.2265 +                         & ~val.getInt(result.length-i-1));
 17.2266 +
 17.2267 +        return valueOf(result);
 17.2268 +    }
 17.2269 +
 17.2270 +
 17.2271 +    // Single Bit Operations
 17.2272 +
 17.2273 +    /**
 17.2274 +     * Returns {@code true} if and only if the designated bit is set.
 17.2275 +     * (Computes {@code ((this & (1<<n)) != 0)}.)
 17.2276 +     *
 17.2277 +     * @param  n index of bit to test.
 17.2278 +     * @return {@code true} if and only if the designated bit is set.
 17.2279 +     * @throws ArithmeticException {@code n} is negative.
 17.2280 +     */
 17.2281 +    public boolean testBit(int n) {
 17.2282 +        if (n<0)
 17.2283 +            throw new ArithmeticException("Negative bit address");
 17.2284 +
 17.2285 +        return (getInt(n >>> 5) & (1 << (n & 31))) != 0;
 17.2286 +    }
 17.2287 +
 17.2288 +    /**
 17.2289 +     * Returns a BigInteger whose value is equivalent to this BigInteger
 17.2290 +     * with the designated bit set.  (Computes {@code (this | (1<<n))}.)
 17.2291 +     *
 17.2292 +     * @param  n index of bit to set.
 17.2293 +     * @return {@code this | (1<<n)}
 17.2294 +     * @throws ArithmeticException {@code n} is negative.
 17.2295 +     */
 17.2296 +    public BigInteger setBit(int n) {
 17.2297 +        if (n<0)
 17.2298 +            throw new ArithmeticException("Negative bit address");
 17.2299 +
 17.2300 +        int intNum = n >>> 5;
 17.2301 +        int[] result = new int[Math.max(intLength(), intNum+2)];
 17.2302 +
 17.2303 +        for (int i=0; i<result.length; i++)
 17.2304 +            result[result.length-i-1] = getInt(i);
 17.2305 +
 17.2306 +        result[result.length-intNum-1] |= (1 << (n & 31));
 17.2307 +
 17.2308 +        return valueOf(result);
 17.2309 +    }
 17.2310 +
 17.2311 +    /**
 17.2312 +     * Returns a BigInteger whose value is equivalent to this BigInteger
 17.2313 +     * with the designated bit cleared.
 17.2314 +     * (Computes {@code (this & ~(1<<n))}.)
 17.2315 +     *
 17.2316 +     * @param  n index of bit to clear.
 17.2317 +     * @return {@code this & ~(1<<n)}
 17.2318 +     * @throws ArithmeticException {@code n} is negative.
 17.2319 +     */
 17.2320 +    public BigInteger clearBit(int n) {
 17.2321 +        if (n<0)
 17.2322 +            throw new ArithmeticException("Negative bit address");
 17.2323 +
 17.2324 +        int intNum = n >>> 5;
 17.2325 +        int[] result = new int[Math.max(intLength(), ((n + 1) >>> 5) + 1)];
 17.2326 +
 17.2327 +        for (int i=0; i<result.length; i++)
 17.2328 +            result[result.length-i-1] = getInt(i);
 17.2329 +
 17.2330 +        result[result.length-intNum-1] &= ~(1 << (n & 31));
 17.2331 +
 17.2332 +        return valueOf(result);
 17.2333 +    }
 17.2334 +
 17.2335 +    /**
 17.2336 +     * Returns a BigInteger whose value is equivalent to this BigInteger
 17.2337 +     * with the designated bit flipped.
 17.2338 +     * (Computes {@code (this ^ (1<<n))}.)
 17.2339 +     *
 17.2340 +     * @param  n index of bit to flip.
 17.2341 +     * @return {@code this ^ (1<<n)}
 17.2342 +     * @throws ArithmeticException {@code n} is negative.
 17.2343 +     */
 17.2344 +    public BigInteger flipBit(int n) {
 17.2345 +        if (n<0)
 17.2346 +            throw new ArithmeticException("Negative bit address");
 17.2347 +
 17.2348 +        int intNum = n >>> 5;
 17.2349 +        int[] result = new int[Math.max(intLength(), intNum+2)];
 17.2350 +
 17.2351 +        for (int i=0; i<result.length; i++)
 17.2352 +            result[result.length-i-1] = getInt(i);
 17.2353 +
 17.2354 +        result[result.length-intNum-1] ^= (1 << (n & 31));
 17.2355 +
 17.2356 +        return valueOf(result);
 17.2357 +    }
 17.2358 +
 17.2359 +    /**
 17.2360 +     * Returns the index of the rightmost (lowest-order) one bit in this
 17.2361 +     * BigInteger (the number of zero bits to the right of the rightmost
 17.2362 +     * one bit).  Returns -1 if this BigInteger contains no one bits.
 17.2363 +     * (Computes {@code (this==0? -1 : log2(this & -this))}.)
 17.2364 +     *
 17.2365 +     * @return index of the rightmost one bit in this BigInteger.
 17.2366 +     */
 17.2367 +    public int getLowestSetBit() {
 17.2368 +        @SuppressWarnings("deprecation") int lsb = lowestSetBit - 2;
 17.2369 +        if (lsb == -2) {  // lowestSetBit not initialized yet
 17.2370 +            lsb = 0;
 17.2371 +            if (signum == 0) {
 17.2372 +                lsb -= 1;
 17.2373 +            } else {
 17.2374 +                // Search for lowest order nonzero int
 17.2375 +                int i,b;
 17.2376 +                for (i=0; (b = getInt(i))==0; i++)
 17.2377 +                    ;
 17.2378 +                lsb += (i << 5) + Integer.numberOfTrailingZeros(b);
 17.2379 +            }
 17.2380 +            lowestSetBit = lsb + 2;
 17.2381 +        }
 17.2382 +        return lsb;
 17.2383 +    }
 17.2384 +
 17.2385 +
 17.2386 +    // Miscellaneous Bit Operations
 17.2387 +
 17.2388 +    /**
 17.2389 +     * Returns the number of bits in the minimal two's-complement
 17.2390 +     * representation of this BigInteger, <i>excluding</i> a sign bit.
 17.2391 +     * For positive BigIntegers, this is equivalent to the number of bits in
 17.2392 +     * the ordinary binary representation.  (Computes
 17.2393 +     * {@code (ceil(log2(this < 0 ? -this : this+1)))}.)
 17.2394 +     *
 17.2395 +     * @return number of bits in the minimal two's-complement
 17.2396 +     *         representation of this BigInteger, <i>excluding</i> a sign bit.
 17.2397 +     */
 17.2398 +    public int bitLength() {
 17.2399 +        @SuppressWarnings("deprecation") int n = bitLength - 1;
 17.2400 +        if (n == -1) { // bitLength not initialized yet
 17.2401 +            int[] m = mag;
 17.2402 +            int len = m.length;
 17.2403 +            if (len == 0) {
 17.2404 +                n = 0; // offset by one to initialize
 17.2405 +            }  else {
 17.2406 +                // Calculate the bit length of the magnitude
 17.2407 +                int magBitLength = ((len - 1) << 5) + bitLengthForInt(mag[0]);
 17.2408 +                 if (signum < 0) {
 17.2409 +                     // Check if magnitude is a power of two
 17.2410 +                     boolean pow2 = (Integer.bitCount(mag[0]) == 1);
 17.2411 +                     for(int i=1; i< len && pow2; i++)
 17.2412 +                         pow2 = (mag[i] == 0);
 17.2413 +
 17.2414 +                     n = (pow2 ? magBitLength -1 : magBitLength);
 17.2415 +                 } else {
 17.2416 +                     n = magBitLength;
 17.2417 +                 }
 17.2418 +            }
 17.2419 +            bitLength = n + 1;
 17.2420 +        }
 17.2421 +        return n;
 17.2422 +    }
 17.2423 +
 17.2424 +    /**
 17.2425 +     * Returns the number of bits in the two's complement representation
 17.2426 +     * of this BigInteger that differ from its sign bit.  This method is
 17.2427 +     * useful when implementing bit-vector style sets atop BigIntegers.
 17.2428 +     *
 17.2429 +     * @return number of bits in the two's complement representation
 17.2430 +     *         of this BigInteger that differ from its sign bit.
 17.2431 +     */
 17.2432 +    public int bitCount() {
 17.2433 +        @SuppressWarnings("deprecation") int bc = bitCount - 1;
 17.2434 +        if (bc == -1) {  // bitCount not initialized yet
 17.2435 +            bc = 0;      // offset by one to initialize
 17.2436 +            // Count the bits in the magnitude
 17.2437 +            for (int i=0; i<mag.length; i++)
 17.2438 +                bc += Integer.bitCount(mag[i]);
 17.2439 +            if (signum < 0) {
 17.2440 +                // Count the trailing zeros in the magnitude
 17.2441 +                int magTrailingZeroCount = 0, j;
 17.2442 +                for (j=mag.length-1; mag[j]==0; j--)
 17.2443 +                    magTrailingZeroCount += 32;
 17.2444 +                magTrailingZeroCount += Integer.numberOfTrailingZeros(mag[j]);
 17.2445 +                bc += magTrailingZeroCount - 1;
 17.2446 +            }
 17.2447 +            bitCount = bc + 1;
 17.2448 +        }
 17.2449 +        return bc;
 17.2450 +    }
 17.2451 +
 17.2452 +    // Primality Testing
 17.2453 +
 17.2454 +    /**
 17.2455 +     * Returns {@code true} if this BigInteger is probably prime,
 17.2456 +     * {@code false} if it's definitely composite.  If
 17.2457 +     * {@code certainty} is &le; 0, {@code true} is
 17.2458 +     * returned.
 17.2459 +     *
 17.2460 +     * @param  certainty a measure of the uncertainty that the caller is
 17.2461 +     *         willing to tolerate: if the call returns {@code true}
 17.2462 +     *         the probability that this BigInteger is prime exceeds
 17.2463 +     *         (1 - 1/2<sup>{@code certainty}</sup>).  The execution time of
 17.2464 +     *         this method is proportional to the value of this parameter.
 17.2465 +     * @return {@code true} if this BigInteger is probably prime,
 17.2466 +     *         {@code false} if it's definitely composite.
 17.2467 +     */
 17.2468 +    public boolean isProbablePrime(int certainty) {
 17.2469 +        if (certainty <= 0)
 17.2470 +            return true;
 17.2471 +        BigInteger w = this.abs();
 17.2472 +        if (w.equals(TWO))
 17.2473 +            return true;
 17.2474 +        if (!w.testBit(0) || w.equals(ONE))
 17.2475 +            return false;
 17.2476 +
 17.2477 +        return w.primeToCertainty(certainty, null);
 17.2478 +    }
 17.2479 +
 17.2480 +    // Comparison Operations
 17.2481 +
 17.2482 +    /**
 17.2483 +     * Compares this BigInteger with the specified BigInteger.  This
 17.2484 +     * method is provided in preference to individual methods for each
 17.2485 +     * of the six boolean comparison operators ({@literal <}, ==,
 17.2486 +     * {@literal >}, {@literal >=}, !=, {@literal <=}).  The suggested
 17.2487 +     * idiom for performing these comparisons is: {@code
 17.2488 +     * (x.compareTo(y)} &lt;<i>op</i>&gt; {@code 0)}, where
 17.2489 +     * &lt;<i>op</i>&gt; is one of the six comparison operators.
 17.2490 +     *
 17.2491 +     * @param  val BigInteger to which this BigInteger is to be compared.
 17.2492 +     * @return -1, 0 or 1 as this BigInteger is numerically less than, equal
 17.2493 +     *         to, or greater than {@code val}.
 17.2494 +     */
 17.2495 +    public int compareTo(BigInteger val) {
 17.2496 +        if (signum == val.signum) {
 17.2497 +            switch (signum) {
 17.2498 +            case 1:
 17.2499 +                return compareMagnitude(val);
 17.2500 +            case -1:
 17.2501 +                return val.compareMagnitude(this);
 17.2502 +            default:
 17.2503 +                return 0;
 17.2504 +            }
 17.2505 +        }
 17.2506 +        return signum > val.signum ? 1 : -1;
 17.2507 +    }
 17.2508 +
 17.2509 +    /**
 17.2510 +     * Compares the magnitude array of this BigInteger with the specified
 17.2511 +     * BigInteger's. This is the version of compareTo ignoring sign.
 17.2512 +     *
 17.2513 +     * @param val BigInteger whose magnitude array to be compared.
 17.2514 +     * @return -1, 0 or 1 as this magnitude array is less than, equal to or
 17.2515 +     *         greater than the magnitude aray for the specified BigInteger's.
 17.2516 +     */
 17.2517 +    final int compareMagnitude(BigInteger val) {
 17.2518 +        int[] m1 = mag;
 17.2519 +        int len1 = m1.length;
 17.2520 +        int[] m2 = val.mag;
 17.2521 +        int len2 = m2.length;
 17.2522 +        if (len1 < len2)
 17.2523 +            return -1;
 17.2524 +        if (len1 > len2)
 17.2525 +            return 1;
 17.2526 +        for (int i = 0; i < len1; i++) {
 17.2527 +            int a = m1[i];
 17.2528 +            int b = m2[i];
 17.2529 +            if (a != b)
 17.2530 +                return ((a & LONG_MASK) < (b & LONG_MASK)) ? -1 : 1;
 17.2531 +        }
 17.2532 +        return 0;
 17.2533 +    }
 17.2534 +
 17.2535 +    /**
 17.2536 +     * Compares this BigInteger with the specified Object for equality.
 17.2537 +     *
 17.2538 +     * @param  x Object to which this BigInteger is to be compared.
 17.2539 +     * @return {@code true} if and only if the specified Object is a
 17.2540 +     *         BigInteger whose value is numerically equal to this BigInteger.
 17.2541 +     */
 17.2542 +    public boolean equals(Object x) {
 17.2543 +        // This test is just an optimization, which may or may not help
 17.2544 +        if (x == this)
 17.2545 +            return true;
 17.2546 +
 17.2547 +        if (!(x instanceof BigInteger))
 17.2548 +            return false;
 17.2549 +
 17.2550 +        BigInteger xInt = (BigInteger) x;
 17.2551 +        if (xInt.signum != signum)
 17.2552 +            return false;
 17.2553 +
 17.2554 +        int[] m = mag;
 17.2555 +        int len = m.length;
 17.2556 +        int[] xm = xInt.mag;
 17.2557 +        if (len != xm.length)
 17.2558 +            return false;
 17.2559 +
 17.2560 +        for (int i = 0; i < len; i++)
 17.2561 +            if (xm[i] != m[i])
 17.2562 +                return false;
 17.2563 +
 17.2564 +        return true;
 17.2565 +    }
 17.2566 +
 17.2567 +    /**
 17.2568 +     * Returns the minimum of this BigInteger and {@code val}.
 17.2569 +     *
 17.2570 +     * @param  val value with which the minimum is to be computed.
 17.2571 +     * @return the BigInteger whose value is the lesser of this BigInteger and
 17.2572 +     *         {@code val}.  If they are equal, either may be returned.
 17.2573 +     */
 17.2574 +    public BigInteger min(BigInteger val) {
 17.2575 +        return (compareTo(val)<0 ? this : val);
 17.2576 +    }
 17.2577 +
 17.2578 +    /**
 17.2579 +     * Returns the maximum of this BigInteger and {@code val}.
 17.2580 +     *
 17.2581 +     * @param  val value with which the maximum is to be computed.
 17.2582 +     * @return the BigInteger whose value is the greater of this and
 17.2583 +     *         {@code val}.  If they are equal, either may be returned.
 17.2584 +     */
 17.2585 +    public BigInteger max(BigInteger val) {
 17.2586 +        return (compareTo(val)>0 ? this : val);
 17.2587 +    }
 17.2588 +
 17.2589 +
 17.2590 +    // Hash Function
 17.2591 +
 17.2592 +    /**
 17.2593 +     * Returns the hash code for this BigInteger.
 17.2594 +     *
 17.2595 +     * @return hash code for this BigInteger.
 17.2596 +     */
 17.2597 +    public int hashCode() {
 17.2598 +        int hashCode = 0;
 17.2599 +
 17.2600 +        for (int i=0; i<mag.length; i++)
 17.2601 +            hashCode = (int)(31*hashCode + (mag[i] & LONG_MASK));
 17.2602 +
 17.2603 +        return hashCode * signum;
 17.2604 +    }
 17.2605 +
 17.2606 +    /**
 17.2607 +     * Returns the String representation of this BigInteger in the
 17.2608 +     * given radix.  If the radix is outside the range from {@link
 17.2609 +     * Character#MIN_RADIX} to {@link Character#MAX_RADIX} inclusive,
 17.2610 +     * it will default to 10 (as is the case for
 17.2611 +     * {@code Integer.toString}).  The digit-to-character mapping
 17.2612 +     * provided by {@code Character.forDigit} is used, and a minus
 17.2613 +     * sign is prepended if appropriate.  (This representation is
 17.2614 +     * compatible with the {@link #BigInteger(String, int) (String,
 17.2615 +     * int)} constructor.)
 17.2616 +     *
 17.2617 +     * @param  radix  radix of the String representation.
 17.2618 +     * @return String representation of this BigInteger in the given radix.
 17.2619 +     * @see    Integer#toString
 17.2620 +     * @see    Character#forDigit
 17.2621 +     * @see    #BigInteger(java.lang.String, int)
 17.2622 +     */
 17.2623 +    public String toString(int radix) {
 17.2624 +        if (signum == 0)
 17.2625 +            return "0";
 17.2626 +        if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
 17.2627 +            radix = 10;
 17.2628 +
 17.2629 +        // Compute upper bound on number of digit groups and allocate space
 17.2630 +        int maxNumDigitGroups = (4*mag.length + 6)/7;
 17.2631 +        String digitGroup[] = new String[maxNumDigitGroups];
 17.2632 +
 17.2633 +        // Translate number to string, a digit group at a time
 17.2634 +        BigInteger tmp = this.abs();
 17.2635 +        int numGroups = 0;
 17.2636 +        while (tmp.signum != 0) {
 17.2637 +            BigInteger d = longRadix[radix];
 17.2638 +
 17.2639 +            MutableBigInteger q = new MutableBigInteger(),
 17.2640 +                              a = new MutableBigInteger(tmp.mag),
 17.2641 +                              b = new MutableBigInteger(d.mag);
 17.2642 +            MutableBigInteger r = a.divide(b, q);
 17.2643 +            BigInteger q2 = q.toBigInteger(tmp.signum * d.signum);
 17.2644 +            BigInteger r2 = r.toBigInteger(tmp.signum * d.signum);
 17.2645 +
 17.2646 +            digitGroup[numGroups++] = Long.toString(r2.longValue(), radix);
 17.2647 +            tmp = q2;
 17.2648 +        }
 17.2649 +
 17.2650 +        // Put sign (if any) and first digit group into result buffer
 17.2651 +        StringBuilder buf = new StringBuilder(numGroups*digitsPerLong[radix]+1);
 17.2652 +        if (signum<0)
 17.2653 +            buf.append('-');
 17.2654 +        buf.append(digitGroup[numGroups-1]);
 17.2655 +
 17.2656 +        // Append remaining digit groups padded with leading zeros
 17.2657 +        for (int i=numGroups-2; i>=0; i--) {
 17.2658 +            // Prepend (any) leading zeros for this digit group
 17.2659 +            int numLeadingZeros = digitsPerLong[radix]-digitGroup[i].length();
 17.2660 +            if (numLeadingZeros != 0)
 17.2661 +                buf.append(zeros[numLeadingZeros]);
 17.2662 +            buf.append(digitGroup[i]);
 17.2663 +        }
 17.2664 +        return buf.toString();
 17.2665 +    }
 17.2666 +
 17.2667 +    /* zero[i] is a string of i consecutive zeros. */
 17.2668 +    private static String zeros[] = new String[64];
 17.2669 +    static {
 17.2670 +        zeros[63] =
 17.2671 +            "000000000000000000000000000000000000000000000000000000000000000";
 17.2672 +        for (int i=0; i<63; i++)
 17.2673 +            zeros[i] = zeros[63].substring(0, i);
 17.2674 +    }
 17.2675 +
 17.2676 +    /**
 17.2677 +     * Returns the decimal String representation of this BigInteger.
 17.2678 +     * The digit-to-character mapping provided by
 17.2679 +     * {@code Character.forDigit} is used, and a minus sign is
 17.2680 +     * prepended if appropriate.  (This representation is compatible
 17.2681 +     * with the {@link #BigInteger(String) (String)} constructor, and
 17.2682 +     * allows for String concatenation with Java's + operator.)
 17.2683 +     *
 17.2684 +     * @return decimal String representation of this BigInteger.
 17.2685 +     * @see    Character#forDigit
 17.2686 +     * @see    #BigInteger(java.lang.String)
 17.2687 +     */
 17.2688 +    public String toString() {
 17.2689 +        return toString(10);
 17.2690 +    }
 17.2691 +
 17.2692 +    /**
 17.2693 +     * Returns a byte array containing the two's-complement
 17.2694 +     * representation of this BigInteger.  The byte array will be in
 17.2695 +     * <i>big-endian</i> byte-order: the most significant byte is in
 17.2696 +     * the zeroth element.  The array will contain the minimum number
 17.2697 +     * of bytes required to represent this BigInteger, including at
 17.2698 +     * least one sign bit, which is {@code (ceil((this.bitLength() +
 17.2699 +     * 1)/8))}.  (This representation is compatible with the
 17.2700 +     * {@link #BigInteger(byte[]) (byte[])} constructor.)
 17.2701 +     *
 17.2702 +     * @return a byte array containing the two's-complement representation of
 17.2703 +     *         this BigInteger.
 17.2704 +     * @see    #BigInteger(byte[])
 17.2705 +     */
 17.2706 +    public byte[] toByteArray() {
 17.2707 +        int byteLen = bitLength()/8 + 1;
 17.2708 +        byte[] byteArray = new byte[byteLen];
 17.2709 +
 17.2710 +        for (int i=byteLen-1, bytesCopied=4, nextInt=0, intIndex=0; i>=0; i--) {
 17.2711 +            if (bytesCopied == 4) {
 17.2712 +                nextInt = getInt(intIndex++);
 17.2713 +                bytesCopied = 1;
 17.2714 +            } else {
 17.2715 +                nextInt >>>= 8;
 17.2716 +                bytesCopied++;
 17.2717 +            }
 17.2718 +            byteArray[i] = (byte)nextInt;
 17.2719 +        }
 17.2720 +        return byteArray;
 17.2721 +    }
 17.2722 +
 17.2723 +    /**
 17.2724 +     * Converts this BigInteger to an {@code int}.  This
 17.2725 +     * conversion is analogous to a
 17.2726 +     * <i>narrowing primitive conversion</i> from {@code long} to
 17.2727 +     * {@code int} as defined in section 5.1.3 of
 17.2728 +     * <cite>The Java&trade; Language Specification</cite>:
 17.2729 +     * if this BigInteger is too big to fit in an
 17.2730 +     * {@code int}, only the low-order 32 bits are returned.
 17.2731 +     * Note that this conversion can lose information about the
 17.2732 +     * overall magnitude of the BigInteger value as well as return a
 17.2733 +     * result with the opposite sign.
 17.2734 +     *
 17.2735 +     * @return this BigInteger converted to an {@code int}.
 17.2736 +     */
 17.2737 +    public int intValue() {
 17.2738 +        int result = 0;
 17.2739 +        result = getInt(0);
 17.2740 +        return result;
 17.2741 +    }
 17.2742 +
 17.2743 +    /**
 17.2744 +     * Converts this BigInteger to a {@code long}.  This
 17.2745 +     * conversion is analogous to a
 17.2746 +     * <i>narrowing primitive conversion</i> from {@code long} to
 17.2747 +     * {@code int} as defined in section 5.1.3 of
 17.2748 +     * <cite>The Java&trade; Language Specification</cite>:
 17.2749 +     * if this BigInteger is too big to fit in a
 17.2750 +     * {@code long}, only the low-order 64 bits are returned.
 17.2751 +     * Note that this conversion can lose information about the
 17.2752 +     * overall magnitude of the BigInteger value as well as return a
 17.2753 +     * result with the opposite sign.
 17.2754 +     *
 17.2755 +     * @return this BigInteger converted to a {@code long}.
 17.2756 +     */
 17.2757 +    public long longValue() {
 17.2758 +        long result = 0;
 17.2759 +
 17.2760 +        for (int i=1; i>=0; i--)
 17.2761 +            result = (result << 32) + (getInt(i) & LONG_MASK);
 17.2762 +        return result;
 17.2763 +    }
 17.2764 +
 17.2765 +    /**
 17.2766 +     * Converts this BigInteger to a {@code float}.  This
 17.2767 +     * conversion is similar to the
 17.2768 +     * <i>narrowing primitive conversion</i> from {@code double} to
 17.2769 +     * {@code float} as defined in section 5.1.3 of
 17.2770 +     * <cite>The Java&trade; Language Specification</cite>:
 17.2771 +     * if this BigInteger has too great a magnitude
 17.2772 +     * to represent as a {@code float}, it will be converted to
 17.2773 +     * {@link Float#NEGATIVE_INFINITY} or {@link
 17.2774 +     * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 17.2775 +     * the return value is finite, this conversion can lose
 17.2776 +     * information about the precision of the BigInteger value.
 17.2777 +     *
 17.2778 +     * @return this BigInteger converted to a {@code float}.
 17.2779 +     */
 17.2780 +    public float floatValue() {
 17.2781 +        // Somewhat inefficient, but guaranteed to work.
 17.2782 +        return Float.parseFloat(this.toString());
 17.2783 +    }
 17.2784 +
 17.2785 +    /**
 17.2786 +     * Converts this BigInteger to a {@code double}.  This
 17.2787 +     * conversion is similar to the
 17.2788 +     * <i>narrowing primitive conversion</i> from {@code double} to
 17.2789 +     * {@code float} as defined in section 5.1.3 of
 17.2790 +     * <cite>The Java&trade; Language Specification</cite>:
 17.2791 +     * if this BigInteger has too great a magnitude
 17.2792 +     * to represent as a {@code double}, it will be converted to
 17.2793 +     * {@link Double#NEGATIVE_INFINITY} or {@link
 17.2794 +     * Double#POSITIVE_INFINITY} as appropriate.  Note that even when
 17.2795 +     * the return value is finite, this conversion can lose
 17.2796 +     * information about the precision of the BigInteger value.
 17.2797 +     *
 17.2798 +     * @return this BigInteger converted to a {@code double}.
 17.2799 +     */
 17.2800 +    public double doubleValue() {
 17.2801 +        // Somewhat inefficient, but guaranteed to work.
 17.2802 +        return Double.parseDouble(this.toString());
 17.2803 +    }
 17.2804 +
 17.2805 +    /**
 17.2806 +     * Returns a copy of the input array stripped of any leading zero bytes.
 17.2807 +     */
 17.2808 +    private static int[] stripLeadingZeroInts(int val[]) {
 17.2809 +        int vlen = val.length;
 17.2810 +        int keep;
 17.2811 +
 17.2812 +        // Find first nonzero byte
 17.2813 +        for (keep = 0; keep < vlen && val[keep] == 0; keep++)
 17.2814 +            ;
 17.2815 +        return java.util.Arrays.copyOfRange(val, keep, vlen);
 17.2816 +    }
 17.2817 +
 17.2818 +    /**
 17.2819 +     * Returns the input array stripped of any leading zero bytes.
 17.2820 +     * Since the source is trusted the copying may be skipped.
 17.2821 +     */
 17.2822 +    private static int[] trustedStripLeadingZeroInts(int val[]) {
 17.2823 +        int vlen = val.length;
 17.2824 +        int keep;
 17.2825 +
 17.2826 +        // Find first nonzero byte
 17.2827 +        for (keep = 0; keep < vlen && val[keep] == 0; keep++)
 17.2828 +            ;
 17.2829 +        return keep == 0 ? val : java.util.Arrays.copyOfRange(val, keep, vlen);
 17.2830 +    }
 17.2831 +
 17.2832 +    /**
 17.2833 +     * Returns a copy of the input array stripped of any leading zero bytes.
 17.2834 +     */
 17.2835 +    private static int[] stripLeadingZeroBytes(byte a[]) {
 17.2836 +        int byteLength = a.length;
 17.2837 +        int keep;
 17.2838 +
 17.2839 +        // Find first nonzero byte
 17.2840 +        for (keep = 0; keep < byteLength && a[keep]==0; keep++)
 17.2841 +            ;
 17.2842 +
 17.2843 +        // Allocate new array and copy relevant part of input array
 17.2844 +        int intLength = ((byteLength - keep) + 3) >>> 2;
 17.2845 +        int[] result = new int[intLength];
 17.2846 +        int b = byteLength - 1;
 17.2847 +        for (int i = intLength-1; i >= 0; i--) {
 17.2848 +            result[i] = a[b--] & 0xff;
 17.2849 +            int bytesRemaining = b - keep + 1;
 17.2850 +            int bytesToTransfer = Math.min(3, bytesRemaining);
 17.2851 +            for (int j=8; j <= (bytesToTransfer << 3); j += 8)
 17.2852 +                result[i] |= ((a[b--] & 0xff) << j);
 17.2853 +        }
 17.2854 +        return result;
 17.2855 +    }
 17.2856 +
 17.2857 +    /**
 17.2858 +     * Takes an array a representing a negative 2's-complement number and
 17.2859 +     * returns the minimal (no leading zero bytes) unsigned whose value is -a.
 17.2860 +     */
 17.2861 +    private static int[] makePositive(byte a[]) {
 17.2862 +        int keep, k;
 17.2863 +        int byteLength = a.length;
 17.2864 +
 17.2865 +        // Find first non-sign (0xff) byte of input
 17.2866 +        for (keep=0; keep<byteLength && a[keep]==-1; keep++)
 17.2867 +            ;
 17.2868 +
 17.2869 +
 17.2870 +        /* Allocate output array.  If all non-sign bytes are 0x00, we must
 17.2871 +         * allocate space for one extra output byte. */
 17.2872 +        for (k=keep; k<byteLength && a[k]==0; k++)
 17.2873 +            ;
 17.2874 +
 17.2875 +        int extraByte = (k==byteLength) ? 1 : 0;
 17.2876 +        int intLength = ((byteLength - keep + extraByte) + 3)/4;
 17.2877 +        int result[] = new int[intLength];
 17.2878 +
 17.2879 +        /* Copy one's complement of input into output, leaving extra
 17.2880 +         * byte (if it exists) == 0x00 */
 17.2881 +        int b = byteLength - 1;
 17.2882 +        for (int i = intLength-1; i >= 0; i--) {
 17.2883 +            result[i] = a[b--] & 0xff;
 17.2884 +            int numBytesToTransfer = Math.min(3, b-keep+1);
 17.2885 +            if (numBytesToTransfer < 0)
 17.2886 +                numBytesToTransfer = 0;
 17.2887 +            for (int j=8; j <= 8*numBytesToTransfer; j += 8)
 17.2888 +                result[i] |= ((a[b--] & 0xff) << j);
 17.2889 +
 17.2890 +            // Mask indicates which bits must be complemented
 17.2891 +            int mask = -1 >>> (8*(3-numBytesToTransfer));
 17.2892 +            result[i] = ~result[i] & mask;
 17.2893 +        }
 17.2894 +
 17.2895 +        // Add one to one's complement to generate two's complement
 17.2896 +        for (int i=result.length-1; i>=0; i--) {
 17.2897 +            result[i] = (int)((result[i] & LONG_MASK) + 1);
 17.2898 +            if (result[i] != 0)
 17.2899 +                break;
 17.2900 +        }
 17.2901 +
 17.2902 +        return result;
 17.2903 +    }
 17.2904 +
 17.2905 +    /**
 17.2906 +     * Takes an array a representing a negative 2's-complement number and
 17.2907 +     * returns the minimal (no leading zero ints) unsigned whose value is -a.
 17.2908 +     */
 17.2909 +    private static int[] makePositive(int a[]) {
 17.2910 +        int keep, j;
 17.2911 +
 17.2912 +        // Find first non-sign (0xffffffff) int of input
 17.2913 +        for (keep=0; keep<a.length && a[keep]==-1; keep++)
 17.2914 +            ;
 17.2915 +
 17.2916 +        /* Allocate output array.  If all non-sign ints are 0x00, we must
 17.2917 +         * allocate space for one extra output int. */
 17.2918 +        for (j=keep; j<a.length && a[j]==0; j++)
 17.2919 +            ;
 17.2920 +        int extraInt = (j==a.length ? 1 : 0);
 17.2921 +        int result[] = new int[a.length - keep + extraInt];
 17.2922 +
 17.2923 +        /* Copy one's complement of input into output, leaving extra
 17.2924 +         * int (if it exists) == 0x00 */
 17.2925 +        for (int i = keep; i<a.length; i++)
 17.2926 +            result[i - keep + extraInt] = ~a[i];
 17.2927 +
 17.2928 +        // Add one to one's complement to generate two's complement
 17.2929 +        for (int i=result.length-1; ++result[i]==0; i--)
 17.2930 +            ;
 17.2931 +
 17.2932 +        return result;
 17.2933 +    }
 17.2934 +
 17.2935 +    /*
 17.2936 +     * The following two arrays are used for fast String conversions.  Both
 17.2937 +     * are indexed by radix.  The first is the number of digits of the given
 17.2938 +     * radix that can fit in a Java long without "going negative", i.e., the
 17.2939 +     * highest integer n such that radix**n < 2**63.  The second is the
 17.2940 +     * "long radix" that tears each number into "long digits", each of which
 17.2941 +     * consists of the number of digits in the corresponding element in
 17.2942 +     * digitsPerLong (longRadix[i] = i**digitPerLong[i]).  Both arrays have
 17.2943 +     * nonsense values in their 0 and 1 elements, as radixes 0 and 1 are not
 17.2944 +     * used.
 17.2945 +     */
 17.2946 +    private static int digitsPerLong[] = {0, 0,
 17.2947 +        62, 39, 31, 27, 24, 22, 20, 19, 18, 18, 17, 17, 16, 16, 15, 15, 15, 14,
 17.2948 +        14, 14, 14, 13, 13, 13, 13, 13, 13, 12, 12, 12, 12, 12, 12, 12, 12};
 17.2949 +
 17.2950 +    private static BigInteger longRadix[] = {null, null,
 17.2951 +        valueOf(0x4000000000000000L), valueOf(0x383d9170b85ff80bL),
 17.2952 +        valueOf(0x4000000000000000L), valueOf(0x6765c793fa10079dL),
 17.2953 +        valueOf(0x41c21cb8e1000000L), valueOf(0x3642798750226111L),
 17.2954 +        valueOf(0x1000000000000000L), valueOf(0x12bf307ae81ffd59L),
 17.2955 +        valueOf( 0xde0b6b3a7640000L), valueOf(0x4d28cb56c33fa539L),
 17.2956 +        valueOf(0x1eca170c00000000L), valueOf(0x780c7372621bd74dL),
 17.2957 +        valueOf(0x1e39a5057d810000L), valueOf(0x5b27ac993df97701L),
 17.2958 +        valueOf(0x1000000000000000L), valueOf(0x27b95e997e21d9f1L),
 17.2959 +        valueOf(0x5da0e1e53c5c8000L), valueOf( 0xb16a458ef403f19L),
 17.2960 +        valueOf(0x16bcc41e90000000L), valueOf(0x2d04b7fdd9c0ef49L),
 17.2961 +        valueOf(0x5658597bcaa24000L), valueOf( 0x6feb266931a75b7L),
 17.2962 +        valueOf( 0xc29e98000000000L), valueOf(0x14adf4b7320334b9L),
 17.2963 +        valueOf(0x226ed36478bfa000L), valueOf(0x383d9170b85ff80bL),
 17.2964 +        valueOf(0x5a3c23e39c000000L), valueOf( 0x4e900abb53e6b71L),
 17.2965 +        valueOf( 0x7600ec618141000L), valueOf( 0xaee5720ee830681L),
 17.2966 +        valueOf(0x1000000000000000L), valueOf(0x172588ad4f5f0981L),
 17.2967 +        valueOf(0x211e44f7d02c1000L), valueOf(0x2ee56725f06e5c71L),
 17.2968 +        valueOf(0x41c21cb8e1000000L)};
 17.2969 +
 17.2970 +    /*
 17.2971 +     * These two arrays are the integer analogue of above.
 17.2972 +     */
 17.2973 +    private static int digitsPerInt[] = {0, 0, 30, 19, 15, 13, 11,
 17.2974 +        11, 10, 9, 9, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6,
 17.2975 +        6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5};
 17.2976 +
 17.2977 +    private static int intRadix[] = {0, 0,
 17.2978 +        0x40000000, 0x4546b3db, 0x40000000, 0x48c27395, 0x159fd800,
 17.2979 +        0x75db9c97, 0x40000000, 0x17179149, 0x3b9aca00, 0xcc6db61,
 17.2980 +        0x19a10000, 0x309f1021, 0x57f6c100, 0xa2f1b6f,  0x10000000,
 17.2981 +        0x18754571, 0x247dbc80, 0x3547667b, 0x4c4b4000, 0x6b5a6e1d,
 17.2982 +        0x6c20a40,  0x8d2d931,  0xb640000,  0xe8d4a51,  0x1269ae40,
 17.2983 +        0x17179149, 0x1cb91000, 0x23744899, 0x2b73a840, 0x34e63b41,
 17.2984 +        0x40000000, 0x4cfa3cc1, 0x5c13d840, 0x6d91b519, 0x39aa400
 17.2985 +    };
 17.2986 +
 17.2987 +    /**
 17.2988 +     * These routines provide access to the two's complement representation
 17.2989 +     * of BigIntegers.
 17.2990 +     */
 17.2991 +
 17.2992 +    /**
 17.2993 +     * Returns the length of the two's complement representation in ints,
 17.2994 +     * including space for at least one sign bit.
 17.2995 +     */
 17.2996 +    private int intLength() {
 17.2997 +        return (bitLength() >>> 5) + 1;
 17.2998 +    }
 17.2999 +
 17.3000 +    /* Returns sign bit */
 17.3001 +    private int signBit() {
 17.3002 +        return signum < 0 ? 1 : 0;
 17.3003 +    }
 17.3004 +
 17.3005 +    /* Returns an int of sign bits */
 17.3006 +    private int signInt() {
 17.3007 +        return signum < 0 ? -1 : 0;
 17.3008 +    }
 17.3009 +
 17.3010 +    /**
 17.3011 +     * Returns the specified int of the little-endian two's complement
 17.3012 +     * representation (int 0 is the least significant).  The int number can
 17.3013 +     * be arbitrarily high (values are logically preceded by infinitely many
 17.3014 +     * sign ints).
 17.3015 +     */
 17.3016 +    private int getInt(int n) {
 17.3017 +        if (n < 0)
 17.3018 +            return 0;
 17.3019 +        if (n >= mag.length)
 17.3020 +            return signInt();
 17.3021 +
 17.3022 +        int magInt = mag[mag.length-n-1];
 17.3023 +
 17.3024 +        return (signum >= 0 ? magInt :
 17.3025 +                (n <= firstNonzeroIntNum() ? -magInt : ~magInt));
 17.3026 +    }
 17.3027 +
 17.3028 +    /**
 17.3029 +     * Returns the index of the int that contains the first nonzero int in the
 17.3030 +     * little-endian binary representation of the magnitude (int 0 is the
 17.3031 +     * least significant). If the magnitude is zero, return value is undefined.
 17.3032 +     */
 17.3033 +     private int firstNonzeroIntNum() {
 17.3034 +         int fn = firstNonzeroIntNum - 2;
 17.3035 +         if (fn == -2) { // firstNonzeroIntNum not initialized yet
 17.3036 +             fn = 0;
 17.3037 +
 17.3038 +             // Search for the first nonzero int
 17.3039 +             int i;
 17.3040 +             int mlen = mag.length;
 17.3041 +             for (i = mlen - 1; i >= 0 && mag[i] == 0; i--)
 17.3042 +                 ;
 17.3043 +             fn = mlen - i - 1;
 17.3044 +             firstNonzeroIntNum = fn + 2; // offset by two to initialize
 17.3045 +         }
 17.3046 +         return fn;
 17.3047 +     }
 17.3048 +
 17.3049 +    /** use serialVersionUID from JDK 1.1. for interoperability */
 17.3050 +    private static final long serialVersionUID = -8287574255936472291L;
 17.3051 +
 17.3052 +    /**
 17.3053 +     * Serializable fields for BigInteger.
 17.3054 +     *
 17.3055 +     * @serialField signum  int
 17.3056 +     *              signum of this BigInteger.
 17.3057 +     * @serialField magnitude int[]
 17.3058 +     *              magnitude array of this BigInteger.
 17.3059 +     * @serialField bitCount  int
 17.3060 +     *              number of bits in this BigInteger
 17.3061 +     * @serialField bitLength int
 17.3062 +     *              the number of bits in the minimal two's-complement
 17.3063 +     *              representation of this BigInteger
 17.3064 +     * @serialField lowestSetBit int
 17.3065 +     *              lowest set bit in the twos complement representation
 17.3066 +     */
 17.3067 +    private static final ObjectStreamField[] serialPersistentFields = {
 17.3068 +        new ObjectStreamField("signum", Integer.TYPE),
 17.3069 +        new ObjectStreamField("magnitude", byte[].class),
 17.3070 +        new ObjectStreamField("bitCount", Integer.TYPE),
 17.3071 +        new ObjectStreamField("bitLength", Integer.TYPE),
 17.3072 +        new ObjectStreamField("firstNonzeroByteNum", Integer.TYPE),
 17.3073 +        new ObjectStreamField("lowestSetBit", Integer.TYPE)
 17.3074 +        };
 17.3075 +
 17.3076 +
 17.3077 +
 17.3078 +    /**
 17.3079 +     * Save the {@code BigInteger} instance to a stream.
 17.3080 +     * The magnitude of a BigInteger is serialized as a byte array for
 17.3081 +     * historical reasons.
 17.3082 +     *
 17.3083 +     * @serialData two necessary fields are written as well as obsolete
 17.3084 +     *             fields for compatibility with older versions.
 17.3085 +     */
 17.3086 +    private void writeObject(ObjectOutputStream s) throws IOException {
 17.3087 +        // set the values of the Serializable fields
 17.3088 +        ObjectOutputStream.PutField fields = s.putFields();
 17.3089 +        fields.put("signum", signum);
 17.3090 +        fields.put("magnitude", magSerializedForm());
 17.3091 +        // The values written for cached fields are compatible with older
 17.3092 +        // versions, but are ignored in readObject so don't otherwise matter.
 17.3093 +        fields.put("bitCount", -1);
 17.3094 +        fields.put("bitLength", -1);
 17.3095 +        fields.put("lowestSetBit", -2);
 17.3096 +        fields.put("firstNonzeroByteNum", -2);
 17.3097 +
 17.3098 +        // save them
 17.3099 +        s.writeFields();
 17.3100 +}
 17.3101 +
 17.3102 +    /**
 17.3103 +     * Returns the mag array as an array of bytes.
 17.3104 +     */
 17.3105 +    private byte[] magSerializedForm() {
 17.3106 +        int len = mag.length;
 17.3107 +
 17.3108 +        int bitLen = (len == 0 ? 0 : ((len - 1) << 5) + bitLengthForInt(mag[0]));
 17.3109 +        int byteLen = (bitLen + 7) >>> 3;
 17.3110 +        byte[] result = new byte[byteLen];
 17.3111 +
 17.3112 +        for (int i = byteLen - 1, bytesCopied = 4, intIndex = len - 1, nextInt = 0;
 17.3113 +             i>=0; i--) {
 17.3114 +            if (bytesCopied == 4) {
 17.3115 +                nextInt = mag[intIndex--];
 17.3116 +                bytesCopied = 1;
 17.3117 +            } else {
 17.3118 +                nextInt >>>= 8;
 17.3119 +                bytesCopied++;
 17.3120 +            }
 17.3121 +            result[i] = (byte)nextInt;
 17.3122 +        }
 17.3123 +        return result;
 17.3124 +    }
 17.3125 +}
    18.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.2 +++ b/rt/emul/compact/src/main/java/java/math/BitSieve.java	Sun Sep 08 11:22:51 2013 +0200
    18.3 @@ -0,0 +1,212 @@
    18.4 +/*
    18.5 + * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
    18.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    18.7 + *
    18.8 + * This code is free software; you can redistribute it and/or modify it
    18.9 + * under the terms of the GNU General Public License version 2 only, as
   18.10 + * published by the Free Software Foundation.  Oracle designates this
   18.11 + * particular file as subject to the "Classpath" exception as provided
   18.12 + * by Oracle in the LICENSE file that accompanied this code.
   18.13 + *
   18.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   18.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   18.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   18.17 + * version 2 for more details (a copy is included in the LICENSE file that
   18.18 + * accompanied this code).
   18.19 + *
   18.20 + * You should have received a copy of the GNU General Public License version
   18.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   18.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   18.23 + *
   18.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   18.25 + * or visit www.oracle.com if you need additional information or have any
   18.26 + * questions.
   18.27 + */
   18.28 +
   18.29 +package java.math;
   18.30 +
   18.31 +/**
   18.32 + * A simple bit sieve used for finding prime number candidates. Allows setting
   18.33 + * and clearing of bits in a storage array. The size of the sieve is assumed to
   18.34 + * be constant to reduce overhead. All the bits of a new bitSieve are zero, and
   18.35 + * bits are removed from it by setting them.
   18.36 + *
   18.37 + * To reduce storage space and increase efficiency, no even numbers are
   18.38 + * represented in the sieve (each bit in the sieve represents an odd number).
   18.39 + * The relationship between the index of a bit and the number it represents is
   18.40 + * given by
   18.41 + * N = offset + (2*index + 1);
   18.42 + * Where N is the integer represented by a bit in the sieve, offset is some
   18.43 + * even integer offset indicating where the sieve begins, and index is the
   18.44 + * index of a bit in the sieve array.
   18.45 + *
   18.46 + * @see     BigInteger
   18.47 + * @author  Michael McCloskey
   18.48 + * @since   1.3
   18.49 + */
   18.50 +class BitSieve {
   18.51 +    /**
   18.52 +     * Stores the bits in this bitSieve.
   18.53 +     */
   18.54 +    private long bits[];
   18.55 +
   18.56 +    /**
   18.57 +     * Length is how many bits this sieve holds.
   18.58 +     */
   18.59 +    private int length;
   18.60 +
   18.61 +    /**
   18.62 +     * A small sieve used to filter out multiples of small primes in a search
   18.63 +     * sieve.
   18.64 +     */
   18.65 +    private static BitSieve smallSieve = new BitSieve();
   18.66 +
   18.67 +    /**
   18.68 +     * Construct a "small sieve" with a base of 0.  This constructor is
   18.69 +     * used internally to generate the set of "small primes" whose multiples
   18.70 +     * are excluded from sieves generated by the main (package private)
   18.71 +     * constructor, BitSieve(BigInteger base, int searchLen).  The length
   18.72 +     * of the sieve generated by this constructor was chosen for performance;
   18.73 +     * it controls a tradeoff between how much time is spent constructing
   18.74 +     * other sieves, and how much time is wasted testing composite candidates
   18.75 +     * for primality.  The length was chosen experimentally to yield good
   18.76 +     * performance.
   18.77 +     */
   18.78 +    private BitSieve() {
   18.79 +        length = 150 * 64;
   18.80 +        bits = new long[(unitIndex(length - 1) + 1)];
   18.81 +
   18.82 +        // Mark 1 as composite
   18.83 +        set(0);
   18.84 +        int nextIndex = 1;
   18.85 +        int nextPrime = 3;
   18.86 +
   18.87 +        // Find primes and remove their multiples from sieve
   18.88 +        do {
   18.89 +            sieveSingle(length, nextIndex + nextPrime, nextPrime);
   18.90 +            nextIndex = sieveSearch(length, nextIndex + 1);
   18.91 +            nextPrime = 2*nextIndex + 1;
   18.92 +        } while((nextIndex > 0) && (nextPrime < length));
   18.93 +    }
   18.94 +
   18.95 +    /**
   18.96 +     * Construct a bit sieve of searchLen bits used for finding prime number
   18.97 +     * candidates. The new sieve begins at the specified base, which must
   18.98 +     * be even.
   18.99 +     */
  18.100 +    BitSieve(BigInteger base, int searchLen) {
  18.101 +        /*
  18.102 +         * Candidates are indicated by clear bits in the sieve. As a candidates
  18.103 +         * nonprimality is calculated, a bit is set in the sieve to eliminate
  18.104 +         * it. To reduce storage space and increase efficiency, no even numbers
  18.105 +         * are represented in the sieve (each bit in the sieve represents an
  18.106 +         * odd number).
  18.107 +         */
  18.108 +        bits = new long[(unitIndex(searchLen-1) + 1)];
  18.109 +        length = searchLen;
  18.110 +        int start = 0;
  18.111 +
  18.112 +        int step = smallSieve.sieveSearch(smallSieve.length, start);
  18.113 +        int convertedStep = (step *2) + 1;
  18.114 +
  18.115 +        // Construct the large sieve at an even offset specified by base
  18.116 +        MutableBigInteger b = new MutableBigInteger(base);
  18.117 +        MutableBigInteger q = new MutableBigInteger();
  18.118 +        do {
  18.119 +            // Calculate base mod convertedStep
  18.120 +            start = b.divideOneWord(convertedStep, q);
  18.121 +
  18.122 +            // Take each multiple of step out of sieve
  18.123 +            start = convertedStep - start;
  18.124 +            if (start%2 == 0)
  18.125 +                start += convertedStep;
  18.126 +            sieveSingle(searchLen, (start-1)/2, convertedStep);
  18.127 +
  18.128 +            // Find next prime from small sieve
  18.129 +            step = smallSieve.sieveSearch(smallSieve.length, step+1);
  18.130 +            convertedStep = (step *2) + 1;
  18.131 +        } while (step > 0);
  18.132 +    }
  18.133 +
  18.134 +    /**
  18.135 +     * Given a bit index return unit index containing it.
  18.136 +     */
  18.137 +    private static int unitIndex(int bitIndex) {
  18.138 +        return bitIndex >>> 6;
  18.139 +    }
  18.140 +
  18.141 +    /**
  18.142 +     * Return a unit that masks the specified bit in its unit.
  18.143 +     */
  18.144 +    private static long bit(int bitIndex) {
  18.145 +        return 1L << (bitIndex & ((1<<6) - 1));
  18.146 +    }
  18.147 +
  18.148 +    /**
  18.149 +     * Get the value of the bit at the specified index.
  18.150 +     */
  18.151 +    private boolean get(int bitIndex) {
  18.152 +        int unitIndex = unitIndex(bitIndex);
  18.153 +        return ((bits[unitIndex] & bit(bitIndex)) != 0);
  18.154 +    }
  18.155 +
  18.156 +    /**
  18.157 +     * Set the bit at the specified index.
  18.158 +     */
  18.159 +    private void set(int bitIndex) {
  18.160 +        int unitIndex = unitIndex(bitIndex);
  18.161 +        bits[unitIndex] |= bit(bitIndex);
  18.162 +    }
  18.163 +
  18.164 +    /**
  18.165 +     * This method returns the index of the first clear bit in the search
  18.166 +     * array that occurs at or after start. It will not search past the
  18.167 +     * specified limit. It returns -1 if there is no such clear bit.
  18.168 +     */
  18.169 +    private int sieveSearch(int limit, int start) {
  18.170 +        if (start >= limit)
  18.171 +            return -1;
  18.172 +
  18.173 +        int index = start;
  18.174 +        do {
  18.175 +            if (!get(index))
  18.176 +                return index;
  18.177 +            index++;
  18.178 +        } while(index < limit-1);
  18.179 +        return -1;
  18.180 +    }
  18.181 +
  18.182 +    /**
  18.183 +     * Sieve a single set of multiples out of the sieve. Begin to remove
  18.184 +     * multiples of the specified step starting at the specified start index,
  18.185 +     * up to the specified limit.
  18.186 +     */
  18.187 +    private void sieveSingle(int limit, int start, int step) {
  18.188 +        while(start < limit) {
  18.189 +            set(start);
  18.190 +            start += step;
  18.191 +        }
  18.192 +    }
  18.193 +
  18.194 +    /**
  18.195 +     * Test probable primes in the sieve and return successful candidates.
  18.196 +     */
  18.197 +    BigInteger retrieve(BigInteger initValue, int certainty, java.util.Random random) {
  18.198 +        // Examine the sieve one long at a time to find possible primes
  18.199 +        int offset = 1;
  18.200 +        for (int i=0; i<bits.length; i++) {
  18.201 +            long nextLong = ~bits[i];
  18.202 +            for (int j=0; j<64; j++) {
  18.203 +                if ((nextLong & 1) == 1) {
  18.204 +                    BigInteger candidate = initValue.add(
  18.205 +                                           BigInteger.valueOf(offset));
  18.206 +                    if (candidate.primeToCertainty(certainty, random))
  18.207 +                        return candidate;
  18.208 +                }
  18.209 +                nextLong >>>= 1;
  18.210 +                offset+=2;
  18.211 +            }
  18.212 +        }
  18.213 +        return null;
  18.214 +    }
  18.215 +}
    19.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.2 +++ b/rt/emul/compact/src/main/java/java/math/MathContext.java	Sun Sep 08 11:22:51 2013 +0200
    19.3 @@ -0,0 +1,326 @@
    19.4 +/*
    19.5 + * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
    19.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    19.7 + *
    19.8 + * This code is free software; you can redistribute it and/or modify it
    19.9 + * under the terms of the GNU General Public License version 2 only, as
   19.10 + * published by the Free Software Foundation.  Oracle designates this
   19.11 + * particular file as subject to the "Classpath" exception as provided
   19.12 + * by Oracle in the LICENSE file that accompanied this code.
   19.13 + *
   19.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   19.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   19.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   19.17 + * version 2 for more details (a copy is included in the LICENSE file that
   19.18 + * accompanied this code).
   19.19 + *
   19.20 + * You should have received a copy of the GNU General Public License version
   19.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   19.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   19.23 + *
   19.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   19.25 + * or visit www.oracle.com if you need additional information or have any
   19.26 + * questions.
   19.27 + */
   19.28 +
   19.29 +/*
   19.30 + * Portions Copyright IBM Corporation, 1997, 2001. All Rights Reserved.
   19.31 + */
   19.32 +
   19.33 +package java.math;
   19.34 +import java.io.*;
   19.35 +
   19.36 +/**
   19.37 + * Immutable objects which encapsulate the context settings which
   19.38 + * describe certain rules for numerical operators, such as those
   19.39 + * implemented by the {@link BigDecimal} class.
   19.40 + *
   19.41 + * <p>The base-independent settings are:
   19.42 + * <ol>
   19.43 + * <li>{@code precision}:
   19.44 + * the number of digits to be used for an operation; results are
   19.45 + * rounded to this precision
   19.46 + *
   19.47 + * <li>{@code roundingMode}:
   19.48 + * a {@link RoundingMode} object which specifies the algorithm to be
   19.49 + * used for rounding.
   19.50 + * </ol>
   19.51 + *
   19.52 + * @see     BigDecimal
   19.53 + * @see     RoundingMode
   19.54 + * @author  Mike Cowlishaw
   19.55 + * @author  Joseph D. Darcy
   19.56 + * @since 1.5
   19.57 + */
   19.58 +
   19.59 +public final class MathContext implements Serializable {
   19.60 +
   19.61 +    /* ----- Constants ----- */
   19.62 +
   19.63 +    // defaults for constructors
   19.64 +    private static final int DEFAULT_DIGITS = 9;
   19.65 +    private static final RoundingMode DEFAULT_ROUNDINGMODE = RoundingMode.HALF_UP;
   19.66 +    // Smallest values for digits (Maximum is Integer.MAX_VALUE)
   19.67 +    private static final int MIN_DIGITS = 0;
   19.68 +
   19.69 +    // Serialization version
   19.70 +    private static final long serialVersionUID = 5579720004786848255L;
   19.71 +
   19.72 +    /* ----- Public Properties ----- */
   19.73 +    /**
   19.74 +     *  A {@code MathContext} object whose settings have the values
   19.75 +     *  required for unlimited precision arithmetic.
   19.76 +     *  The values of the settings are:
   19.77 +     *  <code>
   19.78 +     *  precision=0 roundingMode=HALF_UP
   19.79 +     *  </code>
   19.80 +     */
   19.81 +    public static final MathContext UNLIMITED =
   19.82 +        new MathContext(0, RoundingMode.HALF_UP);
   19.83 +
   19.84 +    /**
   19.85 +     *  A {@code MathContext} object with a precision setting
   19.86 +     *  matching the IEEE 754R Decimal32 format, 7 digits, and a
   19.87 +     *  rounding mode of {@link RoundingMode#HALF_EVEN HALF_EVEN}, the
   19.88 +     *  IEEE 754R default.
   19.89 +     */
   19.90 +    public static final MathContext DECIMAL32 =
   19.91 +        new MathContext(7, RoundingMode.HALF_EVEN);
   19.92 +
   19.93 +    /**
   19.94 +     *  A {@code MathContext} object with a precision setting
   19.95 +     *  matching the IEEE 754R Decimal64 format, 16 digits, and a
   19.96 +     *  rounding mode of {@link RoundingMode#HALF_EVEN HALF_EVEN}, the
   19.97 +     *  IEEE 754R default.
   19.98 +     */
   19.99 +    public static final MathContext DECIMAL64 =
  19.100 +        new MathContext(16, RoundingMode.HALF_EVEN);
  19.101 +
  19.102 +    /**
  19.103 +     *  A {@code MathContext} object with a precision setting
  19.104 +     *  matching the IEEE 754R Decimal128 format, 34 digits, and a
  19.105 +     *  rounding mode of {@link RoundingMode#HALF_EVEN HALF_EVEN}, the
  19.106 +     *  IEEE 754R default.
  19.107 +     */
  19.108 +    public static final MathContext DECIMAL128 =
  19.109 +        new MathContext(34, RoundingMode.HALF_EVEN);
  19.110 +
  19.111 +    /* ----- Shared Properties ----- */
  19.112 +    /**
  19.113 +     * The number of digits to be used for an operation.  A value of 0
  19.114 +     * indicates that unlimited precision (as many digits as are
  19.115 +     * required) will be used.  Note that leading zeros (in the
  19.116 +     * coefficient of a number) are never significant.
  19.117 +     *
  19.118 +     * <p>{@code precision} will always be non-negative.
  19.119 +     *
  19.120 +     * @serial
  19.121 +     */
  19.122 +    final int precision;
  19.123 +
  19.124 +    /**
  19.125 +     * The rounding algorithm to be used for an operation.
  19.126 +     *
  19.127 +     * @see RoundingMode
  19.128 +     * @serial
  19.129 +     */
  19.130 +    final RoundingMode roundingMode;
  19.131 +
  19.132 +    /* ----- Constructors ----- */
  19.133 +
  19.134 +    /**
  19.135 +     * Constructs a new {@code MathContext} with the specified
  19.136 +     * precision and the {@link RoundingMode#HALF_UP HALF_UP} rounding
  19.137 +     * mode.
  19.138 +     *
  19.139 +     * @param setPrecision The non-negative {@code int} precision setting.
  19.140 +     * @throws IllegalArgumentException if the {@code setPrecision} parameter is less
  19.141 +     *         than zero.
  19.142 +     */
  19.143 +    public MathContext(int setPrecision) {
  19.144 +        this(setPrecision, DEFAULT_ROUNDINGMODE);
  19.145 +        return;
  19.146 +    }
  19.147 +
  19.148 +    /**
  19.149 +     * Constructs a new {@code MathContext} with a specified
  19.150 +     * precision and rounding mode.
  19.151 +     *
  19.152 +     * @param setPrecision The non-negative {@code int} precision setting.
  19.153 +     * @param setRoundingMode The rounding mode to use.
  19.154 +     * @throws IllegalArgumentException if the {@code setPrecision} parameter is less
  19.155 +     *         than zero.
  19.156 +     * @throws NullPointerException if the rounding mode argument is {@code null}
  19.157 +     */
  19.158 +    public MathContext(int setPrecision,
  19.159 +                       RoundingMode setRoundingMode) {
  19.160 +        if (setPrecision < MIN_DIGITS)
  19.161 +            throw new IllegalArgumentException("Digits < 0");
  19.162 +        if (setRoundingMode == null)
  19.163 +            throw new NullPointerException("null RoundingMode");
  19.164 +
  19.165 +        precision = setPrecision;
  19.166 +        roundingMode = setRoundingMode;
  19.167 +        return;
  19.168 +    }
  19.169 +
  19.170 +    /**
  19.171 +     * Constructs a new {@code MathContext} from a string.
  19.172 +     *
  19.173 +     * The string must be in the same format as that produced by the
  19.174 +     * {@link #toString} method.
  19.175 +     *
  19.176 +     * <p>An {@code IllegalArgumentException} is thrown if the precision
  19.177 +     * section of the string is out of range ({@code < 0}) or the string is
  19.178 +     * not in the format created by the {@link #toString} method.
  19.179 +     *
  19.180 +     * @param val The string to be parsed
  19.181 +     * @throws IllegalArgumentException if the precision section is out of range
  19.182 +     * or of incorrect format
  19.183 +     * @throws NullPointerException if the argument is {@code null}
  19.184 +     */
  19.185 +    public MathContext(String val) {
  19.186 +        boolean bad = false;
  19.187 +        int setPrecision;
  19.188 +        if (val == null)
  19.189 +            throw new NullPointerException("null String");
  19.190 +        try { // any error here is a string format problem
  19.191 +            if (!val.startsWith("precision=")) throw new RuntimeException();
  19.192 +            int fence = val.indexOf(' ');    // could be -1
  19.193 +            int off = 10;                     // where value starts
  19.194 +            setPrecision = Integer.parseInt(val.substring(10, fence));
  19.195 +
  19.196 +            if (!val.startsWith("roundingMode=", fence+1))
  19.197 +                throw new RuntimeException();
  19.198 +            off = fence + 1 + 13;
  19.199 +            String str = val.substring(off, val.length());
  19.200 +            roundingMode = RoundingMode.valueOf(str);
  19.201 +        } catch (RuntimeException re) {
  19.202 +            throw new IllegalArgumentException("bad string format");
  19.203 +        }
  19.204 +
  19.205 +        if (setPrecision < MIN_DIGITS)
  19.206 +            throw new IllegalArgumentException("Digits < 0");
  19.207 +        // the other parameters cannot be invalid if we got here
  19.208 +        precision = setPrecision;
  19.209 +    }
  19.210 +
  19.211 +    /**
  19.212 +     * Returns the {@code precision} setting.
  19.213 +     * This value is always non-negative.
  19.214 +     *
  19.215 +     * @return an {@code int} which is the value of the {@code precision}
  19.216 +     *         setting
  19.217 +     */
  19.218 +    public int getPrecision() {
  19.219 +        return precision;
  19.220 +    }
  19.221 +
  19.222 +    /**
  19.223 +     * Returns the roundingMode setting.
  19.224 +     * This will be one of
  19.225 +     * {@link  RoundingMode#CEILING},
  19.226 +     * {@link  RoundingMode#DOWN},
  19.227 +     * {@link  RoundingMode#FLOOR},
  19.228 +     * {@link  RoundingMode#HALF_DOWN},
  19.229 +     * {@link  RoundingMode#HALF_EVEN},
  19.230 +     * {@link  RoundingMode#HALF_UP},
  19.231 +     * {@link  RoundingMode#UNNECESSARY}, or
  19.232 +     * {@link  RoundingMode#UP}.
  19.233 +     *
  19.234 +     * @return a {@code RoundingMode} object which is the value of the
  19.235 +     *         {@code roundingMode} setting
  19.236 +     */
  19.237 +
  19.238 +    public RoundingMode getRoundingMode() {
  19.239 +        return roundingMode;
  19.240 +    }
  19.241 +
  19.242 +    /**
  19.243 +     * Compares this {@code MathContext} with the specified
  19.244 +     * {@code Object} for equality.
  19.245 +     *
  19.246 +     * @param  x {@code Object} to which this {@code MathContext} is to
  19.247 +     *         be compared.
  19.248 +     * @return {@code true} if and only if the specified {@code Object} is
  19.249 +     *         a {@code MathContext} object which has exactly the same
  19.250 +     *         settings as this object
  19.251 +     */
  19.252 +    public boolean equals(Object x){
  19.253 +        MathContext mc;
  19.254 +        if (!(x instanceof MathContext))
  19.255 +            return false;
  19.256 +        mc = (MathContext) x;
  19.257 +        return mc.precision == this.precision
  19.258 +            && mc.roundingMode == this.roundingMode; // no need for .equals()
  19.259 +    }
  19.260 +
  19.261 +    /**
  19.262 +     * Returns the hash code for this {@code MathContext}.
  19.263 +     *
  19.264 +     * @return hash code for this {@code MathContext}
  19.265 +     */
  19.266 +    public int hashCode() {
  19.267 +        return this.precision + roundingMode.hashCode() * 59;
  19.268 +    }
  19.269 +
  19.270 +    /**
  19.271 +     * Returns the string representation of this {@code MathContext}.
  19.272 +     * The {@code String} returned represents the settings of the
  19.273 +     * {@code MathContext} object as two space-delimited words
  19.274 +     * (separated by a single space character, <tt>'&#92;u0020'</tt>,
  19.275 +     * and with no leading or trailing white space), as follows:
  19.276 +     * <ol>
  19.277 +     * <li>
  19.278 +     * The string {@code "precision="}, immediately followed
  19.279 +     * by the value of the precision setting as a numeric string as if
  19.280 +     * generated by the {@link Integer#toString(int) Integer.toString}
  19.281 +     * method.
  19.282 +     *
  19.283 +     * <li>
  19.284 +     * The string {@code "roundingMode="}, immediately
  19.285 +     * followed by the value of the {@code roundingMode} setting as a
  19.286 +     * word.  This word will be the same as the name of the
  19.287 +     * corresponding public constant in the {@link RoundingMode}
  19.288 +     * enum.
  19.289 +     * </ol>
  19.290 +     * <p>
  19.291 +     * For example:
  19.292 +     * <pre>
  19.293 +     * precision=9 roundingMode=HALF_UP
  19.294 +     * </pre>
  19.295 +     *
  19.296 +     * Additional words may be appended to the result of
  19.297 +     * {@code toString} in the future if more properties are added to
  19.298 +     * this class.
  19.299 +     *
  19.300 +     * @return a {@code String} representing the context settings
  19.301 +     */
  19.302 +    public java.lang.String toString() {
  19.303 +        return "precision=" +           precision + " " +
  19.304 +               "roundingMode=" +        roundingMode.toString();
  19.305 +    }
  19.306 +
  19.307 +    // Private methods
  19.308 +
  19.309 +    /**
  19.310 +     * Reconstitute the {@code MathContext} instance from a stream (that is,
  19.311 +     * deserialize it).
  19.312 +     *
  19.313 +     * @param s the stream being read.
  19.314 +     */
  19.315 +    private void readObject(java.io.ObjectInputStream s)
  19.316 +        throws java.io.IOException, ClassNotFoundException {
  19.317 +        s.defaultReadObject();     // read in all fields
  19.318 +        // validate possibly bad fields
  19.319 +        if (precision < MIN_DIGITS) {
  19.320 +            String message = "MathContext: invalid digits in stream";
  19.321 +            throw new java.io.StreamCorruptedException(message);
  19.322 +        }
  19.323 +        if (roundingMode == null) {
  19.324 +            String message = "MathContext: null roundingMode in stream";
  19.325 +            throw new java.io.StreamCorruptedException(message);
  19.326 +        }
  19.327 +    }
  19.328 +
  19.329 +}
    20.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    20.2 +++ b/rt/emul/compact/src/main/java/java/math/MutableBigInteger.java	Sun Sep 08 11:22:51 2013 +0200
    20.3 @@ -0,0 +1,1477 @@
    20.4 +/*
    20.5 + * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
    20.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    20.7 + *
    20.8 + * This code is free software; you can redistribute it and/or modify it
    20.9 + * under the terms of the GNU General Public License version 2 only, as
   20.10 + * published by the Free Software Foundation.  Oracle designates this
   20.11 + * particular file as subject to the "Classpath" exception as provided
   20.12 + * by Oracle in the LICENSE file that accompanied this code.
   20.13 + *
   20.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   20.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   20.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   20.17 + * version 2 for more details (a copy is included in the LICENSE file that
   20.18 + * accompanied this code).
   20.19 + *
   20.20 + * You should have received a copy of the GNU General Public License version
   20.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   20.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   20.23 + *
   20.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   20.25 + * or visit www.oracle.com if you need additional information or have any
   20.26 + * questions.
   20.27 + */
   20.28 +
   20.29 +package java.math;
   20.30 +
   20.31 +/**
   20.32 + * A class used to represent multiprecision integers that makes efficient
   20.33 + * use of allocated space by allowing a number to occupy only part of
   20.34 + * an array so that the arrays do not have to be reallocated as often.
   20.35 + * When performing an operation with many iterations the array used to
   20.36 + * hold a number is only reallocated when necessary and does not have to
   20.37 + * be the same size as the number it represents. A mutable number allows
   20.38 + * calculations to occur on the same number without having to create
   20.39 + * a new number for every step of the calculation as occurs with
   20.40 + * BigIntegers.
   20.41 + *
   20.42 + * @see     BigInteger
   20.43 + * @author  Michael McCloskey
   20.44 + * @since   1.3
   20.45 + */
   20.46 +
   20.47 +import java.util.Arrays;
   20.48 +
   20.49 +import static java.math.BigInteger.LONG_MASK;
   20.50 +import static java.math.BigDecimal.INFLATED;
   20.51 +
   20.52 +class MutableBigInteger {
   20.53 +    /**
   20.54 +     * Holds the magnitude of this MutableBigInteger in big endian order.
   20.55 +     * The magnitude may start at an offset into the value array, and it may
   20.56 +     * end before the length of the value array.
   20.57 +     */
   20.58 +    int[] value;
   20.59 +
   20.60 +    /**
   20.61 +     * The number of ints of the value array that are currently used
   20.62 +     * to hold the magnitude of this MutableBigInteger. The magnitude starts
   20.63 +     * at an offset and offset + intLen may be less than value.length.
   20.64 +     */
   20.65 +    int intLen;
   20.66 +
   20.67 +    /**
   20.68 +     * The offset into the value array where the magnitude of this
   20.69 +     * MutableBigInteger begins.
   20.70 +     */
   20.71 +    int offset = 0;
   20.72 +
   20.73 +    // Constants
   20.74 +    /**
   20.75 +     * MutableBigInteger with one element value array with the value 1. Used by
   20.76 +     * BigDecimal divideAndRound to increment the quotient. Use this constant
   20.77 +     * only when the method is not going to modify this object.
   20.78 +     */
   20.79 +    static final MutableBigInteger ONE = new MutableBigInteger(1);
   20.80 +
   20.81 +    // Constructors
   20.82 +
   20.83 +    /**
   20.84 +     * The default constructor. An empty MutableBigInteger is created with
   20.85 +     * a one word capacity.
   20.86 +     */
   20.87 +    MutableBigInteger() {
   20.88 +        value = new int[1];
   20.89 +        intLen = 0;
   20.90 +    }
   20.91 +
   20.92 +    /**
   20.93 +     * Construct a new MutableBigInteger with a magnitude specified by
   20.94 +     * the int val.
   20.95 +     */
   20.96 +    MutableBigInteger(int val) {
   20.97 +        value = new int[1];
   20.98 +        intLen = 1;
   20.99 +        value[0] = val;
  20.100 +    }
  20.101 +
  20.102 +    /**
  20.103 +     * Construct a new MutableBigInteger with the specified value array
  20.104 +     * up to the length of the array supplied.
  20.105 +     */
  20.106 +    MutableBigInteger(int[] val) {
  20.107 +        value = val;
  20.108 +        intLen = val.length;
  20.109 +    }
  20.110 +
  20.111 +    /**
  20.112 +     * Construct a new MutableBigInteger with a magnitude equal to the
  20.113 +     * specified BigInteger.
  20.114 +     */
  20.115 +    MutableBigInteger(BigInteger b) {
  20.116 +        intLen = b.mag.length;
  20.117 +        value = Arrays.copyOf(b.mag, intLen);
  20.118 +    }
  20.119 +
  20.120 +    /**
  20.121 +     * Construct a new MutableBigInteger with a magnitude equal to the
  20.122 +     * specified MutableBigInteger.
  20.123 +     */
  20.124 +    MutableBigInteger(MutableBigInteger val) {
  20.125 +        intLen = val.intLen;
  20.126 +        value = Arrays.copyOfRange(val.value, val.offset, val.offset + intLen);
  20.127 +    }
  20.128 +
  20.129 +    /**
  20.130 +     * Internal helper method to return the magnitude array. The caller is not
  20.131 +     * supposed to modify the returned array.
  20.132 +     */
  20.133 +    private int[] getMagnitudeArray() {
  20.134 +        if (offset > 0 || value.length != intLen)
  20.135 +            return Arrays.copyOfRange(value, offset, offset + intLen);
  20.136 +        return value;
  20.137 +    }
  20.138 +
  20.139 +    /**
  20.140 +     * Convert this MutableBigInteger to a long value. The caller has to make
  20.141 +     * sure this MutableBigInteger can be fit into long.
  20.142 +     */
  20.143 +    private long toLong() {
  20.144 +        assert (intLen <= 2) : "this MutableBigInteger exceeds the range of long";
  20.145 +        if (intLen == 0)
  20.146 +            return 0;
  20.147 +        long d = value[offset] & LONG_MASK;
  20.148 +        return (intLen == 2) ? d << 32 | (value[offset + 1] & LONG_MASK) : d;
  20.149 +    }
  20.150 +
  20.151 +    /**
  20.152 +     * Convert this MutableBigInteger to a BigInteger object.
  20.153 +     */
  20.154 +    BigInteger toBigInteger(int sign) {
  20.155 +        if (intLen == 0 || sign == 0)
  20.156 +            return BigInteger.ZERO;
  20.157 +        return new BigInteger(getMagnitudeArray(), sign);
  20.158 +    }
  20.159 +
  20.160 +    /**
  20.161 +     * Convert this MutableBigInteger to BigDecimal object with the specified sign
  20.162 +     * and scale.
  20.163 +     */
  20.164 +    BigDecimal toBigDecimal(int sign, int scale) {
  20.165 +        if (intLen == 0 || sign == 0)
  20.166 +            return BigDecimal.valueOf(0, scale);
  20.167 +        int[] mag = getMagnitudeArray();
  20.168 +        int len = mag.length;
  20.169 +        int d = mag[0];
  20.170 +        // If this MutableBigInteger can't be fit into long, we need to
  20.171 +        // make a BigInteger object for the resultant BigDecimal object.
  20.172 +        if (len > 2 || (d < 0 && len == 2))
  20.173 +            return new BigDecimal(new BigInteger(mag, sign), INFLATED, scale, 0);
  20.174 +        long v = (len == 2) ?
  20.175 +            ((mag[1] & LONG_MASK) | (d & LONG_MASK) << 32) :
  20.176 +            d & LONG_MASK;
  20.177 +        return new BigDecimal(null, sign == -1 ? -v : v, scale, 0);
  20.178 +    }
  20.179 +
  20.180 +    /**
  20.181 +     * Clear out a MutableBigInteger for reuse.
  20.182 +     */
  20.183 +    void clear() {
  20.184 +        offset = intLen = 0;
  20.185 +        for (int index=0, n=value.length; index < n; index++)
  20.186 +            value[index] = 0;
  20.187 +    }
  20.188 +
  20.189 +    /**
  20.190 +     * Set a MutableBigInteger to zero, removing its offset.
  20.191 +     */
  20.192 +    void reset() {
  20.193 +        offset = intLen = 0;
  20.194 +    }
  20.195 +
  20.196 +    /**
  20.197 +     * Compare the magnitude of two MutableBigIntegers. Returns -1, 0 or 1
  20.198 +     * as this MutableBigInteger is numerically less than, equal to, or
  20.199 +     * greater than <tt>b</tt>.
  20.200 +     */
  20.201 +    final int compare(MutableBigInteger b) {
  20.202 +        int blen = b.intLen;
  20.203 +        if (intLen < blen)
  20.204 +            return -1;
  20.205 +        if (intLen > blen)
  20.206 +           return 1;
  20.207 +
  20.208 +        // Add Integer.MIN_VALUE to make the comparison act as unsigned integer
  20.209 +        // comparison.
  20.210 +        int[] bval = b.value;
  20.211 +        for (int i = offset, j = b.offset; i < intLen + offset; i++, j++) {
  20.212 +            int b1 = value[i] + 0x80000000;
  20.213 +            int b2 = bval[j]  + 0x80000000;
  20.214 +            if (b1 < b2)
  20.215 +                return -1;
  20.216 +            if (b1 > b2)
  20.217 +                return 1;
  20.218 +        }
  20.219 +        return 0;
  20.220 +    }
  20.221 +
  20.222 +    /**
  20.223 +     * Compare this against half of a MutableBigInteger object (Needed for
  20.224 +     * remainder tests).
  20.225 +     * Assumes no leading unnecessary zeros, which holds for results
  20.226 +     * from divide().
  20.227 +     */
  20.228 +    final int compareHalf(MutableBigInteger b) {
  20.229 +        int blen = b.intLen;
  20.230 +        int len = intLen;
  20.231 +        if (len <= 0)
  20.232 +            return blen <=0 ? 0 : -1;
  20.233 +        if (len > blen)
  20.234 +            return 1;
  20.235 +        if (len < blen - 1)
  20.236 +            return -1;
  20.237 +        int[] bval = b.value;
  20.238 +        int bstart = 0;
  20.239 +        int carry = 0;
  20.240 +        // Only 2 cases left:len == blen or len == blen - 1
  20.241 +        if (len != blen) { // len == blen - 1
  20.242 +            if (bval[bstart] == 1) {
  20.243 +                ++bstart;
  20.244 +                carry = 0x80000000;
  20.245 +            } else
  20.246 +                return -1;
  20.247 +        }
  20.248 +        // compare values with right-shifted values of b,
  20.249 +        // carrying shifted-out bits across words
  20.250 +        int[] val = value;
  20.251 +        for (int i = offset, j = bstart; i < len + offset;) {
  20.252 +            int bv = bval[j++];
  20.253 +            long hb = ((bv >>> 1) + carry) & LONG_MASK;
  20.254 +            long v = val[i++] & LONG_MASK;
  20.255 +            if (v != hb)
  20.256 +                return v < hb ? -1 : 1;
  20.257 +            carry = (bv & 1) << 31; // carray will be either 0x80000000 or 0
  20.258 +        }
  20.259 +        return carry == 0? 0 : -1;
  20.260 +    }
  20.261 +
  20.262 +    /**
  20.263 +     * Return the index of the lowest set bit in this MutableBigInteger. If the
  20.264 +     * magnitude of this MutableBigInteger is zero, -1 is returned.
  20.265 +     */
  20.266 +    private final int getLowestSetBit() {
  20.267 +        if (intLen == 0)
  20.268 +            return -1;
  20.269 +        int j, b;
  20.270 +        for (j=intLen-1; (j>0) && (value[j+offset]==0); j--)
  20.271 +            ;
  20.272 +        b = value[j+offset];
  20.273 +        if (b==0)
  20.274 +            return -1;
  20.275 +        return ((intLen-1-j)<<5) + Integer.numberOfTrailingZeros(b);
  20.276 +    }
  20.277 +
  20.278 +    /**
  20.279 +     * Return the int in use in this MutableBigInteger at the specified
  20.280 +     * index. This method is not used because it is not inlined on all
  20.281 +     * platforms.
  20.282 +     */
  20.283 +    private final int getInt(int index) {
  20.284 +        return value[offset+index];
  20.285 +    }
  20.286 +
  20.287 +    /**
  20.288 +     * Return a long which is equal to the unsigned value of the int in
  20.289 +     * use in this MutableBigInteger at the specified index. This method is
  20.290 +     * not used because it is not inlined on all platforms.
  20.291 +     */
  20.292 +    private final long getLong(int index) {
  20.293 +        return value[offset+index] & LONG_MASK;
  20.294 +    }
  20.295 +
  20.296 +    /**
  20.297 +     * Ensure that the MutableBigInteger is in normal form, specifically
  20.298 +     * making sure that there are no leading zeros, and that if the
  20.299 +     * magnitude is zero, then intLen is zero.
  20.300 +     */
  20.301 +    final void normalize() {
  20.302 +        if (intLen == 0) {
  20.303 +            offset = 0;
  20.304 +            return;
  20.305 +        }
  20.306 +
  20.307 +        int index = offset;
  20.308 +        if (value[index] != 0)
  20.309 +            return;
  20.310 +
  20.311 +        int indexBound = index+intLen;
  20.312 +        do {
  20.313 +            index++;
  20.314 +        } while(index < indexBound && value[index]==0);
  20.315 +
  20.316 +        int numZeros = index - offset;
  20.317 +        intLen -= numZeros;
  20.318 +        offset = (intLen==0 ?  0 : offset+numZeros);
  20.319 +    }
  20.320 +
  20.321 +    /**
  20.322 +     * If this MutableBigInteger cannot hold len words, increase the size
  20.323 +     * of the value array to len words.
  20.324 +     */
  20.325 +    private final void ensureCapacity(int len) {
  20.326 +        if (value.length < len) {
  20.327 +            value = new int[len];
  20.328 +            offset = 0;
  20.329 +            intLen = len;
  20.330 +        }
  20.331 +    }
  20.332 +
  20.333 +    /**
  20.334 +     * Convert this MutableBigInteger into an int array with no leading
  20.335 +     * zeros, of a length that is equal to this MutableBigInteger's intLen.
  20.336 +     */
  20.337 +    int[] toIntArray() {
  20.338 +        int[] result = new int[intLen];
  20.339 +        for(int i=0; i<intLen; i++)
  20.340 +            result[i] = value[offset+i];
  20.341 +        return result;
  20.342 +    }
  20.343 +
  20.344 +    /**
  20.345 +     * Sets the int at index+offset in this MutableBigInteger to val.
  20.346 +     * This does not get inlined on all platforms so it is not used
  20.347 +     * as often as originally intended.
  20.348 +     */
  20.349 +    void setInt(int index, int val) {
  20.350 +        value[offset + index] = val;
  20.351 +    }
  20.352 +
  20.353 +    /**
  20.354 +     * Sets this MutableBigInteger's value array to the specified array.
  20.355 +     * The intLen is set to the specified length.
  20.356 +     */
  20.357 +    void setValue(int[] val, int length) {
  20.358 +        value = val;
  20.359 +        intLen = length;
  20.360 +        offset = 0;
  20.361 +    }
  20.362 +
  20.363 +    /**
  20.364 +     * Sets this MutableBigInteger's value array to a copy of the specified
  20.365 +     * array. The intLen is set to the length of the new array.
  20.366 +     */
  20.367 +    void copyValue(MutableBigInteger src) {
  20.368 +        int len = src.intLen;
  20.369 +        if (value.length < len)
  20.370 +            value = new int[len];
  20.371 +        System.arraycopy(src.value, src.offset, value, 0, len);
  20.372 +        intLen = len;
  20.373 +        offset = 0;
  20.374 +    }
  20.375 +
  20.376 +    /**
  20.377 +     * Sets this MutableBigInteger's value array to a copy of the specified
  20.378 +     * array. The intLen is set to the length of the specified array.
  20.379 +     */
  20.380 +    void copyValue(int[] val) {
  20.381 +        int len = val.length;
  20.382 +        if (value.length < len)
  20.383 +            value = new int[len];
  20.384 +        System.arraycopy(val, 0, value, 0, len);
  20.385 +        intLen = len;
  20.386 +        offset = 0;
  20.387 +    }
  20.388 +
  20.389 +    /**
  20.390 +     * Returns true iff this MutableBigInteger has a value of one.
  20.391 +     */
  20.392 +    boolean isOne() {
  20.393 +        return (intLen == 1) && (value[offset] == 1);
  20.394 +    }
  20.395 +
  20.396 +    /**
  20.397 +     * Returns true iff this MutableBigInteger has a value of zero.
  20.398 +     */
  20.399 +    boolean isZero() {
  20.400 +        return (intLen == 0);
  20.401 +    }
  20.402 +
  20.403 +    /**
  20.404 +     * Returns true iff this MutableBigInteger is even.
  20.405 +     */
  20.406 +    boolean isEven() {
  20.407 +        return (intLen == 0) || ((value[offset + intLen - 1] & 1) == 0);
  20.408 +    }
  20.409 +
  20.410 +    /**
  20.411 +     * Returns true iff this MutableBigInteger is odd.
  20.412 +     */
  20.413 +    boolean isOdd() {
  20.414 +        return isZero() ? false : ((value[offset + intLen - 1] & 1) == 1);
  20.415 +    }
  20.416 +
  20.417 +    /**
  20.418 +     * Returns true iff this MutableBigInteger is in normal form. A
  20.419 +     * MutableBigInteger is in normal form if it has no leading zeros
  20.420 +     * after the offset, and intLen + offset <= value.length.
  20.421 +     */
  20.422 +    boolean isNormal() {
  20.423 +        if (intLen + offset > value.length)
  20.424 +            return false;
  20.425 +        if (intLen ==0)
  20.426 +            return true;
  20.427 +        return (value[offset] != 0);
  20.428 +    }
  20.429 +
  20.430 +    /**
  20.431 +     * Returns a String representation of this MutableBigInteger in radix 10.
  20.432 +     */
  20.433 +    public String toString() {
  20.434 +        BigInteger b = toBigInteger(1);
  20.435 +        return b.toString();
  20.436 +    }
  20.437 +
  20.438 +    /**
  20.439 +     * Right shift this MutableBigInteger n bits. The MutableBigInteger is left
  20.440 +     * in normal form.
  20.441 +     */
  20.442 +    void rightShift(int n) {
  20.443 +        if (intLen == 0)
  20.444 +            return;
  20.445 +        int nInts = n >>> 5;
  20.446 +        int nBits = n & 0x1F;
  20.447 +        this.intLen -= nInts;
  20.448 +        if (nBits == 0)
  20.449 +            return;
  20.450 +        int bitsInHighWord = BigInteger.bitLengthForInt(value[offset]);
  20.451 +        if (nBits >= bitsInHighWord) {
  20.452 +            this.primitiveLeftShift(32 - nBits);
  20.453 +            this.intLen--;
  20.454 +        } else {
  20.455 +            primitiveRightShift(nBits);
  20.456 +        }
  20.457 +    }
  20.458 +
  20.459 +    /**
  20.460 +     * Left shift this MutableBigInteger n bits.
  20.461 +     */
  20.462 +    void leftShift(int n) {
  20.463 +        /*
  20.464 +         * If there is enough storage space in this MutableBigInteger already
  20.465 +         * the available space will be used. Space to the right of the used
  20.466 +         * ints in the value array is faster to utilize, so the extra space
  20.467 +         * will be taken from the right if possible.
  20.468 +         */
  20.469 +        if (intLen == 0)
  20.470 +           return;
  20.471 +        int nInts = n >>> 5;
  20.472 +        int nBits = n&0x1F;
  20.473 +        int bitsInHighWord = BigInteger.bitLengthForInt(value[offset]);
  20.474 +
  20.475 +        // If shift can be done without moving words, do so
  20.476 +        if (n <= (32-bitsInHighWord)) {
  20.477 +            primitiveLeftShift(nBits);
  20.478 +            return;
  20.479 +        }
  20.480 +
  20.481 +        int newLen = intLen + nInts +1;
  20.482 +        if (nBits <= (32-bitsInHighWord))
  20.483 +            newLen--;
  20.484 +        if (value.length < newLen) {
  20.485 +            // The array must grow
  20.486 +            int[] result = new int[newLen];
  20.487 +            for (int i=0; i<intLen; i++)
  20.488 +                result[i] = value[offset+i];
  20.489 +            setValue(result, newLen);
  20.490 +        } else if (value.length - offset >= newLen) {
  20.491 +            // Use space on right
  20.492 +            for(int i=0; i<newLen - intLen; i++)
  20.493 +                value[offset+intLen+i] = 0;
  20.494 +        } else {
  20.495 +            // Must use space on left
  20.496 +            for (int i=0; i<intLen; i++)
  20.497 +                value[i] = value[offset+i];
  20.498 +            for (int i=intLen; i<newLen; i++)
  20.499 +                value[i] = 0;
  20.500 +            offset = 0;
  20.501 +        }
  20.502 +        intLen = newLen;
  20.503 +        if (nBits == 0)
  20.504 +            return;
  20.505 +        if (nBits <= (32-bitsInHighWord))
  20.506 +            primitiveLeftShift(nBits);
  20.507 +        else
  20.508 +            primitiveRightShift(32 -nBits);
  20.509 +    }
  20.510 +
  20.511 +    /**
  20.512 +     * A primitive used for division. This method adds in one multiple of the
  20.513 +     * divisor a back to the dividend result at a specified offset. It is used
  20.514 +     * when qhat was estimated too large, and must be adjusted.
  20.515 +     */
  20.516 +    private int divadd(int[] a, int[] result, int offset) {
  20.517 +        long carry = 0;
  20.518 +
  20.519 +        for (int j=a.length-1; j >= 0; j--) {
  20.520 +            long sum = (a[j] & LONG_MASK) +
  20.521 +                       (result[j+offset] & LONG_MASK) + carry;
  20.522 +            result[j+offset] = (int)sum;
  20.523 +            carry = sum >>> 32;
  20.524 +        }
  20.525 +        return (int)carry;
  20.526 +    }
  20.527 +
  20.528 +    /**
  20.529 +     * This method is used for division. It multiplies an n word input a by one
  20.530 +     * word input x, and subtracts the n word product from q. This is needed
  20.531 +     * when subtracting qhat*divisor from dividend.
  20.532 +     */
  20.533 +    private int mulsub(int[] q, int[] a, int x, int len, int offset) {
  20.534 +        long xLong = x & LONG_MASK;
  20.535 +        long carry = 0;
  20.536 +        offset += len;
  20.537 +
  20.538 +        for (int j=len-1; j >= 0; j--) {
  20.539 +            long product = (a[j] & LONG_MASK) * xLong + carry;
  20.540 +            long difference = q[offset] - product;
  20.541 +            q[offset--] = (int)difference;
  20.542 +            carry = (product >>> 32)
  20.543 +                     + (((difference & LONG_MASK) >
  20.544 +                         (((~(int)product) & LONG_MASK))) ? 1:0);
  20.545 +        }
  20.546 +        return (int)carry;
  20.547 +    }
  20.548 +
  20.549 +    /**
  20.550 +     * Right shift this MutableBigInteger n bits, where n is
  20.551 +     * less than 32.
  20.552 +     * Assumes that intLen > 0, n > 0 for speed
  20.553 +     */
  20.554 +    private final void primitiveRightShift(int n) {
  20.555 +        int[] val = value;
  20.556 +        int n2 = 32 - n;
  20.557 +        for (int i=offset+intLen-1, c=val[i]; i>offset; i--) {
  20.558 +            int b = c;
  20.559 +            c = val[i-1];
  20.560 +            val[i] = (c << n2) | (b >>> n);
  20.561 +        }
  20.562 +        val[offset] >>>= n;
  20.563 +    }
  20.564 +
  20.565 +    /**
  20.566 +     * Left shift this MutableBigInteger n bits, where n is
  20.567 +     * less than 32.
  20.568 +     * Assumes that intLen > 0, n > 0 for speed
  20.569 +     */
  20.570 +    private final void primitiveLeftShift(int n) {
  20.571 +        int[] val = value;
  20.572 +        int n2 = 32 - n;
  20.573 +        for (int i=offset, c=val[i], m=i+intLen-1; i<m; i++) {
  20.574 +            int b = c;
  20.575 +            c = val[i+1];
  20.576 +            val[i] = (b << n) | (c >>> n2);
  20.577 +        }
  20.578 +        val[offset+intLen-1] <<= n;
  20.579 +    }
  20.580 +
  20.581 +    /**
  20.582 +     * Adds the contents of two MutableBigInteger objects.The result
  20.583 +     * is placed within this MutableBigInteger.
  20.584 +     * The contents of the addend are not changed.
  20.585 +     */
  20.586 +    void add(MutableBigInteger addend) {
  20.587 +        int x = intLen;
  20.588 +        int y = addend.intLen;
  20.589 +        int resultLen = (intLen > addend.intLen ? intLen : addend.intLen);
  20.590 +        int[] result = (value.length < resultLen ? new int[resultLen] : value);
  20.591 +
  20.592 +        int rstart = result.length-1;
  20.593 +        long sum;
  20.594 +        long carry = 0;
  20.595 +
  20.596 +        // Add common parts of both numbers
  20.597 +        while(x>0 && y>0) {
  20.598 +            x--; y--;
  20.599 +            sum = (value[x+offset] & LONG_MASK) +
  20.600 +                (addend.value[y+addend.offset] & LONG_MASK) + carry;
  20.601 +            result[rstart--] = (int)sum;
  20.602 +            carry = sum >>> 32;
  20.603 +        }
  20.604 +
  20.605 +        // Add remainder of the longer number
  20.606 +        while(x>0) {
  20.607 +            x--;
  20.608 +            if (carry == 0 && result == value && rstart == (x + offset))
  20.609 +                return;
  20.610 +            sum = (value[x+offset] & LONG_MASK) + carry;
  20.611 +            result[rstart--] = (int)sum;
  20.612 +            carry = sum >>> 32;
  20.613 +        }
  20.614 +        while(y>0) {
  20.615 +            y--;
  20.616 +            sum = (addend.value[y+addend.offset] & LONG_MASK) + carry;
  20.617 +            result[rstart--] = (int)sum;
  20.618 +            carry = sum >>> 32;
  20.619 +        }
  20.620 +
  20.621 +        if (carry > 0) { // Result must grow in length
  20.622 +            resultLen++;
  20.623 +            if (result.length < resultLen) {
  20.624 +                int temp[] = new int[resultLen];
  20.625 +                // Result one word longer from carry-out; copy low-order
  20.626 +                // bits into new result.
  20.627 +                System.arraycopy(result, 0, temp, 1, result.length);
  20.628 +                temp[0] = 1;
  20.629 +                result = temp;
  20.630 +            } else {
  20.631 +                result[rstart--] = 1;
  20.632 +            }
  20.633 +        }
  20.634 +
  20.635 +        value = result;
  20.636 +        intLen = resultLen;
  20.637 +        offset = result.length - resultLen;
  20.638 +    }
  20.639 +
  20.640 +
  20.641 +    /**
  20.642 +     * Subtracts the smaller of this and b from the larger and places the
  20.643 +     * result into this MutableBigInteger.
  20.644 +     */
  20.645 +    int subtract(MutableBigInteger b) {
  20.646 +        MutableBigInteger a = this;
  20.647 +
  20.648 +        int[] result = value;
  20.649 +        int sign = a.compare(b);
  20.650 +
  20.651 +        if (sign == 0) {
  20.652 +            reset();
  20.653 +            return 0;
  20.654 +        }
  20.655 +        if (sign < 0) {
  20.656 +            MutableBigInteger tmp = a;
  20.657 +            a = b;
  20.658 +            b = tmp;
  20.659 +        }
  20.660 +
  20.661 +        int resultLen = a.intLen;
  20.662 +        if (result.length < resultLen)
  20.663 +            result = new int[resultLen];
  20.664 +
  20.665 +        long diff = 0;
  20.666 +        int x = a.intLen;
  20.667 +        int y = b.intLen;
  20.668 +        int rstart = result.length - 1;
  20.669 +
  20.670 +        // Subtract common parts of both numbers
  20.671 +        while (y>0) {
  20.672 +            x--; y--;
  20.673 +
  20.674 +            diff = (a.value[x+a.offset] & LONG_MASK) -
  20.675 +                   (b.value[y+b.offset] & LONG_MASK) - ((int)-(diff>>32));
  20.676 +            result[rstart--] = (int)diff;
  20.677 +        }
  20.678 +        // Subtract remainder of longer number
  20.679 +        while (x>0) {
  20.680 +            x--;
  20.681 +            diff = (a.value[x+a.offset] & LONG_MASK) - ((int)-(diff>>32));
  20.682 +            result[rstart--] = (int)diff;
  20.683 +        }
  20.684 +
  20.685 +        value = result;
  20.686 +        intLen = resultLen;
  20.687 +        offset = value.length - resultLen;
  20.688 +        normalize();
  20.689 +        return sign;
  20.690 +    }
  20.691 +
  20.692 +    /**
  20.693 +     * Subtracts the smaller of a and b from the larger and places the result
  20.694 +     * into the larger. Returns 1 if the answer is in a, -1 if in b, 0 if no
  20.695 +     * operation was performed.
  20.696 +     */
  20.697 +    private int difference(MutableBigInteger b) {
  20.698 +        MutableBigInteger a = this;
  20.699 +        int sign = a.compare(b);
  20.700 +        if (sign ==0)
  20.701 +            return 0;
  20.702 +        if (sign < 0) {
  20.703 +            MutableBigInteger tmp = a;
  20.704 +            a = b;
  20.705 +            b = tmp;
  20.706 +        }
  20.707 +
  20.708 +        long diff = 0;
  20.709 +        int x = a.intLen;
  20.710 +        int y = b.intLen;
  20.711 +
  20.712 +        // Subtract common parts of both numbers
  20.713 +        while (y>0) {
  20.714 +            x--; y--;
  20.715 +            diff = (a.value[a.offset+ x] & LONG_MASK) -
  20.716 +                (b.value[b.offset+ y] & LONG_MASK) - ((int)-(diff>>32));
  20.717 +            a.value[a.offset+x] = (int)diff;
  20.718 +        }
  20.719 +        // Subtract remainder of longer number
  20.720 +        while (x>0) {
  20.721 +            x--;
  20.722 +            diff = (a.value[a.offset+ x] & LONG_MASK) - ((int)-(diff>>32));
  20.723 +            a.value[a.offset+x] = (int)diff;
  20.724 +        }
  20.725 +
  20.726 +        a.normalize();
  20.727 +        return sign;
  20.728 +    }
  20.729 +
  20.730 +    /**
  20.731 +     * Multiply the contents of two MutableBigInteger objects. The result is
  20.732 +     * placed into MutableBigInteger z. The contents of y are not changed.
  20.733 +     */
  20.734 +    void multiply(MutableBigInteger y, MutableBigInteger z) {
  20.735 +        int xLen = intLen;
  20.736 +        int yLen = y.intLen;
  20.737 +        int newLen = xLen + yLen;
  20.738 +
  20.739 +        // Put z into an appropriate state to receive product
  20.740 +        if (z.value.length < newLen)
  20.741 +            z.value = new int[newLen];
  20.742 +        z.offset = 0;
  20.743 +        z.intLen = newLen;
  20.744 +
  20.745 +        // The first iteration is hoisted out of the loop to avoid extra add
  20.746 +        long carry = 0;
  20.747 +        for (int j=yLen-1, k=yLen+xLen-1; j >= 0; j--, k--) {
  20.748 +                long product = (y.value[j+y.offset] & LONG_MASK) *
  20.749 +                               (value[xLen-1+offset] & LONG_MASK) + carry;
  20.750 +                z.value[k] = (int)product;
  20.751 +                carry = product >>> 32;
  20.752 +        }
  20.753 +        z.value[xLen-1] = (int)carry;
  20.754 +
  20.755 +        // Perform the multiplication word by word
  20.756 +        for (int i = xLen-2; i >= 0; i--) {
  20.757 +            carry = 0;
  20.758 +            for (int j=yLen-1, k=yLen+i; j >= 0; j--, k--) {
  20.759 +                long product = (y.value[j+y.offset] & LONG_MASK) *
  20.760 +                               (value[i+offset] & LONG_MASK) +
  20.761 +                               (z.value[k] & LONG_MASK) + carry;
  20.762 +                z.value[k] = (int)product;
  20.763 +                carry = product >>> 32;
  20.764 +            }
  20.765 +            z.value[i] = (int)carry;
  20.766 +        }
  20.767 +
  20.768 +        // Remove leading zeros from product
  20.769 +        z.normalize();
  20.770 +    }
  20.771 +
  20.772 +    /**
  20.773 +     * Multiply the contents of this MutableBigInteger by the word y. The
  20.774 +     * result is placed into z.
  20.775 +     */
  20.776 +    void mul(int y, MutableBigInteger z) {
  20.777 +        if (y == 1) {
  20.778 +            z.copyValue(this);
  20.779 +            return;
  20.780 +        }
  20.781 +
  20.782 +        if (y == 0) {
  20.783 +            z.clear();
  20.784 +            return;
  20.785 +        }
  20.786 +
  20.787 +        // Perform the multiplication word by word
  20.788 +        long ylong = y & LONG_MASK;
  20.789 +        int[] zval = (z.value.length<intLen+1 ? new int[intLen + 1]
  20.790 +                                              : z.value);
  20.791 +        long carry = 0;
  20.792 +        for (int i = intLen-1; i >= 0; i--) {
  20.793 +            long product = ylong * (value[i+offset] & LONG_MASK) + carry;
  20.794 +            zval[i+1] = (int)product;
  20.795 +            carry = product >>> 32;
  20.796 +        }
  20.797 +
  20.798 +        if (carry == 0) {
  20.799 +            z.offset = 1;
  20.800 +            z.intLen = intLen;
  20.801 +        } else {
  20.802 +            z.offset = 0;
  20.803 +            z.intLen = intLen + 1;
  20.804 +            zval[0] = (int)carry;
  20.805 +        }
  20.806 +        z.value = zval;
  20.807 +    }
  20.808 +
  20.809 +     /**
  20.810 +     * This method is used for division of an n word dividend by a one word
  20.811 +     * divisor. The quotient is placed into quotient. The one word divisor is
  20.812 +     * specified by divisor.
  20.813 +     *
  20.814 +     * @return the remainder of the division is returned.
  20.815 +     *
  20.816 +     */
  20.817 +    int divideOneWord(int divisor, MutableBigInteger quotient) {
  20.818 +        long divisorLong = divisor & LONG_MASK;
  20.819 +
  20.820 +        // Special case of one word dividend
  20.821 +        if (intLen == 1) {
  20.822 +            long dividendValue = value[offset] & LONG_MASK;
  20.823 +            int q = (int) (dividendValue / divisorLong);
  20.824 +            int r = (int) (dividendValue - q * divisorLong);
  20.825 +            quotient.value[0] = q;
  20.826 +            quotient.intLen = (q == 0) ? 0 : 1;
  20.827 +            quotient.offset = 0;
  20.828 +            return r;
  20.829 +        }
  20.830 +
  20.831 +        if (quotient.value.length < intLen)
  20.832 +            quotient.value = new int[intLen];
  20.833 +        quotient.offset = 0;
  20.834 +        quotient.intLen = intLen;
  20.835 +
  20.836 +        // Normalize the divisor
  20.837 +        int shift = Integer.numberOfLeadingZeros(divisor);
  20.838 +
  20.839 +        int rem = value[offset];
  20.840 +        long remLong = rem & LONG_MASK;
  20.841 +        if (remLong < divisorLong) {
  20.842 +            quotient.value[0] = 0;
  20.843 +        } else {
  20.844 +            quotient.value[0] = (int)(remLong / divisorLong);
  20.845 +            rem = (int) (remLong - (quotient.value[0] * divisorLong));
  20.846 +            remLong = rem & LONG_MASK;
  20.847 +        }
  20.848 +
  20.849 +        int xlen = intLen;
  20.850 +        int[] qWord = new int[2];
  20.851 +        while (--xlen > 0) {
  20.852 +            long dividendEstimate = (remLong<<32) |
  20.853 +                (value[offset + intLen - xlen] & LONG_MASK);
  20.854 +            if (dividendEstimate >= 0) {
  20.855 +                qWord[0] = (int) (dividendEstimate / divisorLong);
  20.856 +                qWord[1] = (int) (dividendEstimate - qWord[0] * divisorLong);
  20.857 +            } else {
  20.858 +                divWord(qWord, dividendEstimate, divisor);
  20.859 +            }
  20.860 +            quotient.value[intLen - xlen] = qWord[0];
  20.861 +            rem = qWord[1];
  20.862 +            remLong = rem & LONG_MASK;
  20.863 +        }
  20.864 +
  20.865 +        quotient.normalize();
  20.866 +        // Unnormalize
  20.867 +        if (shift > 0)
  20.868 +            return rem % divisor;
  20.869 +        else
  20.870 +            return rem;
  20.871 +    }
  20.872 +
  20.873 +    /**
  20.874 +     * Calculates the quotient of this div b and places the quotient in the
  20.875 +     * provided MutableBigInteger objects and the remainder object is returned.
  20.876 +     *
  20.877 +     * Uses Algorithm D in Knuth section 4.3.1.
  20.878 +     * Many optimizations to that algorithm have been adapted from the Colin
  20.879 +     * Plumb C library.
  20.880 +     * It special cases one word divisors for speed. The content of b is not
  20.881 +     * changed.
  20.882 +     *
  20.883 +     */
  20.884 +    MutableBigInteger divide(MutableBigInteger b, MutableBigInteger quotient) {
  20.885 +        if (b.intLen == 0)
  20.886 +            throw new ArithmeticException("BigInteger divide by zero");
  20.887 +
  20.888 +        // Dividend is zero
  20.889 +        if (intLen == 0) {
  20.890 +            quotient.intLen = quotient.offset;
  20.891 +            return new MutableBigInteger();
  20.892 +        }
  20.893 +
  20.894 +        int cmp = compare(b);
  20.895 +        // Dividend less than divisor
  20.896 +        if (cmp < 0) {
  20.897 +            quotient.intLen = quotient.offset = 0;
  20.898 +            return new MutableBigInteger(this);
  20.899 +        }
  20.900 +        // Dividend equal to divisor
  20.901 +        if (cmp == 0) {
  20.902 +            quotient.value[0] = quotient.intLen = 1;
  20.903 +            quotient.offset = 0;
  20.904 +            return new MutableBigInteger();
  20.905 +        }
  20.906 +
  20.907 +        quotient.clear();
  20.908 +        // Special case one word divisor
  20.909 +        if (b.intLen == 1) {
  20.910 +            int r = divideOneWord(b.value[b.offset], quotient);
  20.911 +            if (r == 0)
  20.912 +                return new MutableBigInteger();
  20.913 +            return new MutableBigInteger(r);
  20.914 +        }
  20.915 +
  20.916 +        // Copy divisor value to protect divisor
  20.917 +        int[] div = Arrays.copyOfRange(b.value, b.offset, b.offset + b.intLen);
  20.918 +        return divideMagnitude(div, quotient);
  20.919 +    }
  20.920 +
  20.921 +    /**
  20.922 +     * Internally used  to calculate the quotient of this div v and places the
  20.923 +     * quotient in the provided MutableBigInteger object and the remainder is
  20.924 +     * returned.
  20.925 +     *
  20.926 +     * @return the remainder of the division will be returned.
  20.927 +     */
  20.928 +    long divide(long v, MutableBigInteger quotient) {
  20.929 +        if (v == 0)
  20.930 +            throw new ArithmeticException("BigInteger divide by zero");
  20.931 +
  20.932 +        // Dividend is zero
  20.933 +        if (intLen == 0) {
  20.934 +            quotient.intLen = quotient.offset = 0;
  20.935 +            return 0;
  20.936 +        }
  20.937 +        if (v < 0)
  20.938 +            v = -v;
  20.939 +
  20.940 +        int d = (int)(v >>> 32);
  20.941 +        quotient.clear();
  20.942 +        // Special case on word divisor
  20.943 +        if (d == 0)
  20.944 +            return divideOneWord((int)v, quotient) & LONG_MASK;
  20.945 +        else {
  20.946 +            int[] div = new int[]{ d, (int)(v & LONG_MASK) };
  20.947 +            return divideMagnitude(div, quotient).toLong();
  20.948 +        }
  20.949 +    }
  20.950 +
  20.951 +    /**
  20.952 +     * Divide this MutableBigInteger by the divisor represented by its magnitude
  20.953 +     * array. The quotient will be placed into the provided quotient object &
  20.954 +     * the remainder object is returned.
  20.955 +     */
  20.956 +    private MutableBigInteger divideMagnitude(int[] divisor,
  20.957 +                                              MutableBigInteger quotient) {
  20.958 +
  20.959 +        // Remainder starts as dividend with space for a leading zero
  20.960 +        MutableBigInteger rem = new MutableBigInteger(new int[intLen + 1]);
  20.961 +        System.arraycopy(value, offset, rem.value, 1, intLen);
  20.962 +        rem.intLen = intLen;
  20.963 +        rem.offset = 1;
  20.964 +
  20.965 +        int nlen = rem.intLen;
  20.966 +
  20.967 +        // Set the quotient size
  20.968 +        int dlen = divisor.length;
  20.969 +        int limit = nlen - dlen + 1;
  20.970 +        if (quotient.value.length < limit) {
  20.971 +            quotient.value = new int[limit];
  20.972 +            quotient.offset = 0;
  20.973 +        }
  20.974 +        quotient.intLen = limit;
  20.975 +        int[] q = quotient.value;
  20.976 +
  20.977 +        // D1 normalize the divisor
  20.978 +        int shift = Integer.numberOfLeadingZeros(divisor[0]);
  20.979 +        if (shift > 0) {
  20.980 +            // First shift will not grow array
  20.981 +            BigInteger.primitiveLeftShift(divisor, dlen, shift);
  20.982 +            // But this one might
  20.983 +            rem.leftShift(shift);
  20.984 +        }
  20.985 +
  20.986 +        // Must insert leading 0 in rem if its length did not change
  20.987 +        if (rem.intLen == nlen) {
  20.988 +            rem.offset = 0;
  20.989 +            rem.value[0] = 0;
  20.990 +            rem.intLen++;
  20.991 +        }
  20.992 +
  20.993 +        int dh = divisor[0];
  20.994 +        long dhLong = dh & LONG_MASK;
  20.995 +        int dl = divisor[1];
  20.996 +        int[] qWord = new int[2];
  20.997 +
  20.998 +        // D2 Initialize j
  20.999 +        for(int j=0; j<limit; j++) {
 20.1000 +            // D3 Calculate qhat
 20.1001 +            // estimate qhat
 20.1002 +            int qhat = 0;
 20.1003 +            int qrem = 0;
 20.1004 +            boolean skipCorrection = false;
 20.1005 +            int nh = rem.value[j+rem.offset];
 20.1006 +            int nh2 = nh + 0x80000000;
 20.1007 +            int nm = rem.value[j+1+rem.offset];
 20.1008 +
 20.1009 +            if (nh == dh) {
 20.1010 +                qhat = ~0;
 20.1011 +                qrem = nh + nm;
 20.1012 +                skipCorrection = qrem + 0x80000000 < nh2;
 20.1013 +            } else {
 20.1014 +                long nChunk = (((long)nh) << 32) | (nm & LONG_MASK);
 20.1015 +                if (nChunk >= 0) {
 20.1016 +                    qhat = (int) (nChunk / dhLong);
 20.1017 +                    qrem = (int) (nChunk - (qhat * dhLong));
 20.1018 +                } else {
 20.1019 +                    divWord(qWord, nChunk, dh);
 20.1020 +                    qhat = qWord[0];
 20.1021 +                    qrem = qWord[1];
 20.1022 +                }
 20.1023 +            }
 20.1024 +
 20.1025 +            if (qhat == 0)
 20.1026 +                continue;
 20.1027 +
 20.1028 +            if (!skipCorrection) { // Correct qhat
 20.1029 +                long nl = rem.value[j+2+rem.offset] & LONG_MASK;
 20.1030 +                long rs = ((qrem & LONG_MASK) << 32) | nl;
 20.1031 +                long estProduct = (dl & LONG_MASK) * (qhat & LONG_MASK);
 20.1032 +
 20.1033 +                if (unsignedLongCompare(estProduct, rs)) {
 20.1034 +                    qhat--;
 20.1035 +                    qrem = (int)((qrem & LONG_MASK) + dhLong);
 20.1036 +                    if ((qrem & LONG_MASK) >=  dhLong) {
 20.1037 +                        estProduct -= (dl & LONG_MASK);
 20.1038 +                        rs = ((qrem & LONG_MASK) << 32) | nl;
 20.1039 +                        if (unsignedLongCompare(estProduct, rs))
 20.1040 +                            qhat--;
 20.1041 +                    }
 20.1042 +                }
 20.1043 +            }
 20.1044 +
 20.1045 +            // D4 Multiply and subtract
 20.1046 +            rem.value[j+rem.offset] = 0;
 20.1047 +            int borrow = mulsub(rem.value, divisor, qhat, dlen, j+rem.offset);
 20.1048 +
 20.1049 +            // D5 Test remainder
 20.1050 +            if (borrow + 0x80000000 > nh2) {
 20.1051 +                // D6 Add back
 20.1052 +                divadd(divisor, rem.value, j+1+rem.offset);
 20.1053 +                qhat--;
 20.1054 +            }
 20.1055 +
 20.1056 +            // Store the quotient digit
 20.1057 +            q[j] = qhat;
 20.1058 +        } // D7 loop on j
 20.1059 +
 20.1060 +        // D8 Unnormalize
 20.1061 +        if (shift > 0)
 20.1062 +            rem.rightShift(shift);
 20.1063 +
 20.1064 +        quotient.normalize();
 20.1065 +        rem.normalize();
 20.1066 +        return rem;
 20.1067 +    }
 20.1068 +
 20.1069 +    /**
 20.1070 +     * Compare two longs as if they were unsigned.
 20.1071 +     * Returns true iff one is bigger than two.
 20.1072 +     */
 20.1073 +    private boolean unsignedLongCompare(long one, long two) {
 20.1074 +        return (one+Long.MIN_VALUE) > (two+Long.MIN_VALUE);
 20.1075 +    }
 20.1076 +
 20.1077 +    /**
 20.1078 +     * This method divides a long quantity by an int to estimate
 20.1079 +     * qhat for two multi precision numbers. It is used when
 20.1080 +     * the signed value of n is less than zero.
 20.1081 +     */
 20.1082 +    private void divWord(int[] result, long n, int d) {
 20.1083 +        long dLong = d & LONG_MASK;
 20.1084 +
 20.1085 +        if (dLong == 1) {
 20.1086 +            result[0] = (int)n;
 20.1087 +            result[1] = 0;
 20.1088 +            return;
 20.1089 +        }
 20.1090 +
 20.1091 +        // Approximate the quotient and remainder
 20.1092 +        long q = (n >>> 1) / (dLong >>> 1);
 20.1093 +        long r = n - q*dLong;
 20.1094 +
 20.1095 +        // Correct the approximation
 20.1096 +        while (r < 0) {
 20.1097 +            r += dLong;
 20.1098 +            q--;
 20.1099 +        }
 20.1100 +        while (r >= dLong) {
 20.1101 +            r -= dLong;
 20.1102 +            q++;
 20.1103 +        }
 20.1104 +
 20.1105 +        // n - q*dlong == r && 0 <= r <dLong, hence we're done.
 20.1106 +        result[0] = (int)q;
 20.1107 +        result[1] = (int)r;
 20.1108 +    }
 20.1109 +
 20.1110 +    /**
 20.1111 +     * Calculate GCD of this and b. This and b are changed by the computation.
 20.1112 +     */
 20.1113 +    MutableBigInteger hybridGCD(MutableBigInteger b) {
 20.1114 +        // Use Euclid's algorithm until the numbers are approximately the
 20.1115 +        // same length, then use the binary GCD algorithm to find the GCD.
 20.1116 +        MutableBigInteger a = this;
 20.1117 +        MutableBigInteger q = new MutableBigInteger();
 20.1118 +
 20.1119 +        while (b.intLen != 0) {
 20.1120 +            if (Math.abs(a.intLen - b.intLen) < 2)
 20.1121 +                return a.binaryGCD(b);
 20.1122 +
 20.1123 +            MutableBigInteger r = a.divide(b, q);
 20.1124 +            a = b;
 20.1125 +            b = r;
 20.1126 +        }
 20.1127 +        return a;
 20.1128 +    }
 20.1129 +
 20.1130 +    /**
 20.1131 +     * Calculate GCD of this and v.
 20.1132 +     * Assumes that this and v are not zero.
 20.1133 +     */
 20.1134 +    private MutableBigInteger binaryGCD(MutableBigInteger v) {
 20.1135 +        // Algorithm B from Knuth section 4.5.2
 20.1136 +        MutableBigInteger u = this;
 20.1137 +        MutableBigInteger r = new MutableBigInteger();
 20.1138 +
 20.1139 +        // step B1
 20.1140 +        int s1 = u.getLowestSetBit();
 20.1141 +        int s2 = v.getLowestSetBit();
 20.1142 +        int k = (s1 < s2) ? s1 : s2;
 20.1143 +        if (k != 0) {
 20.1144 +            u.rightShift(k);
 20.1145 +            v.rightShift(k);
 20.1146 +        }
 20.1147 +
 20.1148 +        // step B2
 20.1149 +        boolean uOdd = (k==s1);
 20.1150 +        MutableBigInteger t = uOdd ? v: u;
 20.1151 +        int tsign = uOdd ? -1 : 1;
 20.1152 +
 20.1153 +        int lb;
 20.1154 +        while ((lb = t.getLowestSetBit()) >= 0) {
 20.1155 +            // steps B3 and B4
 20.1156 +            t.rightShift(lb);
 20.1157 +            // step B5
 20.1158 +            if (tsign > 0)
 20.1159 +                u = t;
 20.1160 +            else
 20.1161 +                v = t;
 20.1162 +
 20.1163 +            // Special case one word numbers
 20.1164 +            if (u.intLen < 2 && v.intLen < 2) {
 20.1165 +                int x = u.value[u.offset];
 20.1166 +                int y = v.value[v.offset];
 20.1167 +                x  = binaryGcd(x, y);
 20.1168 +                r.value[0] = x;
 20.1169 +                r.intLen = 1;
 20.1170 +                r.offset = 0;
 20.1171 +                if (k > 0)
 20.1172 +                    r.leftShift(k);
 20.1173 +                return r;
 20.1174 +            }
 20.1175 +
 20.1176 +            // step B6
 20.1177 +            if ((tsign = u.difference(v)) == 0)
 20.1178 +                break;
 20.1179 +            t = (tsign >= 0) ? u : v;
 20.1180 +        }
 20.1181 +
 20.1182 +        if (k > 0)
 20.1183 +            u.leftShift(k);
 20.1184 +        return u;
 20.1185 +    }
 20.1186 +
 20.1187 +    /**
 20.1188 +     * Calculate GCD of a and b interpreted as unsigned integers.
 20.1189 +     */
 20.1190 +    static int binaryGcd(int a, int b) {
 20.1191 +        if (b==0)
 20.1192 +            return a;
 20.1193 +        if (a==0)
 20.1194 +            return b;
 20.1195 +
 20.1196 +        // Right shift a & b till their last bits equal to 1.
 20.1197 +        int aZeros = Integer.numberOfTrailingZeros(a);
 20.1198 +        int bZeros = Integer.numberOfTrailingZeros(b);
 20.1199 +        a >>>= aZeros;
 20.1200 +        b >>>= bZeros;
 20.1201 +
 20.1202 +        int t = (aZeros < bZeros ? aZeros : bZeros);
 20.1203 +
 20.1204 +        while (a != b) {
 20.1205 +            if ((a+0x80000000) > (b+0x80000000)) {  // a > b as unsigned
 20.1206 +                a -= b;
 20.1207 +                a >>>= Integer.numberOfTrailingZeros(a);
 20.1208 +            } else {
 20.1209 +                b -= a;
 20.1210 +                b >>>= Integer.numberOfTrailingZeros(b);
 20.1211 +            }
 20.1212 +        }
 20.1213 +        return a<<t;
 20.1214 +    }
 20.1215 +
 20.1216 +    /**
 20.1217 +     * Returns the modInverse of this mod p. This and p are not affected by
 20.1218 +     * the operation.
 20.1219 +     */
 20.1220 +    MutableBigInteger mutableModInverse(MutableBigInteger p) {
 20.1221 +        // Modulus is odd, use Schroeppel's algorithm
 20.1222 +        if (p.isOdd())
 20.1223 +            return modInverse(p);
 20.1224 +
 20.1225 +        // Base and modulus are even, throw exception
 20.1226 +        if (isEven())
 20.1227 +            throw new ArithmeticException("BigInteger not invertible.");
 20.1228 +
 20.1229 +        // Get even part of modulus expressed as a power of 2
 20.1230 +        int powersOf2 = p.getLowestSetBit();
 20.1231 +
 20.1232 +        // Construct odd part of modulus
 20.1233 +        MutableBigInteger oddMod = new MutableBigInteger(p);
 20.1234 +        oddMod.rightShift(powersOf2);
 20.1235 +
 20.1236 +        if (oddMod.isOne())
 20.1237 +            return modInverseMP2(powersOf2);
 20.1238 +
 20.1239 +        // Calculate 1/a mod oddMod
 20.1240 +        MutableBigInteger oddPart = modInverse(oddMod);
 20.1241 +
 20.1242 +        // Calculate 1/a mod evenMod
 20.1243 +        MutableBigInteger evenPart = modInverseMP2(powersOf2);
 20.1244 +
 20.1245 +        // Combine the results using Chinese Remainder Theorem
 20.1246 +        MutableBigInteger y1 = modInverseBP2(oddMod, powersOf2);
 20.1247 +        MutableBigInteger y2 = oddMod.modInverseMP2(powersOf2);
 20.1248 +
 20.1249 +        MutableBigInteger temp1 = new MutableBigInteger();
 20.1250 +        MutableBigInteger temp2 = new MutableBigInteger();
 20.1251 +        MutableBigInteger result = new MutableBigInteger();
 20.1252 +
 20.1253 +        oddPart.leftShift(powersOf2);
 20.1254 +        oddPart.multiply(y1, result);
 20.1255 +
 20.1256 +        evenPart.multiply(oddMod, temp1);
 20.1257 +        temp1.multiply(y2, temp2);
 20.1258 +
 20.1259 +        result.add(temp2);
 20.1260 +        return result.divide(p, temp1);
 20.1261 +    }
 20.1262 +
 20.1263 +    /*
 20.1264 +     * Calculate the multiplicative inverse of this mod 2^k.
 20.1265 +     */
 20.1266 +    MutableBigInteger modInverseMP2(int k) {
 20.1267 +        if (isEven())
 20.1268 +            throw new ArithmeticException("Non-invertible. (GCD != 1)");
 20.1269 +
 20.1270 +        if (k > 64)
 20.1271 +            return euclidModInverse(k);
 20.1272 +
 20.1273 +        int t = inverseMod32(value[offset+intLen-1]);
 20.1274 +
 20.1275 +        if (k < 33) {
 20.1276 +            t = (k == 32 ? t : t & ((1 << k) - 1));
 20.1277 +            return new MutableBigInteger(t);
 20.1278 +        }
 20.1279 +
 20.1280 +        long pLong = (value[offset+intLen-1] & LONG_MASK);
 20.1281 +        if (intLen > 1)
 20.1282 +            pLong |=  ((long)value[offset+intLen-2] << 32);
 20.1283 +        long tLong = t & LONG_MASK;
 20.1284 +        tLong = tLong * (2 - pLong * tLong);  // 1 more Newton iter step
 20.1285 +        tLong = (k == 64 ? tLong : tLong & ((1L << k) - 1));
 20.1286 +
 20.1287 +        MutableBigInteger result = new MutableBigInteger(new int[2]);
 20.1288 +        result.value[0] = (int)(tLong >>> 32);
 20.1289 +        result.value[1] = (int)tLong;
 20.1290 +        result.intLen = 2;
 20.1291 +        result.normalize();
 20.1292 +        return result;
 20.1293 +    }
 20.1294 +
 20.1295 +    /*
 20.1296 +     * Returns the multiplicative inverse of val mod 2^32.  Assumes val is odd.
 20.1297 +     */
 20.1298 +    static int inverseMod32(int val) {
 20.1299 +        // Newton's iteration!
 20.1300 +        int t = val;
 20.1301 +        t *= 2 - val*t;
 20.1302 +        t *= 2 - val*t;
 20.1303 +        t *= 2 - val*t;
 20.1304 +        t *= 2 - val*t;
 20.1305 +        return t;
 20.1306 +    }
 20.1307 +
 20.1308 +    /*
 20.1309 +     * Calculate the multiplicative inverse of 2^k mod mod, where mod is odd.
 20.1310 +     */
 20.1311 +    static MutableBigInteger modInverseBP2(MutableBigInteger mod, int k) {
 20.1312 +        // Copy the mod to protect original
 20.1313 +        return fixup(new MutableBigInteger(1), new MutableBigInteger(mod), k);
 20.1314 +    }
 20.1315 +
 20.1316 +    /**
 20.1317 +     * Calculate the multiplicative inverse of this mod mod, where mod is odd.
 20.1318 +     * This and mod are not changed by the calculation.
 20.1319 +     *
 20.1320 +     * This method implements an algorithm due to Richard Schroeppel, that uses
 20.1321 +     * the same intermediate representation as Montgomery Reduction
 20.1322 +     * ("Montgomery Form").  The algorithm is described in an unpublished
 20.1323 +     * manuscript entitled "Fast Modular Reciprocals."
 20.1324 +     */
 20.1325 +    private MutableBigInteger modInverse(MutableBigInteger mod) {
 20.1326 +        MutableBigInteger p = new MutableBigInteger(mod);
 20.1327 +        MutableBigInteger f = new MutableBigInteger(this);
 20.1328 +        MutableBigInteger g = new MutableBigInteger(p);
 20.1329 +        SignedMutableBigInteger c = new SignedMutableBigInteger(1);
 20.1330 +        SignedMutableBigInteger d = new SignedMutableBigInteger();
 20.1331 +        MutableBigInteger temp = null;
 20.1332 +        SignedMutableBigInteger sTemp = null;
 20.1333 +
 20.1334 +        int k = 0;
 20.1335 +        // Right shift f k times until odd, left shift d k times
 20.1336 +        if (f.isEven()) {
 20.1337 +            int trailingZeros = f.getLowestSetBit();
 20.1338 +            f.rightShift(trailingZeros);
 20.1339 +            d.leftShift(trailingZeros);
 20.1340 +            k = trailingZeros;
 20.1341 +        }
 20.1342 +
 20.1343 +        // The Almost Inverse Algorithm
 20.1344 +        while(!f.isOne()) {
 20.1345 +            // If gcd(f, g) != 1, number is not invertible modulo mod
 20.1346 +            if (f.isZero())
 20.1347 +                throw new ArithmeticException("BigInteger not invertible.");
 20.1348 +
 20.1349 +            // If f < g exchange f, g and c, d
 20.1350 +            if (f.compare(g) < 0) {
 20.1351 +                temp = f; f = g; g = temp;
 20.1352 +                sTemp = d; d = c; c = sTemp;
 20.1353 +            }
 20.1354 +
 20.1355 +            // If f == g (mod 4)
 20.1356 +            if (((f.value[f.offset + f.intLen - 1] ^
 20.1357 +                 g.value[g.offset + g.intLen - 1]) & 3) == 0) {
 20.1358 +                f.subtract(g);
 20.1359 +                c.signedSubtract(d);
 20.1360 +            } else { // If f != g (mod 4)
 20.1361 +                f.add(g);
 20.1362 +                c.signedAdd(d);
 20.1363 +            }
 20.1364 +
 20.1365 +            // Right shift f k times until odd, left shift d k times
 20.1366 +            int trailingZeros = f.getLowestSetBit();
 20.1367 +            f.rightShift(trailingZeros);
 20.1368 +            d.leftShift(trailingZeros);
 20.1369 +            k += trailingZeros;
 20.1370 +        }
 20.1371 +
 20.1372 +        while (c.sign < 0)
 20.1373 +           c.signedAdd(p);
 20.1374 +
 20.1375 +        return fixup(c, p, k);
 20.1376 +    }
 20.1377 +
 20.1378 +    /*
 20.1379 +     * The Fixup Algorithm
 20.1380 +     * Calculates X such that X = C * 2^(-k) (mod P)
 20.1381 +     * Assumes C<P and P is odd.
 20.1382 +     */
 20.1383 +    static MutableBigInteger fixup(MutableBigInteger c, MutableBigInteger p,
 20.1384 +                                                                      int k) {
 20.1385 +        MutableBigInteger temp = new MutableBigInteger();
 20.1386 +        // Set r to the multiplicative inverse of p mod 2^32
 20.1387 +        int r = -inverseMod32(p.value[p.offset+p.intLen-1]);
 20.1388 +
 20.1389 +        for(int i=0, numWords = k >> 5; i<numWords; i++) {
 20.1390 +            // V = R * c (mod 2^j)
 20.1391 +            int  v = r * c.value[c.offset + c.intLen-1];
 20.1392 +            // c = c + (v * p)
 20.1393 +            p.mul(v, temp);
 20.1394 +            c.add(temp);
 20.1395 +            // c = c / 2^j
 20.1396 +            c.intLen--;
 20.1397 +        }
 20.1398 +        int numBits = k & 0x1f;
 20.1399 +        if (numBits != 0) {
 20.1400 +            // V = R * c (mod 2^j)
 20.1401 +            int v = r * c.value[c.offset + c.intLen-1];
 20.1402 +            v &= ((1<<numBits) - 1);
 20.1403 +            // c = c + (v * p)
 20.1404 +            p.mul(v, temp);
 20.1405 +            c.add(temp);
 20.1406 +            // c = c / 2^j
 20.1407 +            c.rightShift(numBits);
 20.1408 +        }
 20.1409 +
 20.1410 +        // In theory, c may be greater than p at this point (Very rare!)
 20.1411 +        while (c.compare(p) >= 0)
 20.1412 +            c.subtract(p);
 20.1413 +
 20.1414 +        return c;
 20.1415 +    }
 20.1416 +
 20.1417 +    /**
 20.1418 +     * Uses the extended Euclidean algorithm to compute the modInverse of base
 20.1419 +     * mod a modulus that is a power of 2. The modulus is 2^k.
 20.1420 +     */
 20.1421 +    MutableBigInteger euclidModInverse(int k) {
 20.1422 +        MutableBigInteger b = new MutableBigInteger(1);
 20.1423 +        b.leftShift(k);
 20.1424 +        MutableBigInteger mod = new MutableBigInteger(b);
 20.1425 +
 20.1426 +        MutableBigInteger a = new MutableBigInteger(this);
 20.1427 +        MutableBigInteger q = new MutableBigInteger();
 20.1428 +        MutableBigInteger r = b.divide(a, q);
 20.1429 +
 20.1430 +        MutableBigInteger swapper = b;
 20.1431 +        // swap b & r
 20.1432 +        b = r;
 20.1433 +        r = swapper;
 20.1434 +
 20.1435 +        MutableBigInteger t1 = new MutableBigInteger(q);
 20.1436 +        MutableBigInteger t0 = new MutableBigInteger(1);
 20.1437 +        MutableBigInteger temp = new MutableBigInteger();
 20.1438 +
 20.1439 +        while (!b.isOne()) {
 20.1440 +            r = a.divide(b, q);
 20.1441 +
 20.1442 +            if (r.intLen == 0)
 20.1443 +                throw new ArithmeticException("BigInteger not invertible.");
 20.1444 +
 20.1445 +            swapper = r;
 20.1446 +            a = swapper;
 20.1447 +
 20.1448 +            if (q.intLen == 1)
 20.1449 +                t1.mul(q.value[q.offset], temp);
 20.1450 +            else
 20.1451 +                q.multiply(t1, temp);
 20.1452 +            swapper = q;
 20.1453 +            q = temp;
 20.1454 +            temp = swapper;
 20.1455 +            t0.add(q);
 20.1456 +
 20.1457 +            if (a.isOne())
 20.1458 +                return t0;
 20.1459 +
 20.1460 +            r = b.divide(a, q);
 20.1461 +
 20.1462 +            if (r.intLen == 0)
 20.1463 +                throw new ArithmeticException("BigInteger not invertible.");
 20.1464 +
 20.1465 +            swapper = b;
 20.1466 +            b =  r;
 20.1467 +
 20.1468 +            if (q.intLen == 1)
 20.1469 +                t0.mul(q.value[q.offset], temp);
 20.1470 +            else
 20.1471 +                q.multiply(t0, temp);
 20.1472 +            swapper = q; q = temp; temp = swapper;
 20.1473 +
 20.1474 +            t1.add(q);
 20.1475 +        }
 20.1476 +        mod.subtract(t1);
 20.1477 +        return mod;
 20.1478 +    }
 20.1479 +
 20.1480 +}
    21.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    21.2 +++ b/rt/emul/compact/src/main/java/java/math/RoundingMode.java	Sun Sep 08 11:22:51 2013 +0200
    21.3 @@ -0,0 +1,349 @@
    21.4 +/*
    21.5 + * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
    21.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    21.7 + *
    21.8 + * This code is free software; you can redistribute it and/or modify it
    21.9 + * under the terms of the GNU General Public License version 2 only, as
   21.10 + * published by the Free Software Foundation.  Oracle designates this
   21.11 + * particular file as subject to the "Classpath" exception as provided
   21.12 + * by Oracle in the LICENSE file that accompanied this code.
   21.13 + *
   21.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   21.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   21.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   21.17 + * version 2 for more details (a copy is included in the LICENSE file that
   21.18 + * accompanied this code).
   21.19 + *
   21.20 + * You should have received a copy of the GNU General Public License version
   21.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   21.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   21.23 + *
   21.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   21.25 + * or visit www.oracle.com if you need additional information or have any
   21.26 + * questions.
   21.27 + */
   21.28 +
   21.29 +/*
   21.30 + * Portions Copyright IBM Corporation, 2001. All Rights Reserved.
   21.31 + */
   21.32 +package java.math;
   21.33 +
   21.34 +/**
   21.35 + * Specifies a <i>rounding behavior</i> for numerical operations
   21.36 + * capable of discarding precision. Each rounding mode indicates how
   21.37 + * the least significant returned digit of a rounded result is to be
   21.38 + * calculated.  If fewer digits are returned than the digits needed to
   21.39 + * represent the exact numerical result, the discarded digits will be
   21.40 + * referred to as the <i>discarded fraction</i> regardless the digits'
   21.41 + * contribution to the value of the number.  In other words,
   21.42 + * considered as a numerical value, the discarded fraction could have
   21.43 + * an absolute value greater than one.
   21.44 + *
   21.45 + * <p>Each rounding mode description includes a table listing how
   21.46 + * different two-digit decimal values would round to a one digit
   21.47 + * decimal value under the rounding mode in question.  The result
   21.48 + * column in the tables could be gotten by creating a
   21.49 + * {@code BigDecimal} number with the specified value, forming a
   21.50 + * {@link MathContext} object with the proper settings
   21.51 + * ({@code precision} set to {@code 1}, and the
   21.52 + * {@code roundingMode} set to the rounding mode in question), and
   21.53 + * calling {@link BigDecimal#round round} on this number with the
   21.54 + * proper {@code MathContext}.  A summary table showing the results
   21.55 + * of these rounding operations for all rounding modes appears below.
   21.56 + *
   21.57 + *<p>
   21.58 + *<table border>
   21.59 + * <caption><b>Summary of Rounding Operations Under Different Rounding Modes</b></caption>
   21.60 + * <tr><th></th><th colspan=8>Result of rounding input to one digit with the given
   21.61 + *                           rounding mode</th>
   21.62 + * <tr valign=top>
   21.63 + * <th>Input Number</th>         <th>{@code UP}</th>
   21.64 + *                                           <th>{@code DOWN}</th>
   21.65 + *                                                        <th>{@code CEILING}</th>
   21.66 + *                                                                       <th>{@code FLOOR}</th>
   21.67 + *                                                                                    <th>{@code HALF_UP}</th>
   21.68 + *                                                                                                   <th>{@code HALF_DOWN}</th>
   21.69 + *                                                                                                                    <th>{@code HALF_EVEN}</th>
   21.70 + *                                                                                                                                     <th>{@code UNNECESSARY}</th>
   21.71 + *
   21.72 + * <tr align=right><td>5.5</td>  <td>6</td>  <td>5</td>    <td>6</td>    <td>5</td>  <td>6</td>      <td>5</td>       <td>6</td>       <td>throw {@code ArithmeticException}</td>
   21.73 + * <tr align=right><td>2.5</td>  <td>3</td>  <td>2</td>    <td>3</td>    <td>2</td>  <td>3</td>      <td>2</td>       <td>2</td>       <td>throw {@code ArithmeticException}</td>
   21.74 + * <tr align=right><td>1.6</td>  <td>2</td>  <td>1</td>    <td>2</td>    <td>1</td>  <td>2</td>      <td>2</td>       <td>2</td>       <td>throw {@code ArithmeticException}</td>
   21.75 + * <tr align=right><td>1.1</td>  <td>2</td>  <td>1</td>    <td>2</td>    <td>1</td>  <td>1</td>      <td>1</td>       <td>1</td>       <td>throw {@code ArithmeticException}</td>
   21.76 + * <tr align=right><td>1.0</td>  <td>1</td>  <td>1</td>    <td>1</td>    <td>1</td>  <td>1</td>      <td>1</td>       <td>1</td>       <td>1</td>
   21.77 + * <tr align=right><td>-1.0</td> <td>-1</td> <td>-1</td>   <td>-1</td>   <td>-1</td> <td>-1</td>     <td>-1</td>      <td>-1</td>      <td>-1</td>
   21.78 + * <tr align=right><td>-1.1</td> <td>-2</td> <td>-1</td>   <td>-1</td>   <td>-2</td> <td>-1</td>     <td>-1</td>      <td>-1</td>      <td>throw {@code ArithmeticException}</td>
   21.79 + * <tr align=right><td>-1.6</td> <td>-2</td> <td>-1</td>   <td>-1</td>   <td>-2</td> <td>-2</td>     <td>-2</td>      <td>-2</td>      <td>throw {@code ArithmeticException}</td>
   21.80 + * <tr align=right><td>-2.5</td> <td>-3</td> <td>-2</td>   <td>-2</td>   <td>-3</td> <td>-3</td>     <td>-2</td>      <td>-2</td>      <td>throw {@code ArithmeticException}</td>
   21.81 + * <tr align=right><td>-5.5</td> <td>-6</td> <td>-5</td>   <td>-5</td>   <td>-6</td> <td>-6</td>     <td>-5</td>      <td>-6</td>      <td>throw {@code ArithmeticException}</td>
   21.82 + *</table>
   21.83 + *
   21.84 + *
   21.85 + * <p>This {@code enum} is intended to replace the integer-based
   21.86 + * enumeration of rounding mode constants in {@link BigDecimal}
   21.87 + * ({@link BigDecimal#ROUND_UP}, {@link BigDecimal#ROUND_DOWN},
   21.88 + * etc. ).
   21.89 + *
   21.90 + * @see     BigDecimal
   21.91 + * @see     MathContext
   21.92 + * @author  Josh Bloch
   21.93 + * @author  Mike Cowlishaw
   21.94 + * @author  Joseph D. Darcy
   21.95 + * @since 1.5
   21.96 + */
   21.97 +public enum RoundingMode {
   21.98 +
   21.99 +        /**
  21.100 +         * Rounding mode to round away from zero.  Always increments the
  21.101 +         * digit prior to a non-zero discarded fraction.  Note that this
  21.102 +         * rounding mode never decreases the magnitude of the calculated
  21.103 +         * value.
  21.104 +         *
  21.105 +         *<p>Example:
  21.106 +         *<table border>
  21.107 +         *<tr valign=top><th>Input Number</th>
  21.108 +         *    <th>Input rounded to one digit<br> with {@code UP} rounding
  21.109 +         *<tr align=right><td>5.5</td>  <td>6</td>
  21.110 +         *<tr align=right><td>2.5</td>  <td>3</td>
  21.111 +         *<tr align=right><td>1.6</td>  <td>2</td>
  21.112 +         *<tr align=right><td>1.1</td>  <td>2</td>
  21.113 +         *<tr align=right><td>1.0</td>  <td>1</td>
  21.114 +         *<tr align=right><td>-1.0</td> <td>-1</td>
  21.115 +         *<tr align=right><td>-1.1</td> <td>-2</td>
  21.116 +         *<tr align=right><td>-1.6</td> <td>-2</td>
  21.117 +         *<tr align=right><td>-2.5</td> <td>-3</td>
  21.118 +         *<tr align=right><td>-5.5</td> <td>-6</td>
  21.119 +         *</table>
  21.120 +         */
  21.121 +    UP(BigDecimal.ROUND_UP),
  21.122 +
  21.123 +        /**
  21.124 +         * Rounding mode to round towards zero.  Never increments the digit
  21.125 +         * prior to a discarded fraction (i.e., truncates).  Note that this
  21.126 +         * rounding mode never increases the magnitude of the calculated value.
  21.127 +         *
  21.128 +         *<p>Example:
  21.129 +         *<table border>
  21.130 +         *<tr valign=top><th>Input Number</th>
  21.131 +         *    <th>Input rounded to one digit<br> with {@code DOWN} rounding
  21.132 +         *<tr align=right><td>5.5</td>  <td>5</td>
  21.133 +         *<tr align=right><td>2.5</td>  <td>2</td>
  21.134 +         *<tr align=right><td>1.6</td>  <td>1</td>
  21.135 +         *<tr align=right><td>1.1</td>  <td>1</td>
  21.136 +         *<tr align=right><td>1.0</td>  <td>1</td>
  21.137 +         *<tr align=right><td>-1.0</td> <td>-1</td>
  21.138 +         *<tr align=right><td>-1.1</td> <td>-1</td>
  21.139 +         *<tr align=right><td>-1.6</td> <td>-1</td>
  21.140 +         *<tr align=right><td>-2.5</td> <td>-2</td>
  21.141 +         *<tr align=right><td>-5.5</td> <td>-5</td>
  21.142 +         *</table>
  21.143 +         */
  21.144 +    DOWN(BigDecimal.ROUND_DOWN),
  21.145 +
  21.146 +        /**
  21.147 +         * Rounding mode to round towards positive infinity.  If the
  21.148 +         * result is positive, behaves as for {@code RoundingMode.UP};
  21.149 +         * if negative, behaves as for {@code RoundingMode.DOWN}.  Note
  21.150 +         * that this rounding mode never decreases the calculated value.
  21.151 +         *
  21.152 +         *<p>Example:
  21.153 +         *<table border>
  21.154 +         *<tr valign=top><th>Input Number</th>
  21.155 +         *    <th>Input rounded to one digit<br> with {@code CEILING} rounding
  21.156 +         *<tr align=right><td>5.5</td>  <td>6</td>
  21.157 +         *<tr align=right><td>2.5</td>  <td>3</td>
  21.158 +         *<tr align=right><td>1.6</td>  <td>2</td>
  21.159 +         *<tr align=right><td>1.1</td>  <td>2</td>
  21.160 +         *<tr align=right><td>1.0</td>  <td>1</td>
  21.161 +         *<tr align=right><td>-1.0</td> <td>-1</td>
  21.162 +         *<tr align=right><td>-1.1</td> <td>-1</td>
  21.163 +         *<tr align=right><td>-1.6</td> <td>-1</td>
  21.164 +         *<tr align=right><td>-2.5</td> <td>-2</td>
  21.165 +         *<tr align=right><td>-5.5</td> <td>-5</td>
  21.166 +         *</table>
  21.167 +         */
  21.168 +    CEILING(BigDecimal.ROUND_CEILING),
  21.169 +
  21.170 +        /**
  21.171 +         * Rounding mode to round towards negative infinity.  If the
  21.172 +         * result is positive, behave as for {@code RoundingMode.DOWN};
  21.173 +         * if negative, behave as for {@code RoundingMode.UP}.  Note that
  21.174 +         * this rounding mode never increases the calculated value.
  21.175 +         *
  21.176 +         *<p>Example:
  21.177 +         *<table border>
  21.178 +         *<tr valign=top><th>Input Number</th>
  21.179 +         *    <th>Input rounded to one digit<br> with {@code FLOOR} rounding
  21.180 +         *<tr align=right><td>5.5</td>  <td>5</td>
  21.181 +         *<tr align=right><td>2.5</td>  <td>2</td>
  21.182 +         *<tr align=right><td>1.6</td>  <td>1</td>
  21.183 +         *<tr align=right><td>1.1</td>  <td>1</td>
  21.184 +         *<tr align=right><td>1.0</td>  <td>1</td>
  21.185 +         *<tr align=right><td>-1.0</td> <td>-1</td>
  21.186 +         *<tr align=right><td>-1.1</td> <td>-2</td>
  21.187 +         *<tr align=right><td>-1.6</td> <td>-2</td>
  21.188 +         *<tr align=right><td>-2.5</td> <td>-3</td>
  21.189 +         *<tr align=right><td>-5.5</td> <td>-6</td>
  21.190 +         *</table>
  21.191 +         */
  21.192 +    FLOOR(BigDecimal.ROUND_FLOOR),
  21.193 +
  21.194 +        /**
  21.195 +         * Rounding mode to round towards {@literal "nearest neighbor"}
  21.196 +         * unless both neighbors are equidistant, in which case round up.
  21.197 +         * Behaves as for {@code RoundingMode.UP} if the discarded
  21.198 +         * fraction is &ge; 0.5; otherwise, behaves as for
  21.199 +         * {@code RoundingMode.DOWN}.  Note that this is the rounding
  21.200 +         * mode commonly taught at school.
  21.201 +         *
  21.202 +         *<p>Example:
  21.203 +         *<table border>
  21.204 +         *<tr valign=top><th>Input Number</th>
  21.205 +         *    <th>Input rounded to one digit<br> with {@code HALF_UP} rounding
  21.206 +         *<tr align=right><td>5.5</td>  <td>6</td>
  21.207 +         *<tr align=right><td>2.5</td>  <td>3</td>
  21.208 +         *<tr align=right><td>1.6</td>  <td>2</td>
  21.209 +         *<tr align=right><td>1.1</td>  <td>1</td>
  21.210 +         *<tr align=right><td>1.0</td>  <td>1</td>
  21.211 +         *<tr align=right><td>-1.0</td> <td>-1</td>
  21.212 +         *<tr align=right><td>-1.1</td> <td>-1</td>
  21.213 +         *<tr align=right><td>-1.6</td> <td>-2</td>
  21.214 +         *<tr align=right><td>-2.5</td> <td>-3</td>
  21.215 +         *<tr align=right><td>-5.5</td> <td>-6</td>
  21.216 +         *</table>
  21.217 +         */
  21.218 +    HALF_UP(BigDecimal.ROUND_HALF_UP),
  21.219 +
  21.220 +        /**
  21.221 +         * Rounding mode to round towards {@literal "nearest neighbor"}
  21.222 +         * unless both neighbors are equidistant, in which case round
  21.223 +         * down.  Behaves as for {@code RoundingMode.UP} if the discarded
  21.224 +         * fraction is &gt; 0.5; otherwise, behaves as for
  21.225 +         * {@code RoundingMode.DOWN}.
  21.226 +         *
  21.227 +         *<p>Example:
  21.228 +         *<table border>
  21.229 +         *<tr valign=top><th>Input Number</th>
  21.230 +         *    <th>Input rounded to one digit<br> with {@code HALF_DOWN} rounding
  21.231 +         *<tr align=right><td>5.5</td>  <td>5</td>
  21.232 +         *<tr align=right><td>2.5</td>  <td>2</td>
  21.233 +         *<tr align=right><td>1.6</td>  <td>2</td>
  21.234 +         *<tr align=right><td>1.1</td>  <td>1</td>
  21.235 +         *<tr align=right><td>1.0</td>  <td>1</td>
  21.236 +         *<tr align=right><td>-1.0</td> <td>-1</td>
  21.237 +         *<tr align=right><td>-1.1</td> <td>-1</td>
  21.238 +         *<tr align=right><td>-1.6</td> <td>-2</td>
  21.239 +         *<tr align=right><td>-2.5</td> <td>-2</td>
  21.240 +         *<tr align=right><td>-5.5</td> <td>-5</td>
  21.241 +         *</table>
  21.242 +         */
  21.243 +    HALF_DOWN(BigDecimal.ROUND_HALF_DOWN),
  21.244 +
  21.245 +        /**
  21.246 +         * Rounding mode to round towards the {@literal "nearest neighbor"}
  21.247 +         * unless both neighbors are equidistant, in which case, round
  21.248 +         * towards the even neighbor.  Behaves as for
  21.249 +         * {@code RoundingMode.HALF_UP} if the digit to the left of the
  21.250 +         * discarded fraction is odd; behaves as for
  21.251 +         * {@code RoundingMode.HALF_DOWN} if it's even.  Note that this
  21.252 +         * is the rounding mode that statistically minimizes cumulative
  21.253 +         * error when applied repeatedly over a sequence of calculations.
  21.254 +         * It is sometimes known as {@literal "Banker's rounding,"} and is
  21.255 +         * chiefly used in the USA.  This rounding mode is analogous to
  21.256 +         * the rounding policy used for {@code float} and {@code double}
  21.257 +         * arithmetic in Java.
  21.258 +         *
  21.259 +         *<p>Example:
  21.260 +         *<table border>
  21.261 +         *<tr valign=top><th>Input Number</th>
  21.262 +         *    <th>Input rounded to one digit<br> with {@code HALF_EVEN} rounding
  21.263 +         *<tr align=right><td>5.5</td>  <td>6</td>
  21.264 +         *<tr align=right><td>2.5</td>  <td>2</td>
  21.265 +         *<tr align=right><td>1.6</td>  <td>2</td>
  21.266 +         *<tr align=right><td>1.1</td>  <td>1</td>
  21.267 +         *<tr align=right><td>1.0</td>  <td>1</td>
  21.268 +         *<tr align=right><td>-1.0</td> <td>-1</td>
  21.269 +         *<tr align=right><td>-1.1</td> <td>-1</td>
  21.270 +         *<tr align=right><td>-1.6</td> <td>-2</td>
  21.271 +         *<tr align=right><td>-2.5</td> <td>-2</td>
  21.272 +         *<tr align=right><td>-5.5</td> <td>-6</td>
  21.273 +         *</table>
  21.274 +         */
  21.275 +    HALF_EVEN(BigDecimal.ROUND_HALF_EVEN),
  21.276 +
  21.277 +        /**
  21.278 +         * Rounding mode to assert that the requested operation has an exact
  21.279 +         * result, hence no rounding is necessary.  If this rounding mode is
  21.280 +         * specified on an operation that yields an inexact result, an
  21.281 +         * {@code ArithmeticException} is thrown.
  21.282 +         *<p>Example:
  21.283 +         *<table border>
  21.284 +         *<tr valign=top><th>Input Number</th>
  21.285 +         *    <th>Input rounded to one digit<br> with {@code UNNECESSARY} rounding
  21.286 +         *<tr align=right><td>5.5</td>  <td>throw {@code ArithmeticException}</td>
  21.287 +         *<tr align=right><td>2.5</td>  <td>throw {@code ArithmeticException}</td>
  21.288 +         *<tr align=right><td>1.6</td>  <td>throw {@code ArithmeticException}</td>
  21.289 +         *<tr align=right><td>1.1</td>  <td>throw {@code ArithmeticException}</td>
  21.290 +         *<tr align=right><td>1.0</td>  <td>1</td>
  21.291 +         *<tr align=right><td>-1.0</td> <td>-1</td>
  21.292 +         *<tr align=right><td>-1.1</td> <td>throw {@code ArithmeticException}</td>
  21.293 +         *<tr align=right><td>-1.6</td> <td>throw {@code ArithmeticException}</td>
  21.294 +         *<tr align=right><td>-2.5</td> <td>throw {@code ArithmeticException}</td>
  21.295 +         *<tr align=right><td>-5.5</td> <td>throw {@code ArithmeticException}</td>
  21.296 +         *</table>
  21.297 +         */
  21.298 +    UNNECESSARY(BigDecimal.ROUND_UNNECESSARY);
  21.299 +
  21.300 +    // Corresponding BigDecimal rounding constant
  21.301 +    final int oldMode;
  21.302 +
  21.303 +    /**
  21.304 +     * Constructor
  21.305 +     *
  21.306 +     * @param oldMode The {@code BigDecimal} constant corresponding to
  21.307 +     *        this mode
  21.308 +     */
  21.309 +    private RoundingMode(int oldMode) {
  21.310 +        this.oldMode = oldMode;
  21.311 +    }
  21.312 +
  21.313 +    /**
  21.314 +     * Returns the {@code RoundingMode} object corresponding to a
  21.315 +     * legacy integer rounding mode constant in {@link BigDecimal}.
  21.316 +     *
  21.317 +     * @param  rm legacy integer rounding mode to convert
  21.318 +     * @return {@code RoundingMode} corresponding to the given integer.
  21.319 +     * @throws IllegalArgumentException integer is out of range
  21.320 +     */
  21.321 +    public static RoundingMode valueOf(int rm) {
  21.322 +        switch(rm) {
  21.323 +
  21.324 +        case BigDecimal.ROUND_UP:
  21.325 +            return UP;
  21.326 +
  21.327 +        case BigDecimal.ROUND_DOWN:
  21.328 +            return DOWN;
  21.329 +
  21.330 +        case BigDecimal.ROUND_CEILING:
  21.331 +            return CEILING;
  21.332 +
  21.333 +        case BigDecimal.ROUND_FLOOR:
  21.334 +            return FLOOR;
  21.335 +
  21.336 +        case BigDecimal.ROUND_HALF_UP:
  21.337 +            return HALF_UP;
  21.338 +
  21.339 +        case BigDecimal.ROUND_HALF_DOWN:
  21.340 +            return HALF_DOWN;
  21.341 +
  21.342 +        case BigDecimal.ROUND_HALF_EVEN:
  21.343 +            return HALF_EVEN;
  21.344 +
  21.345 +        case BigDecimal.ROUND_UNNECESSARY:
  21.346 +            return UNNECESSARY;
  21.347 +
  21.348 +        default:
  21.349 +            throw new IllegalArgumentException("argument out of range");
  21.350 +        }
  21.351 +    }
  21.352 +}
    22.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    22.2 +++ b/rt/emul/compact/src/main/java/java/math/SignedMutableBigInteger.java	Sun Sep 08 11:22:51 2013 +0200
    22.3 @@ -0,0 +1,135 @@
    22.4 +/*
    22.5 + * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
    22.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    22.7 + *
    22.8 + * This code is free software; you can redistribute it and/or modify it
    22.9 + * under the terms of the GNU General Public License version 2 only, as
   22.10 + * published by the Free Software Foundation.  Oracle designates this
   22.11 + * particular file as subject to the "Classpath" exception as provided
   22.12 + * by Oracle in the LICENSE file that accompanied this code.
   22.13 + *
   22.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   22.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   22.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   22.17 + * version 2 for more details (a copy is included in the LICENSE file that
   22.18 + * accompanied this code).
   22.19 + *
   22.20 + * You should have received a copy of the GNU General Public License version
   22.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   22.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   22.23 + *
   22.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   22.25 + * or visit www.oracle.com if you need additional information or have any
   22.26 + * questions.
   22.27 + */
   22.28 +
   22.29 +package java.math;
   22.30 +
   22.31 +/**
   22.32 + * A class used to represent multiprecision integers that makes efficient
   22.33 + * use of allocated space by allowing a number to occupy only part of
   22.34 + * an array so that the arrays do not have to be reallocated as often.
   22.35 + * When performing an operation with many iterations the array used to
   22.36 + * hold a number is only increased when necessary and does not have to
   22.37 + * be the same size as the number it represents. A mutable number allows
   22.38 + * calculations to occur on the same number without having to create
   22.39 + * a new number for every step of the calculation as occurs with
   22.40 + * BigIntegers.
   22.41 + *
   22.42 + * Note that SignedMutableBigIntegers only support signed addition and
   22.43 + * subtraction. All other operations occur as with MutableBigIntegers.
   22.44 + *
   22.45 + * @see     BigInteger
   22.46 + * @author  Michael McCloskey
   22.47 + * @since   1.3
   22.48 + */
   22.49 +
   22.50 +class SignedMutableBigInteger extends MutableBigInteger {
   22.51 +
   22.52 +   /**
   22.53 +     * The sign of this MutableBigInteger.
   22.54 +     */
   22.55 +    int sign = 1;
   22.56 +
   22.57 +    // Constructors
   22.58 +
   22.59 +    /**
   22.60 +     * The default constructor. An empty MutableBigInteger is created with
   22.61 +     * a one word capacity.
   22.62 +     */
   22.63 +    SignedMutableBigInteger() {
   22.64 +        super();
   22.65 +    }
   22.66 +
   22.67 +    /**
   22.68 +     * Construct a new MutableBigInteger with a magnitude specified by
   22.69 +     * the int val.
   22.70 +     */
   22.71 +    SignedMutableBigInteger(int val) {
   22.72 +        super(val);
   22.73 +    }
   22.74 +
   22.75 +    /**
   22.76 +     * Construct a new MutableBigInteger with a magnitude equal to the
   22.77 +     * specified MutableBigInteger.
   22.78 +     */
   22.79 +    SignedMutableBigInteger(MutableBigInteger val) {
   22.80 +        super(val);
   22.81 +    }
   22.82 +
   22.83 +   // Arithmetic Operations
   22.84 +
   22.85 +   /**
   22.86 +     * Signed addition built upon unsigned add and subtract.
   22.87 +     */
   22.88 +    void signedAdd(SignedMutableBigInteger addend) {
   22.89 +        if (sign == addend.sign)
   22.90 +            add(addend);
   22.91 +        else
   22.92 +            sign = sign * subtract(addend);
   22.93 +
   22.94 +    }
   22.95 +
   22.96 +   /**
   22.97 +     * Signed addition built upon unsigned add and subtract.
   22.98 +     */
   22.99 +    void signedAdd(MutableBigInteger addend) {
  22.100 +        if (sign == 1)
  22.101 +            add(addend);
  22.102 +        else
  22.103 +            sign = sign * subtract(addend);
  22.104 +
  22.105 +    }
  22.106 +
  22.107 +   /**
  22.108 +     * Signed subtraction built upon unsigned add and subtract.
  22.109 +     */
  22.110 +    void signedSubtract(SignedMutableBigInteger addend) {
  22.111 +        if (sign == addend.sign)
  22.112 +            sign = sign * subtract(addend);
  22.113 +        else
  22.114 +            add(addend);
  22.115 +
  22.116 +    }
  22.117 +
  22.118 +   /**
  22.119 +     * Signed subtraction built upon unsigned add and subtract.
  22.120 +     */
  22.121 +    void signedSubtract(MutableBigInteger addend) {
  22.122 +        if (sign == 1)
  22.123 +            sign = sign * subtract(addend);
  22.124 +        else
  22.125 +            add(addend);
  22.126 +        if (intLen == 0)
  22.127 +             sign = 1;
  22.128 +    }
  22.129 +
  22.130 +    /**
  22.131 +     * Print out the first intLen ints of this MutableBigInteger's value
  22.132 +     * array starting at offset.
  22.133 +     */
  22.134 +    public String toString() {
  22.135 +        return this.toBigInteger(sign).toString();
  22.136 +    }
  22.137 +
  22.138 +}
    23.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    23.2 +++ b/rt/emul/compact/src/main/java/java/math/package-info.java	Sun Sep 08 11:22:51 2013 +0200
    23.3 @@ -0,0 +1,45 @@
    23.4 +/*
    23.5 + * Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
    23.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    23.7 + *
    23.8 + * This code is free software; you can redistribute it and/or modify it
    23.9 + * under the terms of the GNU General Public License version 2 only, as
   23.10 + * published by the Free Software Foundation.  Oracle designates this
   23.11 + * particular file as subject to the "Classpath" exception as provided
   23.12 + * by Oracle in the LICENSE file that accompanied this code.
   23.13 + *
   23.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   23.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   23.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   23.17 + * version 2 for more details (a copy is included in the LICENSE file that
   23.18 + * accompanied this code).
   23.19 + *
   23.20 + * You should have received a copy of the GNU General Public License version
   23.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   23.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   23.23 + *
   23.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   23.25 + * or visit www.oracle.com if you need additional information or have any
   23.26 + * questions.
   23.27 + */
   23.28 +
   23.29 +/**
   23.30 + * Provides classes for performing arbitrary-precision integer
   23.31 + * arithmetic ({@code BigInteger}) and arbitrary-precision decimal
   23.32 + * arithmetic ({@code BigDecimal}).  {@code BigInteger} is analogous
   23.33 + * to the primitive integer types except that it provides arbitrary
   23.34 + * precision, hence operations on {@code BigInteger}s do not overflow
   23.35 + * or lose precision.  In addition to standard arithmetic operations,
   23.36 + * {@code BigInteger} provides modular arithmetic, GCD calculation,
   23.37 + * primality testing, prime generation, bit manipulation, and a few
   23.38 + * other miscellaneous operations.
   23.39 + *
   23.40 + * {@code BigDecimal} provides arbitrary-precision signed decimal
   23.41 + * numbers suitable for currency calculations and the like.  {@code
   23.42 + * BigDecimal} gives the user complete control over rounding behavior,
   23.43 + * allowing the user to choose from a comprehensive set of eight
   23.44 + * rounding modes.
   23.45 + *
   23.46 + * @since JDK1.1
   23.47 + */
   23.48 +package java.math;
    24.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    24.2 +++ b/rt/emul/compact/src/main/java/java/net/URI.java	Sun Sep 08 11:22:51 2013 +0200
    24.3 @@ -0,0 +1,3520 @@
    24.4 +/*
    24.5 + * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
    24.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    24.7 + *
    24.8 + * This code is free software; you can redistribute it and/or modify it
    24.9 + * under the terms of the GNU General Public License version 2 only, as
   24.10 + * published by the Free Software Foundation.  Oracle designates this
   24.11 + * particular file as subject to the "Classpath" exception as provided
   24.12 + * by Oracle in the LICENSE file that accompanied this code.
   24.13 + *
   24.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   24.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   24.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   24.17 + * version 2 for more details (a copy is included in the LICENSE file that
   24.18 + * accompanied this code).
   24.19 + *
   24.20 + * You should have received a copy of the GNU General Public License version
   24.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   24.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   24.23 + *
   24.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   24.25 + * or visit www.oracle.com if you need additional information or have any
   24.26 + * questions.
   24.27 + */
   24.28 +
   24.29 +package java.net;
   24.30 +
   24.31 +import java.io.IOException;
   24.32 +import java.io.InvalidObjectException;
   24.33 +import java.io.ObjectInputStream;
   24.34 +import java.io.ObjectOutputStream;
   24.35 +import java.io.Serializable;
   24.36 +
   24.37 +import java.lang.Character;             // for javadoc
   24.38 +import java.lang.NullPointerException;  // for javadoc
   24.39 +
   24.40 +
   24.41 +/**
   24.42 + * Represents a Uniform Resource Identifier (URI) reference.
   24.43 + *
   24.44 + * <p> Aside from some minor deviations noted below, an instance of this
   24.45 + * class represents a URI reference as defined by
   24.46 + * <a href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC&nbsp;2396: Uniform
   24.47 + * Resource Identifiers (URI): Generic Syntax</i></a>, amended by <a
   24.48 + * href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC&nbsp;2732: Format for
   24.49 + * Literal IPv6 Addresses in URLs</i></a>. The Literal IPv6 address format
   24.50 + * also supports scope_ids. The syntax and usage of scope_ids is described
   24.51 + * <a href="Inet6Address.html#scoped">here</a>.
   24.52 + * This class provides constructors for creating URI instances from
   24.53 + * their components or by parsing their string forms, methods for accessing the
   24.54 + * various components of an instance, and methods for normalizing, resolving,
   24.55 + * and relativizing URI instances.  Instances of this class are immutable.
   24.56 + *
   24.57 + *
   24.58 + * <h4> URI syntax and components </h4>
   24.59 + *
   24.60 + * At the highest level a URI reference (hereinafter simply "URI") in string
   24.61 + * form has the syntax
   24.62 + *
   24.63 + * <blockquote>
   24.64 + * [<i>scheme</i><tt><b>:</b></tt><i></i>]<i>scheme-specific-part</i>[<tt><b>#</b></tt><i>fragment</i>]
   24.65 + * </blockquote>
   24.66 + *
   24.67 + * where square brackets [...] delineate optional components and the characters
   24.68 + * <tt><b>:</b></tt> and <tt><b>#</b></tt> stand for themselves.
   24.69 + *
   24.70 + * <p> An <i>absolute</i> URI specifies a scheme; a URI that is not absolute is
   24.71 + * said to be <i>relative</i>.  URIs are also classified according to whether
   24.72 + * they are <i>opaque</i> or <i>hierarchical</i>.
   24.73 + *
   24.74 + * <p> An <i>opaque</i> URI is an absolute URI whose scheme-specific part does
   24.75 + * not begin with a slash character (<tt>'/'</tt>).  Opaque URIs are not
   24.76 + * subject to further parsing.  Some examples of opaque URIs are:
   24.77 + *
   24.78 + * <blockquote><table cellpadding=0 cellspacing=0 summary="layout">
   24.79 + * <tr><td><tt>mailto:java-net@java.sun.com</tt><td></tr>
   24.80 + * <tr><td><tt>news:comp.lang.java</tt><td></tr>
   24.81 + * <tr><td><tt>urn:isbn:096139210x</tt></td></tr>
   24.82 + * </table></blockquote>
   24.83 + *
   24.84 + * <p> A <i>hierarchical</i> URI is either an absolute URI whose
   24.85 + * scheme-specific part begins with a slash character, or a relative URI, that
   24.86 + * is, a URI that does not specify a scheme.  Some examples of hierarchical
   24.87 + * URIs are:
   24.88 + *
   24.89 + * <blockquote>
   24.90 + * <tt>http://java.sun.com/j2se/1.3/</tt><br>
   24.91 + * <tt>docs/guide/collections/designfaq.html#28</tt><br>
   24.92 + * <tt>../../../demo/jfc/SwingSet2/src/SwingSet2.java</tt><br>
   24.93 + * <tt>file:///~/calendar</tt>
   24.94 + * </blockquote>
   24.95 + *
   24.96 + * <p> A hierarchical URI is subject to further parsing according to the syntax
   24.97 + *
   24.98 + * <blockquote>
   24.99 + * [<i>scheme</i><tt><b>:</b></tt>][<tt><b>//</b></tt><i>authority</i>][<i>path</i>][<tt><b>?</b></tt><i>query</i>][<tt><b>#</b></tt><i>fragment</i>]
  24.100 + * </blockquote>
  24.101 + *
  24.102 + * where the characters <tt><b>:</b></tt>, <tt><b>/</b></tt>,
  24.103 + * <tt><b>?</b></tt>, and <tt><b>#</b></tt> stand for themselves.  The
  24.104 + * scheme-specific part of a hierarchical URI consists of the characters
  24.105 + * between the scheme and fragment components.
  24.106 + *
  24.107 + * <p> The authority component of a hierarchical URI is, if specified, either
  24.108 + * <i>server-based</i> or <i>registry-based</i>.  A server-based authority
  24.109 + * parses according to the familiar syntax
  24.110 + *
  24.111 + * <blockquote>
  24.112 + * [<i>user-info</i><tt><b>@</b></tt>]<i>host</i>[<tt><b>:</b></tt><i>port</i>]
  24.113 + * </blockquote>
  24.114 + *
  24.115 + * where the characters <tt><b>@</b></tt> and <tt><b>:</b></tt> stand for
  24.116 + * themselves.  Nearly all URI schemes currently in use are server-based.  An
  24.117 + * authority component that does not parse in this way is considered to be
  24.118 + * registry-based.
  24.119 + *
  24.120 + * <p> The path component of a hierarchical URI is itself said to be absolute
  24.121 + * if it begins with a slash character (<tt>'/'</tt>); otherwise it is
  24.122 + * relative.  The path of a hierarchical URI that is either absolute or
  24.123 + * specifies an authority is always absolute.
  24.124 + *
  24.125 + * <p> All told, then, a URI instance has the following nine components:
  24.126 + *
  24.127 + * <blockquote><table summary="Describes the components of a URI:scheme,scheme-specific-part,authority,user-info,host,port,path,query,fragment">
  24.128 + * <tr><th><i>Component</i></th><th><i>Type</i></th></tr>
  24.129 + * <tr><td>scheme</td><td><tt>String</tt></td></tr>
  24.130 + * <tr><td>scheme-specific-part&nbsp;&nbsp;&nbsp;&nbsp;</td><td><tt>String</tt></td></tr>
  24.131 + * <tr><td>authority</td><td><tt>String</tt></td></tr>
  24.132 + * <tr><td>user-info</td><td><tt>String</tt></td></tr>
  24.133 + * <tr><td>host</td><td><tt>String</tt></td></tr>
  24.134 + * <tr><td>port</td><td><tt>int</tt></td></tr>
  24.135 + * <tr><td>path</td><td><tt>String</tt></td></tr>
  24.136 + * <tr><td>query</td><td><tt>String</tt></td></tr>
  24.137 + * <tr><td>fragment</td><td><tt>String</tt></td></tr>
  24.138 + * </table></blockquote>
  24.139 + *
  24.140 + * In a given instance any particular component is either <i>undefined</i> or
  24.141 + * <i>defined</i> with a distinct value.  Undefined string components are
  24.142 + * represented by <tt>null</tt>, while undefined integer components are
  24.143 + * represented by <tt>-1</tt>.  A string component may be defined to have the
  24.144 + * empty string as its value; this is not equivalent to that component being
  24.145 + * undefined.
  24.146 + *
  24.147 + * <p> Whether a particular component is or is not defined in an instance
  24.148 + * depends upon the type of the URI being represented.  An absolute URI has a
  24.149 + * scheme component.  An opaque URI has a scheme, a scheme-specific part, and
  24.150 + * possibly a fragment, but has no other components.  A hierarchical URI always
  24.151 + * has a path (though it may be empty) and a scheme-specific-part (which at
  24.152 + * least contains the path), and may have any of the other components.  If the
  24.153 + * authority component is present and is server-based then the host component
  24.154 + * will be defined and the user-information and port components may be defined.
  24.155 + *
  24.156 + *
  24.157 + * <h4> Operations on URI instances </h4>
  24.158 + *
  24.159 + * The key operations supported by this class are those of
  24.160 + * <i>normalization</i>, <i>resolution</i>, and <i>relativization</i>.
  24.161 + *
  24.162 + * <p> <i>Normalization</i> is the process of removing unnecessary <tt>"."</tt>
  24.163 + * and <tt>".."</tt> segments from the path component of a hierarchical URI.
  24.164 + * Each <tt>"."</tt> segment is simply removed.  A <tt>".."</tt> segment is
  24.165 + * removed only if it is preceded by a non-<tt>".."</tt> segment.
  24.166 + * Normalization has no effect upon opaque URIs.
  24.167 + *
  24.168 + * <p> <i>Resolution</i> is the process of resolving one URI against another,
  24.169 + * <i>base</i> URI.  The resulting URI is constructed from components of both
  24.170 + * URIs in the manner specified by RFC&nbsp;2396, taking components from the
  24.171 + * base URI for those not specified in the original.  For hierarchical URIs,
  24.172 + * the path of the original is resolved against the path of the base and then
  24.173 + * normalized.  The result, for example, of resolving
  24.174 + *
  24.175 + * <blockquote>
  24.176 + * <tt>docs/guide/collections/designfaq.html#28&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt>(1)
  24.177 + * </blockquote>
  24.178 + *
  24.179 + * against the base URI <tt>http://java.sun.com/j2se/1.3/</tt> is the result
  24.180 + * URI
  24.181 + *
  24.182 + * <blockquote>
  24.183 + * <tt>http://java.sun.com/j2se/1.3/docs/guide/collections/designfaq.html#28</tt>
  24.184 + * </blockquote>
  24.185 + *
  24.186 + * Resolving the relative URI
  24.187 + *
  24.188 + * <blockquote>
  24.189 + * <tt>../../../demo/jfc/SwingSet2/src/SwingSet2.java&nbsp;&nbsp;&nbsp;&nbsp;</tt>(2)
  24.190 + * </blockquote>
  24.191 + *
  24.192 + * against this result yields, in turn,
  24.193 + *
  24.194 + * <blockquote>
  24.195 + * <tt>http://java.sun.com/j2se/1.3/demo/jfc/SwingSet2/src/SwingSet2.java</tt>
  24.196 + * </blockquote>
  24.197 + *
  24.198 + * Resolution of both absolute and relative URIs, and of both absolute and
  24.199 + * relative paths in the case of hierarchical URIs, is supported.  Resolving
  24.200 + * the URI <tt>file:///~calendar</tt> against any other URI simply yields the
  24.201 + * original URI, since it is absolute.  Resolving the relative URI (2) above
  24.202 + * against the relative base URI (1) yields the normalized, but still relative,
  24.203 + * URI
  24.204 + *
  24.205 + * <blockquote>
  24.206 + * <tt>demo/jfc/SwingSet2/src/SwingSet2.java</tt>
  24.207 + * </blockquote>
  24.208 + *
  24.209 + * <p> <i>Relativization</i>, finally, is the inverse of resolution: For any
  24.210 + * two normalized URIs <i>u</i> and&nbsp;<i>v</i>,
  24.211 + *
  24.212 + * <blockquote>
  24.213 + *   <i>u</i><tt>.relativize(</tt><i>u</i><tt>.resolve(</tt><i>v</i><tt>)).equals(</tt><i>v</i><tt>)</tt>&nbsp;&nbsp;and<br>
  24.214 + *   <i>u</i><tt>.resolve(</tt><i>u</i><tt>.relativize(</tt><i>v</i><tt>)).equals(</tt><i>v</i><tt>)</tt>&nbsp;&nbsp;.<br>
  24.215 + * </blockquote>
  24.216 + *
  24.217 + * This operation is often useful when constructing a document containing URIs
  24.218 + * that must be made relative to the base URI of the document wherever
  24.219 + * possible.  For example, relativizing the URI
  24.220 + *
  24.221 + * <blockquote>
  24.222 + * <tt>http://java.sun.com/j2se/1.3/docs/guide/index.html</tt>
  24.223 + * </blockquote>
  24.224 + *
  24.225 + * against the base URI
  24.226 + *
  24.227 + * <blockquote>
  24.228 + * <tt>http://java.sun.com/j2se/1.3</tt>
  24.229 + * </blockquote>
  24.230 + *
  24.231 + * yields the relative URI <tt>docs/guide/index.html</tt>.
  24.232 + *
  24.233 + *
  24.234 + * <h4> Character categories </h4>
  24.235 + *
  24.236 + * RFC&nbsp;2396 specifies precisely which characters are permitted in the
  24.237 + * various components of a URI reference.  The following categories, most of
  24.238 + * which are taken from that specification, are used below to describe these
  24.239 + * constraints:
  24.240 + *
  24.241 + * <blockquote><table cellspacing=2 summary="Describes categories alpha,digit,alphanum,unreserved,punct,reserved,escaped,and other">
  24.242 + *   <tr><th valign=top><i>alpha</i></th>
  24.243 + *       <td>The US-ASCII alphabetic characters,
  24.244 + *        <tt>'A'</tt>&nbsp;through&nbsp;<tt>'Z'</tt>
  24.245 + *        and <tt>'a'</tt>&nbsp;through&nbsp;<tt>'z'</tt></td></tr>
  24.246 + *   <tr><th valign=top><i>digit</i></th>
  24.247 + *       <td>The US-ASCII decimal digit characters,
  24.248 + *       <tt>'0'</tt>&nbsp;through&nbsp;<tt>'9'</tt></td></tr>
  24.249 + *   <tr><th valign=top><i>alphanum</i></th>
  24.250 + *       <td>All <i>alpha</i> and <i>digit</i> characters</td></tr>
  24.251 + *   <tr><th valign=top><i>unreserved</i>&nbsp;&nbsp;&nbsp;&nbsp;</th>
  24.252 + *       <td>All <i>alphanum</i> characters together with those in the string
  24.253 + *        <tt>"_-!.~'()*"</tt></td></tr>
  24.254 + *   <tr><th valign=top><i>punct</i></th>
  24.255 + *       <td>The characters in the string <tt>",;:$&+="</tt></td></tr>
  24.256 + *   <tr><th valign=top><i>reserved</i></th>
  24.257 + *       <td>All <i>punct</i> characters together with those in the string
  24.258 + *        <tt>"?/[]@"</tt></td></tr>
  24.259 + *   <tr><th valign=top><i>escaped</i></th>
  24.260 + *       <td>Escaped octets, that is, triplets consisting of the percent
  24.261 + *           character (<tt>'%'</tt>) followed by two hexadecimal digits
  24.262 + *           (<tt>'0'</tt>-<tt>'9'</tt>, <tt>'A'</tt>-<tt>'F'</tt>, and
  24.263 + *           <tt>'a'</tt>-<tt>'f'</tt>)</td></tr>
  24.264 + *   <tr><th valign=top><i>other</i></th>
  24.265 + *       <td>The Unicode characters that are not in the US-ASCII character set,
  24.266 + *           are not control characters (according to the {@link
  24.267 + *           java.lang.Character#isISOControl(char) Character.isISOControl}
  24.268 + *           method), and are not space characters (according to the {@link
  24.269 + *           java.lang.Character#isSpaceChar(char) Character.isSpaceChar}
  24.270 + *           method)&nbsp;&nbsp;<i>(<b>Deviation from RFC 2396</b>, which is
  24.271 + *           limited to US-ASCII)</i></td></tr>
  24.272 + * </table></blockquote>
  24.273 + *
  24.274 + * <p><a name="legal-chars"></a> The set of all legal URI characters consists of
  24.275 + * the <i>unreserved</i>, <i>reserved</i>, <i>escaped</i>, and <i>other</i>
  24.276 + * characters.
  24.277 + *
  24.278 + *
  24.279 + * <h4> Escaped octets, quotation, encoding, and decoding </h4>
  24.280 + *
  24.281 + * RFC 2396 allows escaped octets to appear in the user-info, path, query, and
  24.282 + * fragment components.  Escaping serves two purposes in URIs:
  24.283 + *
  24.284 + * <ul>
  24.285 + *
  24.286 + *   <li><p> To <i>encode</i> non-US-ASCII characters when a URI is required to
  24.287 + *   conform strictly to RFC&nbsp;2396 by not containing any <i>other</i>
  24.288 + *   characters.  </p></li>
  24.289 + *
  24.290 + *   <li><p> To <i>quote</i> characters that are otherwise illegal in a
  24.291 + *   component.  The user-info, path, query, and fragment components differ
  24.292 + *   slightly in terms of which characters are considered legal and illegal.
  24.293 + *   </p></li>
  24.294 + *
  24.295 + * </ul>
  24.296 + *
  24.297 + * These purposes are served in this class by three related operations:
  24.298 + *
  24.299 + * <ul>
  24.300 + *
  24.301 + *   <li><p><a name="encode"></a> A character is <i>encoded</i> by replacing it
  24.302 + *   with the sequence of escaped octets that represent that character in the
  24.303 + *   UTF-8 character set.  The Euro currency symbol (<tt>'&#92;u20AC'</tt>),
  24.304 + *   for example, is encoded as <tt>"%E2%82%AC"</tt>.  <i>(<b>Deviation from
  24.305 + *   RFC&nbsp;2396</b>, which does not specify any particular character
  24.306 + *   set.)</i> </p></li>
  24.307 + *
  24.308 + *   <li><p><a name="quote"></a> An illegal character is <i>quoted</i> simply by
  24.309 + *   encoding it.  The space character, for example, is quoted by replacing it
  24.310 + *   with <tt>"%20"</tt>.  UTF-8 contains US-ASCII, hence for US-ASCII
  24.311 + *   characters this transformation has exactly the effect required by
  24.312 + *   RFC&nbsp;2396. </p></li>
  24.313 + *
  24.314 + *   <li><p><a name="decode"></a>
  24.315 + *   A sequence of escaped octets is <i>decoded</i> by
  24.316 + *   replacing it with the sequence of characters that it represents in the
  24.317 + *   UTF-8 character set.  UTF-8 contains US-ASCII, hence decoding has the
  24.318 + *   effect of de-quoting any quoted US-ASCII characters as well as that of
  24.319 + *   decoding any encoded non-US-ASCII characters.  If a <a
  24.320 + *   href="../nio/charset/CharsetDecoder.html#ce">decoding error</a> occurs
  24.321 + *   when decoding the escaped octets then the erroneous octets are replaced by
  24.322 + *   <tt>'&#92;uFFFD'</tt>, the Unicode replacement character.  </p></li>
  24.323 + *
  24.324 + * </ul>
  24.325 + *
  24.326 + * These operations are exposed in the constructors and methods of this class
  24.327 + * as follows:
  24.328 + *
  24.329 + * <ul>
  24.330 + *
  24.331 + *   <li><p> The {@link #URI(java.lang.String) <code>single-argument
  24.332 + *   constructor</code>} requires any illegal characters in its argument to be
  24.333 + *   quoted and preserves any escaped octets and <i>other</i> characters that
  24.334 + *   are present.  </p></li>
  24.335 + *
  24.336 + *   <li><p> The {@link
  24.337 + *   #URI(java.lang.String,java.lang.String,java.lang.String,int,java.lang.String,java.lang.String,java.lang.String)
  24.338 + *   <code>multi-argument constructors</code>} quote illegal characters as
  24.339 + *   required by the components in which they appear.  The percent character
  24.340 + *   (<tt>'%'</tt>) is always quoted by these constructors.  Any <i>other</i>
  24.341 + *   characters are preserved.  </p></li>
  24.342 + *
  24.343 + *   <li><p> The {@link #getRawUserInfo() getRawUserInfo}, {@link #getRawPath()
  24.344 + *   getRawPath}, {@link #getRawQuery() getRawQuery}, {@link #getRawFragment()
  24.345 + *   getRawFragment}, {@link #getRawAuthority() getRawAuthority}, and {@link
  24.346 + *   #getRawSchemeSpecificPart() getRawSchemeSpecificPart} methods return the
  24.347 + *   values of their corresponding components in raw form, without interpreting
  24.348 + *   any escaped octets.  The strings returned by these methods may contain
  24.349 + *   both escaped octets and <i>other</i> characters, and will not contain any
  24.350 + *   illegal characters.  </p></li>
  24.351 + *
  24.352 + *   <li><p> The {@link #getUserInfo() getUserInfo}, {@link #getPath()
  24.353 + *   getPath}, {@link #getQuery() getQuery}, {@link #getFragment()
  24.354 + *   getFragment}, {@link #getAuthority() getAuthority}, and {@link
  24.355 + *   #getSchemeSpecificPart() getSchemeSpecificPart} methods decode any escaped
  24.356 + *   octets in their corresponding components.  The strings returned by these
  24.357 + *   methods may contain both <i>other</i> characters and illegal characters,
  24.358 + *   and will not contain any escaped octets.  </p></li>
  24.359 + *
  24.360 + *   <li><p> The {@link #toString() toString} method returns a URI string with
  24.361 + *   all necessary quotation but which may contain <i>other</i> characters.
  24.362 + *   </p></li>
  24.363 + *
  24.364 + *   <li><p> The {@link #toASCIIString() toASCIIString} method returns a fully
  24.365 + *   quoted and encoded URI string that does not contain any <i>other</i>
  24.366 + *   characters.  </p></li>
  24.367 + *
  24.368 + * </ul>
  24.369 + *
  24.370 + *
  24.371 + * <h4> Identities </h4>
  24.372 + *
  24.373 + * For any URI <i>u</i>, it is always the case that
  24.374 + *
  24.375 + * <blockquote>
  24.376 + * <tt>new URI(</tt><i>u</i><tt>.toString()).equals(</tt><i>u</i><tt>)</tt>&nbsp;.
  24.377 + * </blockquote>
  24.378 + *
  24.379 + * For any URI <i>u</i> that does not contain redundant syntax such as two
  24.380 + * slashes before an empty authority (as in <tt>file:///tmp/</tt>&nbsp;) or a
  24.381 + * colon following a host name but no port (as in
  24.382 + * <tt>http://java.sun.com:</tt>&nbsp;), and that does not encode characters
  24.383 + * except those that must be quoted, the following identities also hold:
  24.384 + *
  24.385 + * <blockquote>
  24.386 + * <tt>new URI(</tt><i>u</i><tt>.getScheme(),<br>
  24.387 + * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt><i>u</i><tt>.getSchemeSpecificPart(),<br>
  24.388 + * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt><i>u</i><tt>.getFragment())<br>
  24.389 + * .equals(</tt><i>u</i><tt>)</tt>
  24.390 + * </blockquote>
  24.391 + *
  24.392 + * in all cases,
  24.393 + *
  24.394 + * <blockquote>
  24.395 + * <tt>new URI(</tt><i>u</i><tt>.getScheme(),<br>
  24.396 + * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt><i>u</i><tt>.getUserInfo(),&nbsp;</tt><i>u</i><tt>.getAuthority(),<br>
  24.397 + * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt><i>u</i><tt>.getPath(),&nbsp;</tt><i>u</i><tt>.getQuery(),<br>
  24.398 + * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt><i>u</i><tt>.getFragment())<br>
  24.399 + * .equals(</tt><i>u</i><tt>)</tt>
  24.400 + * </blockquote>
  24.401 + *
  24.402 + * if <i>u</i> is hierarchical, and
  24.403 + *
  24.404 + * <blockquote>
  24.405 + * <tt>new URI(</tt><i>u</i><tt>.getScheme(),<br>
  24.406 + * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt><i>u</i><tt>.getUserInfo(),&nbsp;</tt><i>u</i><tt>.getHost(),&nbsp;</tt><i>u</i><tt>.getPort(),<br>
  24.407 + * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt><i>u</i><tt>.getPath(),&nbsp;</tt><i>u</i><tt>.getQuery(),<br>
  24.408 + * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt><i>u</i><tt>.getFragment())<br>
  24.409 + * .equals(</tt><i>u</i><tt>)</tt>
  24.410 + * </blockquote>
  24.411 + *
  24.412 + * if <i>u</i> is hierarchical and has either no authority or a server-based
  24.413 + * authority.
  24.414 + *
  24.415 + *
  24.416 + * <h4> URIs, URLs, and URNs </h4>
  24.417 + *
  24.418 + * A URI is a uniform resource <i>identifier</i> while a URL is a uniform
  24.419 + * resource <i>locator</i>.  Hence every URL is a URI, abstractly speaking, but
  24.420 + * not every URI is a URL.  This is because there is another subcategory of
  24.421 + * URIs, uniform resource <i>names</i> (URNs), which name resources but do not
  24.422 + * specify how to locate them.  The <tt>mailto</tt>, <tt>news</tt>, and
  24.423 + * <tt>isbn</tt> URIs shown above are examples of URNs.
  24.424 + *
  24.425 + * <p> The conceptual distinction between URIs and URLs is reflected in the
  24.426 + * differences between this class and the {@link URL} class.
  24.427 + *
  24.428 + * <p> An instance of this class represents a URI reference in the syntactic
  24.429 + * sense defined by RFC&nbsp;2396.  A URI may be either absolute or relative.
  24.430 + * A URI string is parsed according to the generic syntax without regard to the
  24.431 + * scheme, if any, that it specifies.  No lookup of the host, if any, is
  24.432 + * performed, and no scheme-dependent stream handler is constructed.  Equality,
  24.433 + * hashing, and comparison are defined strictly in terms of the character
  24.434 + * content of the instance.  In other words, a URI instance is little more than
  24.435 + * a structured string that supports the syntactic, scheme-independent
  24.436 + * operations of comparison, normalization, resolution, and relativization.
  24.437 + *
  24.438 + * <p> An instance of the {@link URL} class, by contrast, represents the
  24.439 + * syntactic components of a URL together with some of the information required
  24.440 + * to access the resource that it describes.  A URL must be absolute, that is,
  24.441 + * it must always specify a scheme.  A URL string is parsed according to its
  24.442 + * scheme.  A stream handler is always established for a URL, and in fact it is
  24.443 + * impossible to create a URL instance for a scheme for which no handler is
  24.444 + * available.  Equality and hashing depend upon both the scheme and the
  24.445 + * Internet address of the host, if any; comparison is not defined.  In other
  24.446 + * words, a URL is a structured string that supports the syntactic operation of
  24.447 + * resolution as well as the network I/O operations of looking up the host and
  24.448 + * opening a connection to the specified resource.
  24.449 + *
  24.450 + *
  24.451 + * @author Mark Reinhold
  24.452 + * @since 1.4
  24.453 + *
  24.454 + * @see <a href="http://www.ietf.org/rfc/rfc2279.txt"><i>RFC&nbsp;2279: UTF-8, a
  24.455 + * transformation format of ISO 10646</i></a>, <br><a
  24.456 + * href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IPv6 Addressing
  24.457 + * Architecture</i></a>, <br><a
  24.458 + * href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC&nbsp;2396: Uniform
  24.459 + * Resource Identifiers (URI): Generic Syntax</i></a>, <br><a
  24.460 + * href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC&nbsp;2732: Format for
  24.461 + * Literal IPv6 Addresses in URLs</i></a>, <br><a
  24.462 + * href="URISyntaxException.html">URISyntaxException</a>
  24.463 + */
  24.464 +
  24.465 +public final class URI
  24.466 +    implements Comparable<URI>, Serializable
  24.467 +{
  24.468 +
  24.469 +    // Note: Comments containing the word "ASSERT" indicate places where a
  24.470 +    // throw of an InternalError should be replaced by an appropriate assertion
  24.471 +    // statement once asserts are enabled in the build.
  24.472 +
  24.473 +    static final long serialVersionUID = -6052424284110960213L;
  24.474 +
  24.475 +
  24.476 +    // -- Properties and components of this instance --
  24.477 +
  24.478 +    // Components of all URIs: [<scheme>:]<scheme-specific-part>[#<fragment>]
  24.479 +    private transient String scheme;            // null ==> relative URI
  24.480 +    private transient String fragment;
  24.481 +
  24.482 +    // Hierarchical URI components: [//<authority>]<path>[?<query>]
  24.483 +    private transient String authority;         // Registry or server
  24.484 +
  24.485 +    // Server-based authority: [<userInfo>@]<host>[:<port>]
  24.486 +    private transient String userInfo;
  24.487 +    private transient String host;              // null ==> registry-based
  24.488 +    private transient int port = -1;            // -1 ==> undefined
  24.489 +
  24.490 +    // Remaining components of hierarchical URIs
  24.491 +    private transient String path;              // null ==> opaque
  24.492 +    private transient String query;
  24.493 +
  24.494 +    // The remaining fields may be computed on demand
  24.495 +
  24.496 +    private volatile transient String schemeSpecificPart;
  24.497 +    private volatile transient int hash;        // Zero ==> undefined
  24.498 +
  24.499 +    private volatile transient String decodedUserInfo = null;
  24.500 +    private volatile transient String decodedAuthority = null;
  24.501 +    private volatile transient String decodedPath = null;
  24.502 +    private volatile transient String decodedQuery = null;
  24.503 +    private volatile transient String decodedFragment = null;
  24.504 +    private volatile transient String decodedSchemeSpecificPart = null;
  24.505 +
  24.506 +    /**
  24.507 +     * The string form of this URI.
  24.508 +     *
  24.509 +     * @serial
  24.510 +     */
  24.511 +    private volatile String string;             // The only serializable field
  24.512 +
  24.513 +
  24.514 +
  24.515 +    // -- Constructors and factories --
  24.516 +
  24.517 +    private URI() { }                           // Used internally
  24.518 +
  24.519 +    /**
  24.520 +     * Constructs a URI by parsing the given string.
  24.521 +     *
  24.522 +     * <p> This constructor parses the given string exactly as specified by the
  24.523 +     * grammar in <a
  24.524 +     * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
  24.525 +     * Appendix&nbsp;A, <b><i>except for the following deviations:</i></b> </p>
  24.526 +     *
  24.527 +     * <ul type=disc>
  24.528 +     *
  24.529 +     *   <li><p> An empty authority component is permitted as long as it is
  24.530 +     *   followed by a non-empty path, a query component, or a fragment
  24.531 +     *   component.  This allows the parsing of URIs such as
  24.532 +     *   <tt>"file:///foo/bar"</tt>, which seems to be the intent of
  24.533 +     *   RFC&nbsp;2396 although the grammar does not permit it.  If the
  24.534 +     *   authority component is empty then the user-information, host, and port
  24.535 +     *   components are undefined. </p></li>
  24.536 +     *
  24.537 +     *   <li><p> Empty relative paths are permitted; this seems to be the
  24.538 +     *   intent of RFC&nbsp;2396 although the grammar does not permit it.  The
  24.539 +     *   primary consequence of this deviation is that a standalone fragment
  24.540 +     *   such as <tt>"#foo"</tt> parses as a relative URI with an empty path
  24.541 +     *   and the given fragment, and can be usefully <a
  24.542 +     *   href="#resolve-frag">resolved</a> against a base URI.
  24.543 +     *
  24.544 +     *   <li><p> IPv4 addresses in host components are parsed rigorously, as
  24.545 +     *   specified by <a
  24.546 +     *   href="http://www.ietf.org/rfc/rfc2732.txt">RFC&nbsp;2732</a>: Each
  24.547 +     *   element of a dotted-quad address must contain no more than three
  24.548 +     *   decimal digits.  Each element is further constrained to have a value
  24.549 +     *   no greater than 255. </p></li>
  24.550 +     *
  24.551 +     *   <li> <p> Hostnames in host components that comprise only a single
  24.552 +     *   domain label are permitted to start with an <i>alphanum</i>
  24.553 +     *   character. This seems to be the intent of <a
  24.554 +     *   href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>
  24.555 +     *   section&nbsp;3.2.2 although the grammar does not permit it. The
  24.556 +     *   consequence of this deviation is that the authority component of a
  24.557 +     *   hierarchical URI such as <tt>s://123</tt>, will parse as a server-based
  24.558 +     *   authority. </p></li>
  24.559 +     *
  24.560 +     *   <li><p> IPv6 addresses are permitted for the host component.  An IPv6
  24.561 +     *   address must be enclosed in square brackets (<tt>'['</tt> and
  24.562 +     *   <tt>']'</tt>) as specified by <a
  24.563 +     *   href="http://www.ietf.org/rfc/rfc2732.txt">RFC&nbsp;2732</a>.  The
  24.564 +     *   IPv6 address itself must parse according to <a
  24.565 +     *   href="http://www.ietf.org/rfc/rfc2373.txt">RFC&nbsp;2373</a>.  IPv6
  24.566 +     *   addresses are further constrained to describe no more than sixteen
  24.567 +     *   bytes of address information, a constraint implicit in RFC&nbsp;2373
  24.568 +     *   but not expressible in the grammar. </p></li>
  24.569 +     *
  24.570 +     *   <li><p> Characters in the <i>other</i> category are permitted wherever
  24.571 +     *   RFC&nbsp;2396 permits <i>escaped</i> octets, that is, in the
  24.572 +     *   user-information, path, query, and fragment components, as well as in
  24.573 +     *   the authority component if the authority is registry-based.  This
  24.574 +     *   allows URIs to contain Unicode characters beyond those in the US-ASCII
  24.575 +     *   character set. </p></li>
  24.576 +     *
  24.577 +     * </ul>
  24.578 +     *
  24.579 +     * @param  str   The string to be parsed into a URI
  24.580 +     *
  24.581 +     * @throws  NullPointerException
  24.582 +     *          If <tt>str</tt> is <tt>null</tt>
  24.583 +     *
  24.584 +     * @throws  URISyntaxException
  24.585 +     *          If the given string violates RFC&nbsp;2396, as augmented
  24.586 +     *          by the above deviations
  24.587 +     */
  24.588 +    public URI(String str) throws URISyntaxException {
  24.589 +        new Parser(str).parse(false);
  24.590 +    }
  24.591 +
  24.592 +    /**
  24.593 +     * Constructs a hierarchical URI from the given components.
  24.594 +     *
  24.595 +     * <p> If a scheme is given then the path, if also given, must either be
  24.596 +     * empty or begin with a slash character (<tt>'/'</tt>).  Otherwise a
  24.597 +     * component of the new URI may be left undefined by passing <tt>null</tt>
  24.598 +     * for the corresponding parameter or, in the case of the <tt>port</tt>
  24.599 +     * parameter, by passing <tt>-1</tt>.
  24.600 +     *
  24.601 +     * <p> This constructor first builds a URI string from the given components
  24.602 +     * according to the rules specified in <a
  24.603 +     * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
  24.604 +     * section&nbsp;5.2, step&nbsp;7: </p>
  24.605 +     *
  24.606 +     * <ol>
  24.607 +     *
  24.608 +     *   <li><p> Initially, the result string is empty. </p></li>
  24.609 +     *
  24.610 +     *   <li><p> If a scheme is given then it is appended to the result,
  24.611 +     *   followed by a colon character (<tt>':'</tt>).  </p></li>
  24.612 +     *
  24.613 +     *   <li><p> If user information, a host, or a port are given then the
  24.614 +     *   string <tt>"//"</tt> is appended.  </p></li>
  24.615 +     *
  24.616 +     *   <li><p> If user information is given then it is appended, followed by
  24.617 +     *   a commercial-at character (<tt>'@'</tt>).  Any character not in the
  24.618 +     *   <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, or <i>other</i>
  24.619 +     *   categories is <a href="#quote">quoted</a>.  </p></li>
  24.620 +     *
  24.621 +     *   <li><p> If a host is given then it is appended.  If the host is a
  24.622 +     *   literal IPv6 address but is not enclosed in square brackets
  24.623 +     *   (<tt>'['</tt> and <tt>']'</tt>) then the square brackets are added.
  24.624 +     *   </p></li>
  24.625 +     *
  24.626 +     *   <li><p> If a port number is given then a colon character
  24.627 +     *   (<tt>':'</tt>) is appended, followed by the port number in decimal.
  24.628 +     *   </p></li>
  24.629 +     *
  24.630 +     *   <li><p> If a path is given then it is appended.  Any character not in
  24.631 +     *   the <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, or <i>other</i>
  24.632 +     *   categories, and not equal to the slash character (<tt>'/'</tt>) or the
  24.633 +     *   commercial-at character (<tt>'@'</tt>), is quoted.  </p></li>
  24.634 +     *
  24.635 +     *   <li><p> If a query is given then a question-mark character
  24.636 +     *   (<tt>'?'</tt>) is appended, followed by the query.  Any character that
  24.637 +     *   is not a <a href="#legal-chars">legal URI character</a> is quoted.
  24.638 +     *   </p></li>
  24.639 +     *
  24.640 +     *   <li><p> Finally, if a fragment is given then a hash character
  24.641 +     *   (<tt>'#'</tt>) is appended, followed by the fragment.  Any character
  24.642 +     *   that is not a legal URI character is quoted.  </p></li>
  24.643 +     *
  24.644 +     * </ol>
  24.645 +     *
  24.646 +     * <p> The resulting URI string is then parsed as if by invoking the {@link
  24.647 +     * #URI(String)} constructor and then invoking the {@link
  24.648 +     * #parseServerAuthority()} method upon the result; this may cause a {@link
  24.649 +     * URISyntaxException} to be thrown.  </p>
  24.650 +     *
  24.651 +     * @param   scheme    Scheme name
  24.652 +     * @param   userInfo  User name and authorization information
  24.653 +     * @param   host      Host name
  24.654 +     * @param   port      Port number
  24.655 +     * @param   path      Path
  24.656 +     * @param   query     Query
  24.657 +     * @param   fragment  Fragment
  24.658 +     *
  24.659 +     * @throws URISyntaxException
  24.660 +     *         If both a scheme and a path are given but the path is relative,
  24.661 +     *         if the URI string constructed from the given components violates
  24.662 +     *         RFC&nbsp;2396, or if the authority component of the string is
  24.663 +     *         present but cannot be parsed as a server-based authority
  24.664 +     */
  24.665 +    public URI(String scheme,
  24.666 +               String userInfo, String host, int port,
  24.667 +               String path, String query, String fragment)
  24.668 +        throws URISyntaxException
  24.669 +    {
  24.670 +        String s = toString(scheme, null,
  24.671 +                            null, userInfo, host, port,
  24.672 +                            path, query, fragment);
  24.673 +        checkPath(s, scheme, path);
  24.674 +        new Parser(s).parse(true);
  24.675 +    }
  24.676 +
  24.677 +    /**
  24.678 +     * Constructs a hierarchical URI from the given components.
  24.679 +     *
  24.680 +     * <p> If a scheme is given then the path, if also given, must either be
  24.681 +     * empty or begin with a slash character (<tt>'/'</tt>).  Otherwise a
  24.682 +     * component of the new URI may be left undefined by passing <tt>null</tt>
  24.683 +     * for the corresponding parameter.
  24.684 +     *
  24.685 +     * <p> This constructor first builds a URI string from the given components
  24.686 +     * according to the rules specified in <a
  24.687 +     * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
  24.688 +     * section&nbsp;5.2, step&nbsp;7: </p>
  24.689 +     *
  24.690 +     * <ol>
  24.691 +     *
  24.692 +     *   <li><p> Initially, the result string is empty.  </p></li>
  24.693 +     *
  24.694 +     *   <li><p> If a scheme is given then it is appended to the result,
  24.695 +     *   followed by a colon character (<tt>':'</tt>).  </p></li>
  24.696 +     *
  24.697 +     *   <li><p> If an authority is given then the string <tt>"//"</tt> is
  24.698 +     *   appended, followed by the authority.  If the authority contains a
  24.699 +     *   literal IPv6 address then the address must be enclosed in square
  24.700 +     *   brackets (<tt>'['</tt> and <tt>']'</tt>).  Any character not in the
  24.701 +     *   <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, or <i>other</i>
  24.702 +     *   categories, and not equal to the commercial-at character
  24.703 +     *   (<tt>'@'</tt>), is <a href="#quote">quoted</a>.  </p></li>
  24.704 +     *
  24.705 +     *   <li><p> If a path is given then it is appended.  Any character not in
  24.706 +     *   the <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, or <i>other</i>
  24.707 +     *   categories, and not equal to the slash character (<tt>'/'</tt>) or the
  24.708 +     *   commercial-at character (<tt>'@'</tt>), is quoted.  </p></li>
  24.709 +     *
  24.710 +     *   <li><p> If a query is given then a question-mark character
  24.711 +     *   (<tt>'?'</tt>) is appended, followed by the query.  Any character that
  24.712 +     *   is not a <a href="#legal-chars">legal URI character</a> is quoted.
  24.713 +     *   </p></li>
  24.714 +     *
  24.715 +     *   <li><p> Finally, if a fragment is given then a hash character
  24.716 +     *   (<tt>'#'</tt>) is appended, followed by the fragment.  Any character
  24.717 +     *   that is not a legal URI character is quoted.  </p></li>
  24.718 +     *
  24.719 +     * </ol>
  24.720 +     *
  24.721 +     * <p> The resulting URI string is then parsed as if by invoking the {@link
  24.722 +     * #URI(String)} constructor and then invoking the {@link
  24.723 +     * #parseServerAuthority()} method upon the result; this may cause a {@link
  24.724 +     * URISyntaxException} to be thrown.  </p>
  24.725 +     *
  24.726 +     * @param   scheme     Scheme name
  24.727 +     * @param   authority  Authority
  24.728 +     * @param   path       Path
  24.729 +     * @param   query      Query
  24.730 +     * @param   fragment   Fragment
  24.731 +     *
  24.732 +     * @throws URISyntaxException
  24.733 +     *         If both a scheme and a path are given but the path is relative,
  24.734 +     *         if the URI string constructed from the given components violates
  24.735 +     *         RFC&nbsp;2396, or if the authority component of the string is
  24.736 +     *         present but cannot be parsed as a server-based authority
  24.737 +     */
  24.738 +    public URI(String scheme,
  24.739 +               String authority,
  24.740 +               String path, String query, String fragment)
  24.741 +        throws URISyntaxException
  24.742 +    {
  24.743 +        String s = toString(scheme, null,
  24.744 +                            authority, null, null, -1,
  24.745 +                            path, query, fragment);
  24.746 +        checkPath(s, scheme, path);
  24.747 +        new Parser(s).parse(false);
  24.748 +    }
  24.749 +
  24.750 +    /**
  24.751 +     * Constructs a hierarchical URI from the given components.
  24.752 +     *
  24.753 +     * <p> A component may be left undefined by passing <tt>null</tt>.
  24.754 +     *
  24.755 +     * <p> This convenience constructor works as if by invoking the
  24.756 +     * seven-argument constructor as follows:
  24.757 +     *
  24.758 +     * <blockquote><tt>
  24.759 +     * new&nbsp;{@link #URI(String, String, String, int, String, String, String)
  24.760 +     * URI}(scheme,&nbsp;null,&nbsp;host,&nbsp;-1,&nbsp;path,&nbsp;null,&nbsp;fragment);
  24.761 +     * </tt></blockquote>
  24.762 +     *
  24.763 +     * @param   scheme    Scheme name
  24.764 +     * @param   host      Host name
  24.765 +     * @param   path      Path
  24.766 +     * @param   fragment  Fragment
  24.767 +     *
  24.768 +     * @throws  URISyntaxException
  24.769 +     *          If the URI string constructed from the given components
  24.770 +     *          violates RFC&nbsp;2396
  24.771 +     */
  24.772 +    public URI(String scheme, String host, String path, String fragment)
  24.773 +        throws URISyntaxException
  24.774 +    {
  24.775 +        this(scheme, null, host, -1, path, null, fragment);
  24.776 +    }
  24.777 +
  24.778 +    /**
  24.779 +     * Constructs a URI from the given components.
  24.780 +     *
  24.781 +     * <p> A component may be left undefined by passing <tt>null</tt>.
  24.782 +     *
  24.783 +     * <p> This constructor first builds a URI in string form using the given
  24.784 +     * components as follows:  </p>
  24.785 +     *
  24.786 +     * <ol>
  24.787 +     *
  24.788 +     *   <li><p> Initially, the result string is empty.  </p></li>
  24.789 +     *
  24.790 +     *   <li><p> If a scheme is given then it is appended to the result,
  24.791 +     *   followed by a colon character (<tt>':'</tt>).  </p></li>
  24.792 +     *
  24.793 +     *   <li><p> If a scheme-specific part is given then it is appended.  Any
  24.794 +     *   character that is not a <a href="#legal-chars">legal URI character</a>
  24.795 +     *   is <a href="#quote">quoted</a>.  </p></li>
  24.796 +     *
  24.797 +     *   <li><p> Finally, if a fragment is given then a hash character
  24.798 +     *   (<tt>'#'</tt>) is appended to the string, followed by the fragment.
  24.799 +     *   Any character that is not a legal URI character is quoted.  </p></li>
  24.800 +     *
  24.801 +     * </ol>
  24.802 +     *
  24.803 +     * <p> The resulting URI string is then parsed in order to create the new
  24.804 +     * URI instance as if by invoking the {@link #URI(String)} constructor;
  24.805 +     * this may cause a {@link URISyntaxException} to be thrown.  </p>
  24.806 +     *
  24.807 +     * @param   scheme    Scheme name
  24.808 +     * @param   ssp       Scheme-specific part
  24.809 +     * @param   fragment  Fragment
  24.810 +     *
  24.811 +     * @throws  URISyntaxException
  24.812 +     *          If the URI string constructed from the given components
  24.813 +     *          violates RFC&nbsp;2396
  24.814 +     */
  24.815 +    public URI(String scheme, String ssp, String fragment)
  24.816 +        throws URISyntaxException
  24.817 +    {
  24.818 +        new Parser(toString(scheme, ssp,
  24.819 +                            null, null, null, -1,
  24.820 +                            null, null, fragment))
  24.821 +            .parse(false);
  24.822 +    }
  24.823 +
  24.824 +    /**
  24.825 +     * Creates a URI by parsing the given string.
  24.826 +     *
  24.827 +     * <p> This convenience factory method works as if by invoking the {@link
  24.828 +     * #URI(String)} constructor; any {@link URISyntaxException} thrown by the
  24.829 +     * constructor is caught and wrapped in a new {@link
  24.830 +     * IllegalArgumentException} object, which is then thrown.
  24.831 +     *
  24.832 +     * <p> This method is provided for use in situations where it is known that
  24.833 +     * the given string is a legal URI, for example for URI constants declared
  24.834 +     * within in a program, and so it would be considered a programming error
  24.835 +     * for the string not to parse as such.  The constructors, which throw
  24.836 +     * {@link URISyntaxException} directly, should be used situations where a
  24.837 +     * URI is being constructed from user input or from some other source that
  24.838 +     * may be prone to errors.  </p>
  24.839 +     *
  24.840 +     * @param  str   The string to be parsed into a URI
  24.841 +     * @return The new URI
  24.842 +     *
  24.843 +     * @throws  NullPointerException
  24.844 +     *          If <tt>str</tt> is <tt>null</tt>
  24.845 +     *
  24.846 +     * @throws  IllegalArgumentException
  24.847 +     *          If the given string violates RFC&nbsp;2396
  24.848 +     */
  24.849 +    public static URI create(String str) {
  24.850 +        try {
  24.851 +            return new URI(str);
  24.852 +        } catch (URISyntaxException x) {
  24.853 +            throw new IllegalArgumentException(x.getMessage(), x);
  24.854 +        }
  24.855 +    }
  24.856 +
  24.857 +
  24.858 +    // -- Operations --
  24.859 +
  24.860 +    /**
  24.861 +     * Attempts to parse this URI's authority component, if defined, into
  24.862 +     * user-information, host, and port components.
  24.863 +     *
  24.864 +     * <p> If this URI's authority component has already been recognized as
  24.865 +     * being server-based then it will already have been parsed into
  24.866 +     * user-information, host, and port components.  In this case, or if this
  24.867 +     * URI has no authority component, this method simply returns this URI.
  24.868 +     *
  24.869 +     * <p> Otherwise this method attempts once more to parse the authority
  24.870 +     * component into user-information, host, and port components, and throws
  24.871 +     * an exception describing why the authority component could not be parsed
  24.872 +     * in that way.
  24.873 +     *
  24.874 +     * <p> This method is provided because the generic URI syntax specified in
  24.875 +     * <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>
  24.876 +     * cannot always distinguish a malformed server-based authority from a
  24.877 +     * legitimate registry-based authority.  It must therefore treat some
  24.878 +     * instances of the former as instances of the latter.  The authority
  24.879 +     * component in the URI string <tt>"//foo:bar"</tt>, for example, is not a
  24.880 +     * legal server-based authority but it is legal as a registry-based
  24.881 +     * authority.
  24.882 +     *
  24.883 +     * <p> In many common situations, for example when working URIs that are
  24.884 +     * known to be either URNs or URLs, the hierarchical URIs being used will
  24.885 +     * always be server-based.  They therefore must either be parsed as such or
  24.886 +     * treated as an error.  In these cases a statement such as
  24.887 +     *
  24.888 +     * <blockquote>
  24.889 +     * <tt>URI </tt><i>u</i><tt> = new URI(str).parseServerAuthority();</tt>
  24.890 +     * </blockquote>
  24.891 +     *
  24.892 +     * <p> can be used to ensure that <i>u</i> always refers to a URI that, if
  24.893 +     * it has an authority component, has a server-based authority with proper
  24.894 +     * user-information, host, and port components.  Invoking this method also
  24.895 +     * ensures that if the authority could not be parsed in that way then an
  24.896 +     * appropriate diagnostic message can be issued based upon the exception
  24.897 +     * that is thrown. </p>
  24.898 +     *
  24.899 +     * @return  A URI whose authority field has been parsed
  24.900 +     *          as a server-based authority
  24.901 +     *
  24.902 +     * @throws  URISyntaxException
  24.903 +     *          If the authority component of this URI is defined
  24.904 +     *          but cannot be parsed as a server-based authority
  24.905 +     *          according to RFC&nbsp;2396
  24.906 +     */
  24.907 +    public URI parseServerAuthority()
  24.908 +        throws URISyntaxException
  24.909 +    {
  24.910 +        // We could be clever and cache the error message and index from the
  24.911 +        // exception thrown during the original parse, but that would require
  24.912 +        // either more fields or a more-obscure representation.
  24.913 +        if ((host != null) || (authority == null))
  24.914 +            return this;
  24.915 +        defineString();
  24.916 +        new Parser(string).parse(true);
  24.917 +        return this;
  24.918 +    }
  24.919 +
  24.920 +    /**
  24.921 +     * Normalizes this URI's path.
  24.922 +     *
  24.923 +     * <p> If this URI is opaque, or if its path is already in normal form,
  24.924 +     * then this URI is returned.  Otherwise a new URI is constructed that is
  24.925 +     * identical to this URI except that its path is computed by normalizing
  24.926 +     * this URI's path in a manner consistent with <a
  24.927 +     * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
  24.928 +     * section&nbsp;5.2, step&nbsp;6, sub-steps&nbsp;c through&nbsp;f; that is:
  24.929 +     * </p>
  24.930 +     *
  24.931 +     * <ol>
  24.932 +     *
  24.933 +     *   <li><p> All <tt>"."</tt> segments are removed. </p></li>
  24.934 +     *
  24.935 +     *   <li><p> If a <tt>".."</tt> segment is preceded by a non-<tt>".."</tt>
  24.936 +     *   segment then both of these segments are removed.  This step is
  24.937 +     *   repeated until it is no longer applicable. </p></li>
  24.938 +     *
  24.939 +     *   <li><p> If the path is relative, and if its first segment contains a
  24.940 +     *   colon character (<tt>':'</tt>), then a <tt>"."</tt> segment is
  24.941 +     *   prepended.  This prevents a relative URI with a path such as
  24.942 +     *   <tt>"a:b/c/d"</tt> from later being re-parsed as an opaque URI with a
  24.943 +     *   scheme of <tt>"a"</tt> and a scheme-specific part of <tt>"b/c/d"</tt>.
  24.944 +     *   <b><i>(Deviation from RFC&nbsp;2396)</i></b> </p></li>
  24.945 +     *
  24.946 +     * </ol>
  24.947 +     *
  24.948 +     * <p> A normalized path will begin with one or more <tt>".."</tt> segments
  24.949 +     * if there were insufficient non-<tt>".."</tt> segments preceding them to
  24.950 +     * allow their removal.  A normalized path will begin with a <tt>"."</tt>
  24.951 +     * segment if one was inserted by step 3 above.  Otherwise, a normalized
  24.952 +     * path will not contain any <tt>"."</tt> or <tt>".."</tt> segments. </p>
  24.953 +     *
  24.954 +     * @return  A URI equivalent to this URI,
  24.955 +     *          but whose path is in normal form
  24.956 +     */
  24.957 +    public URI normalize() {
  24.958 +        return normalize(this);
  24.959 +    }
  24.960 +
  24.961 +    /**
  24.962 +     * Resolves the given URI against this URI.
  24.963 +     *
  24.964 +     * <p> If the given URI is already absolute, or if this URI is opaque, then
  24.965 +     * the given URI is returned.
  24.966 +     *
  24.967 +     * <p><a name="resolve-frag"></a> If the given URI's fragment component is
  24.968 +     * defined, its path component is empty, and its scheme, authority, and
  24.969 +     * query components are undefined, then a URI with the given fragment but
  24.970 +     * with all other components equal to those of this URI is returned.  This
  24.971 +     * allows a URI representing a standalone fragment reference, such as
  24.972 +     * <tt>"#foo"</tt>, to be usefully resolved against a base URI.
  24.973 +     *
  24.974 +     * <p> Otherwise this method constructs a new hierarchical URI in a manner
  24.975 +     * consistent with <a
  24.976 +     * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
  24.977 +     * section&nbsp;5.2; that is: </p>
  24.978 +     *
  24.979 +     * <ol>
  24.980 +     *
  24.981 +     *   <li><p> A new URI is constructed with this URI's scheme and the given
  24.982 +     *   URI's query and fragment components. </p></li>
  24.983 +     *
  24.984 +     *   <li><p> If the given URI has an authority component then the new URI's
  24.985 +     *   authority and path are taken from the given URI. </p></li>
  24.986 +     *
  24.987 +     *   <li><p> Otherwise the new URI's authority component is copied from
  24.988 +     *   this URI, and its path is computed as follows: </p>
  24.989 +     *
  24.990 +     *   <ol type=a>
  24.991 +     *
  24.992 +     *     <li><p> If the given URI's path is absolute then the new URI's path
  24.993 +     *     is taken from the given URI. </p></li>
  24.994 +     *
  24.995 +     *     <li><p> Otherwise the given URI's path is relative, and so the new
  24.996 +     *     URI's path is computed by resolving the path of the given URI
  24.997 +     *     against the path of this URI.  This is done by concatenating all but
  24.998 +     *     the last segment of this URI's path, if any, with the given URI's
  24.999 +     *     path and then normalizing the result as if by invoking the {@link
 24.1000 +     *     #normalize() normalize} method. </p></li>
 24.1001 +     *
 24.1002 +     *   </ol></li>
 24.1003 +     *
 24.1004 +     * </ol>
 24.1005 +     *
 24.1006 +     * <p> The result of this method is absolute if, and only if, either this
 24.1007 +     * URI is absolute or the given URI is absolute.  </p>
 24.1008 +     *
 24.1009 +     * @param  uri  The URI to be resolved against this URI
 24.1010 +     * @return The resulting URI
 24.1011 +     *
 24.1012 +     * @throws  NullPointerException
 24.1013 +     *          If <tt>uri</tt> is <tt>null</tt>
 24.1014 +     */
 24.1015 +    public URI resolve(URI uri) {
 24.1016 +        return resolve(this, uri);
 24.1017 +    }
 24.1018 +
 24.1019 +    /**
 24.1020 +     * Constructs a new URI by parsing the given string and then resolving it
 24.1021 +     * against this URI.
 24.1022 +     *
 24.1023 +     * <p> This convenience method works as if invoking it were equivalent to
 24.1024 +     * evaluating the expression <tt>{@link #resolve(java.net.URI)
 24.1025 +     * resolve}(URI.{@link #create(String) create}(str))</tt>. </p>
 24.1026 +     *
 24.1027 +     * @param  str   The string to be parsed into a URI
 24.1028 +     * @return The resulting URI
 24.1029 +     *
 24.1030 +     * @throws  NullPointerException
 24.1031 +     *          If <tt>str</tt> is <tt>null</tt>
 24.1032 +     *
 24.1033 +     * @throws  IllegalArgumentException
 24.1034 +     *          If the given string violates RFC&nbsp;2396
 24.1035 +     */
 24.1036 +    public URI resolve(String str) {
 24.1037 +        return resolve(URI.create(str));
 24.1038 +    }
 24.1039 +
 24.1040 +    /**
 24.1041 +     * Relativizes the given URI against this URI.
 24.1042 +     *
 24.1043 +     * <p> The relativization of the given URI against this URI is computed as
 24.1044 +     * follows: </p>
 24.1045 +     *
 24.1046 +     * <ol>
 24.1047 +     *
 24.1048 +     *   <li><p> If either this URI or the given URI are opaque, or if the
 24.1049 +     *   scheme and authority components of the two URIs are not identical, or
 24.1050 +     *   if the path of this URI is not a prefix of the path of the given URI,
 24.1051 +     *   then the given URI is returned. </p></li>
 24.1052 +     *
 24.1053 +     *   <li><p> Otherwise a new relative hierarchical URI is constructed with
 24.1054 +     *   query and fragment components taken from the given URI and with a path
 24.1055 +     *   component computed by removing this URI's path from the beginning of
 24.1056 +     *   the given URI's path. </p></li>
 24.1057 +     *
 24.1058 +     * </ol>
 24.1059 +     *
 24.1060 +     * @param  uri  The URI to be relativized against this URI
 24.1061 +     * @return The resulting URI
 24.1062 +     *
 24.1063 +     * @throws  NullPointerException
 24.1064 +     *          If <tt>uri</tt> is <tt>null</tt>
 24.1065 +     */
 24.1066 +    public URI relativize(URI uri) {
 24.1067 +        return relativize(this, uri);
 24.1068 +    }
 24.1069 +
 24.1070 +    /**
 24.1071 +     * Constructs a URL from this URI.
 24.1072 +     *
 24.1073 +     * <p> This convenience method works as if invoking it were equivalent to
 24.1074 +     * evaluating the expression <tt>new&nbsp;URL(this.toString())</tt> after
 24.1075 +     * first checking that this URI is absolute. </p>
 24.1076 +     *
 24.1077 +     * @return  A URL constructed from this URI
 24.1078 +     *
 24.1079 +     * @throws  IllegalArgumentException
 24.1080 +     *          If this URL is not absolute
 24.1081 +     *
 24.1082 +     * @throws  MalformedURLException
 24.1083 +     *          If a protocol handler for the URL could not be found,
 24.1084 +     *          or if some other error occurred while constructing the URL
 24.1085 +     */
 24.1086 +    public URL toURL()
 24.1087 +        throws MalformedURLException {
 24.1088 +        if (!isAbsolute())
 24.1089 +            throw new IllegalArgumentException("URI is not absolute");
 24.1090 +        return new URL(toString());
 24.1091 +    }
 24.1092 +
 24.1093 +    // -- Component access methods --
 24.1094 +
 24.1095 +    /**
 24.1096 +     * Returns the scheme component of this URI.
 24.1097 +     *
 24.1098 +     * <p> The scheme component of a URI, if defined, only contains characters
 24.1099 +     * in the <i>alphanum</i> category and in the string <tt>"-.+"</tt>.  A
 24.1100 +     * scheme always starts with an <i>alpha</i> character. <p>
 24.1101 +     *
 24.1102 +     * The scheme component of a URI cannot contain escaped octets, hence this
 24.1103 +     * method does not perform any decoding.
 24.1104 +     *
 24.1105 +     * @return  The scheme component of this URI,
 24.1106 +     *          or <tt>null</tt> if the scheme is undefined
 24.1107 +     */
 24.1108 +    public String getScheme() {
 24.1109 +        return scheme;
 24.1110 +    }
 24.1111 +
 24.1112 +    /**
 24.1113 +     * Tells whether or not this URI is absolute.
 24.1114 +     *
 24.1115 +     * <p> A URI is absolute if, and only if, it has a scheme component. </p>
 24.1116 +     *
 24.1117 +     * @return  <tt>true</tt> if, and only if, this URI is absolute
 24.1118 +     */
 24.1119 +    public boolean isAbsolute() {
 24.1120 +        return scheme != null;
 24.1121 +    }
 24.1122 +
 24.1123 +    /**
 24.1124 +     * Tells whether or not this URI is opaque.
 24.1125 +     *
 24.1126 +     * <p> A URI is opaque if, and only if, it is absolute and its
 24.1127 +     * scheme-specific part does not begin with a slash character ('/').
 24.1128 +     * An opaque URI has a scheme, a scheme-specific part, and possibly
 24.1129 +     * a fragment; all other components are undefined. </p>
 24.1130 +     *
 24.1131 +     * @return  <tt>true</tt> if, and only if, this URI is opaque
 24.1132 +     */
 24.1133 +    public boolean isOpaque() {
 24.1134 +        return path == null;
 24.1135 +    }
 24.1136 +
 24.1137 +    /**
 24.1138 +     * Returns the raw scheme-specific part of this URI.  The scheme-specific
 24.1139 +     * part is never undefined, though it may be empty.
 24.1140 +     *
 24.1141 +     * <p> The scheme-specific part of a URI only contains legal URI
 24.1142 +     * characters. </p>
 24.1143 +     *
 24.1144 +     * @return  The raw scheme-specific part of this URI
 24.1145 +     *          (never <tt>null</tt>)
 24.1146 +     */
 24.1147 +    public String getRawSchemeSpecificPart() {
 24.1148 +        defineSchemeSpecificPart();
 24.1149 +        return schemeSpecificPart;
 24.1150 +    }
 24.1151 +
 24.1152 +    /**
 24.1153 +     * Returns the decoded scheme-specific part of this URI.
 24.1154 +     *
 24.1155 +     * <p> The string returned by this method is equal to that returned by the
 24.1156 +     * {@link #getRawSchemeSpecificPart() getRawSchemeSpecificPart} method
 24.1157 +     * except that all sequences of escaped octets are <a
 24.1158 +     * href="#decode">decoded</a>.  </p>
 24.1159 +     *
 24.1160 +     * @return  The decoded scheme-specific part of this URI
 24.1161 +     *          (never <tt>null</tt>)
 24.1162 +     */
 24.1163 +    public String getSchemeSpecificPart() {
 24.1164 +        if (decodedSchemeSpecificPart == null)
 24.1165 +            decodedSchemeSpecificPart = decode(getRawSchemeSpecificPart());
 24.1166 +        return decodedSchemeSpecificPart;
 24.1167 +    }
 24.1168 +
 24.1169 +    /**
 24.1170 +     * Returns the raw authority component of this URI.
 24.1171 +     *
 24.1172 +     * <p> The authority component of a URI, if defined, only contains the
 24.1173 +     * commercial-at character (<tt>'@'</tt>) and characters in the
 24.1174 +     * <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, and <i>other</i>
 24.1175 +     * categories.  If the authority is server-based then it is further
 24.1176 +     * constrained to have valid user-information, host, and port
 24.1177 +     * components. </p>
 24.1178 +     *
 24.1179 +     * @return  The raw authority component of this URI,
 24.1180 +     *          or <tt>null</tt> if the authority is undefined
 24.1181 +     */
 24.1182 +    public String getRawAuthority() {
 24.1183 +        return authority;
 24.1184 +    }
 24.1185 +
 24.1186 +    /**
 24.1187 +     * Returns the decoded authority component of this URI.
 24.1188 +     *
 24.1189 +     * <p> The string returned by this method is equal to that returned by the
 24.1190 +     * {@link #getRawAuthority() getRawAuthority} method except that all
 24.1191 +     * sequences of escaped octets are <a href="#decode">decoded</a>.  </p>
 24.1192 +     *
 24.1193 +     * @return  The decoded authority component of this URI,
 24.1194 +     *          or <tt>null</tt> if the authority is undefined
 24.1195 +     */
 24.1196 +    public String getAuthority() {
 24.1197 +        if (decodedAuthority == null)
 24.1198 +            decodedAuthority = decode(authority);
 24.1199 +        return decodedAuthority;
 24.1200 +    }
 24.1201 +
 24.1202 +    /**
 24.1203 +     * Returns the raw user-information component of this URI.
 24.1204 +     *
 24.1205 +     * <p> The user-information component of a URI, if defined, only contains
 24.1206 +     * characters in the <i>unreserved</i>, <i>punct</i>, <i>escaped</i>, and
 24.1207 +     * <i>other</i> categories. </p>
 24.1208 +     *
 24.1209 +     * @return  The raw user-information component of this URI,
 24.1210 +     *          or <tt>null</tt> if the user information is undefined
 24.1211 +     */
 24.1212 +    public String getRawUserInfo() {
 24.1213 +        return userInfo;
 24.1214 +    }
 24.1215 +
 24.1216 +    /**
 24.1217 +     * Returns the decoded user-information component of this URI.
 24.1218 +     *
 24.1219 +     * <p> The string returned by this method is equal to that returned by the
 24.1220 +     * {@link #getRawUserInfo() getRawUserInfo} method except that all
 24.1221 +     * sequences of escaped octets are <a href="#decode">decoded</a>.  </p>
 24.1222 +     *
 24.1223 +     * @return  The decoded user-information component of this URI,
 24.1224 +     *          or <tt>null</tt> if the user information is undefined
 24.1225 +     */
 24.1226 +    public String getUserInfo() {
 24.1227 +        if ((decodedUserInfo == null) && (userInfo != null))
 24.1228 +            decodedUserInfo = decode(userInfo);
 24.1229 +        return decodedUserInfo;
 24.1230 +    }
 24.1231 +
 24.1232 +    /**
 24.1233 +     * Returns the host component of this URI.
 24.1234 +     *
 24.1235 +     * <p> The host component of a URI, if defined, will have one of the
 24.1236 +     * following forms: </p>
 24.1237 +     *
 24.1238 +     * <ul type=disc>
 24.1239 +     *
 24.1240 +     *   <li><p> A domain name consisting of one or more <i>labels</i>
 24.1241 +     *   separated by period characters (<tt>'.'</tt>), optionally followed by
 24.1242 +     *   a period character.  Each label consists of <i>alphanum</i> characters
 24.1243 +     *   as well as hyphen characters (<tt>'-'</tt>), though hyphens never
 24.1244 +     *   occur as the first or last characters in a label. The rightmost
 24.1245 +     *   label of a domain name consisting of two or more labels, begins
 24.1246 +     *   with an <i>alpha</i> character. </li>
 24.1247 +     *
 24.1248 +     *   <li><p> A dotted-quad IPv4 address of the form
 24.1249 +     *   <i>digit</i><tt>+.</tt><i>digit</i><tt>+.</tt><i>digit</i><tt>+.</tt><i>digit</i><tt>+</tt>,
 24.1250 +     *   where no <i>digit</i> sequence is longer than three characters and no
 24.1251 +     *   sequence has a value larger than 255. </p></li>
 24.1252 +     *
 24.1253 +     *   <li><p> An IPv6 address enclosed in square brackets (<tt>'['</tt> and
 24.1254 +     *   <tt>']'</tt>) and consisting of hexadecimal digits, colon characters
 24.1255 +     *   (<tt>':'</tt>), and possibly an embedded IPv4 address.  The full
 24.1256 +     *   syntax of IPv6 addresses is specified in <a
 24.1257 +     *   href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC&nbsp;2373: IPv6
 24.1258 +     *   Addressing Architecture</i></a>.  </p></li>
 24.1259 +     *
 24.1260 +     * </ul>
 24.1261 +     *
 24.1262 +     * The host component of a URI cannot contain escaped octets, hence this
 24.1263 +     * method does not perform any decoding.
 24.1264 +     *
 24.1265 +     * @return  The host component of this URI,
 24.1266 +     *          or <tt>null</tt> if the host is undefined
 24.1267 +     */
 24.1268 +    public String getHost() {
 24.1269 +        return host;
 24.1270 +    }
 24.1271 +
 24.1272 +    /**
 24.1273 +     * Returns the port number of this URI.
 24.1274 +     *
 24.1275 +     * <p> The port component of a URI, if defined, is a non-negative
 24.1276 +     * integer. </p>
 24.1277 +     *
 24.1278 +     * @return  The port component of this URI,
 24.1279 +     *          or <tt>-1</tt> if the port is undefined
 24.1280 +     */
 24.1281 +    public int getPort() {
 24.1282 +        return port;
 24.1283 +    }
 24.1284 +
 24.1285 +    /**
 24.1286 +     * Returns the raw path component of this URI.
 24.1287 +     *
 24.1288 +     * <p> The path component of a URI, if defined, only contains the slash
 24.1289 +     * character (<tt>'/'</tt>), the commercial-at character (<tt>'@'</tt>),
 24.1290 +     * and characters in the <i>unreserved</i>, <i>punct</i>, <i>escaped</i>,
 24.1291 +     * and <i>other</i> categories. </p>
 24.1292 +     *
 24.1293 +     * @return  The path component of this URI,
 24.1294 +     *          or <tt>null</tt> if the path is undefined
 24.1295 +     */
 24.1296 +    public String getRawPath() {
 24.1297 +        return path;
 24.1298 +    }
 24.1299 +
 24.1300 +    /**
 24.1301 +     * Returns the decoded path component of this URI.
 24.1302 +     *
 24.1303 +     * <p> The string returned by this method is equal to that returned by the
 24.1304 +     * {@link #getRawPath() getRawPath} method except that all sequences of
 24.1305 +     * escaped octets are <a href="#decode">decoded</a>.  </p>
 24.1306 +     *
 24.1307 +     * @return  The decoded path component of this URI,
 24.1308 +     *          or <tt>null</tt> if the path is undefined
 24.1309 +     */
 24.1310 +    public String getPath() {
 24.1311 +        if ((decodedPath == null) && (path != null))
 24.1312 +            decodedPath = decode(path);
 24.1313 +        return decodedPath;
 24.1314 +    }
 24.1315 +
 24.1316 +    /**
 24.1317 +     * Returns the raw query component of this URI.
 24.1318 +     *
 24.1319 +     * <p> The query component of a URI, if defined, only contains legal URI
 24.1320 +     * characters. </p>
 24.1321 +     *
 24.1322 +     * @return  The raw query component of this URI,
 24.1323 +     *          or <tt>null</tt> if the query is undefined
 24.1324 +     */
 24.1325 +    public String getRawQuery() {
 24.1326 +        return query;
 24.1327 +    }
 24.1328 +
 24.1329 +    /**
 24.1330 +     * Returns the decoded query component of this URI.
 24.1331 +     *
 24.1332 +     * <p> The string returned by this method is equal to that returned by the
 24.1333 +     * {@link #getRawQuery() getRawQuery} method except that all sequences of
 24.1334 +     * escaped octets are <a href="#decode">decoded</a>.  </p>
 24.1335 +     *
 24.1336 +     * @return  The decoded query component of this URI,
 24.1337 +     *          or <tt>null</tt> if the query is undefined
 24.1338 +     */
 24.1339 +    public String getQuery() {
 24.1340 +        if ((decodedQuery == null) && (query != null))
 24.1341 +            decodedQuery = decode(query);
 24.1342 +        return decodedQuery;
 24.1343 +    }
 24.1344 +
 24.1345 +    /**
 24.1346 +     * Returns the raw fragment component of this URI.
 24.1347 +     *
 24.1348 +     * <p> The fragment component of a URI, if defined, only contains legal URI
 24.1349 +     * characters. </p>
 24.1350 +     *
 24.1351 +     * @return  The raw fragment component of this URI,
 24.1352 +     *          or <tt>null</tt> if the fragment is undefined
 24.1353 +     */
 24.1354 +    public String getRawFragment() {
 24.1355 +        return fragment;
 24.1356 +    }
 24.1357 +
 24.1358 +    /**
 24.1359 +     * Returns the decoded fragment component of this URI.
 24.1360 +     *
 24.1361 +     * <p> The string returned by this method is equal to that returned by the
 24.1362 +     * {@link #getRawFragment() getRawFragment} method except that all
 24.1363 +     * sequences of escaped octets are <a href="#decode">decoded</a>.  </p>
 24.1364 +     *
 24.1365 +     * @return  The decoded fragment component of this URI,
 24.1366 +     *          or <tt>null</tt> if the fragment is undefined
 24.1367 +     */
 24.1368 +    public String getFragment() {
 24.1369 +        if ((decodedFragment == null) && (fragment != null))
 24.1370 +            decodedFragment = decode(fragment);
 24.1371 +        return decodedFragment;
 24.1372 +    }
 24.1373 +
 24.1374 +
 24.1375 +    // -- Equality, comparison, hash code, toString, and serialization --
 24.1376 +
 24.1377 +    /**
 24.1378 +     * Tests this URI for equality with another object.
 24.1379 +     *
 24.1380 +     * <p> If the given object is not a URI then this method immediately
 24.1381 +     * returns <tt>false</tt>.
 24.1382 +     *
 24.1383 +     * <p> For two URIs to be considered equal requires that either both are
 24.1384 +     * opaque or both are hierarchical.  Their schemes must either both be
 24.1385 +     * undefined or else be equal without regard to case. Their fragments
 24.1386 +     * must either both be undefined or else be equal.
 24.1387 +     *
 24.1388 +     * <p> For two opaque URIs to be considered equal, their scheme-specific
 24.1389 +     * parts must be equal.
 24.1390 +     *
 24.1391 +     * <p> For two hierarchical URIs to be considered equal, their paths must
 24.1392 +     * be equal and their queries must either both be undefined or else be
 24.1393 +     * equal.  Their authorities must either both be undefined, or both be
 24.1394 +     * registry-based, or both be server-based.  If their authorities are
 24.1395 +     * defined and are registry-based, then they must be equal.  If their
 24.1396 +     * authorities are defined and are server-based, then their hosts must be
 24.1397 +     * equal without regard to case, their port numbers must be equal, and
 24.1398 +     * their user-information components must be equal.
 24.1399 +     *
 24.1400 +     * <p> When testing the user-information, path, query, fragment, authority,
 24.1401 +     * or scheme-specific parts of two URIs for equality, the raw forms rather
 24.1402 +     * than the encoded forms of these components are compared and the
 24.1403 +     * hexadecimal digits of escaped octets are compared without regard to
 24.1404 +     * case.
 24.1405 +     *
 24.1406 +     * <p> This method satisfies the general contract of the {@link
 24.1407 +     * java.lang.Object#equals(Object) Object.equals} method. </p>
 24.1408 +     *
 24.1409 +     * @param   ob   The object to which this object is to be compared
 24.1410 +     *
 24.1411 +     * @return  <tt>true</tt> if, and only if, the given object is a URI that
 24.1412 +     *          is identical to this URI
 24.1413 +     */
 24.1414 +    public boolean equals(Object ob) {
 24.1415 +        if (ob == this)
 24.1416 +            return true;
 24.1417 +        if (!(ob instanceof URI))
 24.1418 +            return false;
 24.1419 +        URI that = (URI)ob;
 24.1420 +        if (this.isOpaque() != that.isOpaque()) return false;
 24.1421 +        if (!equalIgnoringCase(this.scheme, that.scheme)) return false;
 24.1422 +        if (!equal(this.fragment, that.fragment)) return false;
 24.1423 +
 24.1424 +        // Opaque
 24.1425 +        if (this.isOpaque())
 24.1426 +            return equal(this.schemeSpecificPart, that.schemeSpecificPart);
 24.1427 +
 24.1428 +        // Hierarchical
 24.1429 +        if (!equal(this.path, that.path)) return false;
 24.1430 +        if (!equal(this.query, that.query)) return false;
 24.1431 +
 24.1432 +        // Authorities
 24.1433 +        if (this.authority == that.authority) return true;
 24.1434 +        if (this.host != null) {
 24.1435 +            // Server-based
 24.1436 +            if (!equal(this.userInfo, that.userInfo)) return false;
 24.1437 +            if (!equalIgnoringCase(this.host, that.host)) return false;
 24.1438 +            if (this.port != that.port) return false;
 24.1439 +        } else if (this.authority != null) {
 24.1440 +            // Registry-based
 24.1441 +            if (!equal(this.authority, that.authority)) return false;
 24.1442 +        } else if (this.authority != that.authority) {
 24.1443 +            return false;
 24.1444 +        }
 24.1445 +
 24.1446 +        return true;
 24.1447 +    }
 24.1448 +
 24.1449 +    /**
 24.1450 +     * Returns a hash-code value for this URI.  The hash code is based upon all
 24.1451 +     * of the URI's components, and satisfies the general contract of the
 24.1452 +     * {@link java.lang.Object#hashCode() Object.hashCode} method.
 24.1453 +     *
 24.1454 +     * @return  A hash-code value for this URI
 24.1455 +     */
 24.1456 +    public int hashCode() {
 24.1457 +        if (hash != 0)
 24.1458 +            return hash;
 24.1459 +        int h = hashIgnoringCase(0, scheme);
 24.1460 +        h = hash(h, fragment);
 24.1461 +        if (isOpaque()) {
 24.1462 +            h = hash(h, schemeSpecificPart);
 24.1463 +        } else {
 24.1464 +            h = hash(h, path);
 24.1465 +            h = hash(h, query);
 24.1466 +            if (host != null) {
 24.1467 +                h = hash(h, userInfo);
 24.1468 +                h = hashIgnoringCase(h, host);
 24.1469 +                h += 1949 * port;
 24.1470 +            } else {
 24.1471 +                h = hash(h, authority);
 24.1472 +            }
 24.1473 +        }
 24.1474 +        hash = h;
 24.1475 +        return h;
 24.1476 +    }
 24.1477 +
 24.1478 +    /**
 24.1479 +     * Compares this URI to another object, which must be a URI.
 24.1480 +     *
 24.1481 +     * <p> When comparing corresponding components of two URIs, if one
 24.1482 +     * component is undefined but the other is defined then the first is
 24.1483 +     * considered to be less than the second.  Unless otherwise noted, string
 24.1484 +     * components are ordered according to their natural, case-sensitive
 24.1485 +     * ordering as defined by the {@link java.lang.String#compareTo(Object)
 24.1486 +     * String.compareTo} method.  String components that are subject to
 24.1487 +     * encoding are compared by comparing their raw forms rather than their
 24.1488 +     * encoded forms.
 24.1489 +     *
 24.1490 +     * <p> The ordering of URIs is defined as follows: </p>
 24.1491 +     *
 24.1492 +     * <ul type=disc>
 24.1493 +     *
 24.1494 +     *   <li><p> Two URIs with different schemes are ordered according the
 24.1495 +     *   ordering of their schemes, without regard to case. </p></li>
 24.1496 +     *
 24.1497 +     *   <li><p> A hierarchical URI is considered to be less than an opaque URI
 24.1498 +     *   with an identical scheme. </p></li>
 24.1499 +     *
 24.1500 +     *   <li><p> Two opaque URIs with identical schemes are ordered according
 24.1501 +     *   to the ordering of their scheme-specific parts. </p></li>
 24.1502 +     *
 24.1503 +     *   <li><p> Two opaque URIs with identical schemes and scheme-specific
 24.1504 +     *   parts are ordered according to the ordering of their
 24.1505 +     *   fragments. </p></li>
 24.1506 +     *
 24.1507 +     *   <li><p> Two hierarchical URIs with identical schemes are ordered
 24.1508 +     *   according to the ordering of their authority components: </p>
 24.1509 +     *
 24.1510 +     *   <ul type=disc>
 24.1511 +     *
 24.1512 +     *     <li><p> If both authority components are server-based then the URIs
 24.1513 +     *     are ordered according to their user-information components; if these
 24.1514 +     *     components are identical then the URIs are ordered according to the
 24.1515 +     *     ordering of their hosts, without regard to case; if the hosts are
 24.1516 +     *     identical then the URIs are ordered according to the ordering of
 24.1517 +     *     their ports. </p></li>
 24.1518 +     *
 24.1519 +     *     <li><p> If one or both authority components are registry-based then
 24.1520 +     *     the URIs are ordered according to the ordering of their authority
 24.1521 +     *     components. </p></li>
 24.1522 +     *
 24.1523 +     *   </ul></li>
 24.1524 +     *
 24.1525 +     *   <li><p> Finally, two hierarchical URIs with identical schemes and
 24.1526 +     *   authority components are ordered according to the ordering of their
 24.1527 +     *   paths; if their paths are identical then they are ordered according to
 24.1528 +     *   the ordering of their queries; if the queries are identical then they
 24.1529 +     *   are ordered according to the order of their fragments. </p></li>
 24.1530 +     *
 24.1531 +     * </ul>
 24.1532 +     *
 24.1533 +     * <p> This method satisfies the general contract of the {@link
 24.1534 +     * java.lang.Comparable#compareTo(Object) Comparable.compareTo}
 24.1535 +     * method. </p>
 24.1536 +     *
 24.1537 +     * @param   that
 24.1538 +     *          The object to which this URI is to be compared
 24.1539 +     *
 24.1540 +     * @return  A negative integer, zero, or a positive integer as this URI is
 24.1541 +     *          less than, equal to, or greater than the given URI
 24.1542 +     *
 24.1543 +     * @throws  ClassCastException
 24.1544 +     *          If the given object is not a URI
 24.1545 +     */
 24.1546 +    public int compareTo(URI that) {
 24.1547 +        int c;
 24.1548 +
 24.1549 +        if ((c = compareIgnoringCase(this.scheme, that.scheme)) != 0)
 24.1550 +            return c;
 24.1551 +
 24.1552 +        if (this.isOpaque()) {
 24.1553 +            if (that.isOpaque()) {
 24.1554 +                // Both opaque
 24.1555 +                if ((c = compare(this.schemeSpecificPart,
 24.1556 +                                 that.schemeSpecificPart)) != 0)
 24.1557 +                    return c;
 24.1558 +                return compare(this.fragment, that.fragment);
 24.1559 +            }
 24.1560 +            return +1;                  // Opaque > hierarchical
 24.1561 +        } else if (that.isOpaque()) {
 24.1562 +            return -1;                  // Hierarchical < opaque
 24.1563 +        }
 24.1564 +
 24.1565 +        // Hierarchical
 24.1566 +        if ((this.host != null) && (that.host != null)) {
 24.1567 +            // Both server-based
 24.1568 +            if ((c = compare(this.userInfo, that.userInfo)) != 0)
 24.1569 +                return c;
 24.1570 +            if ((c = compareIgnoringCase(this.host, that.host)) != 0)
 24.1571 +                return c;
 24.1572 +            if ((c = this.port - that.port) != 0)
 24.1573 +                return c;
 24.1574 +        } else {
 24.1575 +            // If one or both authorities are registry-based then we simply
 24.1576 +            // compare them in the usual, case-sensitive way.  If one is
 24.1577 +            // registry-based and one is server-based then the strings are
 24.1578 +            // guaranteed to be unequal, hence the comparison will never return
 24.1579 +            // zero and the compareTo and equals methods will remain
 24.1580 +            // consistent.
 24.1581 +            if ((c = compare(this.authority, that.authority)) != 0) return c;
 24.1582 +        }
 24.1583 +
 24.1584 +        if ((c = compare(this.path, that.path)) != 0) return c;
 24.1585 +        if ((c = compare(this.query, that.query)) != 0) return c;
 24.1586 +        return compare(this.fragment, that.fragment);
 24.1587 +    }
 24.1588 +
 24.1589 +    /**
 24.1590 +     * Returns the content of this URI as a string.
 24.1591 +     *
 24.1592 +     * <p> If this URI was created by invoking one of the constructors in this
 24.1593 +     * class then a string equivalent to the original input string, or to the
 24.1594 +     * string computed from the originally-given components, as appropriate, is
 24.1595 +     * returned.  Otherwise this URI was created by normalization, resolution,
 24.1596 +     * or relativization, and so a string is constructed from this URI's
 24.1597 +     * components according to the rules specified in <a
 24.1598 +     * href="http://www.ietf.org/rfc/rfc2396.txt">RFC&nbsp;2396</a>,
 24.1599 +     * section&nbsp;5.2, step&nbsp;7. </p>
 24.1600 +     *
 24.1601 +     * @return  The string form of this URI
 24.1602 +     */
 24.1603 +    public String toString() {
 24.1604 +        defineString();
 24.1605 +        return string;
 24.1606 +    }
 24.1607 +
 24.1608 +    /**
 24.1609 +     * Returns the content of this URI as a US-ASCII string.
 24.1610 +     *
 24.1611 +     * <p> If this URI does not contain any characters in the <i>other</i>
 24.1612 +     * category then an invocation of this method will return the same value as
 24.1613 +     * an invocation of the {@link #toString() toString} method.  Otherwise
 24.1614 +     * this method works as if by invoking that method and then <a
 24.1615 +     * href="#encode">encoding</a> the result.  </p>
 24.1616 +     *
 24.1617 +     * @return  The string form of this URI, encoded as needed
 24.1618 +     *          so that it only contains characters in the US-ASCII
 24.1619 +     *          charset
 24.1620 +     */
 24.1621 +    public String toASCIIString() {
 24.1622 +        defineString();
 24.1623 +        return encode(string);
 24.1624 +    }
 24.1625 +
 24.1626 +
 24.1627 +    // -- Serialization support --
 24.1628 +
 24.1629 +    /**
 24.1630 +     * Saves the content of this URI to the given serial stream.
 24.1631 +     *
 24.1632 +     * <p> The only serializable field of a URI instance is its <tt>string</tt>
 24.1633 +     * field.  That field is given a value, if it does not have one already,
 24.1634 +     * and then the {@link java.io.ObjectOutputStream#defaultWriteObject()}
 24.1635 +     * method of the given object-output stream is invoked. </p>
 24.1636 +     *
 24.1637 +     * @param  os  The object-output stream to which this object
 24.1638 +     *             is to be written
 24.1639 +     */
 24.1640 +    private void writeObject(ObjectOutputStream os)
 24.1641 +        throws IOException
 24.1642 +    {
 24.1643 +        defineString();
 24.1644 +        os.defaultWriteObject();        // Writes the string field only
 24.1645 +    }
 24.1646 +
 24.1647 +    /**
 24.1648 +     * Reconstitutes a URI from the given serial stream.
 24.1649 +     *
 24.1650 +     * <p> The {@link java.io.ObjectInputStream#defaultReadObject()} method is
 24.1651 +     * invoked to read the value of the <tt>string</tt> field.  The result is
 24.1652 +     * then parsed in the usual way.
 24.1653 +     *
 24.1654 +     * @param  is  The object-input stream from which this object
 24.1655 +     *             is being read
 24.1656 +     */
 24.1657 +    private void readObject(ObjectInputStream is)
 24.1658 +        throws ClassNotFoundException, IOException
 24.1659 +    {
 24.1660 +        port = -1;                      // Argh
 24.1661 +        is.defaultReadObject();
 24.1662 +        try {
 24.1663 +            new Parser(string).parse(false);
 24.1664 +        } catch (URISyntaxException x) {
 24.1665 +            IOException y = new InvalidObjectException("Invalid URI");
 24.1666 +            y.initCause(x);
 24.1667 +            throw y;
 24.1668 +        }
 24.1669 +    }
 24.1670 +
 24.1671 +
 24.1672 +    // -- End of public methods --
 24.1673 +
 24.1674 +
 24.1675 +    // -- Utility methods for string-field comparison and hashing --
 24.1676 +
 24.1677 +    // These methods return appropriate values for null string arguments,
 24.1678 +    // thereby simplifying the equals, hashCode, and compareTo methods.
 24.1679 +    //
 24.1680 +    // The case-ignoring methods should only be applied to strings whose
 24.1681 +    // characters are all known to be US-ASCII.  Because of this restriction,
 24.1682 +    // these methods are faster than the similar methods in the String class.
 24.1683 +
 24.1684 +    // US-ASCII only
 24.1685 +    private static int toLower(char c) {
 24.1686 +        if ((c >= 'A') && (c <= 'Z'))
 24.1687 +            return c + ('a' - 'A');
 24.1688 +        return c;
 24.1689 +    }
 24.1690 +
 24.1691 +    private static boolean equal(String s, String t) {
 24.1692 +        if (s == t) return true;
 24.1693 +        if ((s != null) && (t != null)) {
 24.1694 +            if (s.length() != t.length())
 24.1695 +                return false;
 24.1696 +            if (s.indexOf('%') < 0)
 24.1697 +                return s.equals(t);
 24.1698 +            int n = s.length();
 24.1699 +            for (int i = 0; i < n;) {
 24.1700 +                char c = s.charAt(i);
 24.1701 +                char d = t.charAt(i);
 24.1702 +                if (c != '%') {
 24.1703 +                    if (c != d)
 24.1704 +                        return false;
 24.1705 +                    i++;
 24.1706 +                    continue;
 24.1707 +                }
 24.1708 +                i++;
 24.1709 +                if (toLower(s.charAt(i)) != toLower(t.charAt(i)))
 24.1710 +                    return false;
 24.1711 +                i++;
 24.1712 +                if (toLower(s.charAt(i)) != toLower(t.charAt(i)))
 24.1713 +                    return false;
 24.1714 +                i++;
 24.1715 +            }
 24.1716 +            return true;
 24.1717 +        }
 24.1718 +        return false;
 24.1719 +    }
 24.1720 +
 24.1721 +    // US-ASCII only
 24.1722 +    private static boolean equalIgnoringCase(String s, String t) {
 24.1723 +        if (s == t) return true;
 24.1724 +        if ((s != null) && (t != null)) {
 24.1725 +            int n = s.length();
 24.1726 +            if (t.length() != n)
 24.1727 +                return false;
 24.1728 +            for (int i = 0; i < n; i++) {
 24.1729 +                if (toLower(s.charAt(i)) != toLower(t.charAt(i)))
 24.1730 +                    return false;
 24.1731 +            }
 24.1732 +            return true;
 24.1733 +        }
 24.1734 +        return false;
 24.1735 +    }
 24.1736 +
 24.1737 +    private static int hash(int hash, String s) {
 24.1738 +        if (s == null) return hash;
 24.1739 +        return hash * 127 + s.hashCode();
 24.1740 +    }
 24.1741 +
 24.1742 +    // US-ASCII only
 24.1743 +    private static int hashIgnoringCase(int hash, String s) {
 24.1744 +        if (s == null) return hash;
 24.1745 +        int h = hash;
 24.1746 +        int n = s.length();
 24.1747 +        for (int i = 0; i < n; i++)
 24.1748 +            h = 31 * h + toLower(s.charAt(i));
 24.1749 +        return h;
 24.1750 +    }
 24.1751 +
 24.1752 +    private static int compare(String s, String t) {
 24.1753 +        if (s == t) return 0;
 24.1754 +        if (s != null) {
 24.1755 +            if (t != null)
 24.1756 +                return s.compareTo(t);
 24.1757 +            else
 24.1758 +                return +1;
 24.1759 +        } else {
 24.1760 +            return -1;
 24.1761 +        }
 24.1762 +    }
 24.1763 +
 24.1764 +    // US-ASCII only
 24.1765 +    private static int compareIgnoringCase(String s, String t) {
 24.1766 +        if (s == t) return 0;
 24.1767 +        if (s != null) {
 24.1768 +            if (t != null) {
 24.1769 +                int sn = s.length();
 24.1770 +                int tn = t.length();
 24.1771 +                int n = sn < tn ? sn : tn;
 24.1772 +                for (int i = 0; i < n; i++) {
 24.1773 +                    int c = toLower(s.charAt(i)) - toLower(t.charAt(i));
 24.1774 +                    if (c != 0)
 24.1775 +                        return c;
 24.1776 +                }
 24.1777 +                return sn - tn;
 24.1778 +            }
 24.1779 +            return +1;
 24.1780 +        } else {
 24.1781 +            return -1;
 24.1782 +        }
 24.1783 +    }
 24.1784 +
 24.1785 +
 24.1786 +    // -- String construction --
 24.1787 +
 24.1788 +    // If a scheme is given then the path, if given, must be absolute
 24.1789 +    //
 24.1790 +    private static void checkPath(String s, String scheme, String path)
 24.1791 +        throws URISyntaxException
 24.1792 +    {
 24.1793 +        if (scheme != null) {
 24.1794 +            if ((path != null)
 24.1795 +                && ((path.length() > 0) && (path.charAt(0) != '/')))
 24.1796 +                throw new URISyntaxException(s,
 24.1797 +                                             "Relative path in absolute URI");
 24.1798 +        }
 24.1799 +    }
 24.1800 +
 24.1801 +    private void appendAuthority(StringBuffer sb,
 24.1802 +                                 String authority,
 24.1803 +                                 String userInfo,
 24.1804 +                                 String host,
 24.1805 +                                 int port)
 24.1806 +    {
 24.1807 +        if (host != null) {
 24.1808 +            sb.append("//");
 24.1809 +            if (userInfo != null) {
 24.1810 +                sb.append(quote(userInfo, L_USERINFO, H_USERINFO));
 24.1811 +                sb.append('@');
 24.1812 +            }
 24.1813 +            boolean needBrackets = ((host.indexOf(':') >= 0)
 24.1814 +                                    && !host.startsWith("[")
 24.1815 +                                    && !host.endsWith("]"));
 24.1816 +            if (needBrackets) sb.append('[');
 24.1817 +            sb.append(host);
 24.1818 +            if (needBrackets) sb.append(']');
 24.1819 +            if (port != -1) {
 24.1820 +                sb.append(':');
 24.1821 +                sb.append(port);
 24.1822 +            }
 24.1823 +        } else if (authority != null) {
 24.1824 +            sb.append("//");
 24.1825 +            if (authority.startsWith("[")) {
 24.1826 +                // authority should (but may not) contain an embedded IPv6 address
 24.1827 +                int end = authority.indexOf("]");
 24.1828 +                String doquote = authority, dontquote = "";
 24.1829 +                if (end != -1 && authority.indexOf(":") != -1) {
 24.1830 +                    // the authority contains an IPv6 address
 24.1831 +                    if (end == authority.length()) {
 24.1832 +                        dontquote = authority;
 24.1833 +                        doquote = "";
 24.1834 +                    } else {
 24.1835 +                        dontquote = authority.substring(0 , end + 1);
 24.1836 +                        doquote = authority.substring(end + 1);
 24.1837 +                    }
 24.1838 +                }
 24.1839 +                sb.append(dontquote);
 24.1840 +                sb.append(quote(doquote,
 24.1841 +                            L_REG_NAME | L_SERVER,
 24.1842 +                            H_REG_NAME | H_SERVER));
 24.1843 +            } else {
 24.1844 +                sb.append(quote(authority,
 24.1845 +                            L_REG_NAME | L_SERVER,
 24.1846 +                            H_REG_NAME | H_SERVER));
 24.1847 +            }
 24.1848 +        }
 24.1849 +    }
 24.1850 +
 24.1851 +    private void appendSchemeSpecificPart(StringBuffer sb,
 24.1852 +                                          String opaquePart,
 24.1853 +                                          String authority,
 24.1854 +                                          String userInfo,
 24.1855 +                                          String host,
 24.1856 +                                          int port,
 24.1857 +                                          String path,
 24.1858 +                                          String query)
 24.1859 +    {
 24.1860 +        if (opaquePart != null) {
 24.1861 +            /* check if SSP begins with an IPv6 address
 24.1862 +             * because we must not quote a literal IPv6 address
 24.1863 +             */
 24.1864 +            if (opaquePart.startsWith("//[")) {
 24.1865 +                int end =  opaquePart.indexOf("]");
 24.1866 +                if (end != -1 && opaquePart.indexOf(":")!=-1) {
 24.1867 +                    String doquote, dontquote;
 24.1868 +                    if (end == opaquePart.length()) {
 24.1869 +                        dontquote = opaquePart;
 24.1870 +                        doquote = "";
 24.1871 +                    } else {
 24.1872 +                        dontquote = opaquePart.substring(0,end+1);
 24.1873 +                        doquote = opaquePart.substring(end+1);
 24.1874 +                    }
 24.1875 +                    sb.append (dontquote);
 24.1876 +                    sb.append(quote(doquote, L_URIC, H_URIC));
 24.1877 +                }
 24.1878 +            } else {
 24.1879 +                sb.append(quote(opaquePart, L_URIC, H_URIC));
 24.1880 +            }
 24.1881 +        } else {
 24.1882 +            appendAuthority(sb, authority, userInfo, host, port);
 24.1883 +            if (path != null)
 24.1884 +                sb.append(quote(path, L_PATH, H_PATH));
 24.1885 +            if (query != null) {
 24.1886 +                sb.append('?');
 24.1887 +                sb.append(quote(query, L_URIC, H_URIC));
 24.1888 +            }
 24.1889 +        }
 24.1890 +    }
 24.1891 +
 24.1892 +    private void appendFragment(StringBuffer sb, String fragment) {
 24.1893 +        if (fragment != null) {
 24.1894 +            sb.append('#');
 24.1895 +            sb.append(quote(fragment, L_URIC, H_URIC));
 24.1896 +        }
 24.1897 +    }
 24.1898 +
 24.1899 +    private String toString(String scheme,
 24.1900 +                            String opaquePart,
 24.1901 +                            String authority,
 24.1902 +                            String userInfo,
 24.1903 +                            String host,
 24.1904 +                            int port,
 24.1905 +                            String path,
 24.1906 +                            String query,
 24.1907 +                            String fragment)
 24.1908 +    {
 24.1909 +        StringBuffer sb = new StringBuffer();
 24.1910 +        if (scheme != null) {
 24.1911 +            sb.append(scheme);
 24.1912 +            sb.append(':');
 24.1913 +        }
 24.1914 +        appendSchemeSpecificPart(sb, opaquePart,
 24.1915 +                                 authority, userInfo, host, port,
 24.1916 +                                 path, query);
 24.1917 +        appendFragment(sb, fragment);
 24.1918 +        return sb.toString();
 24.1919 +    }
 24.1920 +
 24.1921 +    private void defineSchemeSpecificPart() {
 24.1922 +        if (schemeSpecificPart != null) return;
 24.1923 +        StringBuffer sb = new StringBuffer();
 24.1924 +        appendSchemeSpecificPart(sb, null, getAuthority(), getUserInfo(),
 24.1925 +                                 host, port, getPath(), getQuery());
 24.1926 +        if (sb.length() == 0) return;
 24.1927 +        schemeSpecificPart = sb.toString();
 24.1928 +    }
 24.1929 +
 24.1930 +    private void defineString() {
 24.1931 +        if (string != null) return;
 24.1932 +
 24.1933 +        StringBuffer sb = new StringBuffer();
 24.1934 +        if (scheme != null) {
 24.1935 +            sb.append(scheme);
 24.1936 +            sb.append(':');
 24.1937 +        }
 24.1938 +        if (isOpaque()) {
 24.1939 +            sb.append(schemeSpecificPart);
 24.1940 +        } else {
 24.1941 +            if (host != null) {
 24.1942 +                sb.append("//");
 24.1943 +                if (userInfo != null) {
 24.1944 +                    sb.append(userInfo);
 24.1945 +                    sb.append('@');
 24.1946 +                }
 24.1947 +                boolean needBrackets = ((host.indexOf(':') >= 0)
 24.1948 +                                    && !host.startsWith("[")
 24.1949 +                                    && !host.endsWith("]"));
 24.1950 +                if (needBrackets) sb.append('[');
 24.1951 +                sb.append(host);
 24.1952 +                if (needBrackets) sb.append(']');
 24.1953 +                if (port != -1) {
 24.1954 +                    sb.append(':');
 24.1955 +                    sb.append(port);
 24.1956 +                }
 24.1957 +            } else if (authority != null) {
 24.1958 +                sb.append("//");
 24.1959 +                sb.append(authority);
 24.1960 +            }
 24.1961 +            if (path != null)
 24.1962 +                sb.append(path);
 24.1963 +            if (query != null) {
 24.1964 +                sb.append('?');
 24.1965 +                sb.append(query);
 24.1966 +            }
 24.1967 +        }
 24.1968 +        if (fragment != null) {
 24.1969 +            sb.append('#');
 24.1970 +            sb.append(fragment);
 24.1971 +        }
 24.1972 +        string = sb.toString();
 24.1973 +    }
 24.1974 +
 24.1975 +
 24.1976 +    // -- Normalization, resolution, and relativization --
 24.1977 +
 24.1978 +    // RFC2396 5.2 (6)
 24.1979 +    private static String resolvePath(String base, String child,
 24.1980 +                                      boolean absolute)
 24.1981 +    {
 24.1982 +        int i = base.lastIndexOf('/');
 24.1983 +        int cn = child.length();
 24.1984 +        String path = "";
 24.1985 +
 24.1986 +        if (cn == 0) {
 24.1987 +            // 5.2 (6a)
 24.1988 +            if (i >= 0)
 24.1989 +                path = base.substring(0, i + 1);
 24.1990 +        } else {
 24.1991 +            StringBuffer sb = new StringBuffer(base.length() + cn);
 24.1992 +            // 5.2 (6a)
 24.1993 +            if (i >= 0)
 24.1994 +                sb.append(base.substring(0, i + 1));
 24.1995 +            // 5.2 (6b)
 24.1996 +            sb.append(child);
 24.1997 +            path = sb.toString();
 24.1998 +        }
 24.1999 +
 24.2000 +        // 5.2 (6c-f)
 24.2001 +        String np = normalize(path);
 24.2002 +
 24.2003 +        // 5.2 (6g): If the result is absolute but the path begins with "../",
 24.2004 +        // then we simply leave the path as-is
 24.2005 +
 24.2006 +        return np;
 24.2007 +    }
 24.2008 +
 24.2009 +    // RFC2396 5.2
 24.2010 +    private static URI resolve(URI base, URI child) {
 24.2011 +        // check if child if opaque first so that NPE is thrown
 24.2012 +        // if child is null.
 24.2013 +        if (child.isOpaque() || base.isOpaque())
 24.2014 +            return child;
 24.2015 +
 24.2016 +        // 5.2 (2): Reference to current document (lone fragment)
 24.2017 +        if ((child.scheme == null) && (child.authority == null)
 24.2018 +            && child.path.equals("") && (child.fragment != null)
 24.2019 +            && (child.query == null)) {
 24.2020 +            if ((base.fragment != null)
 24.2021 +                && child.fragment.equals(base.fragment)) {
 24.2022 +                return base;
 24.2023 +            }
 24.2024 +            URI ru = new URI();
 24.2025 +            ru.scheme = base.scheme;
 24.2026 +            ru.authority = base.authority;
 24.2027 +            ru.userInfo = base.userInfo;
 24.2028 +            ru.host = base.host;
 24.2029 +            ru.port = base.port;
 24.2030 +            ru.path = base.path;
 24.2031 +            ru.fragment = child.fragment;
 24.2032 +            ru.query = base.query;
 24.2033 +            return ru;
 24.2034 +        }
 24.2035 +
 24.2036 +        // 5.2 (3): Child is absolute
 24.2037 +        if (child.scheme != null)
 24.2038 +            return child;
 24.2039 +
 24.2040 +        URI ru = new URI();             // Resolved URI
 24.2041 +        ru.scheme = base.scheme;
 24.2042 +        ru.query = child.query;
 24.2043 +        ru.fragment = child.fragment;
 24.2044 +
 24.2045 +        // 5.2 (4): Authority
 24.2046 +        if (child.authority == null) {
 24.2047 +            ru.authority = base.authority;
 24.2048 +            ru.host = base.host;
 24.2049 +            ru.userInfo = base.userInfo;
 24.2050 +            ru.port = base.port;
 24.2051 +
 24.2052 +            String cp = (child.path == null) ? "" : child.path;
 24.2053 +            if ((cp.length() > 0) && (cp.charAt(0) == '/')) {
 24.2054 +                // 5.2 (5): Child path is absolute
 24.2055 +                ru.path = child.path;
 24.2056 +            } else {
 24.2057 +                // 5.2 (6): Resolve relative path
 24.2058 +                ru.path = resolvePath(base.path, cp, base.isAbsolute());
 24.2059 +            }
 24.2060 +        } else {
 24.2061 +            ru.authority = child.authority;
 24.2062 +            ru.host = child.host;
 24.2063 +            ru.userInfo = child.userInfo;
 24.2064 +            ru.host = child.host;
 24.2065 +            ru.port = child.port;
 24.2066 +            ru.path = child.path;
 24.2067 +        }
 24.2068 +
 24.2069 +        // 5.2 (7): Recombine (nothing to do here)
 24.2070 +        return ru;
 24.2071 +    }
 24.2072 +
 24.2073 +    // If the given URI's path is normal then return the URI;
 24.2074 +    // o.w., return a new URI containing the normalized path.
 24.2075 +    //
 24.2076 +    private static URI normalize(URI u) {
 24.2077 +        if (u.isOpaque() || (u.path == null) || (u.path.length() == 0))
 24.2078 +            return u;
 24.2079 +
 24.2080 +        String np = normalize(u.path);
 24.2081 +        if (np == u.path)
 24.2082 +            return u;
 24.2083 +
 24.2084 +        URI v = new URI();
 24.2085 +        v.scheme = u.scheme;
 24.2086 +        v.fragment = u.fragment;
 24.2087 +        v.authority = u.authority;
 24.2088 +        v.userInfo = u.userInfo;
 24.2089 +        v.host = u.host;
 24.2090 +        v.port = u.port;
 24.2091 +        v.path = np;
 24.2092 +        v.query = u.query;
 24.2093 +        return v;
 24.2094 +    }
 24.2095 +
 24.2096 +    // If both URIs are hierarchical, their scheme and authority components are
 24.2097 +    // identical, and the base path is a prefix of the child's path, then
 24.2098 +    // return a relative URI that, when resolved against the base, yields the
 24.2099 +    // child; otherwise, return the child.
 24.2100 +    //
 24.2101 +    private static URI relativize(URI base, URI child) {
 24.2102 +        // check if child if opaque first so that NPE is thrown
 24.2103 +        // if child is null.
 24.2104 +        if (child.isOpaque() || base.isOpaque())
 24.2105 +            return child;
 24.2106 +        if (!equalIgnoringCase(base.scheme, child.scheme)
 24.2107 +            || !equal(base.authority, child.authority))
 24.2108 +            return child;
 24.2109 +
 24.2110 +        String bp = normalize(base.path);
 24.2111 +        String cp = normalize(child.path);
 24.2112 +        if (!bp.equals(cp)) {
 24.2113 +            if (!bp.endsWith("/"))
 24.2114 +                bp = bp + "/";
 24.2115 +            if (!cp.startsWith(bp))
 24.2116 +                return child;
 24.2117 +        }
 24.2118 +
 24.2119 +        URI v = new URI();
 24.2120 +        v.path = cp.substring(bp.length());
 24.2121 +        v.query = child.query;
 24.2122 +        v.fragment = child.fragment;
 24.2123 +        return v;
 24.2124 +    }
 24.2125 +
 24.2126 +
 24.2127 +
 24.2128 +    // -- Path normalization --
 24.2129 +
 24.2130 +    // The following algorithm for path normalization avoids the creation of a
 24.2131 +    // string object for each segment, as well as the use of a string buffer to
 24.2132 +    // compute the final result, by using a single char array and editing it in
 24.2133 +    // place.  The array is first split into segments, replacing each slash
 24.2134 +    // with '\0' and creating a segment-index array, each element of which is
 24.2135 +    // the index of the first char in the corresponding segment.  We then walk
 24.2136 +    // through both arrays, removing ".", "..", and other segments as necessary
 24.2137 +    // by setting their entries in the index array to -1.  Finally, the two
 24.2138 +    // arrays are used to rejoin the segments and compute the final result.
 24.2139 +    //
 24.2140 +    // This code is based upon src/solaris/native/java/io/canonicalize_md.c
 24.2141 +
 24.2142 +
 24.2143 +    // Check the given path to see if it might need normalization.  A path
 24.2144 +    // might need normalization if it contains duplicate slashes, a "."
 24.2145 +    // segment, or a ".." segment.  Return -1 if no further normalization is
 24.2146 +    // possible, otherwise return the number of segments found.
 24.2147 +    //
 24.2148 +    // This method takes a string argument rather than a char array so that
 24.2149 +    // this test can be performed without invoking path.toCharArray().
 24.2150 +    //
 24.2151 +    static private int needsNormalization(String path) {
 24.2152 +        boolean normal = true;
 24.2153 +        int ns = 0;                     // Number of segments
 24.2154 +        int end = path.length() - 1;    // Index of last char in path
 24.2155 +        int p = 0;                      // Index of next char in path
 24.2156 +
 24.2157 +        // Skip initial slashes
 24.2158 +        while (p <= end) {
 24.2159 +            if (path.charAt(p) != '/') break;
 24.2160 +            p++;
 24.2161 +        }
 24.2162 +        if (p > 1) normal = false;
 24.2163 +
 24.2164 +        // Scan segments
 24.2165 +        while (p <= end) {
 24.2166 +
 24.2167 +            // Looking at "." or ".." ?
 24.2168 +            if ((path.charAt(p) == '.')
 24.2169 +                && ((p == end)
 24.2170 +                    || ((path.charAt(p + 1) == '/')
 24.2171 +                        || ((path.charAt(p + 1) == '.')
 24.2172 +                            && ((p + 1 == end)
 24.2173 +                                || (path.charAt(p + 2) == '/')))))) {
 24.2174 +                normal = false;
 24.2175 +            }
 24.2176 +            ns++;
 24.2177 +
 24.2178 +            // Find beginning of next segment
 24.2179 +            while (p <= end) {
 24.2180 +                if (path.charAt(p++) != '/')
 24.2181 +                    continue;
 24.2182 +
 24.2183 +                // Skip redundant slashes
 24.2184 +                while (p <= end) {
 24.2185 +                    if (path.charAt(p) != '/') break;
 24.2186 +                    normal = false;
 24.2187 +                    p++;
 24.2188 +                }
 24.2189 +
 24.2190 +                break;
 24.2191 +            }
 24.2192 +        }
 24.2193 +
 24.2194 +        return normal ? -1 : ns;
 24.2195 +    }
 24.2196 +
 24.2197 +
 24.2198 +    // Split the given path into segments, replacing slashes with nulls and
 24.2199 +    // filling in the given segment-index array.
 24.2200 +    //
 24.2201 +    // Preconditions:
 24.2202 +    //   segs.length == Number of segments in path
 24.2203 +    //
 24.2204 +    // Postconditions:
 24.2205 +    //   All slashes in path replaced by '\0'
 24.2206 +    //   segs[i] == Index of first char in segment i (0 <= i < segs.length)
 24.2207 +    //
 24.2208 +    static private void split(char[] path, int[] segs) {
 24.2209 +        int end = path.length - 1;      // Index of last char in path
 24.2210 +        int p = 0;                      // Index of next char in path
 24.2211 +        int i = 0;                      // Index of current segment
 24.2212 +
 24.2213 +        // Skip initial slashes
 24.2214 +        while (p <= end) {
 24.2215 +            if (path[p] != '/') break;
 24.2216 +            path[p] = '\0';
 24.2217 +            p++;
 24.2218 +        }
 24.2219 +
 24.2220 +        while (p <= end) {
 24.2221 +
 24.2222 +            // Note start of segment
 24.2223 +            segs[i++] = p++;
 24.2224 +
 24.2225 +            // Find beginning of next segment
 24.2226 +            while (p <= end) {
 24.2227 +                if (path[p++] != '/')
 24.2228 +                    continue;
 24.2229 +                path[p - 1] = '\0';
 24.2230 +
 24.2231 +                // Skip redundant slashes
 24.2232 +                while (p <= end) {
 24.2233 +                    if (path[p] != '/') break;
 24.2234 +                    path[p++] = '\0';
 24.2235 +                }
 24.2236 +                break;
 24.2237 +            }
 24.2238 +        }
 24.2239 +
 24.2240 +        if (i != segs.length)
 24.2241 +            throw new InternalError();  // ASSERT
 24.2242 +    }
 24.2243 +
 24.2244 +
 24.2245 +    // Join the segments in the given path according to the given segment-index
 24.2246 +    // array, ignoring those segments whose index entries have been set to -1,
 24.2247 +    // and inserting slashes as needed.  Return the length of the resulting
 24.2248 +    // path.
 24.2249 +    //
 24.2250 +    // Preconditions:
 24.2251 +    //   segs[i] == -1 implies segment i is to be ignored
 24.2252 +    //   path computed by split, as above, with '\0' having replaced '/'
 24.2253 +    //
 24.2254 +    // Postconditions:
 24.2255 +    //   path[0] .. path[return value] == Resulting path
 24.2256 +    //
 24.2257 +    static private int join(char[] path, int[] segs) {
 24.2258 +        int ns = segs.length;           // Number of segments
 24.2259 +        int end = path.length - 1;      // Index of last char in path
 24.2260 +        int p = 0;                      // Index of next path char to write
 24.2261 +
 24.2262 +        if (path[p] == '\0') {
 24.2263 +            // Restore initial slash for absolute paths
 24.2264 +            path[p++] = '/';
 24.2265 +        }
 24.2266 +
 24.2267 +        for (int i = 0; i < ns; i++) {
 24.2268 +            int q = segs[i];            // Current segment
 24.2269 +            if (q == -1)
 24.2270 +                // Ignore this segment
 24.2271 +                continue;
 24.2272 +
 24.2273 +            if (p == q) {
 24.2274 +                // We're already at this segment, so just skip to its end
 24.2275 +                while ((p <= end) && (path[p] != '\0'))
 24.2276 +                    p++;
 24.2277 +                if (p <= end) {
 24.2278 +                    // Preserve trailing slash
 24.2279 +                    path[p++] = '/';
 24.2280 +                }
 24.2281 +            } else if (p < q) {
 24.2282 +                // Copy q down to p
 24.2283 +                while ((q <= end) && (path[q] != '\0'))
 24.2284 +                    path[p++] = path[q++];
 24.2285 +                if (q <= end) {
 24.2286 +                    // Preserve trailing slash
 24.2287 +                    path[p++] = '/';
 24.2288 +                }
 24.2289 +            } else
 24.2290 +                throw new InternalError(); // ASSERT false
 24.2291 +        }
 24.2292 +
 24.2293 +        return p;
 24.2294 +    }
 24.2295 +
 24.2296 +
 24.2297 +    // Remove "." segments from the given path, and remove segment pairs
 24.2298 +    // consisting of a non-".." segment followed by a ".." segment.
 24.2299 +    //
 24.2300 +    private static void removeDots(char[] path, int[] segs) {
 24.2301 +        int ns = segs.length;
 24.2302 +        int end = path.length - 1;
 24.2303 +
 24.2304 +        for (int i = 0; i < ns; i++) {
 24.2305 +            int dots = 0;               // Number of dots found (0, 1, or 2)
 24.2306 +
 24.2307 +            // Find next occurrence of "." or ".."
 24.2308 +            do {
 24.2309 +                int p = segs[i];
 24.2310 +                if (path[p] == '.') {
 24.2311 +                    if (p == end) {
 24.2312 +                        dots = 1;
 24.2313 +                        break;
 24.2314 +                    } else if (path[p + 1] == '\0') {
 24.2315 +                        dots = 1;
 24.2316 +                        break;
 24.2317 +                    } else if ((path[p + 1] == '.')
 24.2318 +                               && ((p + 1 == end)
 24.2319 +                                   || (path[p + 2] == '\0'))) {
 24.2320 +                        dots = 2;
 24.2321 +                        break;
 24.2322 +                    }
 24.2323 +                }
 24.2324 +                i++;
 24.2325 +            } while (i < ns);
 24.2326 +            if ((i > ns) || (dots == 0))
 24.2327 +                break;
 24.2328 +
 24.2329 +            if (dots == 1) {
 24.2330 +                // Remove this occurrence of "."
 24.2331 +                segs[i] = -1;
 24.2332 +            } else {
 24.2333 +                // If there is a preceding non-".." segment, remove both that
 24.2334 +                // segment and this occurrence of ".."; otherwise, leave this
 24.2335 +                // ".." segment as-is.
 24.2336 +                int j;
 24.2337 +                for (j = i - 1; j >= 0; j--) {
 24.2338 +                    if (segs[j] != -1) break;
 24.2339 +                }
 24.2340 +                if (j >= 0) {
 24.2341 +                    int q = segs[j];
 24.2342 +                    if (!((path[q] == '.')
 24.2343 +                          && (path[q + 1] == '.')
 24.2344 +                          && (path[q + 2] == '\0'))) {
 24.2345 +                        segs[i] = -1;
 24.2346 +                        segs[j] = -1;
 24.2347 +                    }
 24.2348 +                }
 24.2349 +            }
 24.2350 +        }
 24.2351 +    }
 24.2352 +
 24.2353 +
 24.2354 +    // DEVIATION: If the normalized path is relative, and if the first
 24.2355 +    // segment could be parsed as a scheme name, then prepend a "." segment
 24.2356 +    //
 24.2357 +    private static void maybeAddLeadingDot(char[] path, int[] segs) {
 24.2358 +
 24.2359 +        if (path[0] == '\0')
 24.2360 +            // The path is absolute
 24.2361 +            return;
 24.2362 +
 24.2363 +        int ns = segs.length;
 24.2364 +        int f = 0;                      // Index of first segment
 24.2365 +        while (f < ns) {
 24.2366 +            if (segs[f] >= 0)
 24.2367 +                break;
 24.2368 +            f++;
 24.2369 +        }
 24.2370 +        if ((f >= ns) || (f == 0))
 24.2371 +            // The path is empty, or else the original first segment survived,
 24.2372 +            // in which case we already know that no leading "." is needed
 24.2373 +            return;
 24.2374 +
 24.2375 +        int p = segs[f];
 24.2376 +        while ((p < path.length) && (path[p] != ':') && (path[p] != '\0')) p++;
 24.2377 +        if (p >= path.length || path[p] == '\0')
 24.2378 +            // No colon in first segment, so no "." needed
 24.2379 +            return;
 24.2380 +
 24.2381 +        // At this point we know that the first segment is unused,
 24.2382 +        // hence we can insert a "." segment at that position
 24.2383 +        path[0] = '.';
 24.2384 +        path[1] = '\0';
 24.2385 +        segs[0] = 0;
 24.2386 +    }
 24.2387 +
 24.2388 +
 24.2389 +    // Normalize the given path string.  A normal path string has no empty
 24.2390 +    // segments (i.e., occurrences of "//"), no segments equal to ".", and no
 24.2391 +    // segments equal to ".." that are preceded by a segment not equal to "..".
 24.2392 +    // In contrast to Unix-style pathname normalization, for URI paths we
 24.2393 +    // always retain trailing slashes.
 24.2394 +    //
 24.2395 +    private static String normalize(String ps) {
 24.2396 +
 24.2397 +        // Does this path need normalization?
 24.2398 +        int ns = needsNormalization(ps);        // Number of segments
 24.2399 +        if (ns < 0)
 24.2400 +            // Nope -- just return it
 24.2401 +            return ps;
 24.2402 +
 24.2403 +        char[] path = ps.toCharArray();         // Path in char-array form
 24.2404 +
 24.2405 +        // Split path into segments
 24.2406 +        int[] segs = new int[ns];               // Segment-index array
 24.2407 +        split(path, segs);
 24.2408 +
 24.2409 +        // Remove dots
 24.2410 +        removeDots(path, segs);
 24.2411 +
 24.2412 +        // Prevent scheme-name confusion
 24.2413 +        maybeAddLeadingDot(path, segs);
 24.2414 +
 24.2415 +        // Join the remaining segments and return the result
 24.2416 +        String s = new String(path, 0, join(path, segs));
 24.2417 +        if (s.equals(ps)) {
 24.2418 +            // string was already normalized
 24.2419 +            return ps;
 24.2420 +        }
 24.2421 +        return s;
 24.2422 +    }
 24.2423 +
 24.2424 +
 24.2425 +
 24.2426 +    // -- Character classes for parsing --
 24.2427 +
 24.2428 +    // RFC2396 precisely specifies which characters in the US-ASCII charset are
 24.2429 +    // permissible in the various components of a URI reference.  We here
 24.2430 +    // define a set of mask pairs to aid in enforcing these restrictions.  Each
 24.2431 +    // mask pair consists of two longs, a low mask and a high mask.  Taken
 24.2432 +    // together they represent a 128-bit mask, where bit i is set iff the
 24.2433 +    // character with value i is permitted.
 24.2434 +    //
 24.2435 +    // This approach is more efficient than sequentially searching arrays of
 24.2436 +    // permitted characters.  It could be made still more efficient by
 24.2437 +    // precompiling the mask information so that a character's presence in a
 24.2438 +    // given mask could be determined by a single table lookup.
 24.2439 +
 24.2440 +    // Compute the low-order mask for the characters in the given string
 24.2441 +    private static long lowMask(String chars) {
 24.2442 +        int n = chars.length();
 24.2443 +        long m = 0;
 24.2444 +        for (int i = 0; i < n; i++) {
 24.2445 +            char c = chars.charAt(i);
 24.2446 +            if (c < 64)
 24.2447 +                m |= (1L << c);
 24.2448 +        }
 24.2449 +        return m;
 24.2450 +    }
 24.2451 +
 24.2452 +    // Compute the high-order mask for the characters in the given string
 24.2453 +    private static long highMask(String chars) {
 24.2454 +        int n = chars.length();
 24.2455 +        long m = 0;
 24.2456 +        for (int i = 0; i < n; i++) {
 24.2457 +            char c = chars.charAt(i);
 24.2458 +            if ((c >= 64) && (c < 128))
 24.2459 +                m |= (1L << (c - 64));
 24.2460 +        }
 24.2461 +        return m;
 24.2462 +    }
 24.2463 +
 24.2464 +    // Compute a low-order mask for the characters
 24.2465 +    // between first and last, inclusive
 24.2466 +    private static long lowMask(char first, char last) {
 24.2467 +        long m = 0;
 24.2468 +        int f = Math.max(Math.min(first, 63), 0);
 24.2469 +        int l = Math.max(Math.min(last, 63), 0);
 24.2470 +        for (int i = f; i <= l; i++)
 24.2471 +            m |= 1L << i;
 24.2472 +        return m;
 24.2473 +    }
 24.2474 +
 24.2475 +    // Compute a high-order mask for the characters
 24.2476 +    // between first and last, inclusive
 24.2477 +    private static long highMask(char first, char last) {
 24.2478 +        long m = 0;
 24.2479 +        int f = Math.max(Math.min(first, 127), 64) - 64;
 24.2480 +        int l = Math.max(Math.min(last, 127), 64) - 64;
 24.2481 +        for (int i = f; i <= l; i++)
 24.2482 +            m |= 1L << i;
 24.2483 +        return m;
 24.2484 +    }
 24.2485 +
 24.2486 +    // Tell whether the given character is permitted by the given mask pair
 24.2487 +    private static boolean match(char c, long lowMask, long highMask) {
 24.2488 +        if (c == 0) // 0 doesn't have a slot in the mask. So, it never matches.
 24.2489 +            return false;
 24.2490 +        if (c < 64)
 24.2491 +            return ((1L << c) & lowMask) != 0;
 24.2492 +        if (c < 128)
 24.2493 +            return ((1L << (c - 64)) & highMask) != 0;
 24.2494 +        return false;
 24.2495 +    }
 24.2496 +
 24.2497 +    // Character-class masks, in reverse order from RFC2396 because
 24.2498 +    // initializers for static fields cannot make forward references.
 24.2499 +
 24.2500 +    // digit    = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
 24.2501 +    //            "8" | "9"
 24.2502 +    private static final long L_DIGIT = lowMask('0', '9');
 24.2503 +    private static final long H_DIGIT = 0L;
 24.2504 +
 24.2505 +    // upalpha  = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |
 24.2506 +    //            "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |
 24.2507 +    //            "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"
 24.2508 +    private static final long L_UPALPHA = 0L;
 24.2509 +    private static final long H_UPALPHA = highMask('A', 'Z');
 24.2510 +
 24.2511 +    // lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |
 24.2512 +    //            "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |
 24.2513 +    //            "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
 24.2514 +    private static final long L_LOWALPHA = 0L;
 24.2515 +    private static final long H_LOWALPHA = highMask('a', 'z');
 24.2516 +
 24.2517 +    // alpha         = lowalpha | upalpha
 24.2518 +    private static final long L_ALPHA = L_LOWALPHA | L_UPALPHA;
 24.2519 +    private static final long H_ALPHA = H_LOWALPHA | H_UPALPHA;
 24.2520 +
 24.2521 +    // alphanum      = alpha | digit
 24.2522 +    private static final long L_ALPHANUM = L_DIGIT | L_ALPHA;
 24.2523 +    private static final long H_ALPHANUM = H_DIGIT | H_ALPHA;
 24.2524 +
 24.2525 +    // hex           = digit | "A" | "B" | "C" | "D" | "E" | "F" |
 24.2526 +    //                         "a" | "b" | "c" | "d" | "e" | "f"
 24.2527 +    private static final long L_HEX = L_DIGIT;
 24.2528 +    private static final long H_HEX = highMask('A', 'F') | highMask('a', 'f');
 24.2529 +
 24.2530 +    // mark          = "-" | "_" | "." | "!" | "~" | "*" | "'" |
 24.2531 +    //                 "(" | ")"
 24.2532 +    private static final long L_MARK = lowMask("-_.!~*'()");
 24.2533 +    private static final long H_MARK = highMask("-_.!~*'()");
 24.2534 +
 24.2535 +    // unreserved    = alphanum | mark
 24.2536 +    private static final long L_UNRESERVED = L_ALPHANUM | L_MARK;
 24.2537 +    private static final long H_UNRESERVED = H_ALPHANUM | H_MARK;
 24.2538 +
 24.2539 +    // reserved      = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
 24.2540 +    //                 "$" | "," | "[" | "]"
 24.2541 +    // Added per RFC2732: "[", "]"
 24.2542 +    private static final long L_RESERVED = lowMask(";/?:@&=+$,[]");
 24.2543 +    private static final long H_RESERVED = highMask(";/?:@&=+$,[]");
 24.2544 +
 24.2545 +    // The zero'th bit is used to indicate that escape pairs and non-US-ASCII
 24.2546 +    // characters are allowed; this is handled by the scanEscape method below.
 24.2547 +    private static final long L_ESCAPED = 1L;
 24.2548 +    private static final long H_ESCAPED = 0L;
 24.2549 +
 24.2550 +    // uric          = reserved | unreserved | escaped
 24.2551 +    private static final long L_URIC = L_RESERVED | L_UNRESERVED | L_ESCAPED;
 24.2552 +    private static final long H_URIC = H_RESERVED | H_UNRESERVED | H_ESCAPED;
 24.2553 +
 24.2554 +    // pchar         = unreserved | escaped |
 24.2555 +    //                 ":" | "@" | "&" | "=" | "+" | "$" | ","
 24.2556 +    private static final long L_PCHAR
 24.2557 +        = L_UNRESERVED | L_ESCAPED | lowMask(":@&=+$,");
 24.2558 +    private static final long H_PCHAR
 24.2559 +        = H_UNRESERVED | H_ESCAPED | highMask(":@&=+$,");
 24.2560 +
 24.2561 +    // All valid path characters
 24.2562 +    private static final long L_PATH = L_PCHAR | lowMask(";/");
 24.2563 +    private static final long H_PATH = H_PCHAR | highMask(";/");
 24.2564 +
 24.2565 +    // Dash, for use in domainlabel and toplabel
 24.2566 +    private static final long L_DASH = lowMask("-");
 24.2567 +    private static final long H_DASH = highMask("-");
 24.2568 +
 24.2569 +    // Dot, for use in hostnames
 24.2570 +    private static final long L_DOT = lowMask(".");
 24.2571 +    private static final long H_DOT = highMask(".");
 24.2572 +
 24.2573 +    // userinfo      = *( unreserved | escaped |
 24.2574 +    //                    ";" | ":" | "&" | "=" | "+" | "$" | "," )
 24.2575 +    private static final long L_USERINFO
 24.2576 +        = L_UNRESERVED | L_ESCAPED | lowMask(";:&=+$,");
 24.2577 +    private static final long H_USERINFO
 24.2578 +        = H_UNRESERVED | H_ESCAPED | highMask(";:&=+$,");
 24.2579 +
 24.2580 +    // reg_name      = 1*( unreserved | escaped | "$" | "," |
 24.2581 +    //                     ";" | ":" | "@" | "&" | "=" | "+" )
 24.2582 +    private static final long L_REG_NAME
 24.2583 +        = L_UNRESERVED | L_ESCAPED | lowMask("$,;:@&=+");
 24.2584 +    private static final long H_REG_NAME
 24.2585 +        = H_UNRESERVED | H_ESCAPED | highMask("$,;:@&=+");
 24.2586 +
 24.2587 +    // All valid characters for server-based authorities
 24.2588 +    private static final long L_SERVER
 24.2589 +        = L_USERINFO | L_ALPHANUM | L_DASH | lowMask(".:@[]");
 24.2590 +    private static final long H_SERVER
 24.2591 +        = H_USERINFO | H_ALPHANUM | H_DASH | highMask(".:@[]");
 24.2592 +
 24.2593 +    // Special case of server authority that represents an IPv6 address
 24.2594 +    // In this case, a % does not signify an escape sequence
 24.2595 +    private static final long L_SERVER_PERCENT
 24.2596 +        = L_SERVER | lowMask("%");
 24.2597 +    private static final long H_SERVER_PERCENT
 24.2598 +        = H_SERVER | highMask("%");
 24.2599 +    private static final long L_LEFT_BRACKET = lowMask("[");
 24.2600 +    private static final long H_LEFT_BRACKET = highMask("[");
 24.2601 +
 24.2602 +    // scheme        = alpha *( alpha | digit | "+" | "-" | "." )
 24.2603 +    private static final long L_SCHEME = L_ALPHA | L_DIGIT | lowMask("+-.");
 24.2604 +    private static final long H_SCHEME = H_ALPHA | H_DIGIT | highMask("+-.");
 24.2605 +
 24.2606 +    // uric_no_slash = unreserved | escaped | ";" | "?" | ":" | "@" |
 24.2607 +    //                 "&" | "=" | "+" | "$" | ","
 24.2608 +    private static final long L_URIC_NO_SLASH
 24.2609 +        = L_UNRESERVED | L_ESCAPED | lowMask(";?:@&=+$,");
 24.2610 +    private static final long H_URIC_NO_SLASH
 24.2611 +        = H_UNRESERVED | H_ESCAPED | highMask(";?:@&=+$,");
 24.2612 +
 24.2613 +
 24.2614 +    // -- Escaping and encoding --
 24.2615 +
 24.2616 +    private final static char[] hexDigits = {
 24.2617 +        '0', '1', '2', '3', '4', '5', '6', '7',
 24.2618 +        '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
 24.2619 +    };
 24.2620 +
 24.2621 +    private static void appendEscape(StringBuffer sb, byte b) {
 24.2622 +        sb.append('%');
 24.2623 +        sb.append(hexDigits[(b >> 4) & 0x0f]);
 24.2624 +        sb.append(hexDigits[(b >> 0) & 0x0f]);
 24.2625 +    }
 24.2626 +
 24.2627 +    private static void appendEncoded(StringBuffer sb, char c) {
 24.2628 +        /*
 24.2629 +        ByteBuffer bb = null;
 24.2630 +        try {
 24.2631 +            bb = ThreadLocalCoders.encoderFor("UTF-8")
 24.2632 +                .encode(CharBuffer.wrap("" + c));
 24.2633 +        } catch (CharacterCodingException x) {
 24.2634 +            assert false;
 24.2635 +        }
 24.2636 +        while (bb.hasRemaining()) {
 24.2637 +            int b = bb.get() & 0xff;
 24.2638 +            if (b >= 0x80)
 24.2639 +                appendEscape(sb, (byte)b);
 24.2640 +            else
 24.2641 +                sb.append((char)b);
 24.2642 +        }
 24.2643 +        */
 24.2644 +    }
 24.2645 +
 24.2646 +    // Quote any characters in s that are not permitted
 24.2647 +    // by the given mask pair
 24.2648 +    //
 24.2649 +    private static String quote(String s, long lowMask, long highMask) {
 24.2650 +        int n = s.length();
 24.2651 +        StringBuffer sb = null;
 24.2652 +        boolean allowNonASCII = ((lowMask & L_ESCAPED) != 0);
 24.2653 +        for (int i = 0; i < s.length(); i++) {
 24.2654 +            char c = s.charAt(i);
 24.2655 +            if (c < '\u0080') {
 24.2656 +                if (!match(c, lowMask, highMask)) {
 24.2657 +                    if (sb == null) {
 24.2658 +                        sb = new StringBuffer();
 24.2659 +                        sb.append(s.substring(0, i));
 24.2660 +                    }
 24.2661 +                    appendEscape(sb, (byte)c);
 24.2662 +                } else {
 24.2663 +                    if (sb != null)
 24.2664 +                        sb.append(c);
 24.2665 +                }
 24.2666 +            } else if (allowNonASCII
 24.2667 +                       && (Character.isSpaceChar(c)
 24.2668 +                           || Character.isISOControl(c))) {
 24.2669 +                if (sb == null) {
 24.2670 +                    sb = new StringBuffer();
 24.2671 +                    sb.append(s.substring(0, i));
 24.2672 +                }
 24.2673 +                appendEncoded(sb, c);
 24.2674 +            } else {
 24.2675 +                if (sb != null)
 24.2676 +                    sb.append(c);
 24.2677 +            }
 24.2678 +        }
 24.2679 +        return (sb == null) ? s : sb.toString();
 24.2680 +    }
 24.2681 +
 24.2682 +    // Encodes all characters >= \u0080 into escaped, normalized UTF-8 octets,
 24.2683 +    // assuming that s is otherwise legal
 24.2684 +    //
 24.2685 +    private static String encode(String s) {
 24.2686 +        int n = s.length();
 24.2687 +        if (n == 0)
 24.2688 +            return s;
 24.2689 +
 24.2690 +        // First check whether we actually need to encode
 24.2691 +        for (int i = 0;;) {
 24.2692 +            if (s.charAt(i) >= '\u0080')
 24.2693 +                break;
 24.2694 +            if (++i >= n)
 24.2695 +                return s;
 24.2696 +        }
 24.2697 +/*
 24.2698 +        String ns = Normalizer.normalize(s, Normalizer.Form.NFC);
 24.2699 +        ByteBuffer bb = null;
 24.2700 +        try {
 24.2701 +            bb = ThreadLocalCoders.encoderFor("UTF-8")
 24.2702 +                .encode(CharBuffer.wrap(ns));
 24.2703 +        } catch (CharacterCodingException x) {
 24.2704 +            assert false;
 24.2705 +        }
 24.2706 +*/
 24.2707 +        StringBuffer sb = new StringBuffer();
 24.2708 +        /*
 24.2709 +        while (bb.hasRemaining()) {
 24.2710 +            int b = bb.get() & 0xff;
 24.2711 +            if (b >= 0x80)
 24.2712 +                appendEscape(sb, (byte)b);
 24.2713 +            else
 24.2714 +                sb.append((char)b);
 24.2715 +        }
 24.2716 +        */
 24.2717 +        return sb.toString();
 24.2718 +    }
 24.2719 +
 24.2720 +    private static int decode(char c) {
 24.2721 +        if ((c >= '0') && (c <= '9'))
 24.2722 +            return c - '0';
 24.2723 +        if ((c >= 'a') && (c <= 'f'))
 24.2724 +            return c - 'a' + 10;
 24.2725 +        if ((c >= 'A') && (c <= 'F'))
 24.2726 +            return c - 'A' + 10;
 24.2727 +        assert false;
 24.2728 +        return -1;
 24.2729 +    }
 24.2730 +
 24.2731 +    private static byte decode(char c1, char c2) {
 24.2732 +        return (byte)(  ((decode(c1) & 0xf) << 4)
 24.2733 +                      | ((decode(c2) & 0xf) << 0));
 24.2734 +    }
 24.2735 +
 24.2736 +    // Evaluates all escapes in s, applying UTF-8 decoding if needed.  Assumes
 24.2737 +    // that escapes are well-formed syntactically, i.e., of the form %XX.  If a
 24.2738 +    // sequence of escaped octets is not valid UTF-8 then the erroneous octets
 24.2739 +    // are replaced with '\uFFFD'.
 24.2740 +    // Exception: any "%" found between "[]" is left alone. It is an IPv6 literal
 24.2741 +    //            with a scope_id
 24.2742 +    //
 24.2743 +    private static String decode(String s) {
 24.2744 +        if (s == null)
 24.2745 +            return s;
 24.2746 +        int n = s.length();
 24.2747 +        if (n == 0)
 24.2748 +            return s;
 24.2749 +        if (s.indexOf('%') < 0)
 24.2750 +            return s;
 24.2751 +
 24.2752 +        StringBuffer sb = new StringBuffer(n);
 24.2753 +        /*
 24.2754 +        ByteBuffer bb = ByteBuffer.allocate(n);
 24.2755 +        CharBuffer cb = CharBuffer.allocate(n);
 24.2756 +        CharsetDecoder dec = ThreadLocalCoders.decoderFor("UTF-8")
 24.2757 +            .onMalformedInput(CodingErrorAction.REPLACE)
 24.2758 +            .onUnmappableCharacter(CodingErrorAction.REPLACE);
 24.2759 +
 24.2760 +        // This is not horribly efficient, but it will do for now
 24.2761 +        char c = s.charAt(0);
 24.2762 +        boolean betweenBrackets = false;
 24.2763 +
 24.2764 +        for (int i = 0; i < n;) {
 24.2765 +            assert c == s.charAt(i);    // Loop invariant
 24.2766 +            if (c == '[') {
 24.2767 +                betweenBrackets = true;
 24.2768 +            } else if (betweenBrackets && c == ']') {
 24.2769 +                betweenBrackets = false;
 24.2770 +            }
 24.2771 +            if (c != '%' || betweenBrackets) {
 24.2772 +                sb.append(c);
 24.2773 +                if (++i >= n)
 24.2774 +                    break;
 24.2775 +                c = s.charAt(i);
 24.2776 +                continue;
 24.2777 +            }
 24.2778 +            bb.clear();
 24.2779 +            int ui = i;
 24.2780 +            for (;;) {
 24.2781 +                assert (n - i >= 2);
 24.2782 +                bb.put(decode(s.charAt(++i), s.charAt(++i)));
 24.2783 +                if (++i >= n)
 24.2784 +                    break;
 24.2785 +                c = s.charAt(i);
 24.2786 +                if (c != '%')
 24.2787 +                    break;
 24.2788 +            }
 24.2789 +            bb.flip();
 24.2790 +            cb.clear();
 24.2791 +            dec.reset();
 24.2792 +            CoderResult cr = dec.decode(bb, cb, true);
 24.2793 +            assert cr.isUnderflow();
 24.2794 +            cr = dec.flush(cb);
 24.2795 +            assert cr.isUnderflow();
 24.2796 +            sb.append(cb.flip().toString());
 24.2797 +        }
 24.2798 +*/
 24.2799 +        return sb.toString();
 24.2800 +    }
 24.2801 +
 24.2802 +
 24.2803 +    // -- Parsing --
 24.2804 +
 24.2805 +    // For convenience we wrap the input URI string in a new instance of the
 24.2806 +    // following internal class.  This saves always having to pass the input
 24.2807 +    // string as an argument to each internal scan/parse method.
 24.2808 +
 24.2809 +    private class Parser {
 24.2810 +
 24.2811 +        private String input;           // URI input string
 24.2812 +        private boolean requireServerAuthority = false;
 24.2813 +
 24.2814 +        Parser(String s) {
 24.2815 +            input = s;
 24.2816 +            string = s;
 24.2817 +        }
 24.2818 +
 24.2819 +        // -- Methods for throwing URISyntaxException in various ways --
 24.2820 +
 24.2821 +        private void fail(String reason) throws URISyntaxException {
 24.2822 +            throw new URISyntaxException(input, reason);
 24.2823 +        }
 24.2824 +
 24.2825 +        private void fail(String reason, int p) throws URISyntaxException {
 24.2826 +            throw new URISyntaxException(input, reason, p);
 24.2827 +        }
 24.2828 +
 24.2829 +        private void failExpecting(String expected, int p)
 24.2830 +            throws URISyntaxException
 24.2831 +        {
 24.2832 +            fail("Expected " + expected, p);
 24.2833 +        }
 24.2834 +
 24.2835 +        private void failExpecting(String expected, String prior, int p)
 24.2836 +            throws URISyntaxException
 24.2837 +        {
 24.2838 +            fail("Expected " + expected + " following " + prior, p);
 24.2839 +        }
 24.2840 +
 24.2841 +
 24.2842 +        // -- Simple access to the input string --
 24.2843 +
 24.2844 +        // Return a substring of the input string
 24.2845 +        //
 24.2846 +        private String substring(int start, int end) {
 24.2847 +            return input.substring(start, end);
 24.2848 +        }
 24.2849 +
 24.2850 +        // Return the char at position p,
 24.2851 +        // assuming that p < input.length()
 24.2852 +        //
 24.2853 +        private char charAt(int p) {
 24.2854 +            return input.charAt(p);
 24.2855 +        }
 24.2856 +
 24.2857 +        // Tells whether start < end and, if so, whether charAt(start) == c
 24.2858 +        //
 24.2859 +        private boolean at(int start, int end, char c) {
 24.2860 +            return (start < end) && (charAt(start) == c);
 24.2861 +        }
 24.2862 +
 24.2863 +        // Tells whether start + s.length() < end and, if so,
 24.2864 +        // whether the chars at the start position match s exactly
 24.2865 +        //
 24.2866 +        private boolean at(int start, int end, String s) {
 24.2867 +            int p = start;
 24.2868 +            int sn = s.length();
 24.2869 +            if (sn > end - p)
 24.2870 +                return false;
 24.2871 +            int i = 0;
 24.2872 +            while (i < sn) {
 24.2873 +                if (charAt(p++) != s.charAt(i)) {
 24.2874 +                    break;
 24.2875 +                }
 24.2876 +                i++;
 24.2877 +            }
 24.2878 +            return (i == sn);
 24.2879 +        }
 24.2880 +
 24.2881 +
 24.2882 +        // -- Scanning --
 24.2883 +
 24.2884 +        // The various scan and parse methods that follow use a uniform
 24.2885 +        // convention of taking the current start position and end index as
 24.2886 +        // their first two arguments.  The start is inclusive while the end is
 24.2887 +        // exclusive, just as in the String class, i.e., a start/end pair
 24.2888 +        // denotes the left-open interval [start, end) of the input string.
 24.2889 +        //
 24.2890 +        // These methods never proceed past the end position.  They may return
 24.2891 +        // -1 to indicate outright failure, but more often they simply return
 24.2892 +        // the position of the first char after the last char scanned.  Thus
 24.2893 +        // a typical idiom is
 24.2894 +        //
 24.2895 +        //     int p = start;
 24.2896 +        //     int q = scan(p, end, ...);
 24.2897 +        //     if (q > p)
 24.2898 +        //         // We scanned something
 24.2899 +        //         ...;
 24.2900 +        //     else if (q == p)
 24.2901 +        //         // We scanned nothing
 24.2902 +        //         ...;
 24.2903 +        //     else if (q == -1)
 24.2904 +        //         // Something went wrong
 24.2905 +        //         ...;
 24.2906 +
 24.2907 +
 24.2908 +        // Scan a specific char: If the char at the given start position is
 24.2909 +        // equal to c, return the index of the next char; otherwise, return the
 24.2910 +        // start position.
 24.2911 +        //
 24.2912 +        private int scan(int start, int end, char c) {
 24.2913 +            if ((start < end) && (charAt(start) == c))
 24.2914 +                return start + 1;
 24.2915 +            return start;
 24.2916 +        }
 24.2917 +
 24.2918 +        // Scan forward from the given start position.  Stop at the first char
 24.2919 +        // in the err string (in which case -1 is returned), or the first char
 24.2920 +        // in the stop string (in which case the index of the preceding char is
 24.2921 +        // returned), or the end of the input string (in which case the length
 24.2922 +        // of the input string is returned).  May return the start position if
 24.2923 +        // nothing matches.
 24.2924 +        //
 24.2925 +        private int scan(int start, int end, String err, String stop) {
 24.2926 +            int p = start;
 24.2927 +            while (p < end) {
 24.2928 +                char c = charAt(p);
 24.2929 +                if (err.indexOf(c) >= 0)
 24.2930 +                    return -1;
 24.2931 +                if (stop.indexOf(c) >= 0)
 24.2932 +                    break;
 24.2933 +                p++;
 24.2934 +            }
 24.2935 +            return p;
 24.2936 +        }
 24.2937 +
 24.2938 +        // Scan a potential escape sequence, starting at the given position,
 24.2939 +        // with the given first char (i.e., charAt(start) == c).
 24.2940 +        //
 24.2941 +        // This method assumes that if escapes are allowed then visible
 24.2942 +        // non-US-ASCII chars are also allowed.
 24.2943 +        //
 24.2944 +        private int scanEscape(int start, int n, char first)
 24.2945 +            throws URISyntaxException
 24.2946 +        {
 24.2947 +            int p = start;
 24.2948 +            char c = first;
 24.2949 +            if (c == '%') {
 24.2950 +                // Process escape pair
 24.2951 +                if ((p + 3 <= n)
 24.2952 +                    && match(charAt(p + 1), L_HEX, H_HEX)
 24.2953 +                    && match(charAt(p + 2), L_HEX, H_HEX)) {
 24.2954 +                    return p + 3;
 24.2955 +                }
 24.2956 +                fail("Malformed escape pair", p);
 24.2957 +            } else if ((c > 128)
 24.2958 +                       && !Character.isSpaceChar(c)
 24.2959 +                       && !Character.isISOControl(c)) {
 24.2960 +                // Allow unescaped but visible non-US-ASCII chars
 24.2961 +                return p + 1;
 24.2962 +            }
 24.2963 +            return p;
 24.2964 +        }
 24.2965 +
 24.2966 +        // Scan chars that match the given mask pair
 24.2967 +        //
 24.2968 +        private int scan(int start, int n, long lowMask, long highMask)
 24.2969 +            throws URISyntaxException
 24.2970 +        {
 24.2971 +            int p = start;
 24.2972 +            while (p < n) {
 24.2973 +                char c = charAt(p);
 24.2974 +                if (match(c, lowMask, highMask)) {
 24.2975 +                    p++;
 24.2976 +                    continue;
 24.2977 +                }
 24.2978 +                if ((lowMask & L_ESCAPED) != 0) {
 24.2979 +                    int q = scanEscape(p, n, c);
 24.2980 +                    if (q > p) {
 24.2981 +                        p = q;
 24.2982 +                        continue;
 24.2983 +                    }
 24.2984 +                }
 24.2985 +                break;
 24.2986 +            }
 24.2987 +            return p;
 24.2988 +        }
 24.2989 +
 24.2990 +        // Check that each of the chars in [start, end) matches the given mask
 24.2991 +        //
 24.2992 +        private void checkChars(int start, int end,
 24.2993 +                                long lowMask, long highMask,
 24.2994 +                                String what)
 24.2995 +            throws URISyntaxException
 24.2996 +        {
 24.2997 +            int p = scan(start, end, lowMask, highMask);
 24.2998 +            if (p < end)
 24.2999 +                fail("Illegal character in " + what, p);
 24.3000 +        }
 24.3001 +
 24.3002 +        // Check that the char at position p matches the given mask
 24.3003 +        //
 24.3004 +        private void checkChar(int p,
 24.3005 +                               long lowMask, long highMask,
 24.3006 +                               String what)
 24.3007 +            throws URISyntaxException
 24.3008 +        {
 24.3009 +            checkChars(p, p + 1, lowMask, highMask, what);
 24.3010 +        }
 24.3011 +
 24.3012 +
 24.3013 +        // -- Parsing --
 24.3014 +
 24.3015 +        // [<scheme>:]<scheme-specific-part>[#<fragment>]
 24.3016 +        //
 24.3017 +        void parse(boolean rsa) throws URISyntaxException {
 24.3018 +            requireServerAuthority = rsa;
 24.3019 +            int ssp;                    // Start of scheme-specific part
 24.3020 +            int n = input.length();
 24.3021 +            int p = scan(0, n, "/?#", ":");
 24.3022 +            if ((p >= 0) && at(p, n, ':')) {
 24.3023 +                if (p == 0)
 24.3024 +                    failExpecting("scheme name", 0);
 24.3025 +                checkChar(0, L_ALPHA, H_ALPHA, "scheme name");
 24.3026 +                checkChars(1, p, L_SCHEME, H_SCHEME, "scheme name");
 24.3027 +                scheme = substring(0, p);
 24.3028 +                p++;                    // Skip ':'
 24.3029 +                ssp = p;
 24.3030 +                if (at(p, n, '/')) {
 24.3031 +                    p = parseHierarchical(p, n);
 24.3032 +                } else {
 24.3033 +                    int q = scan(p, n, "", "#");
 24.3034 +                    if (q <= p)
 24.3035 +                        failExpecting("scheme-specific part", p);
 24.3036 +                    checkChars(p, q, L_URIC, H_URIC, "opaque part");
 24.3037 +                    p = q;
 24.3038 +                }
 24.3039 +            } else {
 24.3040 +                ssp = 0;
 24.3041 +                p = parseHierarchical(0, n);
 24.3042 +            }
 24.3043 +            schemeSpecificPart = substring(ssp, p);
 24.3044 +            if (at(p, n, '#')) {
 24.3045 +                checkChars(p + 1, n, L_URIC, H_URIC, "fragment");
 24.3046 +                fragment = substring(p + 1, n);
 24.3047 +                p = n;
 24.3048 +            }
 24.3049 +            if (p < n)
 24.3050 +                fail("end of URI", p);
 24.3051 +        }
 24.3052 +
 24.3053 +        // [//authority]<path>[?<query>]
 24.3054 +        //
 24.3055 +        // DEVIATION from RFC2396: We allow an empty authority component as
 24.3056 +        // long as it's followed by a non-empty path, query component, or
 24.3057 +        // fragment component.  This is so that URIs such as "file:///foo/bar"
 24.3058 +        // will parse.  This seems to be the intent of RFC2396, though the
 24.3059 +        // grammar does not permit it.  If the authority is empty then the
 24.3060 +        // userInfo, host, and port components are undefined.
 24.3061 +        //
 24.3062 +        // DEVIATION from RFC2396: We allow empty relative paths.  This seems
 24.3063 +        // to be the intent of RFC2396, but the grammar does not permit it.
 24.3064 +        // The primary consequence of this deviation is that "#f" parses as a
 24.3065 +        // relative URI with an empty path.
 24.3066 +        //
 24.3067 +        private int parseHierarchical(int start, int n)
 24.3068 +            throws URISyntaxException
 24.3069 +        {
 24.3070 +            int p = start;
 24.3071 +            if (at(p, n, '/') && at(p + 1, n, '/')) {
 24.3072 +                p += 2;
 24.3073 +                int q = scan(p, n, "", "/?#");
 24.3074 +                if (q > p) {
 24.3075 +                    p = parseAuthority(p, q);
 24.3076 +                } else if (q < n) {
 24.3077 +                    // DEVIATION: Allow empty authority prior to non-empty
 24.3078 +                    // path, query component or fragment identifier
 24.3079 +                } else
 24.3080 +                    failExpecting("authority", p);
 24.3081 +            }
 24.3082 +            int q = scan(p, n, "", "?#"); // DEVIATION: May be empty
 24.3083 +            checkChars(p, q, L_PATH, H_PATH, "path");
 24.3084 +            path = substring(p, q);
 24.3085 +            p = q;
 24.3086 +            if (at(p, n, '?')) {
 24.3087 +                p++;
 24.3088 +                q = scan(p, n, "", "#");
 24.3089 +                checkChars(p, q, L_URIC, H_URIC, "query");
 24.3090 +                query = substring(p, q);
 24.3091 +                p = q;
 24.3092 +            }
 24.3093 +            return p;
 24.3094 +        }
 24.3095 +
 24.3096 +        // authority     = server | reg_name
 24.3097 +        //
 24.3098 +        // Ambiguity: An authority that is a registry name rather than a server
 24.3099 +        // might have a prefix that parses as a server.  We use the fact that
 24.3100 +        // the authority component is always followed by '/' or the end of the
 24.3101 +        // input string to resolve this: If the complete authority did not
 24.3102 +        // parse as a server then we try to parse it as a registry name.
 24.3103 +        //
 24.3104 +        private int parseAuthority(int start, int n)
 24.3105 +            throws URISyntaxException
 24.3106 +        {
 24.3107 +            int p = start;
 24.3108 +            int q = p;
 24.3109 +            URISyntaxException ex = null;
 24.3110 +
 24.3111 +            boolean serverChars;
 24.3112 +            boolean regChars;
 24.3113 +
 24.3114 +            if (scan(p, n, "", "]") > p) {
 24.3115 +                // contains a literal IPv6 address, therefore % is allowed
 24.3116 +                serverChars = (scan(p, n, L_SERVER_PERCENT, H_SERVER_PERCENT) == n);
 24.3117 +            } else {
 24.3118 +                serverChars = (scan(p, n, L_SERVER, H_SERVER) == n);
 24.3119 +            }
 24.3120 +            regChars = (scan(p, n, L_REG_NAME, H_REG_NAME) == n);
 24.3121 +
 24.3122 +            if (regChars && !serverChars) {
 24.3123 +                // Must be a registry-based authority
 24.3124 +                authority = substring(p, n);
 24.3125 +                return n;
 24.3126 +            }
 24.3127 +
 24.3128 +            if (serverChars) {
 24.3129 +                // Might be (probably is) a server-based authority, so attempt
 24.3130 +                // to parse it as such.  If the attempt fails, try to treat it
 24.3131 +                // as a registry-based authority.
 24.3132 +                try {
 24.3133 +                    q = parseServer(p, n);
 24.3134 +                    if (q < n)
 24.3135 +                        failExpecting("end of authority", q);
 24.3136 +                    authority = substring(p, n);
 24.3137 +                } catch (URISyntaxException x) {
 24.3138 +                    // Undo results of failed parse
 24.3139 +                    userInfo = null;
 24.3140 +                    host = null;
 24.3141 +                    port = -1;
 24.3142 +                    if (requireServerAuthority) {
 24.3143 +                        // If we're insisting upon a server-based authority,
 24.3144 +                        // then just re-throw the exception
 24.3145 +                        throw x;
 24.3146 +                    } else {
 24.3147 +                        // Save the exception in case it doesn't parse as a
 24.3148 +                        // registry either
 24.3149 +                        ex = x;
 24.3150 +                        q = p;
 24.3151 +                    }
 24.3152 +                }
 24.3153 +            }
 24.3154 +
 24.3155 +            if (q < n) {
 24.3156 +                if (regChars) {
 24.3157 +                    // Registry-based authority
 24.3158 +                    authority = substring(p, n);
 24.3159 +                } else if (ex != null) {
 24.3160 +                    // Re-throw exception; it was probably due to
 24.3161 +                    // a malformed IPv6 address
 24.3162 +                    throw ex;
 24.3163 +                } else {
 24.3164 +                    fail("Illegal character in authority", q);
 24.3165 +                }
 24.3166 +            }
 24.3167 +
 24.3168 +            return n;
 24.3169 +        }
 24.3170 +
 24.3171 +
 24.3172 +        // [<userinfo>@]<host>[:<port>]
 24.3173 +        //
 24.3174 +        private int parseServer(int start, int n)
 24.3175 +            throws URISyntaxException
 24.3176 +        {
 24.3177 +            int p = start;
 24.3178 +            int q;
 24.3179 +
 24.3180 +            // userinfo
 24.3181 +            q = scan(p, n, "/?#", "@");
 24.3182 +            if ((q >= p) && at(q, n, '@')) {
 24.3183 +                checkChars(p, q, L_USERINFO, H_USERINFO, "user info");
 24.3184 +                userInfo = substring(p, q);
 24.3185 +                p = q + 1;              // Skip '@'
 24.3186 +            }
 24.3187 +
 24.3188 +            // hostname, IPv4 address, or IPv6 address
 24.3189 +            if (at(p, n, '[')) {
 24.3190 +                // DEVIATION from RFC2396: Support IPv6 addresses, per RFC2732
 24.3191 +                p++;
 24.3192 +                q = scan(p, n, "/?#", "]");
 24.3193 +                if ((q > p) && at(q, n, ']')) {
 24.3194 +                    // look for a "%" scope id
 24.3195 +                    int r = scan (p, q, "", "%");
 24.3196 +                    if (r > p) {
 24.3197 +                        parseIPv6Reference(p, r);
 24.3198 +                        if (r+1 == q) {
 24.3199 +                            fail ("scope id expected");
 24.3200 +                        }
 24.3201 +                        checkChars (r+1, q, L_ALPHANUM, H_ALPHANUM,
 24.3202 +                                                "scope id");
 24.3203 +                    } else {
 24.3204 +                        parseIPv6Reference(p, q);
 24.3205 +                    }
 24.3206 +                    host = substring(p-1, q+1);
 24.3207 +                    p = q + 1;
 24.3208 +                } else {
 24.3209 +                    failExpecting("closing bracket for IPv6 address", q);
 24.3210 +                }
 24.3211 +            } else {
 24.3212 +                q = parseIPv4Address(p, n);
 24.3213 +                if (q <= p)
 24.3214 +                    q = parseHostname(p, n);
 24.3215 +                p = q;
 24.3216 +            }
 24.3217 +
 24.3218 +            // port
 24.3219 +            if (at(p, n, ':')) {
 24.3220 +                p++;
 24.3221 +                q = scan(p, n, "", "/");
 24.3222 +                if (q > p) {
 24.3223 +                    checkChars(p, q, L_DIGIT, H_DIGIT, "port number");
 24.3224 +                    try {
 24.3225 +                        port = Integer.parseInt(substring(p, q));
 24.3226 +                    } catch (NumberFormatException x) {
 24.3227 +                        fail("Malformed port number", p);
 24.3228 +                    }
 24.3229 +                    p = q;
 24.3230 +                }
 24.3231 +            }
 24.3232 +            if (p < n)
 24.3233 +                failExpecting("port number", p);
 24.3234 +
 24.3235 +            return p;
 24.3236 +        }
 24.3237 +
 24.3238 +        // Scan a string of decimal digits whose value fits in a byte
 24.3239 +        //
 24.3240 +        private int scanByte(int start, int n)
 24.3241 +            throws URISyntaxException
 24.3242 +        {
 24.3243 +            int p = start;
 24.3244 +            int q = scan(p, n, L_DIGIT, H_DIGIT);
 24.3245 +            if (q <= p) return q;
 24.3246 +            if (Integer.parseInt(substring(p, q)) > 255) return p;
 24.3247 +            return q;
 24.3248 +        }
 24.3249 +
 24.3250 +        // Scan an IPv4 address.
 24.3251 +        //
 24.3252 +        // If the strict argument is true then we require that the given
 24.3253 +        // interval contain nothing besides an IPv4 address; if it is false
 24.3254 +        // then we only require that it start with an IPv4 address.
 24.3255 +        //
 24.3256 +        // If the interval does not contain or start with (depending upon the
 24.3257 +        // strict argument) a legal IPv4 address characters then we return -1
 24.3258 +        // immediately; otherwise we insist that these characters parse as a
 24.3259 +        // legal IPv4 address and throw an exception on failure.
 24.3260 +        //
 24.3261 +        // We assume that any string of decimal digits and dots must be an IPv4
 24.3262 +        // address.  It won't parse as a hostname anyway, so making that
 24.3263 +        // assumption here allows more meaningful exceptions to be thrown.
 24.3264 +        //
 24.3265 +        private int scanIPv4Address(int start, int n, boolean strict)
 24.3266 +            throws URISyntaxException
 24.3267 +        {
 24.3268 +            int p = start;
 24.3269 +            int q;
 24.3270 +            int m = scan(p, n, L_DIGIT | L_DOT, H_DIGIT | H_DOT);
 24.3271 +            if ((m <= p) || (strict && (m != n)))
 24.3272 +                return -1;
 24.3273 +            for (;;) {
 24.3274 +                // Per RFC2732: At most three digits per byte
 24.3275 +                // Further constraint: Each element fits in a byte
 24.3276 +                if ((q = scanByte(p, m)) <= p) break;   p = q;
 24.3277 +                if ((q = scan(p, m, '.')) <= p) break;  p = q;
 24.3278 +                if ((q = scanByte(p, m)) <= p) break;   p = q;
 24.3279 +                if ((q = scan(p, m, '.')) <= p) break;  p = q;
 24.3280 +                if ((q = scanByte(p, m)) <= p) break;   p = q;
 24.3281 +                if ((q = scan(p, m, '.')) <= p) break;  p = q;
 24.3282 +                if ((q = scanByte(p, m)) <= p) break;   p = q;
 24.3283 +                if (q < m) break;
 24.3284 +                return q;
 24.3285 +            }
 24.3286 +            fail("Malformed IPv4 address", q);
 24.3287 +            return -1;
 24.3288 +        }
 24.3289 +
 24.3290 +        // Take an IPv4 address: Throw an exception if the given interval
 24.3291 +        // contains anything except an IPv4 address
 24.3292 +        //
 24.3293 +        private int takeIPv4Address(int start, int n, String expected)
 24.3294 +            throws URISyntaxException
 24.3295 +        {
 24.3296 +            int p = scanIPv4Address(start, n, true);
 24.3297 +            if (p <= start)
 24.3298 +                failExpecting(expected, start);
 24.3299 +            return p;
 24.3300 +        }
 24.3301 +
 24.3302 +        // Attempt to parse an IPv4 address, returning -1 on failure but
 24.3303 +        // allowing the given interval to contain [:<characters>] after
 24.3304 +        // the IPv4 address.
 24.3305 +        //
 24.3306 +        private int parseIPv4Address(int start, int n) {
 24.3307 +            int p;
 24.3308 +
 24.3309 +            try {
 24.3310 +                p = scanIPv4Address(start, n, false);
 24.3311 +            } catch (URISyntaxException x) {
 24.3312 +                return -1;
 24.3313 +            } catch (NumberFormatException nfe) {
 24.3314 +                return -1;
 24.3315 +            }
 24.3316 +
 24.3317 +            if (p > start && p < n) {
 24.3318 +                // IPv4 address is followed by something - check that
 24.3319 +                // it's a ":" as this is the only valid character to
 24.3320 +                // follow an address.
 24.3321 +                if (charAt(p) != ':') {
 24.3322 +                    p = -1;
 24.3323 +                }
 24.3324 +            }
 24.3325 +
 24.3326 +            if (p > start)
 24.3327 +                host = substring(start, p);
 24.3328 +
 24.3329 +            return p;
 24.3330 +        }
 24.3331 +
 24.3332 +        // hostname      = domainlabel [ "." ] | 1*( domainlabel "." ) toplabel [ "." ]
 24.3333 +        // domainlabel   = alphanum | alphanum *( alphanum | "-" ) alphanum
 24.3334 +        // toplabel      = alpha | alpha *( alphanum | "-" ) alphanum
 24.3335 +        //
 24.3336 +        private int parseHostname(int start, int n)
 24.3337 +            throws URISyntaxException
 24.3338 +        {
 24.3339 +            int p = start;
 24.3340 +            int q;
 24.3341 +            int l = -1;                 // Start of last parsed label
 24.3342 +
 24.3343 +            do {
 24.3344 +                // domainlabel = alphanum [ *( alphanum | "-" ) alphanum ]
 24.3345 +                q = scan(p, n, L_ALPHANUM, H_ALPHANUM);
 24.3346 +                if (q <= p)
 24.3347 +                    break;
 24.3348 +                l = p;
 24.3349 +                if (q > p) {
 24.3350 +                    p = q;
 24.3351 +                    q = scan(p, n, L_ALPHANUM | L_DASH, H_ALPHANUM | H_DASH);
 24.3352 +                    if (q > p) {
 24.3353 +                        if (charAt(q - 1) == '-')
 24.3354 +                            fail("Illegal character in hostname", q - 1);
 24.3355 +                        p = q;
 24.3356 +                    }
 24.3357 +                }
 24.3358 +                q = scan(p, n, '.');
 24.3359 +                if (q <= p)
 24.3360 +                    break;
 24.3361 +                p = q;
 24.3362 +            } while (p < n);
 24.3363 +
 24.3364 +            if ((p < n) && !at(p, n, ':'))
 24.3365 +                fail("Illegal character in hostname", p);
 24.3366 +
 24.3367 +            if (l < 0)
 24.3368 +                failExpecting("hostname", start);
 24.3369 +
 24.3370 +            // for a fully qualified hostname check that the rightmost
 24.3371 +            // label starts with an alpha character.
 24.3372 +            if (l > start && !match(charAt(l), L_ALPHA, H_ALPHA)) {
 24.3373 +                fail("Illegal character in hostname", l);
 24.3374 +            }
 24.3375 +
 24.3376 +            host = substring(start, p);
 24.3377 +            return p;
 24.3378 +        }
 24.3379 +
 24.3380 +
 24.3381 +        // IPv6 address parsing, from RFC2373: IPv6 Addressing Architecture
 24.3382 +        //
 24.3383 +        // Bug: The grammar in RFC2373 Appendix B does not allow addresses of
 24.3384 +        // the form ::12.34.56.78, which are clearly shown in the examples
 24.3385 +        // earlier in the document.  Here is the original grammar:
 24.3386 +        //
 24.3387 +        //   IPv6address = hexpart [ ":" IPv4address ]
 24.3388 +        //   hexpart     = hexseq | hexseq "::" [ hexseq ] | "::" [ hexseq ]
 24.3389 +        //   hexseq      = hex4 *( ":" hex4)
 24.3390 +        //   hex4        = 1*4HEXDIG
 24.3391 +        //
 24.3392 +        // We therefore use the following revised grammar:
 24.3393 +        //
 24.3394 +        //   IPv6address = hexseq [ ":" IPv4address ]
 24.3395 +        //                 | hexseq [ "::" [ hexpost ] ]
 24.3396 +        //                 | "::" [ hexpost ]
 24.3397 +        //   hexpost     = hexseq | hexseq ":" IPv4address | IPv4address
 24.3398 +        //   hexseq      = hex4 *( ":" hex4)
 24.3399 +        //   hex4        = 1*4HEXDIG
 24.3400 +        //
 24.3401 +        // This covers all and only the following cases:
 24.3402 +        //
 24.3403 +        //   hexseq
 24.3404 +        //   hexseq : IPv4address
 24.3405 +        //   hexseq ::
 24.3406 +        //   hexseq :: hexseq
 24.3407 +        //   hexseq :: hexseq : IPv4address
 24.3408 +        //   hexseq :: IPv4address
 24.3409 +        //   :: hexseq
 24.3410 +        //   :: hexseq : IPv4address
 24.3411 +        //   :: IPv4address
 24.3412 +        //   ::
 24.3413 +        //
 24.3414 +        // Additionally we constrain the IPv6 address as follows :-
 24.3415 +        //
 24.3416 +        //  i.  IPv6 addresses without compressed zeros should contain
 24.3417 +        //      exactly 16 bytes.
 24.3418 +        //
 24.3419 +        //  ii. IPv6 addresses with compressed zeros should contain
 24.3420 +        //      less than 16 bytes.
 24.3421 +
 24.3422 +        private int ipv6byteCount = 0;
 24.3423 +
 24.3424 +        private int parseIPv6Reference(int start, int n)
 24.3425 +            throws URISyntaxException
 24.3426 +        {
 24.3427 +            int p = start;
 24.3428 +            int q;
 24.3429 +            boolean compressedZeros = false;
 24.3430 +
 24.3431 +            q = scanHexSeq(p, n);
 24.3432 +
 24.3433 +            if (q > p) {
 24.3434 +                p = q;
 24.3435 +                if (at(p, n, "::")) {
 24.3436 +                    compressedZeros = true;
 24.3437 +                    p = scanHexPost(p + 2, n);
 24.3438 +                } else if (at(p, n, ':')) {
 24.3439 +                    p = takeIPv4Address(p + 1,  n, "IPv4 address");
 24.3440 +                    ipv6byteCount += 4;
 24.3441 +                }
 24.3442 +            } else if (at(p, n, "::")) {
 24.3443 +                compressedZeros = true;
 24.3444 +                p = scanHexPost(p + 2, n);
 24.3445 +            }
 24.3446 +            if (p < n)
 24.3447 +                fail("Malformed IPv6 address", start);
 24.3448 +            if (ipv6byteCount > 16)
 24.3449 +                fail("IPv6 address too long", start);
 24.3450 +            if (!compressedZeros && ipv6byteCount < 16)
 24.3451 +                fail("IPv6 address too short", start);
 24.3452 +            if (compressedZeros && ipv6byteCount == 16)
 24.3453 +                fail("Malformed IPv6 address", start);
 24.3454 +
 24.3455 +            return p;
 24.3456 +        }
 24.3457 +
 24.3458 +        private int scanHexPost(int start, int n)
 24.3459 +            throws URISyntaxException
 24.3460 +        {
 24.3461 +            int p = start;
 24.3462 +            int q;
 24.3463 +
 24.3464 +            if (p == n)
 24.3465 +                return p;
 24.3466 +
 24.3467 +            q = scanHexSeq(p, n);
 24.3468 +            if (q > p) {
 24.3469 +                p = q;
 24.3470 +                if (at(p, n, ':')) {
 24.3471 +                    p++;
 24.3472 +                    p = takeIPv4Address(p, n, "hex digits or IPv4 address");
 24.3473 +                    ipv6byteCount += 4;
 24.3474 +                }
 24.3475 +            } else {
 24.3476 +                p = takeIPv4Address(p, n, "hex digits or IPv4 address");
 24.3477 +                ipv6byteCount += 4;
 24.3478 +            }
 24.3479 +            return p;
 24.3480 +        }
 24.3481 +
 24.3482 +        // Scan a hex sequence; return -1 if one could not be scanned
 24.3483 +        //
 24.3484 +        private int scanHexSeq(int start, int n)
 24.3485 +            throws URISyntaxException
 24.3486 +        {
 24.3487 +            int p = start;
 24.3488 +            int q;
 24.3489 +
 24.3490 +            q = scan(p, n, L_HEX, H_HEX);
 24.3491 +            if (q <= p)
 24.3492 +                return -1;
 24.3493 +            if (at(q, n, '.'))          // Beginning of IPv4 address
 24.3494 +                return -1;
 24.3495 +            if (q > p + 4)
 24.3496 +                fail("IPv6 hexadecimal digit sequence too long", p);
 24.3497 +            ipv6byteCount += 2;
 24.3498 +            p = q;
 24.3499 +            while (p < n) {
 24.3500 +                if (!at(p, n, ':'))
 24.3501 +                    break;
 24.3502 +                if (at(p + 1, n, ':'))
 24.3503 +                    break;              // "::"
 24.3504 +                p++;
 24.3505 +                q = scan(p, n, L_HEX, H_HEX);
 24.3506 +                if (q <= p)
 24.3507 +                    failExpecting("digits for an IPv6 address", p);
 24.3508 +                if (at(q, n, '.')) {    // Beginning of IPv4 address
 24.3509 +                    p--;
 24.3510 +                    break;
 24.3511 +                }
 24.3512 +                if (q > p + 4)
 24.3513 +                    fail("IPv6 hexadecimal digit sequence too long", p);
 24.3514 +                ipv6byteCount += 2;
 24.3515 +                p = q;
 24.3516 +            }
 24.3517 +
 24.3518 +            return p;
 24.3519 +        }
 24.3520 +
 24.3521 +    }
 24.3522 +
 24.3523 +}
    25.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    25.2 +++ b/rt/emul/compact/src/main/java/java/net/URISyntaxException.java	Sun Sep 08 11:22:51 2013 +0200
    25.3 @@ -0,0 +1,135 @@
    25.4 +/*
    25.5 + * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
    25.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    25.7 + *
    25.8 + * This code is free software; you can redistribute it and/or modify it
    25.9 + * under the terms of the GNU General Public License version 2 only, as
   25.10 + * published by the Free Software Foundation.  Oracle designates this
   25.11 + * particular file as subject to the "Classpath" exception as provided
   25.12 + * by Oracle in the LICENSE file that accompanied this code.
   25.13 + *
   25.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   25.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   25.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   25.17 + * version 2 for more details (a copy is included in the LICENSE file that
   25.18 + * accompanied this code).
   25.19 + *
   25.20 + * You should have received a copy of the GNU General Public License version
   25.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   25.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   25.23 + *
   25.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   25.25 + * or visit www.oracle.com if you need additional information or have any
   25.26 + * questions.
   25.27 + */
   25.28 +
   25.29 +package java.net;
   25.30 +
   25.31 +
   25.32 +/**
   25.33 + * Checked exception thrown to indicate that a string could not be parsed as a
   25.34 + * URI reference.
   25.35 + *
   25.36 + * @author Mark Reinhold
   25.37 + * @see URI
   25.38 + * @since 1.4
   25.39 + */
   25.40 +
   25.41 +public class URISyntaxException
   25.42 +    extends Exception
   25.43 +{
   25.44 +    private static final long serialVersionUID = 2137979680897488891L;
   25.45 +
   25.46 +    private String input;
   25.47 +    private int index;
   25.48 +
   25.49 +    /**
   25.50 +     * Constructs an instance from the given input string, reason, and error
   25.51 +     * index.
   25.52 +     *
   25.53 +     * @param  input   The input string
   25.54 +     * @param  reason  A string explaining why the input could not be parsed
   25.55 +     * @param  index   The index at which the parse error occurred,
   25.56 +     *                 or <tt>-1</tt> if the index is not known
   25.57 +     *
   25.58 +     * @throws  NullPointerException
   25.59 +     *          If either the input or reason strings are <tt>null</tt>
   25.60 +     *
   25.61 +     * @throws  IllegalArgumentException
   25.62 +     *          If the error index is less than <tt>-1</tt>
   25.63 +     */
   25.64 +    public URISyntaxException(String input, String reason, int index) {
   25.65 +        super(reason);
   25.66 +        if ((input == null) || (reason == null))
   25.67 +            throw new NullPointerException();
   25.68 +        if (index < -1)
   25.69 +            throw new IllegalArgumentException();
   25.70 +        this.input = input;
   25.71 +        this.index = index;
   25.72 +    }
   25.73 +
   25.74 +    /**
   25.75 +     * Constructs an instance from the given input string and reason.  The
   25.76 +     * resulting object will have an error index of <tt>-1</tt>.
   25.77 +     *
   25.78 +     * @param  input   The input string
   25.79 +     * @param  reason  A string explaining why the input could not be parsed
   25.80 +     *
   25.81 +     * @throws  NullPointerException
   25.82 +     *          If either the input or reason strings are <tt>null</tt>
   25.83 +     */
   25.84 +    public URISyntaxException(String input, String reason) {
   25.85 +        this(input, reason, -1);
   25.86 +    }
   25.87 +
   25.88 +    /**
   25.89 +     * Returns the input string.
   25.90 +     *
   25.91 +     * @return  The input string
   25.92 +     */
   25.93 +    public String getInput() {
   25.94 +        return input;
   25.95 +    }
   25.96 +
   25.97 +    /**
   25.98 +     * Returns a string explaining why the input string could not be parsed.
   25.99 +     *
  25.100 +     * @return  The reason string
  25.101 +     */
  25.102 +    public String getReason() {
  25.103 +        return super.getMessage();
  25.104 +    }
  25.105 +
  25.106 +    /**
  25.107 +     * Returns an index into the input string of the position at which the
  25.108 +     * parse error occurred, or <tt>-1</tt> if this position is not known.
  25.109 +     *
  25.110 +     * @return  The error index
  25.111 +     */
  25.112 +    public int getIndex() {
  25.113 +        return index;
  25.114 +    }
  25.115 +
  25.116 +    /**
  25.117 +     * Returns a string describing the parse error.  The resulting string
  25.118 +     * consists of the reason string followed by a colon character
  25.119 +     * (<tt>':'</tt>), a space, and the input string.  If the error index is
  25.120 +     * defined then the string <tt>" at index "</tt> followed by the index, in
  25.121 +     * decimal, is inserted after the reason string and before the colon
  25.122 +     * character.
  25.123 +     *
  25.124 +     * @return  A string describing the parse error
  25.125 +     */
  25.126 +    public String getMessage() {
  25.127 +        StringBuffer sb = new StringBuffer();
  25.128 +        sb.append(getReason());
  25.129 +        if (index > -1) {
  25.130 +            sb.append(" at index ");
  25.131 +            sb.append(index);
  25.132 +        }
  25.133 +        sb.append(": ");
  25.134 +        sb.append(input);
  25.135 +        return sb.toString();
  25.136 +    }
  25.137 +
  25.138 +}
    26.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    26.2 +++ b/rt/emul/compact/src/main/java/java/util/IdentityHashMap.java	Sun Sep 08 11:22:51 2013 +0200
    26.3 @@ -0,0 +1,1243 @@
    26.4 +/*
    26.5 + * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
    26.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    26.7 + *
    26.8 + * This code is free software; you can redistribute it and/or modify it
    26.9 + * under the terms of the GNU General Public License version 2 only, as
   26.10 + * published by the Free Software Foundation.  Oracle designates this
   26.11 + * particular file as subject to the "Classpath" exception as provided
   26.12 + * by Oracle in the LICENSE file that accompanied this code.
   26.13 + *
   26.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   26.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   26.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   26.17 + * version 2 for more details (a copy is included in the LICENSE file that
   26.18 + * accompanied this code).
   26.19 + *
   26.20 + * You should have received a copy of the GNU General Public License version
   26.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   26.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   26.23 + *
   26.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   26.25 + * or visit www.oracle.com if you need additional information or have any
   26.26 + * questions.
   26.27 + */
   26.28 +
   26.29 +package java.util;
   26.30 +import java.io.*;
   26.31 +
   26.32 +/**
   26.33 + * This class implements the <tt>Map</tt> interface with a hash table, using
   26.34 + * reference-equality in place of object-equality when comparing keys (and
   26.35 + * values).  In other words, in an <tt>IdentityHashMap</tt>, two keys
   26.36 + * <tt>k1</tt> and <tt>k2</tt> are considered equal if and only if
   26.37 + * <tt>(k1==k2)</tt>.  (In normal <tt>Map</tt> implementations (like
   26.38 + * <tt>HashMap</tt>) two keys <tt>k1</tt> and <tt>k2</tt> are considered equal
   26.39 + * if and only if <tt>(k1==null ? k2==null : k1.equals(k2))</tt>.)
   26.40 + *
   26.41 + * <p><b>This class is <i>not</i> a general-purpose <tt>Map</tt>
   26.42 + * implementation!  While this class implements the <tt>Map</tt> interface, it
   26.43 + * intentionally violates <tt>Map's</tt> general contract, which mandates the
   26.44 + * use of the <tt>equals</tt> method when comparing objects.  This class is
   26.45 + * designed for use only in the rare cases wherein reference-equality
   26.46 + * semantics are required.</b>
   26.47 + *
   26.48 + * <p>A typical use of this class is <i>topology-preserving object graph
   26.49 + * transformations</i>, such as serialization or deep-copying.  To perform such
   26.50 + * a transformation, a program must maintain a "node table" that keeps track
   26.51 + * of all the object references that have already been processed.  The node
   26.52 + * table must not equate distinct objects even if they happen to be equal.
   26.53 + * Another typical use of this class is to maintain <i>proxy objects</i>.  For
   26.54 + * example, a debugging facility might wish to maintain a proxy object for
   26.55 + * each object in the program being debugged.
   26.56 + *
   26.57 + * <p>This class provides all of the optional map operations, and permits
   26.58 + * <tt>null</tt> values and the <tt>null</tt> key.  This class makes no
   26.59 + * guarantees as to the order of the map; in particular, it does not guarantee
   26.60 + * that the order will remain constant over time.
   26.61 + *
   26.62 + * <p>This class provides constant-time performance for the basic
   26.63 + * operations (<tt>get</tt> and <tt>put</tt>), assuming the system
   26.64 + * identity hash function ({@link System#identityHashCode(Object)})
   26.65 + * disperses elements properly among the buckets.
   26.66 + *
   26.67 + * <p>This class has one tuning parameter (which affects performance but not
   26.68 + * semantics): <i>expected maximum size</i>.  This parameter is the maximum
   26.69 + * number of key-value mappings that the map is expected to hold.  Internally,
   26.70 + * this parameter is used to determine the number of buckets initially
   26.71 + * comprising the hash table.  The precise relationship between the expected
   26.72 + * maximum size and the number of buckets is unspecified.
   26.73 + *
   26.74 + * <p>If the size of the map (the number of key-value mappings) sufficiently
   26.75 + * exceeds the expected maximum size, the number of buckets is increased
   26.76 + * Increasing the number of buckets ("rehashing") may be fairly expensive, so
   26.77 + * it pays to create identity hash maps with a sufficiently large expected
   26.78 + * maximum size.  On the other hand, iteration over collection views requires
   26.79 + * time proportional to the number of buckets in the hash table, so it
   26.80 + * pays not to set the expected maximum size too high if you are especially
   26.81 + * concerned with iteration performance or memory usage.
   26.82 + *
   26.83 + * <p><strong>Note that this implementation is not synchronized.</strong>
   26.84 + * If multiple threads access an identity hash map concurrently, and at
   26.85 + * least one of the threads modifies the map structurally, it <i>must</i>
   26.86 + * be synchronized externally.  (A structural modification is any operation
   26.87 + * that adds or deletes one or more mappings; merely changing the value
   26.88 + * associated with a key that an instance already contains is not a
   26.89 + * structural modification.)  This is typically accomplished by
   26.90 + * synchronizing on some object that naturally encapsulates the map.
   26.91 + *
   26.92 + * If no such object exists, the map should be "wrapped" using the
   26.93 + * {@link Collections#synchronizedMap Collections.synchronizedMap}
   26.94 + * method.  This is best done at creation time, to prevent accidental
   26.95 + * unsynchronized access to the map:<pre>
   26.96 + *   Map m = Collections.synchronizedMap(new IdentityHashMap(...));</pre>
   26.97 + *
   26.98 + * <p>The iterators returned by the <tt>iterator</tt> method of the
   26.99 + * collections returned by all of this class's "collection view
  26.100 + * methods" are <i>fail-fast</i>: if the map is structurally modified
  26.101 + * at any time after the iterator is created, in any way except
  26.102 + * through the iterator's own <tt>remove</tt> method, the iterator
  26.103 + * will throw a {@link ConcurrentModificationException}.  Thus, in the
  26.104 + * face of concurrent modification, the iterator fails quickly and
  26.105 + * cleanly, rather than risking arbitrary, non-deterministic behavior
  26.106 + * at an undetermined time in the future.
  26.107 + *
  26.108 + * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
  26.109 + * as it is, generally speaking, impossible to make any hard guarantees in the
  26.110 + * presence of unsynchronized concurrent modification.  Fail-fast iterators
  26.111 + * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
  26.112 + * Therefore, it would be wrong to write a program that depended on this
  26.113 + * exception for its correctness: <i>fail-fast iterators should be used only
  26.114 + * to detect bugs.</i>
  26.115 + *
  26.116 + * <p>Implementation note: This is a simple <i>linear-probe</i> hash table,
  26.117 + * as described for example in texts by Sedgewick and Knuth.  The array
  26.118 + * alternates holding keys and values.  (This has better locality for large
  26.119 + * tables than does using separate arrays.)  For many JRE implementations
  26.120 + * and operation mixes, this class will yield better performance than
  26.121 + * {@link HashMap} (which uses <i>chaining</i> rather than linear-probing).
  26.122 + *
  26.123 + * <p>This class is a member of the
  26.124 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
  26.125 + * Java Collections Framework</a>.
  26.126 + *
  26.127 + * @see     System#identityHashCode(Object)
  26.128 + * @see     Object#hashCode()
  26.129 + * @see     Collection
  26.130 + * @see     Map
  26.131 + * @see     HashMap
  26.132 + * @see     TreeMap
  26.133 + * @author  Doug Lea and Josh Bloch
  26.134 + * @since   1.4
  26.135 + */
  26.136 +
  26.137 +public class IdentityHashMap<K,V>
  26.138 +    extends AbstractMap<K,V>
  26.139 +    implements Map<K,V>, java.io.Serializable, Cloneable
  26.140 +{
  26.141 +    /**
  26.142 +     * The initial capacity used by the no-args constructor.
  26.143 +     * MUST be a power of two.  The value 32 corresponds to the
  26.144 +     * (specified) expected maximum size of 21, given a load factor
  26.145 +     * of 2/3.
  26.146 +     */
  26.147 +    private static final int DEFAULT_CAPACITY = 32;
  26.148 +
  26.149 +    /**
  26.150 +     * The minimum capacity, used if a lower value is implicitly specified
  26.151 +     * by either of the constructors with arguments.  The value 4 corresponds
  26.152 +     * to an expected maximum size of 2, given a load factor of 2/3.
  26.153 +     * MUST be a power of two.
  26.154 +     */
  26.155 +    private static final int MINIMUM_CAPACITY = 4;
  26.156 +
  26.157 +    /**
  26.158 +     * The maximum capacity, used if a higher value is implicitly specified
  26.159 +     * by either of the constructors with arguments.
  26.160 +     * MUST be a power of two <= 1<<29.
  26.161 +     */
  26.162 +    private static final int MAXIMUM_CAPACITY = 1 << 29;
  26.163 +
  26.164 +    /**
  26.165 +     * The table, resized as necessary. Length MUST always be a power of two.
  26.166 +     */
  26.167 +    private transient Object[] table;
  26.168 +
  26.169 +    /**
  26.170 +     * The number of key-value mappings contained in this identity hash map.
  26.171 +     *
  26.172 +     * @serial
  26.173 +     */
  26.174 +    private int size;
  26.175 +
  26.176 +    /**
  26.177 +     * The number of modifications, to support fast-fail iterators
  26.178 +     */
  26.179 +    private transient int modCount;
  26.180 +
  26.181 +    /**
  26.182 +     * The next size value at which to resize (capacity * load factor).
  26.183 +     */
  26.184 +    private transient int threshold;
  26.185 +
  26.186 +    /**
  26.187 +     * Value representing null keys inside tables.
  26.188 +     */
  26.189 +    private static final Object NULL_KEY = new Object();
  26.190 +
  26.191 +    /**
  26.192 +     * Use NULL_KEY for key if it is null.
  26.193 +     */
  26.194 +    private static Object maskNull(Object key) {
  26.195 +        return (key == null ? NULL_KEY : key);
  26.196 +    }
  26.197 +
  26.198 +    /**
  26.199 +     * Returns internal representation of null key back to caller as null.
  26.200 +     */
  26.201 +    private static Object unmaskNull(Object key) {
  26.202 +        return (key == NULL_KEY ? null : key);
  26.203 +    }
  26.204 +
  26.205 +    /**
  26.206 +     * Constructs a new, empty identity hash map with a default expected
  26.207 +     * maximum size (21).
  26.208 +     */
  26.209 +    public IdentityHashMap() {
  26.210 +        init(DEFAULT_CAPACITY);
  26.211 +    }
  26.212 +
  26.213 +    /**
  26.214 +     * Constructs a new, empty map with the specified expected maximum size.
  26.215 +     * Putting more than the expected number of key-value mappings into
  26.216 +     * the map may cause the internal data structure to grow, which may be
  26.217 +     * somewhat time-consuming.
  26.218 +     *
  26.219 +     * @param expectedMaxSize the expected maximum size of the map
  26.220 +     * @throws IllegalArgumentException if <tt>expectedMaxSize</tt> is negative
  26.221 +     */
  26.222 +    public IdentityHashMap(int expectedMaxSize) {
  26.223 +        if (expectedMaxSize < 0)
  26.224 +            throw new IllegalArgumentException("expectedMaxSize is negative: "
  26.225 +                                               + expectedMaxSize);
  26.226 +        init(capacity(expectedMaxSize));
  26.227 +    }
  26.228 +
  26.229 +    /**
  26.230 +     * Returns the appropriate capacity for the specified expected maximum
  26.231 +     * size.  Returns the smallest power of two between MINIMUM_CAPACITY
  26.232 +     * and MAXIMUM_CAPACITY, inclusive, that is greater than
  26.233 +     * (3 * expectedMaxSize)/2, if such a number exists.  Otherwise
  26.234 +     * returns MAXIMUM_CAPACITY.  If (3 * expectedMaxSize)/2 is negative, it
  26.235 +     * is assumed that overflow has occurred, and MAXIMUM_CAPACITY is returned.
  26.236 +     */
  26.237 +    private int capacity(int expectedMaxSize) {
  26.238 +        // Compute min capacity for expectedMaxSize given a load factor of 2/3
  26.239 +        int minCapacity = (3 * expectedMaxSize)/2;
  26.240 +
  26.241 +        // Compute the appropriate capacity
  26.242 +        int result;
  26.243 +        if (minCapacity > MAXIMUM_CAPACITY || minCapacity < 0) {
  26.244 +            result = MAXIMUM_CAPACITY;
  26.245 +        } else {
  26.246 +            result = MINIMUM_CAPACITY;
  26.247 +            while (result < minCapacity)
  26.248 +                result <<= 1;
  26.249 +        }
  26.250 +        return result;
  26.251 +    }
  26.252 +
  26.253 +    /**
  26.254 +     * Initializes object to be an empty map with the specified initial
  26.255 +     * capacity, which is assumed to be a power of two between
  26.256 +     * MINIMUM_CAPACITY and MAXIMUM_CAPACITY inclusive.
  26.257 +     */
  26.258 +    private void init(int initCapacity) {
  26.259 +        // assert (initCapacity & -initCapacity) == initCapacity; // power of 2
  26.260 +        // assert initCapacity >= MINIMUM_CAPACITY;
  26.261 +        // assert initCapacity <= MAXIMUM_CAPACITY;
  26.262 +
  26.263 +        threshold = (initCapacity * 2)/3;
  26.264 +        table = new Object[2 * initCapacity];
  26.265 +    }
  26.266 +
  26.267 +    /**
  26.268 +     * Constructs a new identity hash map containing the keys-value mappings
  26.269 +     * in the specified map.
  26.270 +     *
  26.271 +     * @param m the map whose mappings are to be placed into this map
  26.272 +     * @throws NullPointerException if the specified map is null
  26.273 +     */
  26.274 +    public IdentityHashMap(Map<? extends K, ? extends V> m) {
  26.275 +        // Allow for a bit of growth
  26.276 +        this((int) ((1 + m.size()) * 1.1));
  26.277 +        putAll(m);
  26.278 +    }
  26.279 +
  26.280 +    /**
  26.281 +     * Returns the number of key-value mappings in this identity hash map.
  26.282 +     *
  26.283 +     * @return the number of key-value mappings in this map
  26.284 +     */
  26.285 +    public int size() {
  26.286 +        return size;
  26.287 +    }
  26.288 +
  26.289 +    /**
  26.290 +     * Returns <tt>true</tt> if this identity hash map contains no key-value
  26.291 +     * mappings.
  26.292 +     *
  26.293 +     * @return <tt>true</tt> if this identity hash map contains no key-value
  26.294 +     *         mappings
  26.295 +     */
  26.296 +    public boolean isEmpty() {
  26.297 +        return size == 0;
  26.298 +    }
  26.299 +
  26.300 +    /**
  26.301 +     * Returns index for Object x.
  26.302 +     */
  26.303 +    private static int hash(Object x, int length) {
  26.304 +        int h = System.identityHashCode(x);
  26.305 +        // Multiply by -127, and left-shift to use least bit as part of hash
  26.306 +        return ((h << 1) - (h << 8)) & (length - 1);
  26.307 +    }
  26.308 +
  26.309 +    /**
  26.310 +     * Circularly traverses table of size len.
  26.311 +     */
  26.312 +    private static int nextKeyIndex(int i, int len) {
  26.313 +        return (i + 2 < len ? i + 2 : 0);
  26.314 +    }
  26.315 +
  26.316 +    /**
  26.317 +     * Returns the value to which the specified key is mapped,
  26.318 +     * or {@code null} if this map contains no mapping for the key.
  26.319 +     *
  26.320 +     * <p>More formally, if this map contains a mapping from a key
  26.321 +     * {@code k} to a value {@code v} such that {@code (key == k)},
  26.322 +     * then this method returns {@code v}; otherwise it returns
  26.323 +     * {@code null}.  (There can be at most one such mapping.)
  26.324 +     *
  26.325 +     * <p>A return value of {@code null} does not <i>necessarily</i>
  26.326 +     * indicate that the map contains no mapping for the key; it's also
  26.327 +     * possible that the map explicitly maps the key to {@code null}.
  26.328 +     * The {@link #containsKey containsKey} operation may be used to
  26.329 +     * distinguish these two cases.
  26.330 +     *
  26.331 +     * @see #put(Object, Object)
  26.332 +     */
  26.333 +    public V get(Object key) {
  26.334 +        Object k = maskNull(key);
  26.335 +        Object[] tab = table;
  26.336 +        int len = tab.length;
  26.337 +        int i = hash(k, len);
  26.338 +        while (true) {
  26.339 +            Object item = tab[i];
  26.340 +            if (item == k)
  26.341 +                return (V) tab[i + 1];
  26.342 +            if (item == null)
  26.343 +                return null;
  26.344 +            i = nextKeyIndex(i, len);
  26.345 +        }
  26.346 +    }
  26.347 +
  26.348 +    /**
  26.349 +     * Tests whether the specified object reference is a key in this identity
  26.350 +     * hash map.
  26.351 +     *
  26.352 +     * @param   key   possible key
  26.353 +     * @return  <code>true</code> if the specified object reference is a key
  26.354 +     *          in this map
  26.355 +     * @see     #containsValue(Object)
  26.356 +     */
  26.357 +    public boolean containsKey(Object key) {
  26.358 +        Object k = maskNull(key);
  26.359 +        Object[] tab = table;
  26.360 +        int len = tab.length;
  26.361 +        int i = hash(k, len);
  26.362 +        while (true) {
  26.363 +            Object item = tab[i];
  26.364 +            if (item == k)
  26.365 +                return true;
  26.366 +            if (item == null)
  26.367 +                return false;
  26.368 +            i = nextKeyIndex(i, len);
  26.369 +        }
  26.370 +    }
  26.371 +
  26.372 +    /**
  26.373 +     * Tests whether the specified object reference is a value in this identity
  26.374 +     * hash map.
  26.375 +     *
  26.376 +     * @param value value whose presence in this map is to be tested
  26.377 +     * @return <tt>true</tt> if this map maps one or more keys to the
  26.378 +     *         specified object reference
  26.379 +     * @see     #containsKey(Object)
  26.380 +     */
  26.381 +    public boolean containsValue(Object value) {
  26.382 +        Object[] tab = table;
  26.383 +        for (int i = 1; i < tab.length; i += 2)
  26.384 +            if (tab[i] == value && tab[i - 1] != null)
  26.385 +                return true;
  26.386 +
  26.387 +        return false;
  26.388 +    }
  26.389 +
  26.390 +    /**
  26.391 +     * Tests if the specified key-value mapping is in the map.
  26.392 +     *
  26.393 +     * @param   key   possible key
  26.394 +     * @param   value possible value
  26.395 +     * @return  <code>true</code> if and only if the specified key-value
  26.396 +     *          mapping is in the map
  26.397 +     */
  26.398 +    private boolean containsMapping(Object key, Object value) {
  26.399 +        Object k = maskNull(key);
  26.400 +        Object[] tab = table;
  26.401 +        int len = tab.length;
  26.402 +        int i = hash(k, len);
  26.403 +        while (true) {
  26.404 +            Object item = tab[i];
  26.405 +            if (item == k)
  26.406 +                return tab[i + 1] == value;
  26.407 +            if (item == null)
  26.408 +                return false;
  26.409 +            i = nextKeyIndex(i, len);
  26.410 +        }
  26.411 +    }
  26.412 +
  26.413 +    /**
  26.414 +     * Associates the specified value with the specified key in this identity
  26.415 +     * hash map.  If the map previously contained a mapping for the key, the
  26.416 +     * old value is replaced.
  26.417 +     *
  26.418 +     * @param key the key with which the specified value is to be associated
  26.419 +     * @param value the value to be associated with the specified key
  26.420 +     * @return the previous value associated with <tt>key</tt>, or
  26.421 +     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
  26.422 +     *         (A <tt>null</tt> return can also indicate that the map
  26.423 +     *         previously associated <tt>null</tt> with <tt>key</tt>.)
  26.424 +     * @see     Object#equals(Object)
  26.425 +     * @see     #get(Object)
  26.426 +     * @see     #containsKey(Object)
  26.427 +     */
  26.428 +    public V put(K key, V value) {
  26.429 +        Object k = maskNull(key);
  26.430 +        Object[] tab = table;
  26.431 +        int len = tab.length;
  26.432 +        int i = hash(k, len);
  26.433 +
  26.434 +        Object item;
  26.435 +        while ( (item = tab[i]) != null) {
  26.436 +            if (item == k) {
  26.437 +                V oldValue = (V) tab[i + 1];
  26.438 +                tab[i + 1] = value;
  26.439 +                return oldValue;
  26.440 +            }
  26.441 +            i = nextKeyIndex(i, len);
  26.442 +        }
  26.443 +
  26.444 +        modCount++;
  26.445 +        tab[i] = k;
  26.446 +        tab[i + 1] = value;
  26.447 +        if (++size >= threshold)
  26.448 +            resize(len); // len == 2 * current capacity.
  26.449 +        return null;
  26.450 +    }
  26.451 +
  26.452 +    /**
  26.453 +     * Resize the table to hold given capacity.
  26.454 +     *
  26.455 +     * @param newCapacity the new capacity, must be a power of two.
  26.456 +     */
  26.457 +    private void resize(int newCapacity) {
  26.458 +        // assert (newCapacity & -newCapacity) == newCapacity; // power of 2
  26.459 +        int newLength = newCapacity * 2;
  26.460 +
  26.461 +        Object[] oldTable = table;
  26.462 +        int oldLength = oldTable.length;
  26.463 +        if (oldLength == 2*MAXIMUM_CAPACITY) { // can't expand any further
  26.464 +            if (threshold == MAXIMUM_CAPACITY-1)
  26.465 +                throw new IllegalStateException("Capacity exhausted.");
  26.466 +            threshold = MAXIMUM_CAPACITY-1;  // Gigantic map!
  26.467 +            return;
  26.468 +        }
  26.469 +        if (oldLength >= newLength)
  26.470 +            return;
  26.471 +
  26.472 +        Object[] newTable = new Object[newLength];
  26.473 +        threshold = newLength / 3;
  26.474 +
  26.475 +        for (int j = 0; j < oldLength; j += 2) {
  26.476 +            Object key = oldTable[j];
  26.477 +            if (key != null) {
  26.478 +                Object value = oldTable[j+1];
  26.479 +                oldTable[j] = null;
  26.480 +                oldTable[j+1] = null;
  26.481 +                int i = hash(key, newLength);
  26.482 +                while (newTable[i] != null)
  26.483 +                    i = nextKeyIndex(i, newLength);
  26.484 +                newTable[i] = key;
  26.485 +                newTable[i + 1] = value;
  26.486 +            }
  26.487 +        }
  26.488 +        table = newTable;
  26.489 +    }
  26.490 +
  26.491 +    /**
  26.492 +     * Copies all of the mappings from the specified map to this map.
  26.493 +     * These mappings will replace any mappings that this map had for
  26.494 +     * any of the keys currently in the specified map.
  26.495 +     *
  26.496 +     * @param m mappings to be stored in this map
  26.497 +     * @throws NullPointerException if the specified map is null
  26.498 +     */
  26.499 +    public void putAll(Map<? extends K, ? extends V> m) {
  26.500 +        int n = m.size();
  26.501 +        if (n == 0)
  26.502 +            return;
  26.503 +        if (n > threshold) // conservatively pre-expand
  26.504 +            resize(capacity(n));
  26.505 +
  26.506 +        for (Entry<? extends K, ? extends V> e : m.entrySet())
  26.507 +            put(e.getKey(), e.getValue());
  26.508 +    }
  26.509 +
  26.510 +    /**
  26.511 +     * Removes the mapping for this key from this map if present.
  26.512 +     *
  26.513 +     * @param key key whose mapping is to be removed from the map
  26.514 +     * @return the previous value associated with <tt>key</tt>, or
  26.515 +     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
  26.516 +     *         (A <tt>null</tt> return can also indicate that the map
  26.517 +     *         previously associated <tt>null</tt> with <tt>key</tt>.)
  26.518 +     */
  26.519 +    public V remove(Object key) {
  26.520 +        Object k = maskNull(key);
  26.521 +        Object[] tab = table;
  26.522 +        int len = tab.length;
  26.523 +        int i = hash(k, len);
  26.524 +
  26.525 +        while (true) {
  26.526 +            Object item = tab[i];
  26.527 +            if (item == k) {
  26.528 +                modCount++;
  26.529 +                size--;
  26.530 +                V oldValue = (V) tab[i + 1];
  26.531 +                tab[i + 1] = null;
  26.532 +                tab[i] = null;
  26.533 +                closeDeletion(i);
  26.534 +                return oldValue;
  26.535 +            }
  26.536 +            if (item == null)
  26.537 +                return null;
  26.538 +            i = nextKeyIndex(i, len);
  26.539 +        }
  26.540 +
  26.541 +    }
  26.542 +
  26.543 +    /**
  26.544 +     * Removes the specified key-value mapping from the map if it is present.
  26.545 +     *
  26.546 +     * @param   key   possible key
  26.547 +     * @param   value possible value
  26.548 +     * @return  <code>true</code> if and only if the specified key-value
  26.549 +     *          mapping was in the map
  26.550 +     */
  26.551 +    private boolean removeMapping(Object key, Object value) {
  26.552 +        Object k = maskNull(key);
  26.553 +        Object[] tab = table;
  26.554 +        int len = tab.length;
  26.555 +        int i = hash(k, len);
  26.556 +
  26.557 +        while (true) {
  26.558 +            Object item = tab[i];
  26.559 +            if (item == k) {
  26.560 +                if (tab[i + 1] != value)
  26.561 +                    return false;
  26.562 +                modCount++;
  26.563 +                size--;
  26.564 +                tab[i] = null;
  26.565 +                tab[i + 1] = null;
  26.566 +                closeDeletion(i);
  26.567 +                return true;
  26.568 +            }
  26.569 +            if (item == null)
  26.570 +                return false;
  26.571 +            i = nextKeyIndex(i, len);
  26.572 +        }
  26.573 +    }
  26.574 +
  26.575 +    /**
  26.576 +     * Rehash all possibly-colliding entries following a
  26.577 +     * deletion. This preserves the linear-probe
  26.578 +     * collision properties required by get, put, etc.
  26.579 +     *
  26.580 +     * @param d the index of a newly empty deleted slot
  26.581 +     */
  26.582 +    private void closeDeletion(int d) {
  26.583 +        // Adapted from Knuth Section 6.4 Algorithm R
  26.584 +        Object[] tab = table;
  26.585 +        int len = tab.length;
  26.586 +
  26.587 +        // Look for items to swap into newly vacated slot
  26.588 +        // starting at index immediately following deletion,
  26.589 +        // and continuing until a null slot is seen, indicating
  26.590 +        // the end of a run of possibly-colliding keys.
  26.591 +        Object item;
  26.592 +        for (int i = nextKeyIndex(d, len); (item = tab[i]) != null;
  26.593 +             i = nextKeyIndex(i, len) ) {
  26.594 +            // The following test triggers if the item at slot i (which
  26.595 +            // hashes to be at slot r) should take the spot vacated by d.
  26.596 +            // If so, we swap it in, and then continue with d now at the
  26.597 +            // newly vacated i.  This process will terminate when we hit
  26.598 +            // the null slot at the end of this run.
  26.599 +            // The test is messy because we are using a circular table.
  26.600 +            int r = hash(item, len);
  26.601 +            if ((i < r && (r <= d || d <= i)) || (r <= d && d <= i)) {
  26.602 +                tab[d] = item;
  26.603 +                tab[d + 1] = tab[i + 1];
  26.604 +                tab[i] = null;
  26.605 +                tab[i + 1] = null;
  26.606 +                d = i;
  26.607 +            }
  26.608 +        }
  26.609 +    }
  26.610 +
  26.611 +    /**
  26.612 +     * Removes all of the mappings from this map.
  26.613 +     * The map will be empty after this call returns.
  26.614 +     */
  26.615 +    public void clear() {
  26.616 +        modCount++;
  26.617 +        Object[] tab = table;
  26.618 +        for (int i = 0; i < tab.length; i++)
  26.619 +            tab[i] = null;
  26.620 +        size = 0;
  26.621 +    }
  26.622 +
  26.623 +    /**
  26.624 +     * Compares the specified object with this map for equality.  Returns
  26.625 +     * <tt>true</tt> if the given object is also a map and the two maps
  26.626 +     * represent identical object-reference mappings.  More formally, this
  26.627 +     * map is equal to another map <tt>m</tt> if and only if
  26.628 +     * <tt>this.entrySet().equals(m.entrySet())</tt>.
  26.629 +     *
  26.630 +     * <p><b>Owing to the reference-equality-based semantics of this map it is
  26.631 +     * possible that the symmetry and transitivity requirements of the
  26.632 +     * <tt>Object.equals</tt> contract may be violated if this map is compared
  26.633 +     * to a normal map.  However, the <tt>Object.equals</tt> contract is
  26.634 +     * guaranteed to hold among <tt>IdentityHashMap</tt> instances.</b>
  26.635 +     *
  26.636 +     * @param  o object to be compared for equality with this map
  26.637 +     * @return <tt>true</tt> if the specified object is equal to this map
  26.638 +     * @see Object#equals(Object)
  26.639 +     */
  26.640 +    public boolean equals(Object o) {
  26.641 +        if (o == this) {
  26.642 +            return true;
  26.643 +        } else if (o instanceof IdentityHashMap) {
  26.644 +            IdentityHashMap m = (IdentityHashMap) o;
  26.645 +            if (m.size() != size)
  26.646 +                return false;
  26.647 +
  26.648 +            Object[] tab = m.table;
  26.649 +            for (int i = 0; i < tab.length; i+=2) {
  26.650 +                Object k = tab[i];
  26.651 +                if (k != null && !containsMapping(k, tab[i + 1]))
  26.652 +                    return false;
  26.653 +            }
  26.654 +            return true;
  26.655 +        } else if (o instanceof Map) {
  26.656 +            Map m = (Map)o;
  26.657 +            return entrySet().equals(m.entrySet());
  26.658 +        } else {
  26.659 +            return false;  // o is not a Map
  26.660 +        }
  26.661 +    }
  26.662 +
  26.663 +    /**
  26.664 +     * Returns the hash code value for this map.  The hash code of a map is
  26.665 +     * defined to be the sum of the hash codes of each entry in the map's
  26.666 +     * <tt>entrySet()</tt> view.  This ensures that <tt>m1.equals(m2)</tt>
  26.667 +     * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two
  26.668 +     * <tt>IdentityHashMap</tt> instances <tt>m1</tt> and <tt>m2</tt>, as
  26.669 +     * required by the general contract of {@link Object#hashCode}.
  26.670 +     *
  26.671 +     * <p><b>Owing to the reference-equality-based semantics of the
  26.672 +     * <tt>Map.Entry</tt> instances in the set returned by this map's
  26.673 +     * <tt>entrySet</tt> method, it is possible that the contractual
  26.674 +     * requirement of <tt>Object.hashCode</tt> mentioned in the previous
  26.675 +     * paragraph will be violated if one of the two objects being compared is
  26.676 +     * an <tt>IdentityHashMap</tt> instance and the other is a normal map.</b>
  26.677 +     *
  26.678 +     * @return the hash code value for this map
  26.679 +     * @see Object#equals(Object)
  26.680 +     * @see #equals(Object)
  26.681 +     */
  26.682 +    public int hashCode() {
  26.683 +        int result = 0;
  26.684 +        Object[] tab = table;
  26.685 +        for (int i = 0; i < tab.length; i +=2) {
  26.686 +            Object key = tab[i];
  26.687 +            if (key != null) {
  26.688 +                Object k = unmaskNull(key);
  26.689 +                result += System.identityHashCode(k) ^
  26.690 +                          System.identityHashCode(tab[i + 1]);
  26.691 +            }
  26.692 +        }
  26.693 +        return result;
  26.694 +    }
  26.695 +
  26.696 +    /**
  26.697 +     * Returns a shallow copy of this identity hash map: the keys and values
  26.698 +     * themselves are not cloned.
  26.699 +     *
  26.700 +     * @return a shallow copy of this map
  26.701 +     */
  26.702 +    public Object clone() {
  26.703 +        try {
  26.704 +            IdentityHashMap<K,V> m = (IdentityHashMap<K,V>) super.clone();
  26.705 +            m.entrySet = null;
  26.706 +            m.table = table.clone();
  26.707 +            return m;
  26.708 +        } catch (CloneNotSupportedException e) {
  26.709 +            throw new InternalError();
  26.710 +        }
  26.711 +    }
  26.712 +
  26.713 +    private abstract class IdentityHashMapIterator<T> implements Iterator<T> {
  26.714 +        int index = (size != 0 ? 0 : table.length); // current slot.
  26.715 +        int expectedModCount = modCount; // to support fast-fail
  26.716 +        int lastReturnedIndex = -1;      // to allow remove()
  26.717 +        boolean indexValid; // To avoid unnecessary next computation
  26.718 +        Object[] traversalTable = table; // reference to main table or copy
  26.719 +
  26.720 +        public boolean hasNext() {
  26.721 +            Object[] tab = traversalTable;
  26.722 +            for (int i = index; i < tab.length; i+=2) {
  26.723 +                Object key = tab[i];
  26.724 +                if (key != null) {
  26.725 +                    index = i;
  26.726 +                    return indexValid = true;
  26.727 +                }
  26.728 +            }
  26.729 +            index = tab.length;
  26.730 +            return false;
  26.731 +        }
  26.732 +
  26.733 +        protected int nextIndex() {
  26.734 +            if (modCount != expectedModCount)
  26.735 +                throw new ConcurrentModificationException();
  26.736 +            if (!indexValid && !hasNext())
  26.737 +                throw new NoSuchElementException();
  26.738 +
  26.739 +            indexValid = false;
  26.740 +            lastReturnedIndex = index;
  26.741 +            index += 2;
  26.742 +            return lastReturnedIndex;
  26.743 +        }
  26.744 +
  26.745 +        public void remove() {
  26.746 +            if (lastReturnedIndex == -1)
  26.747 +                throw new IllegalStateException();
  26.748 +            if (modCount != expectedModCount)
  26.749 +                throw new ConcurrentModificationException();
  26.750 +
  26.751 +            expectedModCount = ++modCount;
  26.752 +            int deletedSlot = lastReturnedIndex;
  26.753 +            lastReturnedIndex = -1;
  26.754 +            // back up index to revisit new contents after deletion
  26.755 +            index = deletedSlot;
  26.756 +            indexValid = false;
  26.757 +
  26.758 +            // Removal code proceeds as in closeDeletion except that
  26.759 +            // it must catch the rare case where an element already
  26.760 +            // seen is swapped into a vacant slot that will be later
  26.761 +            // traversed by this iterator. We cannot allow future
  26.762 +            // next() calls to return it again.  The likelihood of
  26.763 +            // this occurring under 2/3 load factor is very slim, but
  26.764 +            // when it does happen, we must make a copy of the rest of
  26.765 +            // the table to use for the rest of the traversal. Since
  26.766 +            // this can only happen when we are near the end of the table,
  26.767 +            // even in these rare cases, this is not very expensive in
  26.768 +            // time or space.
  26.769 +
  26.770 +            Object[] tab = traversalTable;
  26.771 +            int len = tab.length;
  26.772 +
  26.773 +            int d = deletedSlot;
  26.774 +            K key = (K) tab[d];
  26.775 +            tab[d] = null;        // vacate the slot
  26.776 +            tab[d + 1] = null;
  26.777 +
  26.778 +            // If traversing a copy, remove in real table.
  26.779 +            // We can skip gap-closure on copy.
  26.780 +            if (tab != IdentityHashMap.this.table) {
  26.781 +                IdentityHashMap.this.remove(key);
  26.782 +                expectedModCount = modCount;
  26.783 +                return;
  26.784 +            }
  26.785 +
  26.786 +            size--;
  26.787 +
  26.788 +            Object item;
  26.789 +            for (int i = nextKeyIndex(d, len); (item = tab[i]) != null;
  26.790 +                 i = nextKeyIndex(i, len)) {
  26.791 +                int r = hash(item, len);
  26.792 +                // See closeDeletion for explanation of this conditional
  26.793 +                if ((i < r && (r <= d || d <= i)) ||
  26.794 +                    (r <= d && d <= i)) {
  26.795 +
  26.796 +                    // If we are about to swap an already-seen element
  26.797 +                    // into a slot that may later be returned by next(),
  26.798 +                    // then clone the rest of table for use in future
  26.799 +                    // next() calls. It is OK that our copy will have
  26.800 +                    // a gap in the "wrong" place, since it will never
  26.801 +                    // be used for searching anyway.
  26.802 +
  26.803 +                    if (i < deletedSlot && d >= deletedSlot &&
  26.804 +                        traversalTable == IdentityHashMap.this.table) {
  26.805 +                        int remaining = len - deletedSlot;
  26.806 +                        Object[] newTable = new Object[remaining];
  26.807 +                        System.arraycopy(tab, deletedSlot,
  26.808 +                                         newTable, 0, remaining);
  26.809 +                        traversalTable = newTable;
  26.810 +                        index = 0;
  26.811 +                    }
  26.812 +
  26.813 +                    tab[d] = item;
  26.814 +                    tab[d + 1] = tab[i + 1];
  26.815 +                    tab[i] = null;
  26.816 +                    tab[i + 1] = null;
  26.817 +                    d = i;
  26.818 +                }
  26.819 +            }
  26.820 +        }
  26.821 +    }
  26.822 +
  26.823 +    private class KeyIterator extends IdentityHashMapIterator<K> {
  26.824 +        public K next() {
  26.825 +            return (K) unmaskNull(traversalTable[nextIndex()]);
  26.826 +        }
  26.827 +    }
  26.828 +
  26.829 +    private class ValueIterator extends IdentityHashMapIterator<V> {
  26.830 +        public V next() {
  26.831 +            return (V) traversalTable[nextIndex() + 1];
  26.832 +        }
  26.833 +    }
  26.834 +
  26.835 +    private class EntryIterator
  26.836 +        extends IdentityHashMapIterator<Map.Entry<K,V>>
  26.837 +    {
  26.838 +        private Entry lastReturnedEntry = null;
  26.839 +
  26.840 +        public Map.Entry<K,V> next() {
  26.841 +            lastReturnedEntry = new Entry(nextIndex());
  26.842 +            return lastReturnedEntry;
  26.843 +        }
  26.844 +
  26.845 +        public void remove() {
  26.846 +            lastReturnedIndex =
  26.847 +                ((null == lastReturnedEntry) ? -1 : lastReturnedEntry.index);
  26.848 +            super.remove();
  26.849 +            lastReturnedEntry.index = lastReturnedIndex;
  26.850 +            lastReturnedEntry = null;
  26.851 +        }
  26.852 +
  26.853 +        private class Entry implements Map.Entry<K,V> {
  26.854 +            private int index;
  26.855 +
  26.856 +            private Entry(int index) {
  26.857 +                this.index = index;
  26.858 +            }
  26.859 +
  26.860 +            public K getKey() {
  26.861 +                checkIndexForEntryUse();
  26.862 +                return (K) unmaskNull(traversalTable[index]);
  26.863 +            }
  26.864 +
  26.865 +            public V getValue() {
  26.866 +                checkIndexForEntryUse();
  26.867 +                return (V) traversalTable[index+1];
  26.868 +            }
  26.869 +
  26.870 +            public V setValue(V value) {
  26.871 +                checkIndexForEntryUse();
  26.872 +                V oldValue = (V) traversalTable[index+1];
  26.873 +                traversalTable[index+1] = value;
  26.874 +                // if shadowing, force into main table
  26.875 +                if (traversalTable != IdentityHashMap.this.table)
  26.876 +                    put((K) traversalTable[index], value);
  26.877 +                return oldValue;
  26.878 +            }
  26.879 +
  26.880 +            public boolean equals(Object o) {
  26.881 +                if (index < 0)
  26.882 +                    return super.equals(o);
  26.883 +
  26.884 +                if (!(o instanceof Map.Entry))
  26.885 +                    return false;
  26.886 +                Map.Entry e = (Map.Entry)o;
  26.887 +                return (e.getKey() == unmaskNull(traversalTable[index]) &&
  26.888 +                       e.getValue() == traversalTable[index+1]);
  26.889 +            }
  26.890 +
  26.891 +            public int hashCode() {
  26.892 +                if (lastReturnedIndex < 0)
  26.893 +                    return super.hashCode();
  26.894 +
  26.895 +                return (System.identityHashCode(unmaskNull(traversalTable[index])) ^
  26.896 +                       System.identityHashCode(traversalTable[index+1]));
  26.897 +            }
  26.898 +
  26.899 +            public String toString() {
  26.900 +                if (index < 0)
  26.901 +                    return super.toString();
  26.902 +
  26.903 +                return (unmaskNull(traversalTable[index]) + "="
  26.904 +                        + traversalTable[index+1]);
  26.905 +            }
  26.906 +
  26.907 +            private void checkIndexForEntryUse() {
  26.908 +                if (index < 0)
  26.909 +                    throw new IllegalStateException("Entry was removed");
  26.910 +            }
  26.911 +        }
  26.912 +    }
  26.913 +
  26.914 +    // Views
  26.915 +
  26.916 +    /**
  26.917 +     * This field is initialized to contain an instance of the entry set
  26.918 +     * view the first time this view is requested.  The view is stateless,
  26.919 +     * so there's no reason to create more than one.
  26.920 +     */
  26.921 +    private transient Set<Map.Entry<K,V>> entrySet = null;
  26.922 +
  26.923 +    /**
  26.924 +     * Returns an identity-based set view of the keys contained in this map.
  26.925 +     * The set is backed by the map, so changes to the map are reflected in
  26.926 +     * the set, and vice-versa.  If the map is modified while an iteration
  26.927 +     * over the set is in progress, the results of the iteration are
  26.928 +     * undefined.  The set supports element removal, which removes the
  26.929 +     * corresponding mapping from the map, via the <tt>Iterator.remove</tt>,
  26.930 +     * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, and
  26.931 +     * <tt>clear</tt> methods.  It does not support the <tt>add</tt> or
  26.932 +     * <tt>addAll</tt> methods.
  26.933 +     *
  26.934 +     * <p><b>While the object returned by this method implements the
  26.935 +     * <tt>Set</tt> interface, it does <i>not</i> obey <tt>Set's</tt> general
  26.936 +     * contract.  Like its backing map, the set returned by this method
  26.937 +     * defines element equality as reference-equality rather than
  26.938 +     * object-equality.  This affects the behavior of its <tt>contains</tt>,
  26.939 +     * <tt>remove</tt>, <tt>containsAll</tt>, <tt>equals</tt>, and
  26.940 +     * <tt>hashCode</tt> methods.</b>
  26.941 +     *
  26.942 +     * <p><b>The <tt>equals</tt> method of the returned set returns <tt>true</tt>
  26.943 +     * only if the specified object is a set containing exactly the same
  26.944 +     * object references as the returned set.  The symmetry and transitivity
  26.945 +     * requirements of the <tt>Object.equals</tt> contract may be violated if
  26.946 +     * the set returned by this method is compared to a normal set.  However,
  26.947 +     * the <tt>Object.equals</tt> contract is guaranteed to hold among sets
  26.948 +     * returned by this method.</b>
  26.949 +     *
  26.950 +     * <p>The <tt>hashCode</tt> method of the returned set returns the sum of
  26.951 +     * the <i>identity hashcodes</i> of the elements in the set, rather than
  26.952 +     * the sum of their hashcodes.  This is mandated by the change in the
  26.953 +     * semantics of the <tt>equals</tt> method, in order to enforce the
  26.954 +     * general contract of the <tt>Object.hashCode</tt> method among sets
  26.955 +     * returned by this method.
  26.956 +     *
  26.957 +     * @return an identity-based set view of the keys contained in this map
  26.958 +     * @see Object#equals(Object)
  26.959 +     * @see System#identityHashCode(Object)
  26.960 +     */
  26.961 +    public Set<K> keySet() {
  26.962 +        Set<K> ks = keySet;
  26.963 +        if (ks != null)
  26.964 +            return ks;
  26.965 +        else
  26.966 +            return keySet = new KeySet();
  26.967 +    }
  26.968 +
  26.969 +    private class KeySet extends AbstractSet<K> {
  26.970 +        public Iterator<K> iterator() {
  26.971 +            return new KeyIterator();
  26.972 +        }
  26.973 +        public int size() {
  26.974 +            return size;
  26.975 +        }
  26.976 +        public boolean contains(Object o) {
  26.977 +            return containsKey(o);
  26.978 +        }
  26.979 +        public boolean remove(Object o) {
  26.980 +            int oldSize = size;
  26.981 +            IdentityHashMap.this.remove(o);
  26.982 +            return size != oldSize;
  26.983 +        }
  26.984 +        /*
  26.985 +         * Must revert from AbstractSet's impl to AbstractCollection's, as
  26.986 +         * the former contains an optimization that results in incorrect
  26.987 +         * behavior when c is a smaller "normal" (non-identity-based) Set.
  26.988 +         */
  26.989 +        public boolean removeAll(Collection<?> c) {
  26.990 +            boolean modified = false;
  26.991 +            for (Iterator<K> i = iterator(); i.hasNext(); ) {
  26.992 +                if (c.contains(i.next())) {
  26.993 +                    i.remove();
  26.994 +                    modified = true;
  26.995 +                }
  26.996 +            }
  26.997 +            return modified;
  26.998 +        }
  26.999 +        public void clear() {
 26.1000 +            IdentityHashMap.this.clear();
 26.1001 +        }
 26.1002 +        public int hashCode() {
 26.1003 +            int result = 0;
 26.1004 +            for (K key : this)
 26.1005 +                result += System.identityHashCode(key);
 26.1006 +            return result;
 26.1007 +        }
 26.1008 +    }
 26.1009 +
 26.1010 +    /**
 26.1011 +     * Returns a {@link Collection} view of the values contained in this map.
 26.1012 +     * The collection is backed by the map, so changes to the map are
 26.1013 +     * reflected in the collection, and vice-versa.  If the map is
 26.1014 +     * modified while an iteration over the collection is in progress,
 26.1015 +     * the results of the iteration are undefined.  The collection
 26.1016 +     * supports element removal, which removes the corresponding
 26.1017 +     * mapping from the map, via the <tt>Iterator.remove</tt>,
 26.1018 +     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
 26.1019 +     * <tt>retainAll</tt> and <tt>clear</tt> methods.  It does not
 26.1020 +     * support the <tt>add</tt> or <tt>addAll</tt> methods.
 26.1021 +     *
 26.1022 +     * <p><b>While the object returned by this method implements the
 26.1023 +     * <tt>Collection</tt> interface, it does <i>not</i> obey
 26.1024 +     * <tt>Collection's</tt> general contract.  Like its backing map,
 26.1025 +     * the collection returned by this method defines element equality as
 26.1026 +     * reference-equality rather than object-equality.  This affects the
 26.1027 +     * behavior of its <tt>contains</tt>, <tt>remove</tt> and
 26.1028 +     * <tt>containsAll</tt> methods.</b>
 26.1029 +     */
 26.1030 +    public Collection<V> values() {
 26.1031 +        Collection<V> vs = values;
 26.1032 +        if (vs != null)
 26.1033 +            return vs;
 26.1034 +        else
 26.1035 +            return values = new Values();
 26.1036 +    }
 26.1037 +
 26.1038 +    private class Values extends AbstractCollection<V> {
 26.1039 +        public Iterator<V> iterator() {
 26.1040 +            return new ValueIterator();
 26.1041 +        }
 26.1042 +        public int size() {
 26.1043 +            return size;
 26.1044 +        }
 26.1045 +        public boolean contains(Object o) {
 26.1046 +            return containsValue(o);
 26.1047 +        }
 26.1048 +        public boolean remove(Object o) {
 26.1049 +            for (Iterator<V> i = iterator(); i.hasNext(); ) {
 26.1050 +                if (i.next() == o) {
 26.1051 +                    i.remove();
 26.1052 +                    return true;
 26.1053 +                }
 26.1054 +            }
 26.1055 +            return false;
 26.1056 +        }
 26.1057 +        public void clear() {
 26.1058 +            IdentityHashMap.this.clear();
 26.1059 +        }
 26.1060 +    }
 26.1061 +
 26.1062 +    /**
 26.1063 +     * Returns a {@link Set} view of the mappings contained in this map.
 26.1064 +     * Each element in the returned set is a reference-equality-based
 26.1065 +     * <tt>Map.Entry</tt>.  The set is backed by the map, so changes
 26.1066 +     * to the map are reflected in the set, and vice-versa.  If the
 26.1067 +     * map is modified while an iteration over the set is in progress,
 26.1068 +     * the results of the iteration are undefined.  The set supports
 26.1069 +     * element removal, which removes the corresponding mapping from
 26.1070 +     * the map, via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
 26.1071 +     * <tt>removeAll</tt>, <tt>retainAll</tt> and <tt>clear</tt>
 26.1072 +     * methods.  It does not support the <tt>add</tt> or
 26.1073 +     * <tt>addAll</tt> methods.
 26.1074 +     *
 26.1075 +     * <p>Like the backing map, the <tt>Map.Entry</tt> objects in the set
 26.1076 +     * returned by this method define key and value equality as
 26.1077 +     * reference-equality rather than object-equality.  This affects the
 26.1078 +     * behavior of the <tt>equals</tt> and <tt>hashCode</tt> methods of these
 26.1079 +     * <tt>Map.Entry</tt> objects.  A reference-equality based <tt>Map.Entry
 26.1080 +     * e</tt> is equal to an object <tt>o</tt> if and only if <tt>o</tt> is a
 26.1081 +     * <tt>Map.Entry</tt> and <tt>e.getKey()==o.getKey() &amp;&amp;
 26.1082 +     * e.getValue()==o.getValue()</tt>.  To accommodate these equals
 26.1083 +     * semantics, the <tt>hashCode</tt> method returns
 26.1084 +     * <tt>System.identityHashCode(e.getKey()) ^
 26.1085 +     * System.identityHashCode(e.getValue())</tt>.
 26.1086 +     *
 26.1087 +     * <p><b>Owing to the reference-equality-based semantics of the
 26.1088 +     * <tt>Map.Entry</tt> instances in the set returned by this method,
 26.1089 +     * it is possible that the symmetry and transitivity requirements of
 26.1090 +     * the {@link Object#equals(Object)} contract may be violated if any of
 26.1091 +     * the entries in the set is compared to a normal map entry, or if
 26.1092 +     * the set returned by this method is compared to a set of normal map
 26.1093 +     * entries (such as would be returned by a call to this method on a normal
 26.1094 +     * map).  However, the <tt>Object.equals</tt> contract is guaranteed to
 26.1095 +     * hold among identity-based map entries, and among sets of such entries.
 26.1096 +     * </b>
 26.1097 +     *
 26.1098 +     * @return a set view of the identity-mappings contained in this map
 26.1099 +     */
 26.1100 +    public Set<Map.Entry<K,V>> entrySet() {
 26.1101 +        Set<Map.Entry<K,V>> es = entrySet;
 26.1102 +        if (es != null)
 26.1103 +            return es;
 26.1104 +        else
 26.1105 +            return entrySet = new EntrySet();
 26.1106 +    }
 26.1107 +
 26.1108 +    private class EntrySet extends AbstractSet<Map.Entry<K,V>> {
 26.1109 +        public Iterator<Map.Entry<K,V>> iterator() {
 26.1110 +            return new EntryIterator();
 26.1111 +        }
 26.1112 +        public boolean contains(Object o) {
 26.1113 +            if (!(o instanceof Map.Entry))
 26.1114 +                return false;
 26.1115 +            Map.Entry entry = (Map.Entry)o;
 26.1116 +            return containsMapping(entry.getKey(), entry.getValue());
 26.1117 +        }
 26.1118 +        public boolean remove(Object o) {
 26.1119 +            if (!(o instanceof Map.Entry))
 26.1120 +                return false;
 26.1121 +            Map.Entry entry = (Map.Entry)o;
 26.1122 +            return removeMapping(entry.getKey(), entry.getValue());
 26.1123 +        }
 26.1124 +        public int size() {
 26.1125 +            return size;
 26.1126 +        }
 26.1127 +        public void clear() {
 26.1128 +            IdentityHashMap.this.clear();
 26.1129 +        }
 26.1130 +        /*
 26.1131 +         * Must revert from AbstractSet's impl to AbstractCollection's, as
 26.1132 +         * the former contains an optimization that results in incorrect
 26.1133 +         * behavior when c is a smaller "normal" (non-identity-based) Set.
 26.1134 +         */
 26.1135 +        public boolean removeAll(Collection<?> c) {
 26.1136 +            boolean modified = false;
 26.1137 +            for (Iterator<Map.Entry<K,V>> i = iterator(); i.hasNext(); ) {
 26.1138 +                if (c.contains(i.next())) {
 26.1139 +                    i.remove();
 26.1140 +                    modified = true;
 26.1141 +                }
 26.1142 +            }
 26.1143 +            return modified;
 26.1144 +        }
 26.1145 +
 26.1146 +        public Object[] toArray() {
 26.1147 +            int size = size();
 26.1148 +            Object[] result = new Object[size];
 26.1149 +            Iterator<Map.Entry<K,V>> it = iterator();
 26.1150 +            for (int i = 0; i < size; i++)
 26.1151 +                result[i] = new AbstractMap.SimpleEntry<>(it.next());
 26.1152 +            return result;
 26.1153 +        }
 26.1154 +
 26.1155 +        @SuppressWarnings("unchecked")
 26.1156 +        public <T> T[] toArray(T[] a) {
 26.1157 +            int size = size();
 26.1158 +            if (a.length < size)
 26.1159 +                a = (T[])java.lang.reflect.Array
 26.1160 +                    .newInstance(a.getClass().getComponentType(), size);
 26.1161 +            Iterator<Map.Entry<K,V>> it = iterator();
 26.1162 +            for (int i = 0; i < size; i++)
 26.1163 +                a[i] = (T) new AbstractMap.SimpleEntry<>(it.next());
 26.1164 +            if (a.length > size)
 26.1165 +                a[size] = null;
 26.1166 +            return a;
 26.1167 +        }
 26.1168 +    }
 26.1169 +
 26.1170 +
 26.1171 +    private static final long serialVersionUID = 8188218128353913216L;
 26.1172 +
 26.1173 +    /**
 26.1174 +     * Save the state of the <tt>IdentityHashMap</tt> instance to a stream
 26.1175 +     * (i.e., serialize it).
 26.1176 +     *
 26.1177 +     * @serialData The <i>size</i> of the HashMap (the number of key-value
 26.1178 +     *          mappings) (<tt>int</tt>), followed by the key (Object) and
 26.1179 +     *          value (Object) for each key-value mapping represented by the
 26.1180 +     *          IdentityHashMap.  The key-value mappings are emitted in no
 26.1181 +     *          particular order.
 26.1182 +     */
 26.1183 +    private void writeObject(java.io.ObjectOutputStream s)
 26.1184 +        throws java.io.IOException  {
 26.1185 +        // Write out and any hidden stuff
 26.1186 +        s.defaultWriteObject();
 26.1187 +
 26.1188 +        // Write out size (number of Mappings)
 26.1189 +        s.writeInt(size);
 26.1190 +
 26.1191 +        // Write out keys and values (alternating)
 26.1192 +        Object[] tab = table;
 26.1193 +        for (int i = 0; i < tab.length; i += 2) {
 26.1194 +            Object key = tab[i];
 26.1195 +            if (key != null) {
 26.1196 +                s.writeObject(unmaskNull(key));
 26.1197 +                s.writeObject(tab[i + 1]);
 26.1198 +            }
 26.1199 +        }
 26.1200 +    }
 26.1201 +
 26.1202 +    /**
 26.1203 +     * Reconstitute the <tt>IdentityHashMap</tt> instance from a stream (i.e.,
 26.1204 +     * deserialize it).
 26.1205 +     */
 26.1206 +    private void readObject(java.io.ObjectInputStream s)
 26.1207 +        throws java.io.IOException, ClassNotFoundException  {
 26.1208 +        // Read in any hidden stuff
 26.1209 +        s.defaultReadObject();
 26.1210 +
 26.1211 +        // Read in size (number of Mappings)
 26.1212 +        int size = s.readInt();
 26.1213 +
 26.1214 +        // Allow for 33% growth (i.e., capacity is >= 2* size()).
 26.1215 +        init(capacity((size*4)/3));
 26.1216 +
 26.1217 +        // Read the keys and values, and put the mappings in the table
 26.1218 +        for (int i=0; i<size; i++) {
 26.1219 +            K key = (K) s.readObject();
 26.1220 +            V value = (V) s.readObject();
 26.1221 +            putForCreate(key, value);
 26.1222 +        }
 26.1223 +    }
 26.1224 +
 26.1225 +    /**
 26.1226 +     * The put method for readObject.  It does not resize the table,
 26.1227 +     * update modCount, etc.
 26.1228 +     */
 26.1229 +    private void putForCreate(K key, V value)
 26.1230 +        throws IOException
 26.1231 +    {
 26.1232 +        K k = (K)maskNull(key);
 26.1233 +        Object[] tab = table;
 26.1234 +        int len = tab.length;
 26.1235 +        int i = hash(k, len);
 26.1236 +
 26.1237 +        Object item;
 26.1238 +        while ( (item = tab[i]) != null) {
 26.1239 +            if (item == k)
 26.1240 +                throw new java.io.StreamCorruptedException();
 26.1241 +            i = nextKeyIndex(i, len);
 26.1242 +        }
 26.1243 +        tab[i] = k;
 26.1244 +        tab[i + 1] = value;
 26.1245 +    }
 26.1246 +}
    27.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    27.2 +++ b/rt/emul/compact/src/main/java/java/util/LinkedHashSet.java	Sun Sep 08 11:22:51 2013 +0200
    27.3 @@ -0,0 +1,171 @@
    27.4 +/*
    27.5 + * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
    27.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    27.7 + *
    27.8 + * This code is free software; you can redistribute it and/or modify it
    27.9 + * under the terms of the GNU General Public License version 2 only, as
   27.10 + * published by the Free Software Foundation.  Oracle designates this
   27.11 + * particular file as subject to the "Classpath" exception as provided
   27.12 + * by Oracle in the LICENSE file that accompanied this code.
   27.13 + *
   27.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   27.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   27.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   27.17 + * version 2 for more details (a copy is included in the LICENSE file that
   27.18 + * accompanied this code).
   27.19 + *
   27.20 + * You should have received a copy of the GNU General Public License version
   27.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   27.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   27.23 + *
   27.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   27.25 + * or visit www.oracle.com if you need additional information or have any
   27.26 + * questions.
   27.27 + */
   27.28 +
   27.29 +package java.util;
   27.30 +
   27.31 +/**
   27.32 + * <p>Hash table and linked list implementation of the <tt>Set</tt> interface,
   27.33 + * with predictable iteration order.  This implementation differs from
   27.34 + * <tt>HashSet</tt> in that it maintains a doubly-linked list running through
   27.35 + * all of its entries.  This linked list defines the iteration ordering,
   27.36 + * which is the order in which elements were inserted into the set
   27.37 + * (<i>insertion-order</i>).  Note that insertion order is <i>not</i> affected
   27.38 + * if an element is <i>re-inserted</i> into the set.  (An element <tt>e</tt>
   27.39 + * is reinserted into a set <tt>s</tt> if <tt>s.add(e)</tt> is invoked when
   27.40 + * <tt>s.contains(e)</tt> would return <tt>true</tt> immediately prior to
   27.41 + * the invocation.)
   27.42 + *
   27.43 + * <p>This implementation spares its clients from the unspecified, generally
   27.44 + * chaotic ordering provided by {@link HashSet}, without incurring the
   27.45 + * increased cost associated with {@link TreeSet}.  It can be used to
   27.46 + * produce a copy of a set that has the same order as the original, regardless
   27.47 + * of the original set's implementation:
   27.48 + * <pre>
   27.49 + *     void foo(Set s) {
   27.50 + *         Set copy = new LinkedHashSet(s);
   27.51 + *         ...
   27.52 + *     }
   27.53 + * </pre>
   27.54 + * This technique is particularly useful if a module takes a set on input,
   27.55 + * copies it, and later returns results whose order is determined by that of
   27.56 + * the copy.  (Clients generally appreciate having things returned in the same
   27.57 + * order they were presented.)
   27.58 + *
   27.59 + * <p>This class provides all of the optional <tt>Set</tt> operations, and
   27.60 + * permits null elements.  Like <tt>HashSet</tt>, it provides constant-time
   27.61 + * performance for the basic operations (<tt>add</tt>, <tt>contains</tt> and
   27.62 + * <tt>remove</tt>), assuming the hash function disperses elements
   27.63 + * properly among the buckets.  Performance is likely to be just slightly
   27.64 + * below that of <tt>HashSet</tt>, due to the added expense of maintaining the
   27.65 + * linked list, with one exception: Iteration over a <tt>LinkedHashSet</tt>
   27.66 + * requires time proportional to the <i>size</i> of the set, regardless of
   27.67 + * its capacity.  Iteration over a <tt>HashSet</tt> is likely to be more
   27.68 + * expensive, requiring time proportional to its <i>capacity</i>.
   27.69 + *
   27.70 + * <p>A linked hash set has two parameters that affect its performance:
   27.71 + * <i>initial capacity</i> and <i>load factor</i>.  They are defined precisely
   27.72 + * as for <tt>HashSet</tt>.  Note, however, that the penalty for choosing an
   27.73 + * excessively high value for initial capacity is less severe for this class
   27.74 + * than for <tt>HashSet</tt>, as iteration times for this class are unaffected
   27.75 + * by capacity.
   27.76 + *
   27.77 + * <p><strong>Note that this implementation is not synchronized.</strong>
   27.78 + * If multiple threads access a linked hash set concurrently, and at least
   27.79 + * one of the threads modifies the set, it <em>must</em> be synchronized
   27.80 + * externally.  This is typically accomplished by synchronizing on some
   27.81 + * object that naturally encapsulates the set.
   27.82 + *
   27.83 + * If no such object exists, the set should be "wrapped" using the
   27.84 + * {@link Collections#synchronizedSet Collections.synchronizedSet}
   27.85 + * method.  This is best done at creation time, to prevent accidental
   27.86 + * unsynchronized access to the set: <pre>
   27.87 + *   Set s = Collections.synchronizedSet(new LinkedHashSet(...));</pre>
   27.88 + *
   27.89 + * <p>The iterators returned by this class's <tt>iterator</tt> method are
   27.90 + * <em>fail-fast</em>: if the set is modified at any time after the iterator
   27.91 + * is created, in any way except through the iterator's own <tt>remove</tt>
   27.92 + * method, the iterator will throw a {@link ConcurrentModificationException}.
   27.93 + * Thus, in the face of concurrent modification, the iterator fails quickly
   27.94 + * and cleanly, rather than risking arbitrary, non-deterministic behavior at
   27.95 + * an undetermined time in the future.
   27.96 + *
   27.97 + * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
   27.98 + * as it is, generally speaking, impossible to make any hard guarantees in the
   27.99 + * presence of unsynchronized concurrent modification.  Fail-fast iterators
  27.100 + * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
  27.101 + * Therefore, it would be wrong to write a program that depended on this
  27.102 + * exception for its correctness:   <i>the fail-fast behavior of iterators
  27.103 + * should be used only to detect bugs.</i>
  27.104 + *
  27.105 + * <p>This class is a member of the
  27.106 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
  27.107 + * Java Collections Framework</a>.
  27.108 + *
  27.109 + * @param <E> the type of elements maintained by this set
  27.110 + *
  27.111 + * @author  Josh Bloch
  27.112 + * @see     Object#hashCode()
  27.113 + * @see     Collection
  27.114 + * @see     Set
  27.115 + * @see     HashSet
  27.116 + * @see     TreeSet
  27.117 + * @see     Hashtable
  27.118 + * @since   1.4
  27.119 + */
  27.120 +
  27.121 +public class LinkedHashSet<E>
  27.122 +    extends HashSet<E>
  27.123 +    implements Set<E>, Cloneable, java.io.Serializable {
  27.124 +
  27.125 +    private static final long serialVersionUID = -2851667679971038690L;
  27.126 +
  27.127 +    /**
  27.128 +     * Constructs a new, empty linked hash set with the specified initial
  27.129 +     * capacity and load factor.
  27.130 +     *
  27.131 +     * @param      initialCapacity the initial capacity of the linked hash set
  27.132 +     * @param      loadFactor      the load factor of the linked hash set
  27.133 +     * @throws     IllegalArgumentException  if the initial capacity is less
  27.134 +     *               than zero, or if the load factor is nonpositive
  27.135 +     */
  27.136 +    public LinkedHashSet(int initialCapacity, float loadFactor) {
  27.137 +        super(initialCapacity, loadFactor, true);
  27.138 +    }
  27.139 +
  27.140 +    /**
  27.141 +     * Constructs a new, empty linked hash set with the specified initial
  27.142 +     * capacity and the default load factor (0.75).
  27.143 +     *
  27.144 +     * @param   initialCapacity   the initial capacity of the LinkedHashSet
  27.145 +     * @throws  IllegalArgumentException if the initial capacity is less
  27.146 +     *              than zero
  27.147 +     */
  27.148 +    public LinkedHashSet(int initialCapacity) {
  27.149 +        super(initialCapacity, .75f, true);
  27.150 +    }
  27.151 +
  27.152 +    /**
  27.153 +     * Constructs a new, empty linked hash set with the default initial
  27.154 +     * capacity (16) and load factor (0.75).
  27.155 +     */
  27.156 +    public LinkedHashSet() {
  27.157 +        super(16, .75f, true);
  27.158 +    }
  27.159 +
  27.160 +    /**
  27.161 +     * Constructs a new linked hash set with the same elements as the
  27.162 +     * specified collection.  The linked hash set is created with an initial
  27.163 +     * capacity sufficient to hold the elements in the specified collection
  27.164 +     * and the default load factor (0.75).
  27.165 +     *
  27.166 +     * @param c  the collection whose elements are to be placed into
  27.167 +     *           this set
  27.168 +     * @throws NullPointerException if the specified collection is null
  27.169 +     */
  27.170 +    public LinkedHashSet(Collection<? extends E> c) {
  27.171 +        super(Math.max(2*c.size(), 11), .75f, true);
  27.172 +        addAll(c);
  27.173 +    }
  27.174 +}
    28.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    28.2 +++ b/rt/emul/compact/src/main/java/java/util/NavigableMap.java	Sun Sep 08 11:22:51 2013 +0200
    28.3 @@ -0,0 +1,424 @@
    28.4 +/*
    28.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    28.6 + *
    28.7 + * This code is free software; you can redistribute it and/or modify it
    28.8 + * under the terms of the GNU General Public License version 2 only, as
    28.9 + * published by the Free Software Foundation.  Oracle designates this
   28.10 + * particular file as subject to the "Classpath" exception as provided
   28.11 + * by Oracle in the LICENSE file that accompanied this code.
   28.12 + *
   28.13 + * This code is distributed in the hope that it will be useful, but WITHOUT
   28.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   28.15 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   28.16 + * version 2 for more details (a copy is included in the LICENSE file that
   28.17 + * accompanied this code).
   28.18 + *
   28.19 + * You should have received a copy of the GNU General Public License version
   28.20 + * 2 along with this work; if not, write to the Free Software Foundation,
   28.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   28.22 + *
   28.23 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   28.24 + * or visit www.oracle.com if you need additional information or have any
   28.25 + * questions.
   28.26 + */
   28.27 +
   28.28 +/*
   28.29 + * This file is available under and governed by the GNU General Public
   28.30 + * License version 2 only, as published by the Free Software Foundation.
   28.31 + * However, the following notice accompanied the original version of this
   28.32 + * file:
   28.33 + *
   28.34 + * Written by Doug Lea and Josh Bloch with assistance from members of JCP
   28.35 + * JSR-166 Expert Group and released to the public domain, as explained at
   28.36 + * http://creativecommons.org/publicdomain/zero/1.0/
   28.37 + */
   28.38 +
   28.39 +package java.util;
   28.40 +
   28.41 +/**
   28.42 + * A {@link SortedMap} extended with navigation methods returning the
   28.43 + * closest matches for given search targets. Methods
   28.44 + * {@code lowerEntry}, {@code floorEntry}, {@code ceilingEntry},
   28.45 + * and {@code higherEntry} return {@code Map.Entry} objects
   28.46 + * associated with keys respectively less than, less than or equal,
   28.47 + * greater than or equal, and greater than a given key, returning
   28.48 + * {@code null} if there is no such key.  Similarly, methods
   28.49 + * {@code lowerKey}, {@code floorKey}, {@code ceilingKey}, and
   28.50 + * {@code higherKey} return only the associated keys. All of these
   28.51 + * methods are designed for locating, not traversing entries.
   28.52 + *
   28.53 + * <p>A {@code NavigableMap} may be accessed and traversed in either
   28.54 + * ascending or descending key order.  The {@code descendingMap}
   28.55 + * method returns a view of the map with the senses of all relational
   28.56 + * and directional methods inverted. The performance of ascending
   28.57 + * operations and views is likely to be faster than that of descending
   28.58 + * ones.  Methods {@code subMap}, {@code headMap},
   28.59 + * and {@code tailMap} differ from the like-named {@code
   28.60 + * SortedMap} methods in accepting additional arguments describing
   28.61 + * whether lower and upper bounds are inclusive versus exclusive.
   28.62 + * Submaps of any {@code NavigableMap} must implement the {@code
   28.63 + * NavigableMap} interface.
   28.64 + *
   28.65 + * <p>This interface additionally defines methods {@code firstEntry},
   28.66 + * {@code pollFirstEntry}, {@code lastEntry}, and
   28.67 + * {@code pollLastEntry} that return and/or remove the least and
   28.68 + * greatest mappings, if any exist, else returning {@code null}.
   28.69 + *
   28.70 + * <p>Implementations of entry-returning methods are expected to
   28.71 + * return {@code Map.Entry} pairs representing snapshots of mappings
   28.72 + * at the time they were produced, and thus generally do <em>not</em>
   28.73 + * support the optional {@code Entry.setValue} method. Note however
   28.74 + * that it is possible to change mappings in the associated map using
   28.75 + * method {@code put}.
   28.76 + *
   28.77 + * <p>Methods
   28.78 + * {@link #subMap(Object, Object) subMap(K, K)},
   28.79 + * {@link #headMap(Object) headMap(K)}, and
   28.80 + * {@link #tailMap(Object) tailMap(K)}
   28.81 + * are specified to return {@code SortedMap} to allow existing
   28.82 + * implementations of {@code SortedMap} to be compatibly retrofitted to
   28.83 + * implement {@code NavigableMap}, but extensions and implementations
   28.84 + * of this interface are encouraged to override these methods to return
   28.85 + * {@code NavigableMap}.  Similarly,
   28.86 + * {@link #keySet()} can be overriden to return {@code NavigableSet}.
   28.87 + *
   28.88 + * <p>This interface is a member of the
   28.89 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
   28.90 + * Java Collections Framework</a>.
   28.91 + *
   28.92 + * @author Doug Lea
   28.93 + * @author Josh Bloch
   28.94 + * @param <K> the type of keys maintained by this map
   28.95 + * @param <V> the type of mapped values
   28.96 + * @since 1.6
   28.97 + */
   28.98 +public interface NavigableMap<K,V> extends SortedMap<K,V> {
   28.99 +    /**
  28.100 +     * Returns a key-value mapping associated with the greatest key
  28.101 +     * strictly less than the given key, or {@code null} if there is
  28.102 +     * no such key.
  28.103 +     *
  28.104 +     * @param key the key
  28.105 +     * @return an entry with the greatest key less than {@code key},
  28.106 +     *         or {@code null} if there is no such key
  28.107 +     * @throws ClassCastException if the specified key cannot be compared
  28.108 +     *         with the keys currently in the map
  28.109 +     * @throws NullPointerException if the specified key is null
  28.110 +     *         and this map does not permit null keys
  28.111 +     */
  28.112 +    Map.Entry<K,V> lowerEntry(K key);
  28.113 +
  28.114 +    /**
  28.115 +     * Returns the greatest key strictly less than the given key, or
  28.116 +     * {@code null} if there is no such key.
  28.117 +     *
  28.118 +     * @param key the key
  28.119 +     * @return the greatest key less than {@code key},
  28.120 +     *         or {@code null} if there is no such key
  28.121 +     * @throws ClassCastException if the specified key cannot be compared
  28.122 +     *         with the keys currently in the map
  28.123 +     * @throws NullPointerException if the specified key is null
  28.124 +     *         and this map does not permit null keys
  28.125 +     */
  28.126 +    K lowerKey(K key);
  28.127 +
  28.128 +    /**
  28.129 +     * Returns a key-value mapping associated with the greatest key
  28.130 +     * less than or equal to the given key, or {@code null} if there
  28.131 +     * is no such key.
  28.132 +     *
  28.133 +     * @param key the key
  28.134 +     * @return an entry with the greatest key less than or equal to
  28.135 +     *         {@code key}, or {@code null} if there is no such key
  28.136 +     * @throws ClassCastException if the specified key cannot be compared
  28.137 +     *         with the keys currently in the map
  28.138 +     * @throws NullPointerException if the specified key is null
  28.139 +     *         and this map does not permit null keys
  28.140 +     */
  28.141 +    Map.Entry<K,V> floorEntry(K key);
  28.142 +
  28.143 +    /**
  28.144 +     * Returns the greatest key less than or equal to the given key,
  28.145 +     * or {@code null} if there is no such key.
  28.146 +     *
  28.147 +     * @param key the key
  28.148 +     * @return the greatest key less than or equal to {@code key},
  28.149 +     *         or {@code null} if there is no such key
  28.150 +     * @throws ClassCastException if the specified key cannot be compared
  28.151 +     *         with the keys currently in the map
  28.152 +     * @throws NullPointerException if the specified key is null
  28.153 +     *         and this map does not permit null keys
  28.154 +     */
  28.155 +    K floorKey(K key);
  28.156 +
  28.157 +    /**
  28.158 +     * Returns a key-value mapping associated with the least key
  28.159 +     * greater than or equal to the given key, or {@code null} if
  28.160 +     * there is no such key.
  28.161 +     *
  28.162 +     * @param key the key
  28.163 +     * @return an entry with the least key greater than or equal to
  28.164 +     *         {@code key}, or {@code null} if there is no such key
  28.165 +     * @throws ClassCastException if the specified key cannot be compared
  28.166 +     *         with the keys currently in the map
  28.167 +     * @throws NullPointerException if the specified key is null
  28.168 +     *         and this map does not permit null keys
  28.169 +     */
  28.170 +    Map.Entry<K,V> ceilingEntry(K key);
  28.171 +
  28.172 +    /**
  28.173 +     * Returns the least key greater than or equal to the given key,
  28.174 +     * or {@code null} if there is no such key.
  28.175 +     *
  28.176 +     * @param key the key
  28.177 +     * @return the least key greater than or equal to {@code key},
  28.178 +     *         or {@code null} if there is no such key
  28.179 +     * @throws ClassCastException if the specified key cannot be compared
  28.180 +     *         with the keys currently in the map
  28.181 +     * @throws NullPointerException if the specified key is null
  28.182 +     *         and this map does not permit null keys
  28.183 +     */
  28.184 +    K ceilingKey(K key);
  28.185 +
  28.186 +    /**
  28.187 +     * Returns a key-value mapping associated with the least key
  28.188 +     * strictly greater than the given key, or {@code null} if there
  28.189 +     * is no such key.
  28.190 +     *
  28.191 +     * @param key the key
  28.192 +     * @return an entry with the least key greater than {@code key},
  28.193 +     *         or {@code null} if there is no such key
  28.194 +     * @throws ClassCastException if the specified key cannot be compared
  28.195 +     *         with the keys currently in the map
  28.196 +     * @throws NullPointerException if the specified key is null
  28.197 +     *         and this map does not permit null keys
  28.198 +     */
  28.199 +    Map.Entry<K,V> higherEntry(K key);
  28.200 +
  28.201 +    /**
  28.202 +     * Returns the least key strictly greater than the given key, or
  28.203 +     * {@code null} if there is no such key.
  28.204 +     *
  28.205 +     * @param key the key
  28.206 +     * @return the least key greater than {@code key},
  28.207 +     *         or {@code null} if there is no such key
  28.208 +     * @throws ClassCastException if the specified key cannot be compared
  28.209 +     *         with the keys currently in the map
  28.210 +     * @throws NullPointerException if the specified key is null
  28.211 +     *         and this map does not permit null keys
  28.212 +     */
  28.213 +    K higherKey(K key);
  28.214 +
  28.215 +    /**
  28.216 +     * Returns a key-value mapping associated with the least
  28.217 +     * key in this map, or {@code null} if the map is empty.
  28.218 +     *
  28.219 +     * @return an entry with the least key,
  28.220 +     *         or {@code null} if this map is empty
  28.221 +     */
  28.222 +    Map.Entry<K,V> firstEntry();
  28.223 +
  28.224 +    /**
  28.225 +     * Returns a key-value mapping associated with the greatest
  28.226 +     * key in this map, or {@code null} if the map is empty.
  28.227 +     *
  28.228 +     * @return an entry with the greatest key,
  28.229 +     *         or {@code null} if this map is empty
  28.230 +     */
  28.231 +    Map.Entry<K,V> lastEntry();
  28.232 +
  28.233 +    /**
  28.234 +     * Removes and returns a key-value mapping associated with
  28.235 +     * the least key in this map, or {@code null} if the map is empty.
  28.236 +     *
  28.237 +     * @return the removed first entry of this map,
  28.238 +     *         or {@code null} if this map is empty
  28.239 +     */
  28.240 +    Map.Entry<K,V> pollFirstEntry();
  28.241 +
  28.242 +    /**
  28.243 +     * Removes and returns a key-value mapping associated with
  28.244 +     * the greatest key in this map, or {@code null} if the map is empty.
  28.245 +     *
  28.246 +     * @return the removed last entry of this map,
  28.247 +     *         or {@code null} if this map is empty
  28.248 +     */
  28.249 +    Map.Entry<K,V> pollLastEntry();
  28.250 +
  28.251 +    /**
  28.252 +     * Returns a reverse order view of the mappings contained in this map.
  28.253 +     * The descending map is backed by this map, so changes to the map are
  28.254 +     * reflected in the descending map, and vice-versa.  If either map is
  28.255 +     * modified while an iteration over a collection view of either map
  28.256 +     * is in progress (except through the iterator's own {@code remove}
  28.257 +     * operation), the results of the iteration are undefined.
  28.258 +     *
  28.259 +     * <p>The returned map has an ordering equivalent to
  28.260 +     * <tt>{@link Collections#reverseOrder(Comparator) Collections.reverseOrder}(comparator())</tt>.
  28.261 +     * The expression {@code m.descendingMap().descendingMap()} returns a
  28.262 +     * view of {@code m} essentially equivalent to {@code m}.
  28.263 +     *
  28.264 +     * @return a reverse order view of this map
  28.265 +     */
  28.266 +    NavigableMap<K,V> descendingMap();
  28.267 +
  28.268 +    /**
  28.269 +     * Returns a {@link NavigableSet} view of the keys contained in this map.
  28.270 +     * The set's iterator returns the keys in ascending order.
  28.271 +     * The set is backed by the map, so changes to the map are reflected in
  28.272 +     * the set, and vice-versa.  If the map is modified while an iteration
  28.273 +     * over the set is in progress (except through the iterator's own {@code
  28.274 +     * remove} operation), the results of the iteration are undefined.  The
  28.275 +     * set supports element removal, which removes the corresponding mapping
  28.276 +     * from the map, via the {@code Iterator.remove}, {@code Set.remove},
  28.277 +     * {@code removeAll}, {@code retainAll}, and {@code clear} operations.
  28.278 +     * It does not support the {@code add} or {@code addAll} operations.
  28.279 +     *
  28.280 +     * @return a navigable set view of the keys in this map
  28.281 +     */
  28.282 +    NavigableSet<K> navigableKeySet();
  28.283 +
  28.284 +    /**
  28.285 +     * Returns a reverse order {@link NavigableSet} view of the keys contained in this map.
  28.286 +     * The set's iterator returns the keys in descending order.
  28.287 +     * The set is backed by the map, so changes to the map are reflected in
  28.288 +     * the set, and vice-versa.  If the map is modified while an iteration
  28.289 +     * over the set is in progress (except through the iterator's own {@code
  28.290 +     * remove} operation), the results of the iteration are undefined.  The
  28.291 +     * set supports element removal, which removes the corresponding mapping
  28.292 +     * from the map, via the {@code Iterator.remove}, {@code Set.remove},
  28.293 +     * {@code removeAll}, {@code retainAll}, and {@code clear} operations.
  28.294 +     * It does not support the {@code add} or {@code addAll} operations.
  28.295 +     *
  28.296 +     * @return a reverse order navigable set view of the keys in this map
  28.297 +     */
  28.298 +    NavigableSet<K> descendingKeySet();
  28.299 +
  28.300 +    /**
  28.301 +     * Returns a view of the portion of this map whose keys range from
  28.302 +     * {@code fromKey} to {@code toKey}.  If {@code fromKey} and
  28.303 +     * {@code toKey} are equal, the returned map is empty unless
  28.304 +     * {@code fromInclusive} and {@code toInclusive} are both true.  The
  28.305 +     * returned map is backed by this map, so changes in the returned map are
  28.306 +     * reflected in this map, and vice-versa.  The returned map supports all
  28.307 +     * optional map operations that this map supports.
  28.308 +     *
  28.309 +     * <p>The returned map will throw an {@code IllegalArgumentException}
  28.310 +     * on an attempt to insert a key outside of its range, or to construct a
  28.311 +     * submap either of whose endpoints lie outside its range.
  28.312 +     *
  28.313 +     * @param fromKey low endpoint of the keys in the returned map
  28.314 +     * @param fromInclusive {@code true} if the low endpoint
  28.315 +     *        is to be included in the returned view
  28.316 +     * @param toKey high endpoint of the keys in the returned map
  28.317 +     * @param toInclusive {@code true} if the high endpoint
  28.318 +     *        is to be included in the returned view
  28.319 +     * @return a view of the portion of this map whose keys range from
  28.320 +     *         {@code fromKey} to {@code toKey}
  28.321 +     * @throws ClassCastException if {@code fromKey} and {@code toKey}
  28.322 +     *         cannot be compared to one another using this map's comparator
  28.323 +     *         (or, if the map has no comparator, using natural ordering).
  28.324 +     *         Implementations may, but are not required to, throw this
  28.325 +     *         exception if {@code fromKey} or {@code toKey}
  28.326 +     *         cannot be compared to keys currently in the map.
  28.327 +     * @throws NullPointerException if {@code fromKey} or {@code toKey}
  28.328 +     *         is null and this map does not permit null keys
  28.329 +     * @throws IllegalArgumentException if {@code fromKey} is greater than
  28.330 +     *         {@code toKey}; or if this map itself has a restricted
  28.331 +     *         range, and {@code fromKey} or {@code toKey} lies
  28.332 +     *         outside the bounds of the range
  28.333 +     */
  28.334 +    NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
  28.335 +                             K toKey,   boolean toInclusive);
  28.336 +
  28.337 +    /**
  28.338 +     * Returns a view of the portion of this map whose keys are less than (or
  28.339 +     * equal to, if {@code inclusive} is true) {@code toKey}.  The returned
  28.340 +     * map is backed by this map, so changes in the returned map are reflected
  28.341 +     * in this map, and vice-versa.  The returned map supports all optional
  28.342 +     * map operations that this map supports.
  28.343 +     *
  28.344 +     * <p>The returned map will throw an {@code IllegalArgumentException}
  28.345 +     * on an attempt to insert a key outside its range.
  28.346 +     *
  28.347 +     * @param toKey high endpoint of the keys in the returned map
  28.348 +     * @param inclusive {@code true} if the high endpoint
  28.349 +     *        is to be included in the returned view
  28.350 +     * @return a view of the portion of this map whose keys are less than
  28.351 +     *         (or equal to, if {@code inclusive} is true) {@code toKey}
  28.352 +     * @throws ClassCastException if {@code toKey} is not compatible
  28.353 +     *         with this map's comparator (or, if the map has no comparator,
  28.354 +     *         if {@code toKey} does not implement {@link Comparable}).
  28.355 +     *         Implementations may, but are not required to, throw this
  28.356 +     *         exception if {@code toKey} cannot be compared to keys
  28.357 +     *         currently in the map.
  28.358 +     * @throws NullPointerException if {@code toKey} is null
  28.359 +     *         and this map does not permit null keys
  28.360 +     * @throws IllegalArgumentException if this map itself has a
  28.361 +     *         restricted range, and {@code toKey} lies outside the
  28.362 +     *         bounds of the range
  28.363 +     */
  28.364 +    NavigableMap<K,V> headMap(K toKey, boolean inclusive);
  28.365 +
  28.366 +    /**
  28.367 +     * Returns a view of the portion of this map whose keys are greater than (or
  28.368 +     * equal to, if {@code inclusive} is true) {@code fromKey}.  The returned
  28.369 +     * map is backed by this map, so changes in the returned map are reflected
  28.370 +     * in this map, and vice-versa.  The returned map supports all optional
  28.371 +     * map operations that this map supports.
  28.372 +     *
  28.373 +     * <p>The returned map will throw an {@code IllegalArgumentException}
  28.374 +     * on an attempt to insert a key outside its range.
  28.375 +     *
  28.376 +     * @param fromKey low endpoint of the keys in the returned map
  28.377 +     * @param inclusive {@code true} if the low endpoint
  28.378 +     *        is to be included in the returned view
  28.379 +     * @return a view of the portion of this map whose keys are greater than
  28.380 +     *         (or equal to, if {@code inclusive} is true) {@code fromKey}
  28.381 +     * @throws ClassCastException if {@code fromKey} is not compatible
  28.382 +     *         with this map's comparator (or, if the map has no comparator,
  28.383 +     *         if {@code fromKey} does not implement {@link Comparable}).
  28.384 +     *         Implementations may, but are not required to, throw this
  28.385 +     *         exception if {@code fromKey} cannot be compared to keys
  28.386 +     *         currently in the map.
  28.387 +     * @throws NullPointerException if {@code fromKey} is null
  28.388 +     *         and this map does not permit null keys
  28.389 +     * @throws IllegalArgumentException if this map itself has a
  28.390 +     *         restricted range, and {@code fromKey} lies outside the
  28.391 +     *         bounds of the range
  28.392 +     */
  28.393 +    NavigableMap<K,V> tailMap(K fromKey, boolean inclusive);
  28.394 +
  28.395 +    /**
  28.396 +     * {@inheritDoc}
  28.397 +     *
  28.398 +     * <p>Equivalent to {@code subMap(fromKey, true, toKey, false)}.
  28.399 +     *
  28.400 +     * @throws ClassCastException       {@inheritDoc}
  28.401 +     * @throws NullPointerException     {@inheritDoc}
  28.402 +     * @throws IllegalArgumentException {@inheritDoc}
  28.403 +     */
  28.404 +    SortedMap<K,V> subMap(K fromKey, K toKey);
  28.405 +
  28.406 +    /**
  28.407 +     * {@inheritDoc}
  28.408 +     *
  28.409 +     * <p>Equivalent to {@code headMap(toKey, false)}.
  28.410 +     *
  28.411 +     * @throws ClassCastException       {@inheritDoc}
  28.412 +     * @throws NullPointerException     {@inheritDoc}
  28.413 +     * @throws IllegalArgumentException {@inheritDoc}
  28.414 +     */
  28.415 +    SortedMap<K,V> headMap(K toKey);
  28.416 +
  28.417 +    /**
  28.418 +     * {@inheritDoc}
  28.419 +     *
  28.420 +     * <p>Equivalent to {@code tailMap(fromKey, true)}.
  28.421 +     *
  28.422 +     * @throws ClassCastException       {@inheritDoc}
  28.423 +     * @throws NullPointerException     {@inheritDoc}
  28.424 +     * @throws IllegalArgumentException {@inheritDoc}
  28.425 +     */
  28.426 +    SortedMap<K,V> tailMap(K fromKey);
  28.427 +}
    29.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    29.2 +++ b/rt/emul/compact/src/main/java/java/util/NavigableSet.java	Sun Sep 08 11:22:51 2013 +0200
    29.3 @@ -0,0 +1,319 @@
    29.4 +/*
    29.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    29.6 + *
    29.7 + * This code is free software; you can redistribute it and/or modify it
    29.8 + * under the terms of the GNU General Public License version 2 only, as
    29.9 + * published by the Free Software Foundation.  Oracle designates this
   29.10 + * particular file as subject to the "Classpath" exception as provided
   29.11 + * by Oracle in the LICENSE file that accompanied this code.
   29.12 + *
   29.13 + * This code is distributed in the hope that it will be useful, but WITHOUT
   29.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   29.15 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   29.16 + * version 2 for more details (a copy is included in the LICENSE file that
   29.17 + * accompanied this code).
   29.18 + *
   29.19 + * You should have received a copy of the GNU General Public License version
   29.20 + * 2 along with this work; if not, write to the Free Software Foundation,
   29.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   29.22 + *
   29.23 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   29.24 + * or visit www.oracle.com if you need additional information or have any
   29.25 + * questions.
   29.26 + */
   29.27 +
   29.28 +/*
   29.29 + * This file is available under and governed by the GNU General Public
   29.30 + * License version 2 only, as published by the Free Software Foundation.
   29.31 + * However, the following notice accompanied the original version of this
   29.32 + * file:
   29.33 + *
   29.34 + * Written by Doug Lea and Josh Bloch with assistance from members of JCP
   29.35 + * JSR-166 Expert Group and released to the public domain, as explained at
   29.36 + * http://creativecommons.org/publicdomain/zero/1.0/
   29.37 + */
   29.38 +
   29.39 +package java.util;
   29.40 +
   29.41 +/**
   29.42 + * A {@link SortedSet} extended with navigation methods reporting
   29.43 + * closest matches for given search targets. Methods {@code lower},
   29.44 + * {@code floor}, {@code ceiling}, and {@code higher} return elements
   29.45 + * respectively less than, less than or equal, greater than or equal,
   29.46 + * and greater than a given element, returning {@code null} if there
   29.47 + * is no such element.  A {@code NavigableSet} may be accessed and
   29.48 + * traversed in either ascending or descending order.  The {@code
   29.49 + * descendingSet} method returns a view of the set with the senses of
   29.50 + * all relational and directional methods inverted. The performance of
   29.51 + * ascending operations and views is likely to be faster than that of
   29.52 + * descending ones.  This interface additionally defines methods
   29.53 + * {@code pollFirst} and {@code pollLast} that return and remove the
   29.54 + * lowest and highest element, if one exists, else returning {@code
   29.55 + * null}.  Methods {@code subSet}, {@code headSet},
   29.56 + * and {@code tailSet} differ from the like-named {@code
   29.57 + * SortedSet} methods in accepting additional arguments describing
   29.58 + * whether lower and upper bounds are inclusive versus exclusive.
   29.59 + * Subsets of any {@code NavigableSet} must implement the {@code
   29.60 + * NavigableSet} interface.
   29.61 + *
   29.62 + * <p> The return values of navigation methods may be ambiguous in
   29.63 + * implementations that permit {@code null} elements. However, even
   29.64 + * in this case the result can be disambiguated by checking
   29.65 + * {@code contains(null)}. To avoid such issues, implementations of
   29.66 + * this interface are encouraged to <em>not</em> permit insertion of
   29.67 + * {@code null} elements. (Note that sorted sets of {@link
   29.68 + * Comparable} elements intrinsically do not permit {@code null}.)
   29.69 + *
   29.70 + * <p>Methods
   29.71 + * {@link #subSet(Object, Object) subSet(E, E)},
   29.72 + * {@link #headSet(Object) headSet(E)}, and
   29.73 + * {@link #tailSet(Object) tailSet(E)}
   29.74 + * are specified to return {@code SortedSet} to allow existing
   29.75 + * implementations of {@code SortedSet} to be compatibly retrofitted to
   29.76 + * implement {@code NavigableSet}, but extensions and implementations
   29.77 + * of this interface are encouraged to override these methods to return
   29.78 + * {@code NavigableSet}.
   29.79 + *
   29.80 + * <p>This interface is a member of the
   29.81 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
   29.82 + * Java Collections Framework</a>.
   29.83 + *
   29.84 + * @author Doug Lea
   29.85 + * @author Josh Bloch
   29.86 + * @param <E> the type of elements maintained by this set
   29.87 + * @since 1.6
   29.88 + */
   29.89 +public interface NavigableSet<E> extends SortedSet<E> {
   29.90 +    /**
   29.91 +     * Returns the greatest element in this set strictly less than the
   29.92 +     * given element, or {@code null} if there is no such element.
   29.93 +     *
   29.94 +     * @param e the value to match
   29.95 +     * @return the greatest element less than {@code e},
   29.96 +     *         or {@code null} if there is no such element
   29.97 +     * @throws ClassCastException if the specified element cannot be
   29.98 +     *         compared with the elements currently in the set
   29.99 +     * @throws NullPointerException if the specified element is null
  29.100 +     *         and this set does not permit null elements
  29.101 +     */
  29.102 +    E lower(E e);
  29.103 +
  29.104 +    /**
  29.105 +     * Returns the greatest element in this set less than or equal to
  29.106 +     * the given element, or {@code null} if there is no such element.
  29.107 +     *
  29.108 +     * @param e the value to match
  29.109 +     * @return the greatest element less than or equal to {@code e},
  29.110 +     *         or {@code null} if there is no such element
  29.111 +     * @throws ClassCastException if the specified element cannot be
  29.112 +     *         compared with the elements currently in the set
  29.113 +     * @throws NullPointerException if the specified element is null
  29.114 +     *         and this set does not permit null elements
  29.115 +     */
  29.116 +    E floor(E e);
  29.117 +
  29.118 +    /**
  29.119 +     * Returns the least element in this set greater than or equal to
  29.120 +     * the given element, or {@code null} if there is no such element.
  29.121 +     *
  29.122 +     * @param e the value to match
  29.123 +     * @return the least element greater than or equal to {@code e},
  29.124 +     *         or {@code null} if there is no such element
  29.125 +     * @throws ClassCastException if the specified element cannot be
  29.126 +     *         compared with the elements currently in the set
  29.127 +     * @throws NullPointerException if the specified element is null
  29.128 +     *         and this set does not permit null elements
  29.129 +     */
  29.130 +    E ceiling(E e);
  29.131 +
  29.132 +    /**
  29.133 +     * Returns the least element in this set strictly greater than the
  29.134 +     * given element, or {@code null} if there is no such element.
  29.135 +     *
  29.136 +     * @param e the value to match
  29.137 +     * @return the least element greater than {@code e},
  29.138 +     *         or {@code null} if there is no such element
  29.139 +     * @throws ClassCastException if the specified element cannot be
  29.140 +     *         compared with the elements currently in the set
  29.141 +     * @throws NullPointerException if the specified element is null
  29.142 +     *         and this set does not permit null elements
  29.143 +     */
  29.144 +    E higher(E e);
  29.145 +
  29.146 +    /**
  29.147 +     * Retrieves and removes the first (lowest) element,
  29.148 +     * or returns {@code null} if this set is empty.
  29.149 +     *
  29.150 +     * @return the first element, or {@code null} if this set is empty
  29.151 +     */
  29.152 +    E pollFirst();
  29.153 +
  29.154 +    /**
  29.155 +     * Retrieves and removes the last (highest) element,
  29.156 +     * or returns {@code null} if this set is empty.
  29.157 +     *
  29.158 +     * @return the last element, or {@code null} if this set is empty
  29.159 +     */
  29.160 +    E pollLast();
  29.161 +
  29.162 +    /**
  29.163 +     * Returns an iterator over the elements in this set, in ascending order.
  29.164 +     *
  29.165 +     * @return an iterator over the elements in this set, in ascending order
  29.166 +     */
  29.167 +    Iterator<E> iterator();
  29.168 +
  29.169 +    /**
  29.170 +     * Returns a reverse order view of the elements contained in this set.
  29.171 +     * The descending set is backed by this set, so changes to the set are
  29.172 +     * reflected in the descending set, and vice-versa.  If either set is
  29.173 +     * modified while an iteration over either set is in progress (except
  29.174 +     * through the iterator's own {@code remove} operation), the results of
  29.175 +     * the iteration are undefined.
  29.176 +     *
  29.177 +     * <p>The returned set has an ordering equivalent to
  29.178 +     * <tt>{@link Collections#reverseOrder(Comparator) Collections.reverseOrder}(comparator())</tt>.
  29.179 +     * The expression {@code s.descendingSet().descendingSet()} returns a
  29.180 +     * view of {@code s} essentially equivalent to {@code s}.
  29.181 +     *
  29.182 +     * @return a reverse order view of this set
  29.183 +     */
  29.184 +    NavigableSet<E> descendingSet();
  29.185 +
  29.186 +    /**
  29.187 +     * Returns an iterator over the elements in this set, in descending order.
  29.188 +     * Equivalent in effect to {@code descendingSet().iterator()}.
  29.189 +     *
  29.190 +     * @return an iterator over the elements in this set, in descending order
  29.191 +     */
  29.192 +    Iterator<E> descendingIterator();
  29.193 +
  29.194 +    /**
  29.195 +     * Returns a view of the portion of this set whose elements range from
  29.196 +     * {@code fromElement} to {@code toElement}.  If {@code fromElement} and
  29.197 +     * {@code toElement} are equal, the returned set is empty unless {@code
  29.198 +     * fromInclusive} and {@code toInclusive} are both true.  The returned set
  29.199 +     * is backed by this set, so changes in the returned set are reflected in
  29.200 +     * this set, and vice-versa.  The returned set supports all optional set
  29.201 +     * operations that this set supports.
  29.202 +     *
  29.203 +     * <p>The returned set will throw an {@code IllegalArgumentException}
  29.204 +     * on an attempt to insert an element outside its range.
  29.205 +     *
  29.206 +     * @param fromElement low endpoint of the returned set
  29.207 +     * @param fromInclusive {@code true} if the low endpoint
  29.208 +     *        is to be included in the returned view
  29.209 +     * @param toElement high endpoint of the returned set
  29.210 +     * @param toInclusive {@code true} if the high endpoint
  29.211 +     *        is to be included in the returned view
  29.212 +     * @return a view of the portion of this set whose elements range from
  29.213 +     *         {@code fromElement}, inclusive, to {@code toElement}, exclusive
  29.214 +     * @throws ClassCastException if {@code fromElement} and
  29.215 +     *         {@code toElement} cannot be compared to one another using this
  29.216 +     *         set's comparator (or, if the set has no comparator, using
  29.217 +     *         natural ordering).  Implementations may, but are not required
  29.218 +     *         to, throw this exception if {@code fromElement} or
  29.219 +     *         {@code toElement} cannot be compared to elements currently in
  29.220 +     *         the set.
  29.221 +     * @throws NullPointerException if {@code fromElement} or
  29.222 +     *         {@code toElement} is null and this set does
  29.223 +     *         not permit null elements
  29.224 +     * @throws IllegalArgumentException if {@code fromElement} is
  29.225 +     *         greater than {@code toElement}; or if this set itself
  29.226 +     *         has a restricted range, and {@code fromElement} or
  29.227 +     *         {@code toElement} lies outside the bounds of the range.
  29.228 +     */
  29.229 +    NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
  29.230 +                           E toElement,   boolean toInclusive);
  29.231 +
  29.232 +    /**
  29.233 +     * Returns a view of the portion of this set whose elements are less than
  29.234 +     * (or equal to, if {@code inclusive} is true) {@code toElement}.  The
  29.235 +     * returned set is backed by this set, so changes in the returned set are
  29.236 +     * reflected in this set, and vice-versa.  The returned set supports all
  29.237 +     * optional set operations that this set supports.
  29.238 +     *
  29.239 +     * <p>The returned set will throw an {@code IllegalArgumentException}
  29.240 +     * on an attempt to insert an element outside its range.
  29.241 +     *
  29.242 +     * @param toElement high endpoint of the returned set
  29.243 +     * @param inclusive {@code true} if the high endpoint
  29.244 +     *        is to be included in the returned view
  29.245 +     * @return a view of the portion of this set whose elements are less than
  29.246 +     *         (or equal to, if {@code inclusive} is true) {@code toElement}
  29.247 +     * @throws ClassCastException if {@code toElement} is not compatible
  29.248 +     *         with this set's comparator (or, if the set has no comparator,
  29.249 +     *         if {@code toElement} does not implement {@link Comparable}).
  29.250 +     *         Implementations may, but are not required to, throw this
  29.251 +     *         exception if {@code toElement} cannot be compared to elements
  29.252 +     *         currently in the set.
  29.253 +     * @throws NullPointerException if {@code toElement} is null and
  29.254 +     *         this set does not permit null elements
  29.255 +     * @throws IllegalArgumentException if this set itself has a
  29.256 +     *         restricted range, and {@code toElement} lies outside the
  29.257 +     *         bounds of the range
  29.258 +     */
  29.259 +    NavigableSet<E> headSet(E toElement, boolean inclusive);
  29.260 +
  29.261 +    /**
  29.262 +     * Returns a view of the portion of this set whose elements are greater
  29.263 +     * than (or equal to, if {@code inclusive} is true) {@code fromElement}.
  29.264 +     * The returned set is backed by this set, so changes in the returned set
  29.265 +     * are reflected in this set, and vice-versa.  The returned set supports
  29.266 +     * all optional set operations that this set supports.
  29.267 +     *
  29.268 +     * <p>The returned set will throw an {@code IllegalArgumentException}
  29.269 +     * on an attempt to insert an element outside its range.
  29.270 +     *
  29.271 +     * @param fromElement low endpoint of the returned set
  29.272 +     * @param inclusive {@code true} if the low endpoint
  29.273 +     *        is to be included in the returned view
  29.274 +     * @return a view of the portion of this set whose elements are greater
  29.275 +     *         than or equal to {@code fromElement}
  29.276 +     * @throws ClassCastException if {@code fromElement} is not compatible
  29.277 +     *         with this set's comparator (or, if the set has no comparator,
  29.278 +     *         if {@code fromElement} does not implement {@link Comparable}).
  29.279 +     *         Implementations may, but are not required to, throw this
  29.280 +     *         exception if {@code fromElement} cannot be compared to elements
  29.281 +     *         currently in the set.
  29.282 +     * @throws NullPointerException if {@code fromElement} is null
  29.283 +     *         and this set does not permit null elements
  29.284 +     * @throws IllegalArgumentException if this set itself has a
  29.285 +     *         restricted range, and {@code fromElement} lies outside the
  29.286 +     *         bounds of the range
  29.287 +     */
  29.288 +    NavigableSet<E> tailSet(E fromElement, boolean inclusive);
  29.289 +
  29.290 +    /**
  29.291 +     * {@inheritDoc}
  29.292 +     *
  29.293 +     * <p>Equivalent to {@code subSet(fromElement, true, toElement, false)}.
  29.294 +     *
  29.295 +     * @throws ClassCastException       {@inheritDoc}
  29.296 +     * @throws NullPointerException     {@inheritDoc}
  29.297 +     * @throws IllegalArgumentException {@inheritDoc}
  29.298 +     */
  29.299 +    SortedSet<E> subSet(E fromElement, E toElement);
  29.300 +
  29.301 +    /**
  29.302 +     * {@inheritDoc}
  29.303 +     *
  29.304 +     * <p>Equivalent to {@code headSet(toElement, false)}.
  29.305 +     *
  29.306 +     * @throws ClassCastException       {@inheritDoc}
  29.307 +     * @throws NullPointerException     {@inheritDoc}
  29.308 +     * @throws IllegalArgumentException {@inheritDoc}
  29.309 +na     */
  29.310 +    SortedSet<E> headSet(E toElement);
  29.311 +
  29.312 +    /**
  29.313 +     * {@inheritDoc}
  29.314 +     *
  29.315 +     * <p>Equivalent to {@code tailSet(fromElement, true)}.
  29.316 +     *
  29.317 +     * @throws ClassCastException       {@inheritDoc}
  29.318 +     * @throws NullPointerException     {@inheritDoc}
  29.319 +     * @throws IllegalArgumentException {@inheritDoc}
  29.320 +     */
  29.321 +    SortedSet<E> tailSet(E fromElement);
  29.322 +}
    30.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    30.2 +++ b/rt/emul/compact/src/main/java/java/util/TreeMap.java	Sun Sep 08 11:22:51 2013 +0200
    30.3 @@ -0,0 +1,2442 @@
    30.4 +/*
    30.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
    30.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    30.7 + *
    30.8 + * This code is free software; you can redistribute it and/or modify it
    30.9 + * under the terms of the GNU General Public License version 2 only, as
   30.10 + * published by the Free Software Foundation.  Oracle designates this
   30.11 + * particular file as subject to the "Classpath" exception as provided
   30.12 + * by Oracle in the LICENSE file that accompanied this code.
   30.13 + *
   30.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   30.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   30.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   30.17 + * version 2 for more details (a copy is included in the LICENSE file that
   30.18 + * accompanied this code).
   30.19 + *
   30.20 + * You should have received a copy of the GNU General Public License version
   30.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   30.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   30.23 + *
   30.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   30.25 + * or visit www.oracle.com if you need additional information or have any
   30.26 + * questions.
   30.27 + */
   30.28 +
   30.29 +package java.util;
   30.30 +
   30.31 +/**
   30.32 + * A Red-Black tree based {@link NavigableMap} implementation.
   30.33 + * The map is sorted according to the {@linkplain Comparable natural
   30.34 + * ordering} of its keys, or by a {@link Comparator} provided at map
   30.35 + * creation time, depending on which constructor is used.
   30.36 + *
   30.37 + * <p>This implementation provides guaranteed log(n) time cost for the
   30.38 + * {@code containsKey}, {@code get}, {@code put} and {@code remove}
   30.39 + * operations.  Algorithms are adaptations of those in Cormen, Leiserson, and
   30.40 + * Rivest's <em>Introduction to Algorithms</em>.
   30.41 + *
   30.42 + * <p>Note that the ordering maintained by a tree map, like any sorted map, and
   30.43 + * whether or not an explicit comparator is provided, must be <em>consistent
   30.44 + * with {@code equals}</em> if this sorted map is to correctly implement the
   30.45 + * {@code Map} interface.  (See {@code Comparable} or {@code Comparator} for a
   30.46 + * precise definition of <em>consistent with equals</em>.)  This is so because
   30.47 + * the {@code Map} interface is defined in terms of the {@code equals}
   30.48 + * operation, but a sorted map performs all key comparisons using its {@code
   30.49 + * compareTo} (or {@code compare}) method, so two keys that are deemed equal by
   30.50 + * this method are, from the standpoint of the sorted map, equal.  The behavior
   30.51 + * of a sorted map <em>is</em> well-defined even if its ordering is
   30.52 + * inconsistent with {@code equals}; it just fails to obey the general contract
   30.53 + * of the {@code Map} interface.
   30.54 + *
   30.55 + * <p><strong>Note that this implementation is not synchronized.</strong>
   30.56 + * If multiple threads access a map concurrently, and at least one of the
   30.57 + * threads modifies the map structurally, it <em>must</em> be synchronized
   30.58 + * externally.  (A structural modification is any operation that adds or
   30.59 + * deletes one or more mappings; merely changing the value associated
   30.60 + * with an existing key is not a structural modification.)  This is
   30.61 + * typically accomplished by synchronizing on some object that naturally
   30.62 + * encapsulates the map.
   30.63 + * If no such object exists, the map should be "wrapped" using the
   30.64 + * {@link Collections#synchronizedSortedMap Collections.synchronizedSortedMap}
   30.65 + * method.  This is best done at creation time, to prevent accidental
   30.66 + * unsynchronized access to the map: <pre>
   30.67 + *   SortedMap m = Collections.synchronizedSortedMap(new TreeMap(...));</pre>
   30.68 + *
   30.69 + * <p>The iterators returned by the {@code iterator} method of the collections
   30.70 + * returned by all of this class's "collection view methods" are
   30.71 + * <em>fail-fast</em>: if the map is structurally modified at any time after
   30.72 + * the iterator is created, in any way except through the iterator's own
   30.73 + * {@code remove} method, the iterator will throw a {@link
   30.74 + * ConcurrentModificationException}.  Thus, in the face of concurrent
   30.75 + * modification, the iterator fails quickly and cleanly, rather than risking
   30.76 + * arbitrary, non-deterministic behavior at an undetermined time in the future.
   30.77 + *
   30.78 + * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
   30.79 + * as it is, generally speaking, impossible to make any hard guarantees in the
   30.80 + * presence of unsynchronized concurrent modification.  Fail-fast iterators
   30.81 + * throw {@code ConcurrentModificationException} on a best-effort basis.
   30.82 + * Therefore, it would be wrong to write a program that depended on this
   30.83 + * exception for its correctness:   <em>the fail-fast behavior of iterators
   30.84 + * should be used only to detect bugs.</em>
   30.85 + *
   30.86 + * <p>All {@code Map.Entry} pairs returned by methods in this class
   30.87 + * and its views represent snapshots of mappings at the time they were
   30.88 + * produced. They do <strong>not</strong> support the {@code Entry.setValue}
   30.89 + * method. (Note however that it is possible to change mappings in the
   30.90 + * associated map using {@code put}.)
   30.91 + *
   30.92 + * <p>This class is a member of the
   30.93 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
   30.94 + * Java Collections Framework</a>.
   30.95 + *
   30.96 + * @param <K> the type of keys maintained by this map
   30.97 + * @param <V> the type of mapped values
   30.98 + *
   30.99 + * @author  Josh Bloch and Doug Lea
  30.100 + * @see Map
  30.101 + * @see HashMap
  30.102 + * @see Hashtable
  30.103 + * @see Comparable
  30.104 + * @see Comparator
  30.105 + * @see Collection
  30.106 + * @since 1.2
  30.107 + */
  30.108 +
  30.109 +public class TreeMap<K,V>
  30.110 +    extends AbstractMap<K,V>
  30.111 +    implements NavigableMap<K,V>, Cloneable, java.io.Serializable
  30.112 +{
  30.113 +    /**
  30.114 +     * The comparator used to maintain order in this tree map, or
  30.115 +     * null if it uses the natural ordering of its keys.
  30.116 +     *
  30.117 +     * @serial
  30.118 +     */
  30.119 +    private final Comparator<? super K> comparator;
  30.120 +
  30.121 +    private transient Entry<K,V> root = null;
  30.122 +
  30.123 +    /**
  30.124 +     * The number of entries in the tree
  30.125 +     */
  30.126 +    private transient int size = 0;
  30.127 +
  30.128 +    /**
  30.129 +     * The number of structural modifications to the tree.
  30.130 +     */
  30.131 +    private transient int modCount = 0;
  30.132 +
  30.133 +    /**
  30.134 +     * Constructs a new, empty tree map, using the natural ordering of its
  30.135 +     * keys.  All keys inserted into the map must implement the {@link
  30.136 +     * Comparable} interface.  Furthermore, all such keys must be
  30.137 +     * <em>mutually comparable</em>: {@code k1.compareTo(k2)} must not throw
  30.138 +     * a {@code ClassCastException} for any keys {@code k1} and
  30.139 +     * {@code k2} in the map.  If the user attempts to put a key into the
  30.140 +     * map that violates this constraint (for example, the user attempts to
  30.141 +     * put a string key into a map whose keys are integers), the
  30.142 +     * {@code put(Object key, Object value)} call will throw a
  30.143 +     * {@code ClassCastException}.
  30.144 +     */
  30.145 +    public TreeMap() {
  30.146 +        comparator = null;
  30.147 +    }
  30.148 +
  30.149 +    /**
  30.150 +     * Constructs a new, empty tree map, ordered according to the given
  30.151 +     * comparator.  All keys inserted into the map must be <em>mutually
  30.152 +     * comparable</em> by the given comparator: {@code comparator.compare(k1,
  30.153 +     * k2)} must not throw a {@code ClassCastException} for any keys
  30.154 +     * {@code k1} and {@code k2} in the map.  If the user attempts to put
  30.155 +     * a key into the map that violates this constraint, the {@code put(Object
  30.156 +     * key, Object value)} call will throw a
  30.157 +     * {@code ClassCastException}.
  30.158 +     *
  30.159 +     * @param comparator the comparator that will be used to order this map.
  30.160 +     *        If {@code null}, the {@linkplain Comparable natural
  30.161 +     *        ordering} of the keys will be used.
  30.162 +     */
  30.163 +    public TreeMap(Comparator<? super K> comparator) {
  30.164 +        this.comparator = comparator;
  30.165 +    }
  30.166 +
  30.167 +    /**
  30.168 +     * Constructs a new tree map containing the same mappings as the given
  30.169 +     * map, ordered according to the <em>natural ordering</em> of its keys.
  30.170 +     * All keys inserted into the new map must implement the {@link
  30.171 +     * Comparable} interface.  Furthermore, all such keys must be
  30.172 +     * <em>mutually comparable</em>: {@code k1.compareTo(k2)} must not throw
  30.173 +     * a {@code ClassCastException} for any keys {@code k1} and
  30.174 +     * {@code k2} in the map.  This method runs in n*log(n) time.
  30.175 +     *
  30.176 +     * @param  m the map whose mappings are to be placed in this map
  30.177 +     * @throws ClassCastException if the keys in m are not {@link Comparable},
  30.178 +     *         or are not mutually comparable
  30.179 +     * @throws NullPointerException if the specified map is null
  30.180 +     */
  30.181 +    public TreeMap(Map<? extends K, ? extends V> m) {
  30.182 +        comparator = null;
  30.183 +        putAll(m);
  30.184 +    }
  30.185 +
  30.186 +    /**
  30.187 +     * Constructs a new tree map containing the same mappings and
  30.188 +     * using the same ordering as the specified sorted map.  This
  30.189 +     * method runs in linear time.
  30.190 +     *
  30.191 +     * @param  m the sorted map whose mappings are to be placed in this map,
  30.192 +     *         and whose comparator is to be used to sort this map
  30.193 +     * @throws NullPointerException if the specified map is null
  30.194 +     */
  30.195 +    public TreeMap(SortedMap<K, ? extends V> m) {
  30.196 +        comparator = m.comparator();
  30.197 +        try {
  30.198 +            buildFromSorted(m.size(), m.entrySet().iterator(), null, null);
  30.199 +        } catch (java.io.IOException cannotHappen) {
  30.200 +        } catch (ClassNotFoundException cannotHappen) {
  30.201 +        }
  30.202 +    }
  30.203 +
  30.204 +
  30.205 +    // Query Operations
  30.206 +
  30.207 +    /**
  30.208 +     * Returns the number of key-value mappings in this map.
  30.209 +     *
  30.210 +     * @return the number of key-value mappings in this map
  30.211 +     */
  30.212 +    public int size() {
  30.213 +        return size;
  30.214 +    }
  30.215 +
  30.216 +    /**
  30.217 +     * Returns {@code true} if this map contains a mapping for the specified
  30.218 +     * key.
  30.219 +     *
  30.220 +     * @param key key whose presence in this map is to be tested
  30.221 +     * @return {@code true} if this map contains a mapping for the
  30.222 +     *         specified key
  30.223 +     * @throws ClassCastException if the specified key cannot be compared
  30.224 +     *         with the keys currently in the map
  30.225 +     * @throws NullPointerException if the specified key is null
  30.226 +     *         and this map uses natural ordering, or its comparator
  30.227 +     *         does not permit null keys
  30.228 +     */
  30.229 +    public boolean containsKey(Object key) {
  30.230 +        return getEntry(key) != null;
  30.231 +    }
  30.232 +
  30.233 +    /**
  30.234 +     * Returns {@code true} if this map maps one or more keys to the
  30.235 +     * specified value.  More formally, returns {@code true} if and only if
  30.236 +     * this map contains at least one mapping to a value {@code v} such
  30.237 +     * that {@code (value==null ? v==null : value.equals(v))}.  This
  30.238 +     * operation will probably require time linear in the map size for
  30.239 +     * most implementations.
  30.240 +     *
  30.241 +     * @param value value whose presence in this map is to be tested
  30.242 +     * @return {@code true} if a mapping to {@code value} exists;
  30.243 +     *         {@code false} otherwise
  30.244 +     * @since 1.2
  30.245 +     */
  30.246 +    public boolean containsValue(Object value) {
  30.247 +        for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e))
  30.248 +            if (valEquals(value, e.value))
  30.249 +                return true;
  30.250 +        return false;
  30.251 +    }
  30.252 +
  30.253 +    /**
  30.254 +     * Returns the value to which the specified key is mapped,
  30.255 +     * or {@code null} if this map contains no mapping for the key.
  30.256 +     *
  30.257 +     * <p>More formally, if this map contains a mapping from a key
  30.258 +     * {@code k} to a value {@code v} such that {@code key} compares
  30.259 +     * equal to {@code k} according to the map's ordering, then this
  30.260 +     * method returns {@code v}; otherwise it returns {@code null}.
  30.261 +     * (There can be at most one such mapping.)
  30.262 +     *
  30.263 +     * <p>A return value of {@code null} does not <em>necessarily</em>
  30.264 +     * indicate that the map contains no mapping for the key; it's also
  30.265 +     * possible that the map explicitly maps the key to {@code null}.
  30.266 +     * The {@link #containsKey containsKey} operation may be used to
  30.267 +     * distinguish these two cases.
  30.268 +     *
  30.269 +     * @throws ClassCastException if the specified key cannot be compared
  30.270 +     *         with the keys currently in the map
  30.271 +     * @throws NullPointerException if the specified key is null
  30.272 +     *         and this map uses natural ordering, or its comparator
  30.273 +     *         does not permit null keys
  30.274 +     */
  30.275 +    public V get(Object key) {
  30.276 +        Entry<K,V> p = getEntry(key);
  30.277 +        return (p==null ? null : p.value);
  30.278 +    }
  30.279 +
  30.280 +    public Comparator<? super K> comparator() {
  30.281 +        return comparator;
  30.282 +    }
  30.283 +
  30.284 +    /**
  30.285 +     * @throws NoSuchElementException {@inheritDoc}
  30.286 +     */
  30.287 +    public K firstKey() {
  30.288 +        return key(getFirstEntry());
  30.289 +    }
  30.290 +
  30.291 +    /**
  30.292 +     * @throws NoSuchElementException {@inheritDoc}
  30.293 +     */
  30.294 +    public K lastKey() {
  30.295 +        return key(getLastEntry());
  30.296 +    }
  30.297 +
  30.298 +    /**
  30.299 +     * Copies all of the mappings from the specified map to this map.
  30.300 +     * These mappings replace any mappings that this map had for any
  30.301 +     * of the keys currently in the specified map.
  30.302 +     *
  30.303 +     * @param  map mappings to be stored in this map
  30.304 +     * @throws ClassCastException if the class of a key or value in
  30.305 +     *         the specified map prevents it from being stored in this map
  30.306 +     * @throws NullPointerException if the specified map is null or
  30.307 +     *         the specified map contains a null key and this map does not
  30.308 +     *         permit null keys
  30.309 +     */
  30.310 +    public void putAll(Map<? extends K, ? extends V> map) {
  30.311 +        int mapSize = map.size();
  30.312 +        if (size==0 && mapSize!=0 && map instanceof SortedMap) {
  30.313 +            Comparator c = ((SortedMap)map).comparator();
  30.314 +            if (c == comparator || (c != null && c.equals(comparator))) {
  30.315 +                ++modCount;
  30.316 +                try {
  30.317 +                    buildFromSorted(mapSize, map.entrySet().iterator(),
  30.318 +                                    null, null);
  30.319 +                } catch (java.io.IOException cannotHappen) {
  30.320 +                } catch (ClassNotFoundException cannotHappen) {
  30.321 +                }
  30.322 +                return;
  30.323 +            }
  30.324 +        }
  30.325 +        super.putAll(map);
  30.326 +    }
  30.327 +
  30.328 +    /**
  30.329 +     * Returns this map's entry for the given key, or {@code null} if the map
  30.330 +     * does not contain an entry for the key.
  30.331 +     *
  30.332 +     * @return this map's entry for the given key, or {@code null} if the map
  30.333 +     *         does not contain an entry for the key
  30.334 +     * @throws ClassCastException if the specified key cannot be compared
  30.335 +     *         with the keys currently in the map
  30.336 +     * @throws NullPointerException if the specified key is null
  30.337 +     *         and this map uses natural ordering, or its comparator
  30.338 +     *         does not permit null keys
  30.339 +     */
  30.340 +    final Entry<K,V> getEntry(Object key) {
  30.341 +        // Offload comparator-based version for sake of performance
  30.342 +        if (comparator != null)
  30.343 +            return getEntryUsingComparator(key);
  30.344 +        if (key == null)
  30.345 +            throw new NullPointerException();
  30.346 +        Comparable<? super K> k = (Comparable<? super K>) key;
  30.347 +        Entry<K,V> p = root;
  30.348 +        while (p != null) {
  30.349 +            int cmp = k.compareTo(p.key);
  30.350 +            if (cmp < 0)
  30.351 +                p = p.left;
  30.352 +            else if (cmp > 0)
  30.353 +                p = p.right;
  30.354 +            else
  30.355 +                return p;
  30.356 +        }
  30.357 +        return null;
  30.358 +    }
  30.359 +
  30.360 +    /**
  30.361 +     * Version of getEntry using comparator. Split off from getEntry
  30.362 +     * for performance. (This is not worth doing for most methods,
  30.363 +     * that are less dependent on comparator performance, but is
  30.364 +     * worthwhile here.)
  30.365 +     */
  30.366 +    final Entry<K,V> getEntryUsingComparator(Object key) {
  30.367 +        K k = (K) key;
  30.368 +        Comparator<? super K> cpr = comparator;
  30.369 +        if (cpr != null) {
  30.370 +            Entry<K,V> p = root;
  30.371 +            while (p != null) {
  30.372 +                int cmp = cpr.compare(k, p.key);
  30.373 +                if (cmp < 0)
  30.374 +                    p = p.left;
  30.375 +                else if (cmp > 0)
  30.376 +                    p = p.right;
  30.377 +                else
  30.378 +                    return p;
  30.379 +            }
  30.380 +        }
  30.381 +        return null;
  30.382 +    }
  30.383 +
  30.384 +    /**
  30.385 +     * Gets the entry corresponding to the specified key; if no such entry
  30.386 +     * exists, returns the entry for the least key greater than the specified
  30.387 +     * key; if no such entry exists (i.e., the greatest key in the Tree is less
  30.388 +     * than the specified key), returns {@code null}.
  30.389 +     */
  30.390 +    final Entry<K,V> getCeilingEntry(K key) {
  30.391 +        Entry<K,V> p = root;
  30.392 +        while (p != null) {
  30.393 +            int cmp = compare(key, p.key);
  30.394 +            if (cmp < 0) {
  30.395 +                if (p.left != null)
  30.396 +                    p = p.left;
  30.397 +                else
  30.398 +                    return p;
  30.399 +            } else if (cmp > 0) {
  30.400 +                if (p.right != null) {
  30.401 +                    p = p.right;
  30.402 +                } else {
  30.403 +                    Entry<K,V> parent = p.parent;
  30.404 +                    Entry<K,V> ch = p;
  30.405 +                    while (parent != null && ch == parent.right) {
  30.406 +                        ch = parent;
  30.407 +                        parent = parent.parent;
  30.408 +                    }
  30.409 +                    return parent;
  30.410 +                }
  30.411 +            } else
  30.412 +                return p;
  30.413 +        }
  30.414 +        return null;
  30.415 +    }
  30.416 +
  30.417 +    /**
  30.418 +     * Gets the entry corresponding to the specified key; if no such entry
  30.419 +     * exists, returns the entry for the greatest key less than the specified
  30.420 +     * key; if no such entry exists, returns {@code null}.
  30.421 +     */
  30.422 +    final Entry<K,V> getFloorEntry(K key) {
  30.423 +        Entry<K,V> p = root;
  30.424 +        while (p != null) {
  30.425 +            int cmp = compare(key, p.key);
  30.426 +            if (cmp > 0) {
  30.427 +                if (p.right != null)
  30.428 +                    p = p.right;
  30.429 +                else
  30.430 +                    return p;
  30.431 +            } else if (cmp < 0) {
  30.432 +                if (p.left != null) {
  30.433 +                    p = p.left;
  30.434 +                } else {
  30.435 +                    Entry<K,V> parent = p.parent;
  30.436 +                    Entry<K,V> ch = p;
  30.437 +                    while (parent != null && ch == parent.left) {
  30.438 +                        ch = parent;
  30.439 +                        parent = parent.parent;
  30.440 +                    }
  30.441 +                    return parent;
  30.442 +                }
  30.443 +            } else
  30.444 +                return p;
  30.445 +
  30.446 +        }
  30.447 +        return null;
  30.448 +    }
  30.449 +
  30.450 +    /**
  30.451 +     * Gets the entry for the least key greater than the specified
  30.452 +     * key; if no such entry exists, returns the entry for the least
  30.453 +     * key greater than the specified key; if no such entry exists
  30.454 +     * returns {@code null}.
  30.455 +     */
  30.456 +    final Entry<K,V> getHigherEntry(K key) {
  30.457 +        Entry<K,V> p = root;
  30.458 +        while (p != null) {
  30.459 +            int cmp = compare(key, p.key);
  30.460 +            if (cmp < 0) {
  30.461 +                if (p.left != null)
  30.462 +                    p = p.left;
  30.463 +                else
  30.464 +                    return p;
  30.465 +            } else {
  30.466 +                if (p.right != null) {
  30.467 +                    p = p.right;
  30.468 +                } else {
  30.469 +                    Entry<K,V> parent = p.parent;
  30.470 +                    Entry<K,V> ch = p;
  30.471 +                    while (parent != null && ch == parent.right) {
  30.472 +                        ch = parent;
  30.473 +                        parent = parent.parent;
  30.474 +                    }
  30.475 +                    return parent;
  30.476 +                }
  30.477 +            }
  30.478 +        }
  30.479 +        return null;
  30.480 +    }
  30.481 +
  30.482 +    /**
  30.483 +     * Returns the entry for the greatest key less than the specified key; if
  30.484 +     * no such entry exists (i.e., the least key in the Tree is greater than
  30.485 +     * the specified key), returns {@code null}.
  30.486 +     */
  30.487 +    final Entry<K,V> getLowerEntry(K key) {
  30.488 +        Entry<K,V> p = root;
  30.489 +        while (p != null) {
  30.490 +            int cmp = compare(key, p.key);
  30.491 +            if (cmp > 0) {
  30.492 +                if (p.right != null)
  30.493 +                    p = p.right;
  30.494 +                else
  30.495 +                    return p;
  30.496 +            } else {
  30.497 +                if (p.left != null) {
  30.498 +                    p = p.left;
  30.499 +                } else {
  30.500 +                    Entry<K,V> parent = p.parent;
  30.501 +                    Entry<K,V> ch = p;
  30.502 +                    while (parent != null && ch == parent.left) {
  30.503 +                        ch = parent;
  30.504 +                        parent = parent.parent;
  30.505 +                    }
  30.506 +                    return parent;
  30.507 +                }
  30.508 +            }
  30.509 +        }
  30.510 +        return null;
  30.511 +    }
  30.512 +
  30.513 +    /**
  30.514 +     * Associates the specified value with the specified key in this map.
  30.515 +     * If the map previously contained a mapping for the key, the old
  30.516 +     * value is replaced.
  30.517 +     *
  30.518 +     * @param key key with which the specified value is to be associated
  30.519 +     * @param value value to be associated with the specified key
  30.520 +     *
  30.521 +     * @return the previous value associated with {@code key}, or
  30.522 +     *         {@code null} if there was no mapping for {@code key}.
  30.523 +     *         (A {@code null} return can also indicate that the map
  30.524 +     *         previously associated {@code null} with {@code key}.)
  30.525 +     * @throws ClassCastException if the specified key cannot be compared
  30.526 +     *         with the keys currently in the map
  30.527 +     * @throws NullPointerException if the specified key is null
  30.528 +     *         and this map uses natural ordering, or its comparator
  30.529 +     *         does not permit null keys
  30.530 +     */
  30.531 +    public V put(K key, V value) {
  30.532 +        Entry<K,V> t = root;
  30.533 +        if (t == null) {
  30.534 +            compare(key, key); // type (and possibly null) check
  30.535 +
  30.536 +            root = new Entry<>(key, value, null);
  30.537 +            size = 1;
  30.538 +            modCount++;
  30.539 +            return null;
  30.540 +        }
  30.541 +        int cmp;
  30.542 +        Entry<K,V> parent;
  30.543 +        // split comparator and comparable paths
  30.544 +        Comparator<? super K> cpr = comparator;
  30.545 +        if (cpr != null) {
  30.546 +            do {
  30.547 +                parent = t;
  30.548 +                cmp = cpr.compare(key, t.key);
  30.549 +                if (cmp < 0)
  30.550 +                    t = t.left;
  30.551 +                else if (cmp > 0)
  30.552 +                    t = t.right;
  30.553 +                else
  30.554 +                    return t.setValue(value);
  30.555 +            } while (t != null);
  30.556 +        }
  30.557 +        else {
  30.558 +            if (key == null)
  30.559 +                throw new NullPointerException();
  30.560 +            Comparable<? super K> k = (Comparable<? super K>) key;
  30.561 +            do {
  30.562 +                parent = t;
  30.563 +                cmp = k.compareTo(t.key);
  30.564 +                if (cmp < 0)
  30.565 +                    t = t.left;
  30.566 +                else if (cmp > 0)
  30.567 +                    t = t.right;
  30.568 +                else
  30.569 +                    return t.setValue(value);
  30.570 +            } while (t != null);
  30.571 +        }
  30.572 +        Entry<K,V> e = new Entry<>(key, value, parent);
  30.573 +        if (cmp < 0)
  30.574 +            parent.left = e;
  30.575 +        else
  30.576 +            parent.right = e;
  30.577 +        fixAfterInsertion(e);
  30.578 +        size++;
  30.579 +        modCount++;
  30.580 +        return null;
  30.581 +    }
  30.582 +
  30.583 +    /**
  30.584 +     * Removes the mapping for this key from this TreeMap if present.
  30.585 +     *
  30.586 +     * @param  key key for which mapping should be removed
  30.587 +     * @return the previous value associated with {@code key}, or
  30.588 +     *         {@code null} if there was no mapping for {@code key}.
  30.589 +     *         (A {@code null} return can also indicate that the map
  30.590 +     *         previously associated {@code null} with {@code key}.)
  30.591 +     * @throws ClassCastException if the specified key cannot be compared
  30.592 +     *         with the keys currently in the map
  30.593 +     * @throws NullPointerException if the specified key is null
  30.594 +     *         and this map uses natural ordering, or its comparator
  30.595 +     *         does not permit null keys
  30.596 +     */
  30.597 +    public V remove(Object key) {
  30.598 +        Entry<K,V> p = getEntry(key);
  30.599 +        if (p == null)
  30.600 +            return null;
  30.601 +
  30.602 +        V oldValue = p.value;
  30.603 +        deleteEntry(p);
  30.604 +        return oldValue;
  30.605 +    }
  30.606 +
  30.607 +    /**
  30.608 +     * Removes all of the mappings from this map.
  30.609 +     * The map will be empty after this call returns.
  30.610 +     */
  30.611 +    public void clear() {
  30.612 +        modCount++;
  30.613 +        size = 0;
  30.614 +        root = null;
  30.615 +    }
  30.616 +
  30.617 +    /**
  30.618 +     * Returns a shallow copy of this {@code TreeMap} instance. (The keys and
  30.619 +     * values themselves are not cloned.)
  30.620 +     *
  30.621 +     * @return a shallow copy of this map
  30.622 +     */
  30.623 +    public Object clone() {
  30.624 +        TreeMap<K,V> clone = null;
  30.625 +        try {
  30.626 +            clone = (TreeMap<K,V>) super.clone();
  30.627 +        } catch (CloneNotSupportedException e) {
  30.628 +            throw new InternalError();
  30.629 +        }
  30.630 +
  30.631 +        // Put clone into "virgin" state (except for comparator)
  30.632 +        clone.root = null;
  30.633 +        clone.size = 0;
  30.634 +        clone.modCount = 0;
  30.635 +        clone.entrySet = null;
  30.636 +        clone.navigableKeySet = null;
  30.637 +        clone.descendingMap = null;
  30.638 +
  30.639 +        // Initialize clone with our mappings
  30.640 +        try {
  30.641 +            clone.buildFromSorted(size, entrySet().iterator(), null, null);
  30.642 +        } catch (java.io.IOException cannotHappen) {
  30.643 +        } catch (ClassNotFoundException cannotHappen) {
  30.644 +        }
  30.645 +
  30.646 +        return clone;
  30.647 +    }
  30.648 +
  30.649 +    // NavigableMap API methods
  30.650 +
  30.651 +    /**
  30.652 +     * @since 1.6
  30.653 +     */
  30.654 +    public Map.Entry<K,V> firstEntry() {
  30.655 +        return exportEntry(getFirstEntry());
  30.656 +    }
  30.657 +
  30.658 +    /**
  30.659 +     * @since 1.6
  30.660 +     */
  30.661 +    public Map.Entry<K,V> lastEntry() {
  30.662 +        return exportEntry(getLastEntry());
  30.663 +    }
  30.664 +
  30.665 +    /**
  30.666 +     * @since 1.6
  30.667 +     */
  30.668 +    public Map.Entry<K,V> pollFirstEntry() {
  30.669 +        Entry<K,V> p = getFirstEntry();
  30.670 +        Map.Entry<K,V> result = exportEntry(p);
  30.671 +        if (p != null)
  30.672 +            deleteEntry(p);
  30.673 +        return result;
  30.674 +    }
  30.675 +
  30.676 +    /**
  30.677 +     * @since 1.6
  30.678 +     */
  30.679 +    public Map.Entry<K,V> pollLastEntry() {
  30.680 +        Entry<K,V> p = getLastEntry();
  30.681 +        Map.Entry<K,V> result = exportEntry(p);
  30.682 +        if (p != null)
  30.683 +            deleteEntry(p);
  30.684 +        return result;
  30.685 +    }
  30.686 +
  30.687 +    /**
  30.688 +     * @throws ClassCastException {@inheritDoc}
  30.689 +     * @throws NullPointerException if the specified key is null
  30.690 +     *         and this map uses natural ordering, or its comparator
  30.691 +     *         does not permit null keys
  30.692 +     * @since 1.6
  30.693 +     */
  30.694 +    public Map.Entry<K,V> lowerEntry(K key) {
  30.695 +        return exportEntry(getLowerEntry(key));
  30.696 +    }
  30.697 +
  30.698 +    /**
  30.699 +     * @throws ClassCastException {@inheritDoc}
  30.700 +     * @throws NullPointerException if the specified key is null
  30.701 +     *         and this map uses natural ordering, or its comparator
  30.702 +     *         does not permit null keys
  30.703 +     * @since 1.6
  30.704 +     */
  30.705 +    public K lowerKey(K key) {
  30.706 +        return keyOrNull(getLowerEntry(key));
  30.707 +    }
  30.708 +
  30.709 +    /**
  30.710 +     * @throws ClassCastException {@inheritDoc}
  30.711 +     * @throws NullPointerException if the specified key is null
  30.712 +     *         and this map uses natural ordering, or its comparator
  30.713 +     *         does not permit null keys
  30.714 +     * @since 1.6
  30.715 +     */
  30.716 +    public Map.Entry<K,V> floorEntry(K key) {
  30.717 +        return exportEntry(getFloorEntry(key));
  30.718 +    }
  30.719 +
  30.720 +    /**
  30.721 +     * @throws ClassCastException {@inheritDoc}
  30.722 +     * @throws NullPointerException if the specified key is null
  30.723 +     *         and this map uses natural ordering, or its comparator
  30.724 +     *         does not permit null keys
  30.725 +     * @since 1.6
  30.726 +     */
  30.727 +    public K floorKey(K key) {
  30.728 +        return keyOrNull(getFloorEntry(key));
  30.729 +    }
  30.730 +
  30.731 +    /**
  30.732 +     * @throws ClassCastException {@inheritDoc}
  30.733 +     * @throws NullPointerException if the specified key is null
  30.734 +     *         and this map uses natural ordering, or its comparator
  30.735 +     *         does not permit null keys
  30.736 +     * @since 1.6
  30.737 +     */
  30.738 +    public Map.Entry<K,V> ceilingEntry(K key) {
  30.739 +        return exportEntry(getCeilingEntry(key));
  30.740 +    }
  30.741 +
  30.742 +    /**
  30.743 +     * @throws ClassCastException {@inheritDoc}
  30.744 +     * @throws NullPointerException if the specified key is null
  30.745 +     *         and this map uses natural ordering, or its comparator
  30.746 +     *         does not permit null keys
  30.747 +     * @since 1.6
  30.748 +     */
  30.749 +    public K ceilingKey(K key) {
  30.750 +        return keyOrNull(getCeilingEntry(key));
  30.751 +    }
  30.752 +
  30.753 +    /**
  30.754 +     * @throws ClassCastException {@inheritDoc}
  30.755 +     * @throws NullPointerException if the specified key is null
  30.756 +     *         and this map uses natural ordering, or its comparator
  30.757 +     *         does not permit null keys
  30.758 +     * @since 1.6
  30.759 +     */
  30.760 +    public Map.Entry<K,V> higherEntry(K key) {
  30.761 +        return exportEntry(getHigherEntry(key));
  30.762 +    }
  30.763 +
  30.764 +    /**
  30.765 +     * @throws ClassCastException {@inheritDoc}
  30.766 +     * @throws NullPointerException if the specified key is null
  30.767 +     *         and this map uses natural ordering, or its comparator
  30.768 +     *         does not permit null keys
  30.769 +     * @since 1.6
  30.770 +     */
  30.771 +    public K higherKey(K key) {
  30.772 +        return keyOrNull(getHigherEntry(key));
  30.773 +    }
  30.774 +
  30.775 +    // Views
  30.776 +
  30.777 +    /**
  30.778 +     * Fields initialized to contain an instance of the entry set view
  30.779 +     * the first time this view is requested.  Views are stateless, so
  30.780 +     * there's no reason to create more than one.
  30.781 +     */
  30.782 +    private transient EntrySet entrySet = null;
  30.783 +    private transient KeySet<K> navigableKeySet = null;
  30.784 +    private transient NavigableMap<K,V> descendingMap = null;
  30.785 +
  30.786 +    /**
  30.787 +     * Returns a {@link Set} view of the keys contained in this map.
  30.788 +     * The set's iterator returns the keys in ascending order.
  30.789 +     * The set is backed by the map, so changes to the map are
  30.790 +     * reflected in the set, and vice-versa.  If the map is modified
  30.791 +     * while an iteration over the set is in progress (except through
  30.792 +     * the iterator's own {@code remove} operation), the results of
  30.793 +     * the iteration are undefined.  The set supports element removal,
  30.794 +     * which removes the corresponding mapping from the map, via the
  30.795 +     * {@code Iterator.remove}, {@code Set.remove},
  30.796 +     * {@code removeAll}, {@code retainAll}, and {@code clear}
  30.797 +     * operations.  It does not support the {@code add} or {@code addAll}
  30.798 +     * operations.
  30.799 +     */
  30.800 +    public Set<K> keySet() {
  30.801 +        return navigableKeySet();
  30.802 +    }
  30.803 +
  30.804 +    /**
  30.805 +     * @since 1.6
  30.806 +     */
  30.807 +    public NavigableSet<K> navigableKeySet() {
  30.808 +        KeySet<K> nks = navigableKeySet;
  30.809 +        return (nks != null) ? nks : (navigableKeySet = new KeySet(this));
  30.810 +    }
  30.811 +
  30.812 +    /**
  30.813 +     * @since 1.6
  30.814 +     */
  30.815 +    public NavigableSet<K> descendingKeySet() {
  30.816 +        return descendingMap().navigableKeySet();
  30.817 +    }
  30.818 +
  30.819 +    /**
  30.820 +     * Returns a {@link Collection} view of the values contained in this map.
  30.821 +     * The collection's iterator returns the values in ascending order
  30.822 +     * of the corresponding keys.
  30.823 +     * The collection is backed by the map, so changes to the map are
  30.824 +     * reflected in the collection, and vice-versa.  If the map is
  30.825 +     * modified while an iteration over the collection is in progress
  30.826 +     * (except through the iterator's own {@code remove} operation),
  30.827 +     * the results of the iteration are undefined.  The collection
  30.828 +     * supports element removal, which removes the corresponding
  30.829 +     * mapping from the map, via the {@code Iterator.remove},
  30.830 +     * {@code Collection.remove}, {@code removeAll},
  30.831 +     * {@code retainAll} and {@code clear} operations.  It does not
  30.832 +     * support the {@code add} or {@code addAll} operations.
  30.833 +     */
  30.834 +    public Collection<V> values() {
  30.835 +        Collection<V> vs = values;
  30.836 +        return (vs != null) ? vs : (values = new Values());
  30.837 +    }
  30.838 +
  30.839 +    /**
  30.840 +     * Returns a {@link Set} view of the mappings contained in this map.
  30.841 +     * The set's iterator returns the entries in ascending key order.
  30.842 +     * The set is backed by the map, so changes to the map are
  30.843 +     * reflected in the set, and vice-versa.  If the map is modified
  30.844 +     * while an iteration over the set is in progress (except through
  30.845 +     * the iterator's own {@code remove} operation, or through the
  30.846 +     * {@code setValue} operation on a map entry returned by the
  30.847 +     * iterator) the results of the iteration are undefined.  The set
  30.848 +     * supports element removal, which removes the corresponding
  30.849 +     * mapping from the map, via the {@code Iterator.remove},
  30.850 +     * {@code Set.remove}, {@code removeAll}, {@code retainAll} and
  30.851 +     * {@code clear} operations.  It does not support the
  30.852 +     * {@code add} or {@code addAll} operations.
  30.853 +     */
  30.854 +    public Set<Map.Entry<K,V>> entrySet() {
  30.855 +        EntrySet es = entrySet;
  30.856 +        return (es != null) ? es : (entrySet = new EntrySet());
  30.857 +    }
  30.858 +
  30.859 +    /**
  30.860 +     * @since 1.6
  30.861 +     */
  30.862 +    public NavigableMap<K, V> descendingMap() {
  30.863 +        NavigableMap<K, V> km = descendingMap;
  30.864 +        return (km != null) ? km :
  30.865 +            (descendingMap = new DescendingSubMap(this,
  30.866 +                                                  true, null, true,
  30.867 +                                                  true, null, true));
  30.868 +    }
  30.869 +
  30.870 +    /**
  30.871 +     * @throws ClassCastException       {@inheritDoc}
  30.872 +     * @throws NullPointerException if {@code fromKey} or {@code toKey} is
  30.873 +     *         null and this map uses natural ordering, or its comparator
  30.874 +     *         does not permit null keys
  30.875 +     * @throws IllegalArgumentException {@inheritDoc}
  30.876 +     * @since 1.6
  30.877 +     */
  30.878 +    public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
  30.879 +                                    K toKey,   boolean toInclusive) {
  30.880 +        return new AscendingSubMap(this,
  30.881 +                                   false, fromKey, fromInclusive,
  30.882 +                                   false, toKey,   toInclusive);
  30.883 +    }
  30.884 +
  30.885 +    /**
  30.886 +     * @throws ClassCastException       {@inheritDoc}
  30.887 +     * @throws NullPointerException if {@code toKey} is null
  30.888 +     *         and this map uses natural ordering, or its comparator
  30.889 +     *         does not permit null keys
  30.890 +     * @throws IllegalArgumentException {@inheritDoc}
  30.891 +     * @since 1.6
  30.892 +     */
  30.893 +    public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
  30.894 +        return new AscendingSubMap(this,
  30.895 +                                   true,  null,  true,
  30.896 +                                   false, toKey, inclusive);
  30.897 +    }
  30.898 +
  30.899 +    /**
  30.900 +     * @throws ClassCastException       {@inheritDoc}
  30.901 +     * @throws NullPointerException if {@code fromKey} is null
  30.902 +     *         and this map uses natural ordering, or its comparator
  30.903 +     *         does not permit null keys
  30.904 +     * @throws IllegalArgumentException {@inheritDoc}
  30.905 +     * @since 1.6
  30.906 +     */
  30.907 +    public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {
  30.908 +        return new AscendingSubMap(this,
  30.909 +                                   false, fromKey, inclusive,
  30.910 +                                   true,  null,    true);
  30.911 +    }
  30.912 +
  30.913 +    /**
  30.914 +     * @throws ClassCastException       {@inheritDoc}
  30.915 +     * @throws NullPointerException if {@code fromKey} or {@code toKey} is
  30.916 +     *         null and this map uses natural ordering, or its comparator
  30.917 +     *         does not permit null keys
  30.918 +     * @throws IllegalArgumentException {@inheritDoc}
  30.919 +     */
  30.920 +    public SortedMap<K,V> subMap(K fromKey, K toKey) {
  30.921 +        return subMap(fromKey, true, toKey, false);
  30.922 +    }
  30.923 +
  30.924 +    /**
  30.925 +     * @throws ClassCastException       {@inheritDoc}
  30.926 +     * @throws NullPointerException if {@code toKey} is null
  30.927 +     *         and this map uses natural ordering, or its comparator
  30.928 +     *         does not permit null keys
  30.929 +     * @throws IllegalArgumentException {@inheritDoc}
  30.930 +     */
  30.931 +    public SortedMap<K,V> headMap(K toKey) {
  30.932 +        return headMap(toKey, false);
  30.933 +    }
  30.934 +
  30.935 +    /**
  30.936 +     * @throws ClassCastException       {@inheritDoc}
  30.937 +     * @throws NullPointerException if {@code fromKey} is null
  30.938 +     *         and this map uses natural ordering, or its comparator
  30.939 +     *         does not permit null keys
  30.940 +     * @throws IllegalArgumentException {@inheritDoc}
  30.941 +     */
  30.942 +    public SortedMap<K,V> tailMap(K fromKey) {
  30.943 +        return tailMap(fromKey, true);
  30.944 +    }
  30.945 +
  30.946 +    // View class support
  30.947 +
  30.948 +    class Values extends AbstractCollection<V> {
  30.949 +        public Iterator<V> iterator() {
  30.950 +            return new ValueIterator(getFirstEntry());
  30.951 +        }
  30.952 +
  30.953 +        public int size() {
  30.954 +            return TreeMap.this.size();
  30.955 +        }
  30.956 +
  30.957 +        public boolean contains(Object o) {
  30.958 +            return TreeMap.this.containsValue(o);
  30.959 +        }
  30.960 +
  30.961 +        public boolean remove(Object o) {
  30.962 +            for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e)) {
  30.963 +                if (valEquals(e.getValue(), o)) {
  30.964 +                    deleteEntry(e);
  30.965 +                    return true;
  30.966 +                }
  30.967 +            }
  30.968 +            return false;
  30.969 +        }
  30.970 +
  30.971 +        public void clear() {
  30.972 +            TreeMap.this.clear();
  30.973 +        }
  30.974 +    }
  30.975 +
  30.976 +    class EntrySet extends AbstractSet<Map.Entry<K,V>> {
  30.977 +        public Iterator<Map.Entry<K,V>> iterator() {
  30.978 +            return new EntryIterator(getFirstEntry());
  30.979 +        }
  30.980 +
  30.981 +        public boolean contains(Object o) {
  30.982 +            if (!(o instanceof Map.Entry))
  30.983 +                return false;
  30.984 +            Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
  30.985 +            V value = entry.getValue();
  30.986 +            Entry<K,V> p = getEntry(entry.getKey());
  30.987 +            return p != null && valEquals(p.getValue(), value);
  30.988 +        }
  30.989 +
  30.990 +        public boolean remove(Object o) {
  30.991 +            if (!(o instanceof Map.Entry))
  30.992 +                return false;
  30.993 +            Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
  30.994 +            V value = entry.getValue();
  30.995 +            Entry<K,V> p = getEntry(entry.getKey());
  30.996 +            if (p != null && valEquals(p.getValue(), value)) {
  30.997 +                deleteEntry(p);
  30.998 +                return true;
  30.999 +            }
 30.1000 +            return false;
 30.1001 +        }
 30.1002 +
 30.1003 +        public int size() {
 30.1004 +            return TreeMap.this.size();
 30.1005 +        }
 30.1006 +
 30.1007 +        public void clear() {
 30.1008 +            TreeMap.this.clear();
 30.1009 +        }
 30.1010 +    }
 30.1011 +
 30.1012 +    /*
 30.1013 +     * Unlike Values and EntrySet, the KeySet class is static,
 30.1014 +     * delegating to a NavigableMap to allow use by SubMaps, which
 30.1015 +     * outweighs the ugliness of needing type-tests for the following
 30.1016 +     * Iterator methods that are defined appropriately in main versus
 30.1017 +     * submap classes.
 30.1018 +     */
 30.1019 +
 30.1020 +    Iterator<K> keyIterator() {
 30.1021 +        return new KeyIterator(getFirstEntry());
 30.1022 +    }
 30.1023 +
 30.1024 +    Iterator<K> descendingKeyIterator() {
 30.1025 +        return new DescendingKeyIterator(getLastEntry());
 30.1026 +    }
 30.1027 +
 30.1028 +    static final class KeySet<E> extends AbstractSet<E> implements NavigableSet<E> {
 30.1029 +        private final NavigableMap<E, Object> m;
 30.1030 +        KeySet(NavigableMap<E,Object> map) { m = map; }
 30.1031 +
 30.1032 +        public Iterator<E> iterator() {
 30.1033 +            if (m instanceof TreeMap)
 30.1034 +                return ((TreeMap<E,Object>)m).keyIterator();
 30.1035 +            else
 30.1036 +                return (Iterator<E>)(((TreeMap.NavigableSubMap)m).keyIterator());
 30.1037 +        }
 30.1038 +
 30.1039 +        public Iterator<E> descendingIterator() {
 30.1040 +            if (m instanceof TreeMap)
 30.1041 +                return ((TreeMap<E,Object>)m).descendingKeyIterator();
 30.1042 +            else
 30.1043 +                return (Iterator<E>)(((TreeMap.NavigableSubMap)m).descendingKeyIterator());
 30.1044 +        }
 30.1045 +
 30.1046 +        public int size() { return m.size(); }
 30.1047 +        public boolean isEmpty() { return m.isEmpty(); }
 30.1048 +        public boolean contains(Object o) { return m.containsKey(o); }
 30.1049 +        public void clear() { m.clear(); }
 30.1050 +        public E lower(E e) { return m.lowerKey(e); }
 30.1051 +        public E floor(E e) { return m.floorKey(e); }
 30.1052 +        public E ceiling(E e) { return m.ceilingKey(e); }
 30.1053 +        public E higher(E e) { return m.higherKey(e); }
 30.1054 +        public E first() { return m.firstKey(); }
 30.1055 +        public E last() { return m.lastKey(); }
 30.1056 +        public Comparator<? super E> comparator() { return m.comparator(); }
 30.1057 +        public E pollFirst() {
 30.1058 +            Map.Entry<E,Object> e = m.pollFirstEntry();
 30.1059 +            return (e == null) ? null : e.getKey();
 30.1060 +        }
 30.1061 +        public E pollLast() {
 30.1062 +            Map.Entry<E,Object> e = m.pollLastEntry();
 30.1063 +            return (e == null) ? null : e.getKey();
 30.1064 +        }
 30.1065 +        public boolean remove(Object o) {
 30.1066 +            int oldSize = size();
 30.1067 +            m.remove(o);
 30.1068 +            return size() != oldSize;
 30.1069 +        }
 30.1070 +        public NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
 30.1071 +                                      E toElement,   boolean toInclusive) {
 30.1072 +            return new KeySet<>(m.subMap(fromElement, fromInclusive,
 30.1073 +                                          toElement,   toInclusive));
 30.1074 +        }
 30.1075 +        public NavigableSet<E> headSet(E toElement, boolean inclusive) {
 30.1076 +            return new KeySet<>(m.headMap(toElement, inclusive));
 30.1077 +        }
 30.1078 +        public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
 30.1079 +            return new KeySet<>(m.tailMap(fromElement, inclusive));
 30.1080 +        }
 30.1081 +        public SortedSet<E> subSet(E fromElement, E toElement) {
 30.1082 +            return subSet(fromElement, true, toElement, false);
 30.1083 +        }
 30.1084 +        public SortedSet<E> headSet(E toElement) {
 30.1085 +            return headSet(toElement, false);
 30.1086 +        }
 30.1087 +        public SortedSet<E> tailSet(E fromElement) {
 30.1088 +            return tailSet(fromElement, true);
 30.1089 +        }
 30.1090 +        public NavigableSet<E> descendingSet() {
 30.1091 +            return new KeySet(m.descendingMap());
 30.1092 +        }
 30.1093 +    }
 30.1094 +
 30.1095 +    /**
 30.1096 +     * Base class for TreeMap Iterators
 30.1097 +     */
 30.1098 +    abstract class PrivateEntryIterator<T> implements Iterator<T> {
 30.1099 +        Entry<K,V> next;
 30.1100 +        Entry<K,V> lastReturned;
 30.1101 +        int expectedModCount;
 30.1102 +
 30.1103 +        PrivateEntryIterator(Entry<K,V> first) {
 30.1104 +            expectedModCount = modCount;
 30.1105 +            lastReturned = null;
 30.1106 +            next = first;
 30.1107 +        }
 30.1108 +
 30.1109 +        public final boolean hasNext() {
 30.1110 +            return next != null;
 30.1111 +        }
 30.1112 +
 30.1113 +        final Entry<K,V> nextEntry() {
 30.1114 +            Entry<K,V> e = next;
 30.1115 +            if (e == null)
 30.1116 +                throw new NoSuchElementException();
 30.1117 +            if (modCount != expectedModCount)
 30.1118 +                throw new ConcurrentModificationException();
 30.1119 +            next = successor(e);
 30.1120 +            lastReturned = e;
 30.1121 +            return e;
 30.1122 +        }
 30.1123 +
 30.1124 +        final Entry<K,V> prevEntry() {
 30.1125 +            Entry<K,V> e = next;
 30.1126 +            if (e == null)
 30.1127 +                throw new NoSuchElementException();
 30.1128 +            if (modCount != expectedModCount)
 30.1129 +                throw new ConcurrentModificationException();
 30.1130 +            next = predecessor(e);
 30.1131 +            lastReturned = e;
 30.1132 +            return e;
 30.1133 +        }
 30.1134 +
 30.1135 +        public void remove() {
 30.1136 +            if (lastReturned == null)
 30.1137 +                throw new IllegalStateException();
 30.1138 +            if (modCount != expectedModCount)
 30.1139 +                throw new ConcurrentModificationException();
 30.1140 +            // deleted entries are replaced by their successors
 30.1141 +            if (lastReturned.left != null && lastReturned.right != null)
 30.1142 +                next = lastReturned;
 30.1143 +            deleteEntry(lastReturned);
 30.1144 +            expectedModCount = modCount;
 30.1145 +            lastReturned = null;
 30.1146 +        }
 30.1147 +    }
 30.1148 +
 30.1149 +    final class EntryIterator extends PrivateEntryIterator<Map.Entry<K,V>> {
 30.1150 +        EntryIterator(Entry<K,V> first) {
 30.1151 +            super(first);
 30.1152 +        }
 30.1153 +        public Map.Entry<K,V> next() {
 30.1154 +            return nextEntry();
 30.1155 +        }
 30.1156 +    }
 30.1157 +
 30.1158 +    final class ValueIterator extends PrivateEntryIterator<V> {
 30.1159 +        ValueIterator(Entry<K,V> first) {
 30.1160 +            super(first);
 30.1161 +        }
 30.1162 +        public V next() {
 30.1163 +            return nextEntry().value;
 30.1164 +        }
 30.1165 +    }
 30.1166 +
 30.1167 +    final class KeyIterator extends PrivateEntryIterator<K> {
 30.1168 +        KeyIterator(Entry<K,V> first) {
 30.1169 +            super(first);
 30.1170 +        }
 30.1171 +        public K next() {
 30.1172 +            return nextEntry().key;
 30.1173 +        }
 30.1174 +    }
 30.1175 +
 30.1176 +    final class DescendingKeyIterator extends PrivateEntryIterator<K> {
 30.1177 +        DescendingKeyIterator(Entry<K,V> first) {
 30.1178 +            super(first);
 30.1179 +        }
 30.1180 +        public K next() {
 30.1181 +            return prevEntry().key;
 30.1182 +        }
 30.1183 +    }
 30.1184 +
 30.1185 +    // Little utilities
 30.1186 +
 30.1187 +    /**
 30.1188 +     * Compares two keys using the correct comparison method for this TreeMap.
 30.1189 +     */
 30.1190 +    final int compare(Object k1, Object k2) {
 30.1191 +        return comparator==null ? ((Comparable<? super K>)k1).compareTo((K)k2)
 30.1192 +            : comparator.compare((K)k1, (K)k2);
 30.1193 +    }
 30.1194 +
 30.1195 +    /**
 30.1196 +     * Test two values for equality.  Differs from o1.equals(o2) only in
 30.1197 +     * that it copes with {@code null} o1 properly.
 30.1198 +     */
 30.1199 +    static final boolean valEquals(Object o1, Object o2) {
 30.1200 +        return (o1==null ? o2==null : o1.equals(o2));
 30.1201 +    }
 30.1202 +
 30.1203 +    /**
 30.1204 +     * Return SimpleImmutableEntry for entry, or null if null
 30.1205 +     */
 30.1206 +    static <K,V> Map.Entry<K,V> exportEntry(TreeMap.Entry<K,V> e) {
 30.1207 +        return (e == null) ? null :
 30.1208 +            new AbstractMap.SimpleImmutableEntry<>(e);
 30.1209 +    }
 30.1210 +
 30.1211 +    /**
 30.1212 +     * Return key for entry, or null if null
 30.1213 +     */
 30.1214 +    static <K,V> K keyOrNull(TreeMap.Entry<K,V> e) {
 30.1215 +        return (e == null) ? null : e.key;
 30.1216 +    }
 30.1217 +
 30.1218 +    /**
 30.1219 +     * Returns the key corresponding to the specified Entry.
 30.1220 +     * @throws NoSuchElementException if the Entry is null
 30.1221 +     */
 30.1222 +    static <K> K key(Entry<K,?> e) {
 30.1223 +        if (e==null)
 30.1224 +            throw new NoSuchElementException();
 30.1225 +        return e.key;
 30.1226 +    }
 30.1227 +
 30.1228 +
 30.1229 +    // SubMaps
 30.1230 +
 30.1231 +    /**
 30.1232 +     * Dummy value serving as unmatchable fence key for unbounded
 30.1233 +     * SubMapIterators
 30.1234 +     */
 30.1235 +    private static final Object UNBOUNDED = new Object();
 30.1236 +
 30.1237 +    /**
 30.1238 +     * @serial include
 30.1239 +     */
 30.1240 +    abstract static class NavigableSubMap<K,V> extends AbstractMap<K,V>
 30.1241 +        implements NavigableMap<K,V>, java.io.Serializable {
 30.1242 +        /**
 30.1243 +         * The backing map.
 30.1244 +         */
 30.1245 +        final TreeMap<K,V> m;
 30.1246 +
 30.1247 +        /**
 30.1248 +         * Endpoints are represented as triples (fromStart, lo,
 30.1249 +         * loInclusive) and (toEnd, hi, hiInclusive). If fromStart is
 30.1250 +         * true, then the low (absolute) bound is the start of the
 30.1251 +         * backing map, and the other values are ignored. Otherwise,
 30.1252 +         * if loInclusive is true, lo is the inclusive bound, else lo
 30.1253 +         * is the exclusive bound. Similarly for the upper bound.
 30.1254 +         */
 30.1255 +        final K lo, hi;
 30.1256 +        final boolean fromStart, toEnd;
 30.1257 +        final boolean loInclusive, hiInclusive;
 30.1258 +
 30.1259 +        NavigableSubMap(TreeMap<K,V> m,
 30.1260 +                        boolean fromStart, K lo, boolean loInclusive,
 30.1261 +                        boolean toEnd,     K hi, boolean hiInclusive) {
 30.1262 +            if (!fromStart && !toEnd) {
 30.1263 +                if (m.compare(lo, hi) > 0)
 30.1264 +                    throw new IllegalArgumentException("fromKey > toKey");
 30.1265 +            } else {
 30.1266 +                if (!fromStart) // type check
 30.1267 +                    m.compare(lo, lo);
 30.1268 +                if (!toEnd)
 30.1269 +                    m.compare(hi, hi);
 30.1270 +            }
 30.1271 +
 30.1272 +            this.m = m;
 30.1273 +            this.fromStart = fromStart;
 30.1274 +            this.lo = lo;
 30.1275 +            this.loInclusive = loInclusive;
 30.1276 +            this.toEnd = toEnd;
 30.1277 +            this.hi = hi;
 30.1278 +            this.hiInclusive = hiInclusive;
 30.1279 +        }
 30.1280 +
 30.1281 +        // internal utilities
 30.1282 +
 30.1283 +        final boolean tooLow(Object key) {
 30.1284 +            if (!fromStart) {
 30.1285 +                int c = m.compare(key, lo);
 30.1286 +                if (c < 0 || (c == 0 && !loInclusive))
 30.1287 +                    return true;
 30.1288 +            }
 30.1289 +            return false;
 30.1290 +        }
 30.1291 +
 30.1292 +        final boolean tooHigh(Object key) {
 30.1293 +            if (!toEnd) {
 30.1294 +                int c = m.compare(key, hi);
 30.1295 +                if (c > 0 || (c == 0 && !hiInclusive))
 30.1296 +                    return true;
 30.1297 +            }
 30.1298 +            return false;
 30.1299 +        }
 30.1300 +
 30.1301 +        final boolean inRange(Object key) {
 30.1302 +            return !tooLow(key) && !tooHigh(key);
 30.1303 +        }
 30.1304 +
 30.1305 +        final boolean inClosedRange(Object key) {
 30.1306 +            return (fromStart || m.compare(key, lo) >= 0)
 30.1307 +                && (toEnd || m.compare(hi, key) >= 0);
 30.1308 +        }
 30.1309 +
 30.1310 +        final boolean inRange(Object key, boolean inclusive) {
 30.1311 +            return inclusive ? inRange(key) : inClosedRange(key);
 30.1312 +        }
 30.1313 +
 30.1314 +        /*
 30.1315 +         * Absolute versions of relation operations.
 30.1316 +         * Subclasses map to these using like-named "sub"
 30.1317 +         * versions that invert senses for descending maps
 30.1318 +         */
 30.1319 +
 30.1320 +        final TreeMap.Entry<K,V> absLowest() {
 30.1321 +            TreeMap.Entry<K,V> e =
 30.1322 +                (fromStart ?  m.getFirstEntry() :
 30.1323 +                 (loInclusive ? m.getCeilingEntry(lo) :
 30.1324 +                                m.getHigherEntry(lo)));
 30.1325 +            return (e == null || tooHigh(e.key)) ? null : e;
 30.1326 +        }
 30.1327 +
 30.1328 +        final TreeMap.Entry<K,V> absHighest() {
 30.1329 +            TreeMap.Entry<K,V> e =
 30.1330 +                (toEnd ?  m.getLastEntry() :
 30.1331 +                 (hiInclusive ?  m.getFloorEntry(hi) :
 30.1332 +                                 m.getLowerEntry(hi)));
 30.1333 +            return (e == null || tooLow(e.key)) ? null : e;
 30.1334 +        }
 30.1335 +
 30.1336 +        final TreeMap.Entry<K,V> absCeiling(K key) {
 30.1337 +            if (tooLow(key))
 30.1338 +                return absLowest();
 30.1339 +            TreeMap.Entry<K,V> e = m.getCeilingEntry(key);
 30.1340 +            return (e == null || tooHigh(e.key)) ? null : e;
 30.1341 +        }
 30.1342 +
 30.1343 +        final TreeMap.Entry<K,V> absHigher(K key) {
 30.1344 +            if (tooLow(key))
 30.1345 +                return absLowest();
 30.1346 +            TreeMap.Entry<K,V> e = m.getHigherEntry(key);
 30.1347 +            return (e == null || tooHigh(e.key)) ? null : e;
 30.1348 +        }
 30.1349 +
 30.1350 +        final TreeMap.Entry<K,V> absFloor(K key) {
 30.1351 +            if (tooHigh(key))
 30.1352 +                return absHighest();
 30.1353 +            TreeMap.Entry<K,V> e = m.getFloorEntry(key);
 30.1354 +            return (e == null || tooLow(e.key)) ? null : e;
 30.1355 +        }
 30.1356 +
 30.1357 +        final TreeMap.Entry<K,V> absLower(K key) {
 30.1358 +            if (tooHigh(key))
 30.1359 +                return absHighest();
 30.1360 +            TreeMap.Entry<K,V> e = m.getLowerEntry(key);
 30.1361 +            return (e == null || tooLow(e.key)) ? null : e;
 30.1362 +        }
 30.1363 +
 30.1364 +        /** Returns the absolute high fence for ascending traversal */
 30.1365 +        final TreeMap.Entry<K,V> absHighFence() {
 30.1366 +            return (toEnd ? null : (hiInclusive ?
 30.1367 +                                    m.getHigherEntry(hi) :
 30.1368 +                                    m.getCeilingEntry(hi)));
 30.1369 +        }
 30.1370 +
 30.1371 +        /** Return the absolute low fence for descending traversal  */
 30.1372 +        final TreeMap.Entry<K,V> absLowFence() {
 30.1373 +            return (fromStart ? null : (loInclusive ?
 30.1374 +                                        m.getLowerEntry(lo) :
 30.1375 +                                        m.getFloorEntry(lo)));
 30.1376 +        }
 30.1377 +
 30.1378 +        // Abstract methods defined in ascending vs descending classes
 30.1379 +        // These relay to the appropriate absolute versions
 30.1380 +
 30.1381 +        abstract TreeMap.Entry<K,V> subLowest();
 30.1382 +        abstract TreeMap.Entry<K,V> subHighest();
 30.1383 +        abstract TreeMap.Entry<K,V> subCeiling(K key);
 30.1384 +        abstract TreeMap.Entry<K,V> subHigher(K key);
 30.1385 +        abstract TreeMap.Entry<K,V> subFloor(K key);
 30.1386 +        abstract TreeMap.Entry<K,V> subLower(K key);
 30.1387 +
 30.1388 +        /** Returns ascending iterator from the perspective of this submap */
 30.1389 +        abstract Iterator<K> keyIterator();
 30.1390 +
 30.1391 +        /** Returns descending iterator from the perspective of this submap */
 30.1392 +        abstract Iterator<K> descendingKeyIterator();
 30.1393 +
 30.1394 +        // public methods
 30.1395 +
 30.1396 +        public boolean isEmpty() {
 30.1397 +            return (fromStart && toEnd) ? m.isEmpty() : entrySet().isEmpty();
 30.1398 +        }
 30.1399 +
 30.1400 +        public int size() {
 30.1401 +            return (fromStart && toEnd) ? m.size() : entrySet().size();
 30.1402 +        }
 30.1403 +
 30.1404 +        public final boolean containsKey(Object key) {
 30.1405 +            return inRange(key) && m.containsKey(key);
 30.1406 +        }
 30.1407 +
 30.1408 +        public final V put(K key, V value) {
 30.1409 +            if (!inRange(key))
 30.1410 +                throw new IllegalArgumentException("key out of range");
 30.1411 +            return m.put(key, value);
 30.1412 +        }
 30.1413 +
 30.1414 +        public final V get(Object key) {
 30.1415 +            return !inRange(key) ? null :  m.get(key);
 30.1416 +        }
 30.1417 +
 30.1418 +        public final V remove(Object key) {
 30.1419 +            return !inRange(key) ? null : m.remove(key);
 30.1420 +        }
 30.1421 +
 30.1422 +        public final Map.Entry<K,V> ceilingEntry(K key) {
 30.1423 +            return exportEntry(subCeiling(key));
 30.1424 +        }
 30.1425 +
 30.1426 +        public final K ceilingKey(K key) {
 30.1427 +            return keyOrNull(subCeiling(key));
 30.1428 +        }
 30.1429 +
 30.1430 +        public final Map.Entry<K,V> higherEntry(K key) {
 30.1431 +            return exportEntry(subHigher(key));
 30.1432 +        }
 30.1433 +
 30.1434 +        public final K higherKey(K key) {
 30.1435 +            return keyOrNull(subHigher(key));
 30.1436 +        }
 30.1437 +
 30.1438 +        public final Map.Entry<K,V> floorEntry(K key) {
 30.1439 +            return exportEntry(subFloor(key));
 30.1440 +        }
 30.1441 +
 30.1442 +        public final K floorKey(K key) {
 30.1443 +            return keyOrNull(subFloor(key));
 30.1444 +        }
 30.1445 +
 30.1446 +        public final Map.Entry<K,V> lowerEntry(K key) {
 30.1447 +            return exportEntry(subLower(key));
 30.1448 +        }
 30.1449 +
 30.1450 +        public final K lowerKey(K key) {
 30.1451 +            return keyOrNull(subLower(key));
 30.1452 +        }
 30.1453 +
 30.1454 +        public final K firstKey() {
 30.1455 +            return key(subLowest());
 30.1456 +        }
 30.1457 +
 30.1458 +        public final K lastKey() {
 30.1459 +            return key(subHighest());
 30.1460 +        }
 30.1461 +
 30.1462 +        public final Map.Entry<K,V> firstEntry() {
 30.1463 +            return exportEntry(subLowest());
 30.1464 +        }
 30.1465 +
 30.1466 +        public final Map.Entry<K,V> lastEntry() {
 30.1467 +            return exportEntry(subHighest());
 30.1468 +        }
 30.1469 +
 30.1470 +        public final Map.Entry<K,V> pollFirstEntry() {
 30.1471 +            TreeMap.Entry<K,V> e = subLowest();
 30.1472 +            Map.Entry<K,V> result = exportEntry(e);
 30.1473 +            if (e != null)
 30.1474 +                m.deleteEntry(e);
 30.1475 +            return result;
 30.1476 +        }
 30.1477 +
 30.1478 +        public final Map.Entry<K,V> pollLastEntry() {
 30.1479 +            TreeMap.Entry<K,V> e = subHighest();
 30.1480 +            Map.Entry<K,V> result = exportEntry(e);
 30.1481 +            if (e != null)
 30.1482 +                m.deleteEntry(e);
 30.1483 +            return result;
 30.1484 +        }
 30.1485 +
 30.1486 +        // Views
 30.1487 +        transient NavigableMap<K,V> descendingMapView = null;
 30.1488 +        transient EntrySetView entrySetView = null;
 30.1489 +        transient KeySet<K> navigableKeySetView = null;
 30.1490 +
 30.1491 +        public final NavigableSet<K> navigableKeySet() {
 30.1492 +            KeySet<K> nksv = navigableKeySetView;
 30.1493 +            return (nksv != null) ? nksv :
 30.1494 +                (navigableKeySetView = new TreeMap.KeySet(this));
 30.1495 +        }
 30.1496 +
 30.1497 +        public final Set<K> keySet() {
 30.1498 +            return navigableKeySet();
 30.1499 +        }
 30.1500 +
 30.1501 +        public NavigableSet<K> descendingKeySet() {
 30.1502 +            return descendingMap().navigableKeySet();
 30.1503 +        }
 30.1504 +
 30.1505 +        public final SortedMap<K,V> subMap(K fromKey, K toKey) {
 30.1506 +            return subMap(fromKey, true, toKey, false);
 30.1507 +        }
 30.1508 +
 30.1509 +        public final SortedMap<K,V> headMap(K toKey) {
 30.1510 +            return headMap(toKey, false);
 30.1511 +        }
 30.1512 +
 30.1513 +        public final SortedMap<K,V> tailMap(K fromKey) {
 30.1514 +            return tailMap(fromKey, true);
 30.1515 +        }
 30.1516 +
 30.1517 +        // View classes
 30.1518 +
 30.1519 +        abstract class EntrySetView extends AbstractSet<Map.Entry<K,V>> {
 30.1520 +            private transient int size = -1, sizeModCount;
 30.1521 +
 30.1522 +            public int size() {
 30.1523 +                if (fromStart && toEnd)
 30.1524 +                    return m.size();
 30.1525 +                if (size == -1 || sizeModCount != m.modCount) {
 30.1526 +                    sizeModCount = m.modCount;
 30.1527 +                    size = 0;
 30.1528 +                    Iterator i = iterator();
 30.1529 +                    while (i.hasNext()) {
 30.1530 +                        size++;
 30.1531 +                        i.next();
 30.1532 +                    }
 30.1533 +                }
 30.1534 +                return size;
 30.1535 +            }
 30.1536 +
 30.1537 +            public boolean isEmpty() {
 30.1538 +                TreeMap.Entry<K,V> n = absLowest();
 30.1539 +                return n == null || tooHigh(n.key);
 30.1540 +            }
 30.1541 +
 30.1542 +            public boolean contains(Object o) {
 30.1543 +                if (!(o instanceof Map.Entry))
 30.1544 +                    return false;
 30.1545 +                Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
 30.1546 +                K key = entry.getKey();
 30.1547 +                if (!inRange(key))
 30.1548 +                    return false;
 30.1549 +                TreeMap.Entry node = m.getEntry(key);
 30.1550 +                return node != null &&
 30.1551 +                    valEquals(node.getValue(), entry.getValue());
 30.1552 +            }
 30.1553 +
 30.1554 +            public boolean remove(Object o) {
 30.1555 +                if (!(o instanceof Map.Entry))
 30.1556 +                    return false;
 30.1557 +                Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
 30.1558 +                K key = entry.getKey();
 30.1559 +                if (!inRange(key))
 30.1560 +                    return false;
 30.1561 +                TreeMap.Entry<K,V> node = m.getEntry(key);
 30.1562 +                if (node!=null && valEquals(node.getValue(),
 30.1563 +                                            entry.getValue())) {
 30.1564 +                    m.deleteEntry(node);
 30.1565 +                    return true;
 30.1566 +                }
 30.1567 +                return false;
 30.1568 +            }
 30.1569 +        }
 30.1570 +
 30.1571 +        /**
 30.1572 +         * Iterators for SubMaps
 30.1573 +         */
 30.1574 +        abstract class SubMapIterator<T> implements Iterator<T> {
 30.1575 +            TreeMap.Entry<K,V> lastReturned;
 30.1576 +            TreeMap.Entry<K,V> next;
 30.1577 +            final Object fenceKey;
 30.1578 +            int expectedModCount;
 30.1579 +
 30.1580 +            SubMapIterator(TreeMap.Entry<K,V> first,
 30.1581 +                           TreeMap.Entry<K,V> fence) {
 30.1582 +                expectedModCount = m.modCount;
 30.1583 +                lastReturned = null;
 30.1584 +                next = first;
 30.1585 +                fenceKey = fence == null ? UNBOUNDED : fence.key;
 30.1586 +            }
 30.1587 +
 30.1588 +            public final boolean hasNext() {
 30.1589 +                return next != null && next.key != fenceKey;
 30.1590 +            }
 30.1591 +
 30.1592 +            final TreeMap.Entry<K,V> nextEntry() {
 30.1593 +                TreeMap.Entry<K,V> e = next;
 30.1594 +                if (e == null || e.key == fenceKey)
 30.1595 +                    throw new NoSuchElementException();
 30.1596 +                if (m.modCount != expectedModCount)
 30.1597 +                    throw new ConcurrentModificationException();
 30.1598 +                next = successor(e);
 30.1599 +                lastReturned = e;
 30.1600 +                return e;
 30.1601 +            }
 30.1602 +
 30.1603 +            final TreeMap.Entry<K,V> prevEntry() {
 30.1604 +                TreeMap.Entry<K,V> e = next;
 30.1605 +                if (e == null || e.key == fenceKey)
 30.1606 +                    throw new NoSuchElementException();
 30.1607 +                if (m.modCount != expectedModCount)
 30.1608 +                    throw new ConcurrentModificationException();
 30.1609 +                next = predecessor(e);
 30.1610 +                lastReturned = e;
 30.1611 +                return e;
 30.1612 +            }
 30.1613 +
 30.1614 +            final void removeAscending() {
 30.1615 +                if (lastReturned == null)
 30.1616 +                    throw new IllegalStateException();
 30.1617 +                if (m.modCount != expectedModCount)
 30.1618 +                    throw new ConcurrentModificationException();
 30.1619 +                // deleted entries are replaced by their successors
 30.1620 +                if (lastReturned.left != null && lastReturned.right != null)
 30.1621 +                    next = lastReturned;
 30.1622 +                m.deleteEntry(lastReturned);
 30.1623 +                lastReturned = null;
 30.1624 +                expectedModCount = m.modCount;
 30.1625 +            }
 30.1626 +
 30.1627 +            final void removeDescending() {
 30.1628 +                if (lastReturned == null)
 30.1629 +                    throw new IllegalStateException();
 30.1630 +                if (m.modCount != expectedModCount)
 30.1631 +                    throw new ConcurrentModificationException();
 30.1632 +                m.deleteEntry(lastReturned);
 30.1633 +                lastReturned = null;
 30.1634 +                expectedModCount = m.modCount;
 30.1635 +            }
 30.1636 +
 30.1637 +        }
 30.1638 +
 30.1639 +        final class SubMapEntryIterator extends SubMapIterator<Map.Entry<K,V>> {
 30.1640 +            SubMapEntryIterator(TreeMap.Entry<K,V> first,
 30.1641 +                                TreeMap.Entry<K,V> fence) {
 30.1642 +                super(first, fence);
 30.1643 +            }
 30.1644 +            public Map.Entry<K,V> next() {
 30.1645 +                return nextEntry();
 30.1646 +            }
 30.1647 +            public void remove() {
 30.1648 +                removeAscending();
 30.1649 +            }
 30.1650 +        }
 30.1651 +
 30.1652 +        final class SubMapKeyIterator extends SubMapIterator<K> {
 30.1653 +            SubMapKeyIterator(TreeMap.Entry<K,V> first,
 30.1654 +                              TreeMap.Entry<K,V> fence) {
 30.1655 +                super(first, fence);
 30.1656 +            }
 30.1657 +            public K next() {
 30.1658 +                return nextEntry().key;
 30.1659 +            }
 30.1660 +            public void remove() {
 30.1661 +                removeAscending();
 30.1662 +            }
 30.1663 +        }
 30.1664 +
 30.1665 +        final class DescendingSubMapEntryIterator extends SubMapIterator<Map.Entry<K,V>> {
 30.1666 +            DescendingSubMapEntryIterator(TreeMap.Entry<K,V> last,
 30.1667 +                                          TreeMap.Entry<K,V> fence) {
 30.1668 +                super(last, fence);
 30.1669 +            }
 30.1670 +
 30.1671 +            public Map.Entry<K,V> next() {
 30.1672 +                return prevEntry();
 30.1673 +            }
 30.1674 +            public void remove() {
 30.1675 +                removeDescending();
 30.1676 +            }
 30.1677 +        }
 30.1678 +
 30.1679 +        final class DescendingSubMapKeyIterator extends SubMapIterator<K> {
 30.1680 +            DescendingSubMapKeyIterator(TreeMap.Entry<K,V> last,
 30.1681 +                                        TreeMap.Entry<K,V> fence) {
 30.1682 +                super(last, fence);
 30.1683 +            }
 30.1684 +            public K next() {
 30.1685 +                return prevEntry().key;
 30.1686 +            }
 30.1687 +            public void remove() {
 30.1688 +                removeDescending();
 30.1689 +            }
 30.1690 +        }
 30.1691 +    }
 30.1692 +
 30.1693 +    /**
 30.1694 +     * @serial include
 30.1695 +     */
 30.1696 +    static final class AscendingSubMap<K,V> extends NavigableSubMap<K,V> {
 30.1697 +        private static final long serialVersionUID = 912986545866124060L;
 30.1698 +
 30.1699 +        AscendingSubMap(TreeMap<K,V> m,
 30.1700 +                        boolean fromStart, K lo, boolean loInclusive,
 30.1701 +                        boolean toEnd,     K hi, boolean hiInclusive) {
 30.1702 +            super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
 30.1703 +        }
 30.1704 +
 30.1705 +        public Comparator<? super K> comparator() {
 30.1706 +            return m.comparator();
 30.1707 +        }
 30.1708 +
 30.1709 +        public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
 30.1710 +                                        K toKey,   boolean toInclusive) {
 30.1711 +            if (!inRange(fromKey, fromInclusive))
 30.1712 +                throw new IllegalArgumentException("fromKey out of range");
 30.1713 +            if (!inRange(toKey, toInclusive))
 30.1714 +                throw new IllegalArgumentException("toKey out of range");
 30.1715 +            return new AscendingSubMap(m,
 30.1716 +                                       false, fromKey, fromInclusive,
 30.1717 +                                       false, toKey,   toInclusive);
 30.1718 +        }
 30.1719 +
 30.1720 +        public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
 30.1721 +            if (!inRange(toKey, inclusive))
 30.1722 +                throw new IllegalArgumentException("toKey out of range");
 30.1723 +            return new AscendingSubMap(m,
 30.1724 +                                       fromStart, lo,    loInclusive,
 30.1725 +                                       false,     toKey, inclusive);
 30.1726 +        }
 30.1727 +
 30.1728 +        public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {
 30.1729 +            if (!inRange(fromKey, inclusive))
 30.1730 +                throw new IllegalArgumentException("fromKey out of range");
 30.1731 +            return new AscendingSubMap(m,
 30.1732 +                                       false, fromKey, inclusive,
 30.1733 +                                       toEnd, hi,      hiInclusive);
 30.1734 +        }
 30.1735 +
 30.1736 +        public NavigableMap<K,V> descendingMap() {
 30.1737 +            NavigableMap<K,V> mv = descendingMapView;
 30.1738 +            return (mv != null) ? mv :
 30.1739 +                (descendingMapView =
 30.1740 +                 new DescendingSubMap(m,
 30.1741 +                                      fromStart, lo, loInclusive,
 30.1742 +                                      toEnd,     hi, hiInclusive));
 30.1743 +        }
 30.1744 +
 30.1745 +        Iterator<K> keyIterator() {
 30.1746 +            return new SubMapKeyIterator(absLowest(), absHighFence());
 30.1747 +        }
 30.1748 +
 30.1749 +        Iterator<K> descendingKeyIterator() {
 30.1750 +            return new DescendingSubMapKeyIterator(absHighest(), absLowFence());
 30.1751 +        }
 30.1752 +
 30.1753 +        final class AscendingEntrySetView extends EntrySetView {
 30.1754 +            public Iterator<Map.Entry<K,V>> iterator() {
 30.1755 +                return new SubMapEntryIterator(absLowest(), absHighFence());
 30.1756 +            }
 30.1757 +        }
 30.1758 +
 30.1759 +        public Set<Map.Entry<K,V>> entrySet() {
 30.1760 +            EntrySetView es = entrySetView;
 30.1761 +            return (es != null) ? es : new AscendingEntrySetView();
 30.1762 +        }
 30.1763 +
 30.1764 +        TreeMap.Entry<K,V> subLowest()       { return absLowest(); }
 30.1765 +        TreeMap.Entry<K,V> subHighest()      { return absHighest(); }
 30.1766 +        TreeMap.Entry<K,V> subCeiling(K key) { return absCeiling(key); }
 30.1767 +        TreeMap.Entry<K,V> subHigher(K key)  { return absHigher(key); }
 30.1768 +        TreeMap.Entry<K,V> subFloor(K key)   { return absFloor(key); }
 30.1769 +        TreeMap.Entry<K,V> subLower(K key)   { return absLower(key); }
 30.1770 +    }
 30.1771 +
 30.1772 +    /**
 30.1773 +     * @serial include
 30.1774 +     */
 30.1775 +    static final class DescendingSubMap<K,V>  extends NavigableSubMap<K,V> {
 30.1776 +        private static final long serialVersionUID = 912986545866120460L;
 30.1777 +        DescendingSubMap(TreeMap<K,V> m,
 30.1778 +                        boolean fromStart, K lo, boolean loInclusive,
 30.1779 +                        boolean toEnd,     K hi, boolean hiInclusive) {
 30.1780 +            super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);
 30.1781 +        }
 30.1782 +
 30.1783 +        private final Comparator<? super K> reverseComparator =
 30.1784 +            Collections.reverseOrder(m.comparator);
 30.1785 +
 30.1786 +        public Comparator<? super K> comparator() {
 30.1787 +            return reverseComparator;
 30.1788 +        }
 30.1789 +
 30.1790 +        public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
 30.1791 +                                        K toKey,   boolean toInclusive) {
 30.1792 +            if (!inRange(fromKey, fromInclusive))
 30.1793 +                throw new IllegalArgumentException("fromKey out of range");
 30.1794 +            if (!inRange(toKey, toInclusive))
 30.1795 +                throw new IllegalArgumentException("toKey out of range");
 30.1796 +            return new DescendingSubMap(m,
 30.1797 +                                        false, toKey,   toInclusive,
 30.1798 +                                        false, fromKey, fromInclusive);
 30.1799 +        }
 30.1800 +
 30.1801 +        public NavigableMap<K,V> headMap(K toKey, boolean inclusive) {
 30.1802 +            if (!inRange(toKey, inclusive))
 30.1803 +                throw new IllegalArgumentException("toKey out of range");
 30.1804 +            return new DescendingSubMap(m,
 30.1805 +                                        false, toKey, inclusive,
 30.1806 +                                        toEnd, hi,    hiInclusive);
 30.1807 +        }
 30.1808 +
 30.1809 +        public NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) {
 30.1810 +            if (!inRange(fromKey, inclusive))
 30.1811 +                throw new IllegalArgumentException("fromKey out of range");
 30.1812 +            return new DescendingSubMap(m,
 30.1813 +                                        fromStart, lo, loInclusive,
 30.1814 +                                        false, fromKey, inclusive);
 30.1815 +        }
 30.1816 +
 30.1817 +        public NavigableMap<K,V> descendingMap() {
 30.1818 +            NavigableMap<K,V> mv = descendingMapView;
 30.1819 +            return (mv != null) ? mv :
 30.1820 +                (descendingMapView =
 30.1821 +                 new AscendingSubMap(m,
 30.1822 +                                     fromStart, lo, loInclusive,
 30.1823 +                                     toEnd,     hi, hiInclusive));
 30.1824 +        }
 30.1825 +
 30.1826 +        Iterator<K> keyIterator() {
 30.1827 +            return new DescendingSubMapKeyIterator(absHighest(), absLowFence());
 30.1828 +        }
 30.1829 +
 30.1830 +        Iterator<K> descendingKeyIterator() {
 30.1831 +            return new SubMapKeyIterator(absLowest(), absHighFence());
 30.1832 +        }
 30.1833 +
 30.1834 +        final class DescendingEntrySetView extends EntrySetView {
 30.1835 +            public Iterator<Map.Entry<K,V>> iterator() {
 30.1836 +                return new DescendingSubMapEntryIterator(absHighest(), absLowFence());
 30.1837 +            }
 30.1838 +        }
 30.1839 +
 30.1840 +        public Set<Map.Entry<K,V>> entrySet() {
 30.1841 +            EntrySetView es = entrySetView;
 30.1842 +            return (es != null) ? es : new DescendingEntrySetView();
 30.1843 +        }
 30.1844 +
 30.1845 +        TreeMap.Entry<K,V> subLowest()       { return absHighest(); }
 30.1846 +        TreeMap.Entry<K,V> subHighest()      { return absLowest(); }
 30.1847 +        TreeMap.Entry<K,V> subCeiling(K key) { return absFloor(key); }
 30.1848 +        TreeMap.Entry<K,V> subHigher(K key)  { return absLower(key); }
 30.1849 +        TreeMap.Entry<K,V> subFloor(K key)   { return absCeiling(key); }
 30.1850 +        TreeMap.Entry<K,V> subLower(K key)   { return absHigher(key); }
 30.1851 +    }
 30.1852 +
 30.1853 +    /**
 30.1854 +     * This class exists solely for the sake of serialization
 30.1855 +     * compatibility with previous releases of TreeMap that did not
 30.1856 +     * support NavigableMap.  It translates an old-version SubMap into
 30.1857 +     * a new-version AscendingSubMap. This class is never otherwise
 30.1858 +     * used.
 30.1859 +     *
 30.1860 +     * @serial include
 30.1861 +     */
 30.1862 +    private class SubMap extends AbstractMap<K,V>
 30.1863 +        implements SortedMap<K,V>, java.io.Serializable {
 30.1864 +        private static final long serialVersionUID = -6520786458950516097L;
 30.1865 +        private boolean fromStart = false, toEnd = false;
 30.1866 +        private K fromKey, toKey;
 30.1867 +        private Object readResolve() {
 30.1868 +            return new AscendingSubMap(TreeMap.this,
 30.1869 +                                       fromStart, fromKey, true,
 30.1870 +                                       toEnd, toKey, false);
 30.1871 +        }
 30.1872 +        public Set<Map.Entry<K,V>> entrySet() { throw new InternalError(); }
 30.1873 +        public K lastKey() { throw new InternalError(); }
 30.1874 +        public K firstKey() { throw new InternalError(); }
 30.1875 +        public SortedMap<K,V> subMap(K fromKey, K toKey) { throw new InternalError(); }
 30.1876 +        public SortedMap<K,V> headMap(K toKey) { throw new InternalError(); }
 30.1877 +        public SortedMap<K,V> tailMap(K fromKey) { throw new InternalError(); }
 30.1878 +        public Comparator<? super K> comparator() { throw new InternalError(); }
 30.1879 +    }
 30.1880 +
 30.1881 +
 30.1882 +    // Red-black mechanics
 30.1883 +
 30.1884 +    private static final boolean RED   = false;
 30.1885 +    private static final boolean BLACK = true;
 30.1886 +
 30.1887 +    /**
 30.1888 +     * Node in the Tree.  Doubles as a means to pass key-value pairs back to
 30.1889 +     * user (see Map.Entry).
 30.1890 +     */
 30.1891 +
 30.1892 +    static final class Entry<K,V> implements Map.Entry<K,V> {
 30.1893 +        K key;
 30.1894 +        V value;
 30.1895 +        Entry<K,V> left = null;
 30.1896 +        Entry<K,V> right = null;
 30.1897 +        Entry<K,V> parent;
 30.1898 +        boolean color = BLACK;
 30.1899 +
 30.1900 +        /**
 30.1901 +         * Make a new cell with given key, value, and parent, and with
 30.1902 +         * {@code null} child links, and BLACK color.
 30.1903 +         */
 30.1904 +        Entry(K key, V value, Entry<K,V> parent) {
 30.1905 +            this.key = key;
 30.1906 +            this.value = value;
 30.1907 +            this.parent = parent;
 30.1908 +        }
 30.1909 +
 30.1910 +        /**
 30.1911 +         * Returns the key.
 30.1912 +         *
 30.1913 +         * @return the key
 30.1914 +         */
 30.1915 +        public K getKey() {
 30.1916 +            return key;
 30.1917 +        }
 30.1918 +
 30.1919 +        /**
 30.1920 +         * Returns the value associated with the key.
 30.1921 +         *
 30.1922 +         * @return the value associated with the key
 30.1923 +         */
 30.1924 +        public V getValue() {
 30.1925 +            return value;
 30.1926 +        }
 30.1927 +
 30.1928 +        /**
 30.1929 +         * Replaces the value currently associated with the key with the given
 30.1930 +         * value.
 30.1931 +         *
 30.1932 +         * @return the value associated with the key before this method was
 30.1933 +         *         called
 30.1934 +         */
 30.1935 +        public V setValue(V value) {
 30.1936 +            V oldValue = this.value;
 30.1937 +            this.value = value;
 30.1938 +            return oldValue;
 30.1939 +        }
 30.1940 +
 30.1941 +        public boolean equals(Object o) {
 30.1942 +            if (!(o instanceof Map.Entry))
 30.1943 +                return false;
 30.1944 +            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
 30.1945 +
 30.1946 +            return valEquals(key,e.getKey()) && valEquals(value,e.getValue());
 30.1947 +        }
 30.1948 +
 30.1949 +        public int hashCode() {
 30.1950 +            int keyHash = (key==null ? 0 : key.hashCode());
 30.1951 +            int valueHash = (value==null ? 0 : value.hashCode());
 30.1952 +            return keyHash ^ valueHash;
 30.1953 +        }
 30.1954 +
 30.1955 +        public String toString() {
 30.1956 +            return key + "=" + value;
 30.1957 +        }
 30.1958 +    }
 30.1959 +
 30.1960 +    /**
 30.1961 +     * Returns the first Entry in the TreeMap (according to the TreeMap's
 30.1962 +     * key-sort function).  Returns null if the TreeMap is empty.
 30.1963 +     */
 30.1964 +    final Entry<K,V> getFirstEntry() {
 30.1965 +        Entry<K,V> p = root;
 30.1966 +        if (p != null)
 30.1967 +            while (p.left != null)
 30.1968 +                p = p.left;
 30.1969 +        return p;
 30.1970 +    }
 30.1971 +
 30.1972 +    /**
 30.1973 +     * Returns the last Entry in the TreeMap (according to the TreeMap's
 30.1974 +     * key-sort function).  Returns null if the TreeMap is empty.
 30.1975 +     */
 30.1976 +    final Entry<K,V> getLastEntry() {
 30.1977 +        Entry<K,V> p = root;
 30.1978 +        if (p != null)
 30.1979 +            while (p.right != null)
 30.1980 +                p = p.right;
 30.1981 +        return p;
 30.1982 +    }
 30.1983 +
 30.1984 +    /**
 30.1985 +     * Returns the successor of the specified Entry, or null if no such.
 30.1986 +     */
 30.1987 +    static <K,V> TreeMap.Entry<K,V> successor(Entry<K,V> t) {
 30.1988 +        if (t == null)
 30.1989 +            return null;
 30.1990 +        else if (t.right != null) {
 30.1991 +            Entry<K,V> p = t.right;
 30.1992 +            while (p.left != null)
 30.1993 +                p = p.left;
 30.1994 +            return p;
 30.1995 +        } else {
 30.1996 +            Entry<K,V> p = t.parent;
 30.1997 +            Entry<K,V> ch = t;
 30.1998 +            while (p != null && ch == p.right) {
 30.1999 +                ch = p;
 30.2000 +                p = p.parent;
 30.2001 +            }
 30.2002 +            return p;
 30.2003 +        }
 30.2004 +    }
 30.2005 +
 30.2006 +    /**
 30.2007 +     * Returns the predecessor of the specified Entry, or null if no such.
 30.2008 +     */
 30.2009 +    static <K,V> Entry<K,V> predecessor(Entry<K,V> t) {
 30.2010 +        if (t == null)
 30.2011 +            return null;
 30.2012 +        else if (t.left != null) {
 30.2013 +            Entry<K,V> p = t.left;
 30.2014 +            while (p.right != null)
 30.2015 +                p = p.right;
 30.2016 +            return p;
 30.2017 +        } else {
 30.2018 +            Entry<K,V> p = t.parent;
 30.2019 +            Entry<K,V> ch = t;
 30.2020 +            while (p != null && ch == p.left) {
 30.2021 +                ch = p;
 30.2022 +                p = p.parent;
 30.2023 +            }
 30.2024 +            return p;
 30.2025 +        }
 30.2026 +    }
 30.2027 +
 30.2028 +    /**
 30.2029 +     * Balancing operations.
 30.2030 +     *
 30.2031 +     * Implementations of rebalancings during insertion and deletion are
 30.2032 +     * slightly different than the CLR version.  Rather than using dummy
 30.2033 +     * nilnodes, we use a set of accessors that deal properly with null.  They
 30.2034 +     * are used to avoid messiness surrounding nullness checks in the main
 30.2035 +     * algorithms.
 30.2036 +     */
 30.2037 +
 30.2038 +    private static <K,V> boolean colorOf(Entry<K,V> p) {
 30.2039 +        return (p == null ? BLACK : p.color);
 30.2040 +    }
 30.2041 +
 30.2042 +    private static <K,V> Entry<K,V> parentOf(Entry<K,V> p) {
 30.2043 +        return (p == null ? null: p.parent);
 30.2044 +    }
 30.2045 +
 30.2046 +    private static <K,V> void setColor(Entry<K,V> p, boolean c) {
 30.2047 +        if (p != null)
 30.2048 +            p.color = c;
 30.2049 +    }
 30.2050 +
 30.2051 +    private static <K,V> Entry<K,V> leftOf(Entry<K,V> p) {
 30.2052 +        return (p == null) ? null: p.left;
 30.2053 +    }
 30.2054 +
 30.2055 +    private static <K,V> Entry<K,V> rightOf(Entry<K,V> p) {
 30.2056 +        return (p == null) ? null: p.right;
 30.2057 +    }
 30.2058 +
 30.2059 +    /** From CLR */
 30.2060 +    private void rotateLeft(Entry<K,V> p) {
 30.2061 +        if (p != null) {
 30.2062 +            Entry<K,V> r = p.right;
 30.2063 +            p.right = r.left;
 30.2064 +            if (r.left != null)
 30.2065 +                r.left.parent = p;
 30.2066 +            r.parent = p.parent;
 30.2067 +            if (p.parent == null)
 30.2068 +                root = r;
 30.2069 +            else if (p.parent.left == p)
 30.2070 +                p.parent.left = r;
 30.2071 +            else
 30.2072 +                p.parent.right = r;
 30.2073 +            r.left = p;
 30.2074 +            p.parent = r;
 30.2075 +        }
 30.2076 +    }
 30.2077 +
 30.2078 +    /** From CLR */
 30.2079 +    private void rotateRight(Entry<K,V> p) {
 30.2080 +        if (p != null) {
 30.2081 +            Entry<K,V> l = p.left;
 30.2082 +            p.left = l.right;
 30.2083 +            if (l.right != null) l.right.parent = p;
 30.2084 +            l.parent = p.parent;
 30.2085 +            if (p.parent == null)
 30.2086 +                root = l;
 30.2087 +            else if (p.parent.right == p)
 30.2088 +                p.parent.right = l;
 30.2089 +            else p.parent.left = l;
 30.2090 +            l.right = p;
 30.2091 +            p.parent = l;
 30.2092 +        }
 30.2093 +    }
 30.2094 +
 30.2095 +    /** From CLR */
 30.2096 +    private void fixAfterInsertion(Entry<K,V> x) {
 30.2097 +        x.color = RED;
 30.2098 +
 30.2099 +        while (x != null && x != root && x.parent.color == RED) {
 30.2100 +            if (parentOf(x) == leftOf(parentOf(parentOf(x)))) {
 30.2101 +                Entry<K,V> y = rightOf(parentOf(parentOf(x)));
 30.2102 +                if (colorOf(y) == RED) {
 30.2103 +                    setColor(parentOf(x), BLACK);
 30.2104 +                    setColor(y, BLACK);
 30.2105 +                    setColor(parentOf(parentOf(x)), RED);
 30.2106 +                    x = parentOf(parentOf(x));
 30.2107 +                } else {
 30.2108 +                    if (x == rightOf(parentOf(x))) {
 30.2109 +                        x = parentOf(x);
 30.2110 +                        rotateLeft(x);
 30.2111 +                    }
 30.2112 +                    setColor(parentOf(x), BLACK);
 30.2113 +                    setColor(parentOf(parentOf(x)), RED);
 30.2114 +                    rotateRight(parentOf(parentOf(x)));
 30.2115 +                }
 30.2116 +            } else {
 30.2117 +                Entry<K,V> y = leftOf(parentOf(parentOf(x)));
 30.2118 +                if (colorOf(y) == RED) {
 30.2119 +                    setColor(parentOf(x), BLACK);
 30.2120 +                    setColor(y, BLACK);
 30.2121 +                    setColor(parentOf(parentOf(x)), RED);
 30.2122 +                    x = parentOf(parentOf(x));
 30.2123 +                } else {
 30.2124 +                    if (x == leftOf(parentOf(x))) {
 30.2125 +                        x = parentOf(x);
 30.2126 +                        rotateRight(x);
 30.2127 +                    }
 30.2128 +                    setColor(parentOf(x), BLACK);
 30.2129 +                    setColor(parentOf(parentOf(x)), RED);
 30.2130 +                    rotateLeft(parentOf(parentOf(x)));
 30.2131 +                }
 30.2132 +            }
 30.2133 +        }
 30.2134 +        root.color = BLACK;
 30.2135 +    }
 30.2136 +
 30.2137 +    /**
 30.2138 +     * Delete node p, and then rebalance the tree.
 30.2139 +     */
 30.2140 +    private void deleteEntry(Entry<K,V> p) {
 30.2141 +        modCount++;
 30.2142 +        size--;
 30.2143 +
 30.2144 +        // If strictly internal, copy successor's element to p and then make p
 30.2145 +        // point to successor.
 30.2146 +        if (p.left != null && p.right != null) {
 30.2147 +            Entry<K,V> s = successor(p);
 30.2148 +            p.key = s.key;
 30.2149 +            p.value = s.value;
 30.2150 +            p = s;
 30.2151 +        } // p has 2 children
 30.2152 +
 30.2153 +        // Start fixup at replacement node, if it exists.
 30.2154 +        Entry<K,V> replacement = (p.left != null ? p.left : p.right);
 30.2155 +
 30.2156 +        if (replacement != null) {
 30.2157 +            // Link replacement to parent
 30.2158 +            replacement.parent = p.parent;
 30.2159 +            if (p.parent == null)
 30.2160 +                root = replacement;
 30.2161 +            else if (p == p.parent.left)
 30.2162 +                p.parent.left  = replacement;
 30.2163 +            else
 30.2164 +                p.parent.right = replacement;
 30.2165 +
 30.2166 +            // Null out links so they are OK to use by fixAfterDeletion.
 30.2167 +            p.left = p.right = p.parent = null;
 30.2168 +
 30.2169 +            // Fix replacement
 30.2170 +            if (p.color == BLACK)
 30.2171 +                fixAfterDeletion(replacement);
 30.2172 +        } else if (p.parent == null) { // return if we are the only node.
 30.2173 +            root = null;
 30.2174 +        } else { //  No children. Use self as phantom replacement and unlink.
 30.2175 +            if (p.color == BLACK)
 30.2176 +                fixAfterDeletion(p);
 30.2177 +
 30.2178 +            if (p.parent != null) {
 30.2179 +                if (p == p.parent.left)
 30.2180 +                    p.parent.left = null;
 30.2181 +                else if (p == p.parent.right)
 30.2182 +                    p.parent.right = null;
 30.2183 +                p.parent = null;
 30.2184 +            }
 30.2185 +        }
 30.2186 +    }
 30.2187 +
 30.2188 +    /** From CLR */
 30.2189 +    private void fixAfterDeletion(Entry<K,V> x) {
 30.2190 +        while (x != root && colorOf(x) == BLACK) {
 30.2191 +            if (x == leftOf(parentOf(x))) {
 30.2192 +                Entry<K,V> sib = rightOf(parentOf(x));
 30.2193 +
 30.2194 +                if (colorOf(sib) == RED) {
 30.2195 +                    setColor(sib, BLACK);
 30.2196 +                    setColor(parentOf(x), RED);
 30.2197 +                    rotateLeft(parentOf(x));
 30.2198 +                    sib = rightOf(parentOf(x));
 30.2199 +                }
 30.2200 +
 30.2201 +                if (colorOf(leftOf(sib))  == BLACK &&
 30.2202 +                    colorOf(rightOf(sib)) == BLACK) {
 30.2203 +                    setColor(sib, RED);
 30.2204 +                    x = parentOf(x);
 30.2205 +                } else {
 30.2206 +                    if (colorOf(rightOf(sib)) == BLACK) {
 30.2207 +                        setColor(leftOf(sib), BLACK);
 30.2208 +                        setColor(sib, RED);
 30.2209 +                        rotateRight(sib);
 30.2210 +                        sib = rightOf(parentOf(x));
 30.2211 +                    }
 30.2212 +                    setColor(sib, colorOf(parentOf(x)));
 30.2213 +                    setColor(parentOf(x), BLACK);
 30.2214 +                    setColor(rightOf(sib), BLACK);
 30.2215 +                    rotateLeft(parentOf(x));
 30.2216 +                    x = root;
 30.2217 +                }
 30.2218 +            } else { // symmetric
 30.2219 +                Entry<K,V> sib = leftOf(parentOf(x));
 30.2220 +
 30.2221 +                if (colorOf(sib) == RED) {
 30.2222 +                    setColor(sib, BLACK);
 30.2223 +                    setColor(parentOf(x), RED);
 30.2224 +                    rotateRight(parentOf(x));
 30.2225 +                    sib = leftOf(parentOf(x));
 30.2226 +                }
 30.2227 +
 30.2228 +                if (colorOf(rightOf(sib)) == BLACK &&
 30.2229 +                    colorOf(leftOf(sib)) == BLACK) {
 30.2230 +                    setColor(sib, RED);
 30.2231 +                    x = parentOf(x);
 30.2232 +                } else {
 30.2233 +                    if (colorOf(leftOf(sib)) == BLACK) {
 30.2234 +                        setColor(rightOf(sib), BLACK);
 30.2235 +                        setColor(sib, RED);
 30.2236 +                        rotateLeft(sib);
 30.2237 +                        sib = leftOf(parentOf(x));
 30.2238 +                    }
 30.2239 +                    setColor(sib, colorOf(parentOf(x)));
 30.2240 +                    setColor(parentOf(x), BLACK);
 30.2241 +                    setColor(leftOf(sib), BLACK);
 30.2242 +                    rotateRight(parentOf(x));
 30.2243 +                    x = root;
 30.2244 +                }
 30.2245 +            }
 30.2246 +        }
 30.2247 +
 30.2248 +        setColor(x, BLACK);
 30.2249 +    }
 30.2250 +
 30.2251 +    private static final long serialVersionUID = 919286545866124006L;
 30.2252 +
 30.2253 +    /**
 30.2254 +     * Save the state of the {@code TreeMap} instance to a stream (i.e.,
 30.2255 +     * serialize it).
 30.2256 +     *
 30.2257 +     * @serialData The <em>size</em> of the TreeMap (the number of key-value
 30.2258 +     *             mappings) is emitted (int), followed by the key (Object)
 30.2259 +     *             and value (Object) for each key-value mapping represented
 30.2260 +     *             by the TreeMap. The key-value mappings are emitted in
 30.2261 +     *             key-order (as determined by the TreeMap's Comparator,
 30.2262 +     *             or by the keys' natural ordering if the TreeMap has no
 30.2263 +     *             Comparator).
 30.2264 +     */
 30.2265 +    private void writeObject(java.io.ObjectOutputStream s)
 30.2266 +        throws java.io.IOException {
 30.2267 +        // Write out the Comparator and any hidden stuff
 30.2268 +        s.defaultWriteObject();
 30.2269 +
 30.2270 +        // Write out size (number of Mappings)
 30.2271 +        s.writeInt(size);
 30.2272 +
 30.2273 +        // Write out keys and values (alternating)
 30.2274 +        for (Iterator<Map.Entry<K,V>> i = entrySet().iterator(); i.hasNext(); ) {
 30.2275 +            Map.Entry<K,V> e = i.next();
 30.2276 +            s.writeObject(e.getKey());
 30.2277 +            s.writeObject(e.getValue());
 30.2278 +        }
 30.2279 +    }
 30.2280 +
 30.2281 +    /**
 30.2282 +     * Reconstitute the {@code TreeMap} instance from a stream (i.e.,
 30.2283 +     * deserialize it).
 30.2284 +     */
 30.2285 +    private void readObject(final java.io.ObjectInputStream s)
 30.2286 +        throws java.io.IOException, ClassNotFoundException {
 30.2287 +        // Read in the Comparator and any hidden stuff
 30.2288 +        s.defaultReadObject();
 30.2289 +
 30.2290 +        // Read in size
 30.2291 +        int size = s.readInt();
 30.2292 +
 30.2293 +        buildFromSorted(size, null, s, null);
 30.2294 +    }
 30.2295 +
 30.2296 +    /** Intended to be called only from TreeSet.readObject */
 30.2297 +    void readTreeSet(int size, java.io.ObjectInputStream s, V defaultVal)
 30.2298 +        throws java.io.IOException, ClassNotFoundException {
 30.2299 +        buildFromSorted(size, null, s, defaultVal);
 30.2300 +    }
 30.2301 +
 30.2302 +    /** Intended to be called only from TreeSet.addAll */
 30.2303 +    void addAllForTreeSet(SortedSet<? extends K> set, V defaultVal) {
 30.2304 +        try {
 30.2305 +            buildFromSorted(set.size(), set.iterator(), null, defaultVal);
 30.2306 +        } catch (java.io.IOException cannotHappen) {
 30.2307 +        } catch (ClassNotFoundException cannotHappen) {
 30.2308 +        }
 30.2309 +    }
 30.2310 +
 30.2311 +
 30.2312 +    /**
 30.2313 +     * Linear time tree building algorithm from sorted data.  Can accept keys
 30.2314 +     * and/or values from iterator or stream. This leads to too many
 30.2315 +     * parameters, but seems better than alternatives.  The four formats
 30.2316 +     * that this method accepts are:
 30.2317 +     *
 30.2318 +     *    1) An iterator of Map.Entries.  (it != null, defaultVal == null).
 30.2319 +     *    2) An iterator of keys.         (it != null, defaultVal != null).
 30.2320 +     *    3) A stream of alternating serialized keys and values.
 30.2321 +     *                                   (it == null, defaultVal == null).
 30.2322 +     *    4) A stream of serialized keys. (it == null, defaultVal != null).
 30.2323 +     *
 30.2324 +     * It is assumed that the comparator of the TreeMap is already set prior
 30.2325 +     * to calling this method.
 30.2326 +     *
 30.2327 +     * @param size the number of keys (or key-value pairs) to be read from
 30.2328 +     *        the iterator or stream
 30.2329 +     * @param it If non-null, new entries are created from entries
 30.2330 +     *        or keys read from this iterator.
 30.2331 +     * @param str If non-null, new entries are created from keys and
 30.2332 +     *        possibly values read from this stream in serialized form.
 30.2333 +     *        Exactly one of it and str should be non-null.
 30.2334 +     * @param defaultVal if non-null, this default value is used for
 30.2335 +     *        each value in the map.  If null, each value is read from
 30.2336 +     *        iterator or stream, as described above.
 30.2337 +     * @throws IOException propagated from stream reads. This cannot
 30.2338 +     *         occur if str is null.
 30.2339 +     * @throws ClassNotFoundException propagated from readObject.
 30.2340 +     *         This cannot occur if str is null.
 30.2341 +     */
 30.2342 +    private void buildFromSorted(int size, Iterator it,
 30.2343 +                                 java.io.ObjectInputStream str,
 30.2344 +                                 V defaultVal)
 30.2345 +        throws  java.io.IOException, ClassNotFoundException {
 30.2346 +        this.size = size;
 30.2347 +        root = buildFromSorted(0, 0, size-1, computeRedLevel(size),
 30.2348 +                               it, str, defaultVal);
 30.2349 +    }
 30.2350 +
 30.2351 +    /**
 30.2352 +     * Recursive "helper method" that does the real work of the
 30.2353 +     * previous method.  Identically named parameters have
 30.2354 +     * identical definitions.  Additional parameters are documented below.
 30.2355 +     * It is assumed that the comparator and size fields of the TreeMap are
 30.2356 +     * already set prior to calling this method.  (It ignores both fields.)
 30.2357 +     *
 30.2358 +     * @param level the current level of tree. Initial call should be 0.
 30.2359 +     * @param lo the first element index of this subtree. Initial should be 0.
 30.2360 +     * @param hi the last element index of this subtree.  Initial should be
 30.2361 +     *        size-1.
 30.2362 +     * @param redLevel the level at which nodes should be red.
 30.2363 +     *        Must be equal to computeRedLevel for tree of this size.
 30.2364 +     */
 30.2365 +    private final Entry<K,V> buildFromSorted(int level, int lo, int hi,
 30.2366 +                                             int redLevel,
 30.2367 +                                             Iterator it,
 30.2368 +                                             java.io.ObjectInputStream str,
 30.2369 +                                             V defaultVal)
 30.2370 +        throws  java.io.IOException, ClassNotFoundException {
 30.2371 +        /*
 30.2372 +         * Strategy: The root is the middlemost element. To get to it, we
 30.2373 +         * have to first recursively construct the entire left subtree,
 30.2374 +         * so as to grab all of its elements. We can then proceed with right
 30.2375 +         * subtree.
 30.2376 +         *
 30.2377 +         * The lo and hi arguments are the minimum and maximum
 30.2378 +         * indices to pull out of the iterator or stream for current subtree.
 30.2379 +         * They are not actually indexed, we just proceed sequentially,
 30.2380 +         * ensuring that items are extracted in corresponding order.
 30.2381 +         */
 30.2382 +
 30.2383 +        if (hi < lo) return null;
 30.2384 +
 30.2385 +        int mid = (lo + hi) >>> 1;
 30.2386 +
 30.2387 +        Entry<K,V> left  = null;
 30.2388 +        if (lo < mid)
 30.2389 +            left = buildFromSorted(level+1, lo, mid - 1, redLevel,
 30.2390 +                                   it, str, defaultVal);
 30.2391 +
 30.2392 +        // extract key and/or value from iterator or stream
 30.2393 +        K key;
 30.2394 +        V value;
 30.2395 +        if (it != null) {
 30.2396 +            if (defaultVal==null) {
 30.2397 +                Map.Entry<K,V> entry = (Map.Entry<K,V>)it.next();
 30.2398 +                key = entry.getKey();
 30.2399 +                value = entry.getValue();
 30.2400 +            } else {
 30.2401 +                key = (K)it.next();
 30.2402 +                value = defaultVal;
 30.2403 +            }
 30.2404 +        } else { // use stream
 30.2405 +            key = (K) str.readObject();
 30.2406 +            value = (defaultVal != null ? defaultVal : (V) str.readObject());
 30.2407 +        }
 30.2408 +
 30.2409 +        Entry<K,V> middle =  new Entry<>(key, value, null);
 30.2410 +
 30.2411 +        // color nodes in non-full bottommost level red
 30.2412 +        if (level == redLevel)
 30.2413 +            middle.color = RED;
 30.2414 +
 30.2415 +        if (left != null) {
 30.2416 +            middle.left = left;
 30.2417 +            left.parent = middle;
 30.2418 +        }
 30.2419 +
 30.2420 +        if (mid < hi) {
 30.2421 +            Entry<K,V> right = buildFromSorted(level+1, mid+1, hi, redLevel,
 30.2422 +                                               it, str, defaultVal);
 30.2423 +            middle.right = right;
 30.2424 +            right.parent = middle;
 30.2425 +        }
 30.2426 +
 30.2427 +        return middle;
 30.2428 +    }
 30.2429 +
 30.2430 +    /**
 30.2431 +     * Find the level down to which to assign all nodes BLACK.  This is the
 30.2432 +     * last `full' level of the complete binary tree produced by
 30.2433 +     * buildTree. The remaining nodes are colored RED. (This makes a `nice'
 30.2434 +     * set of color assignments wrt future insertions.) This level number is
 30.2435 +     * computed by finding the number of splits needed to reach the zeroeth
 30.2436 +     * node.  (The answer is ~lg(N), but in any case must be computed by same
 30.2437 +     * quick O(lg(N)) loop.)
 30.2438 +     */
 30.2439 +    private static int computeRedLevel(int sz) {
 30.2440 +        int level = 0;
 30.2441 +        for (int m = sz - 1; m >= 0; m = m / 2 - 1)
 30.2442 +            level++;
 30.2443 +        return level;
 30.2444 +    }
 30.2445 +}
    31.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    31.2 +++ b/rt/emul/compact/src/main/java/java/util/TreeSet.java	Sun Sep 08 11:22:51 2013 +0200
    31.3 @@ -0,0 +1,539 @@
    31.4 +/*
    31.5 + * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
    31.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    31.7 + *
    31.8 + * This code is free software; you can redistribute it and/or modify it
    31.9 + * under the terms of the GNU General Public License version 2 only, as
   31.10 + * published by the Free Software Foundation.  Oracle designates this
   31.11 + * particular file as subject to the "Classpath" exception as provided
   31.12 + * by Oracle in the LICENSE file that accompanied this code.
   31.13 + *
   31.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   31.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   31.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   31.17 + * version 2 for more details (a copy is included in the LICENSE file that
   31.18 + * accompanied this code).
   31.19 + *
   31.20 + * You should have received a copy of the GNU General Public License version
   31.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   31.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   31.23 + *
   31.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   31.25 + * or visit www.oracle.com if you need additional information or have any
   31.26 + * questions.
   31.27 + */
   31.28 +
   31.29 +package java.util;
   31.30 +
   31.31 +/**
   31.32 + * A {@link NavigableSet} implementation based on a {@link TreeMap}.
   31.33 + * The elements are ordered using their {@linkplain Comparable natural
   31.34 + * ordering}, or by a {@link Comparator} provided at set creation
   31.35 + * time, depending on which constructor is used.
   31.36 + *
   31.37 + * <p>This implementation provides guaranteed log(n) time cost for the basic
   31.38 + * operations ({@code add}, {@code remove} and {@code contains}).
   31.39 + *
   31.40 + * <p>Note that the ordering maintained by a set (whether or not an explicit
   31.41 + * comparator is provided) must be <i>consistent with equals</i> if it is to
   31.42 + * correctly implement the {@code Set} interface.  (See {@code Comparable}
   31.43 + * or {@code Comparator} for a precise definition of <i>consistent with
   31.44 + * equals</i>.)  This is so because the {@code Set} interface is defined in
   31.45 + * terms of the {@code equals} operation, but a {@code TreeSet} instance
   31.46 + * performs all element comparisons using its {@code compareTo} (or
   31.47 + * {@code compare}) method, so two elements that are deemed equal by this method
   31.48 + * are, from the standpoint of the set, equal.  The behavior of a set
   31.49 + * <i>is</i> well-defined even if its ordering is inconsistent with equals; it
   31.50 + * just fails to obey the general contract of the {@code Set} interface.
   31.51 + *
   31.52 + * <p><strong>Note that this implementation is not synchronized.</strong>
   31.53 + * If multiple threads access a tree set concurrently, and at least one
   31.54 + * of the threads modifies the set, it <i>must</i> be synchronized
   31.55 + * externally.  This is typically accomplished by synchronizing on some
   31.56 + * object that naturally encapsulates the set.
   31.57 + * If no such object exists, the set should be "wrapped" using the
   31.58 + * {@link Collections#synchronizedSortedSet Collections.synchronizedSortedSet}
   31.59 + * method.  This is best done at creation time, to prevent accidental
   31.60 + * unsynchronized access to the set: <pre>
   31.61 + *   SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));</pre>
   31.62 + *
   31.63 + * <p>The iterators returned by this class's {@code iterator} method are
   31.64 + * <i>fail-fast</i>: if the set is modified at any time after the iterator is
   31.65 + * created, in any way except through the iterator's own {@code remove}
   31.66 + * method, the iterator will throw a {@link ConcurrentModificationException}.
   31.67 + * Thus, in the face of concurrent modification, the iterator fails quickly
   31.68 + * and cleanly, rather than risking arbitrary, non-deterministic behavior at
   31.69 + * an undetermined time in the future.
   31.70 + *
   31.71 + * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
   31.72 + * as it is, generally speaking, impossible to make any hard guarantees in the
   31.73 + * presence of unsynchronized concurrent modification.  Fail-fast iterators
   31.74 + * throw {@code ConcurrentModificationException} on a best-effort basis.
   31.75 + * Therefore, it would be wrong to write a program that depended on this
   31.76 + * exception for its correctness:   <i>the fail-fast behavior of iterators
   31.77 + * should be used only to detect bugs.</i>
   31.78 + *
   31.79 + * <p>This class is a member of the
   31.80 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
   31.81 + * Java Collections Framework</a>.
   31.82 + *
   31.83 + * @param <E> the type of elements maintained by this set
   31.84 + *
   31.85 + * @author  Josh Bloch
   31.86 + * @see     Collection
   31.87 + * @see     Set
   31.88 + * @see     HashSet
   31.89 + * @see     Comparable
   31.90 + * @see     Comparator
   31.91 + * @see     TreeMap
   31.92 + * @since   1.2
   31.93 + */
   31.94 +
   31.95 +public class TreeSet<E> extends AbstractSet<E>
   31.96 +    implements NavigableSet<E>, Cloneable, java.io.Serializable
   31.97 +{
   31.98 +    /**
   31.99 +     * The backing map.
  31.100 +     */
  31.101 +    private transient NavigableMap<E,Object> m;
  31.102 +
  31.103 +    // Dummy value to associate with an Object in the backing Map
  31.104 +    private static final Object PRESENT = new Object();
  31.105 +
  31.106 +    /**
  31.107 +     * Constructs a set backed by the specified navigable map.
  31.108 +     */
  31.109 +    TreeSet(NavigableMap<E,Object> m) {
  31.110 +        this.m = m;
  31.111 +    }
  31.112 +
  31.113 +    /**
  31.114 +     * Constructs a new, empty tree set, sorted according to the
  31.115 +     * natural ordering of its elements.  All elements inserted into
  31.116 +     * the set must implement the {@link Comparable} interface.
  31.117 +     * Furthermore, all such elements must be <i>mutually
  31.118 +     * comparable</i>: {@code e1.compareTo(e2)} must not throw a
  31.119 +     * {@code ClassCastException} for any elements {@code e1} and
  31.120 +     * {@code e2} in the set.  If the user attempts to add an element
  31.121 +     * to the set that violates this constraint (for example, the user
  31.122 +     * attempts to add a string element to a set whose elements are
  31.123 +     * integers), the {@code add} call will throw a
  31.124 +     * {@code ClassCastException}.
  31.125 +     */
  31.126 +    public TreeSet() {
  31.127 +        this(new TreeMap<E,Object>());
  31.128 +    }
  31.129 +
  31.130 +    /**
  31.131 +     * Constructs a new, empty tree set, sorted according to the specified
  31.132 +     * comparator.  All elements inserted into the set must be <i>mutually
  31.133 +     * comparable</i> by the specified comparator: {@code comparator.compare(e1,
  31.134 +     * e2)} must not throw a {@code ClassCastException} for any elements
  31.135 +     * {@code e1} and {@code e2} in the set.  If the user attempts to add
  31.136 +     * an element to the set that violates this constraint, the
  31.137 +     * {@code add} call will throw a {@code ClassCastException}.
  31.138 +     *
  31.139 +     * @param comparator the comparator that will be used to order this set.
  31.140 +     *        If {@code null}, the {@linkplain Comparable natural
  31.141 +     *        ordering} of the elements will be used.
  31.142 +     */
  31.143 +    public TreeSet(Comparator<? super E> comparator) {
  31.144 +        this(new TreeMap<>(comparator));
  31.145 +    }
  31.146 +
  31.147 +    /**
  31.148 +     * Constructs a new tree set containing the elements in the specified
  31.149 +     * collection, sorted according to the <i>natural ordering</i> of its
  31.150 +     * elements.  All elements inserted into the set must implement the
  31.151 +     * {@link Comparable} interface.  Furthermore, all such elements must be
  31.152 +     * <i>mutually comparable</i>: {@code e1.compareTo(e2)} must not throw a
  31.153 +     * {@code ClassCastException} for any elements {@code e1} and
  31.154 +     * {@code e2} in the set.
  31.155 +     *
  31.156 +     * @param c collection whose elements will comprise the new set
  31.157 +     * @throws ClassCastException if the elements in {@code c} are
  31.158 +     *         not {@link Comparable}, or are not mutually comparable
  31.159 +     * @throws NullPointerException if the specified collection is null
  31.160 +     */
  31.161 +    public TreeSet(Collection<? extends E> c) {
  31.162 +        this();
  31.163 +        addAll(c);
  31.164 +    }
  31.165 +
  31.166 +    /**
  31.167 +     * Constructs a new tree set containing the same elements and
  31.168 +     * using the same ordering as the specified sorted set.
  31.169 +     *
  31.170 +     * @param s sorted set whose elements will comprise the new set
  31.171 +     * @throws NullPointerException if the specified sorted set is null
  31.172 +     */
  31.173 +    public TreeSet(SortedSet<E> s) {
  31.174 +        this(s.comparator());
  31.175 +        addAll(s);
  31.176 +    }
  31.177 +
  31.178 +    /**
  31.179 +     * Returns an iterator over the elements in this set in ascending order.
  31.180 +     *
  31.181 +     * @return an iterator over the elements in this set in ascending order
  31.182 +     */
  31.183 +    public Iterator<E> iterator() {
  31.184 +        return m.navigableKeySet().iterator();
  31.185 +    }
  31.186 +
  31.187 +    /**
  31.188 +     * Returns an iterator over the elements in this set in descending order.
  31.189 +     *
  31.190 +     * @return an iterator over the elements in this set in descending order
  31.191 +     * @since 1.6
  31.192 +     */
  31.193 +    public Iterator<E> descendingIterator() {
  31.194 +        return m.descendingKeySet().iterator();
  31.195 +    }
  31.196 +
  31.197 +    /**
  31.198 +     * @since 1.6
  31.199 +     */
  31.200 +    public NavigableSet<E> descendingSet() {
  31.201 +        return new TreeSet<>(m.descendingMap());
  31.202 +    }
  31.203 +
  31.204 +    /**
  31.205 +     * Returns the number of elements in this set (its cardinality).
  31.206 +     *
  31.207 +     * @return the number of elements in this set (its cardinality)
  31.208 +     */
  31.209 +    public int size() {
  31.210 +        return m.size();
  31.211 +    }
  31.212 +
  31.213 +    /**
  31.214 +     * Returns {@code true} if this set contains no elements.
  31.215 +     *
  31.216 +     * @return {@code true} if this set contains no elements
  31.217 +     */
  31.218 +    public boolean isEmpty() {
  31.219 +        return m.isEmpty();
  31.220 +    }
  31.221 +
  31.222 +    /**
  31.223 +     * Returns {@code true} if this set contains the specified element.
  31.224 +     * More formally, returns {@code true} if and only if this set
  31.225 +     * contains an element {@code e} such that
  31.226 +     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
  31.227 +     *
  31.228 +     * @param o object to be checked for containment in this set
  31.229 +     * @return {@code true} if this set contains the specified element
  31.230 +     * @throws ClassCastException if the specified object cannot be compared
  31.231 +     *         with the elements currently in the set
  31.232 +     * @throws NullPointerException if the specified element is null
  31.233 +     *         and this set uses natural ordering, or its comparator
  31.234 +     *         does not permit null elements
  31.235 +     */
  31.236 +    public boolean contains(Object o) {
  31.237 +        return m.containsKey(o);
  31.238 +    }
  31.239 +
  31.240 +    /**
  31.241 +     * Adds the specified element to this set if it is not already present.
  31.242 +     * More formally, adds the specified element {@code e} to this set if
  31.243 +     * the set contains no element {@code e2} such that
  31.244 +     * <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.
  31.245 +     * If this set already contains the element, the call leaves the set
  31.246 +     * unchanged and returns {@code false}.
  31.247 +     *
  31.248 +     * @param e element to be added to this set
  31.249 +     * @return {@code true} if this set did not already contain the specified
  31.250 +     *         element
  31.251 +     * @throws ClassCastException if the specified object cannot be compared
  31.252 +     *         with the elements currently in this set
  31.253 +     * @throws NullPointerException if the specified element is null
  31.254 +     *         and this set uses natural ordering, or its comparator
  31.255 +     *         does not permit null elements
  31.256 +     */
  31.257 +    public boolean add(E e) {
  31.258 +        return m.put(e, PRESENT)==null;
  31.259 +    }
  31.260 +
  31.261 +    /**
  31.262 +     * Removes the specified element from this set if it is present.
  31.263 +     * More formally, removes an element {@code e} such that
  31.264 +     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>,
  31.265 +     * if this set contains such an element.  Returns {@code true} if
  31.266 +     * this set contained the element (or equivalently, if this set
  31.267 +     * changed as a result of the call).  (This set will not contain the
  31.268 +     * element once the call returns.)
  31.269 +     *
  31.270 +     * @param o object to be removed from this set, if present
  31.271 +     * @return {@code true} if this set contained the specified element
  31.272 +     * @throws ClassCastException if the specified object cannot be compared
  31.273 +     *         with the elements currently in this set
  31.274 +     * @throws NullPointerException if the specified element is null
  31.275 +     *         and this set uses natural ordering, or its comparator
  31.276 +     *         does not permit null elements
  31.277 +     */
  31.278 +    public boolean remove(Object o) {
  31.279 +        return m.remove(o)==PRESENT;
  31.280 +    }
  31.281 +
  31.282 +    /**
  31.283 +     * Removes all of the elements from this set.
  31.284 +     * The set will be empty after this call returns.
  31.285 +     */
  31.286 +    public void clear() {
  31.287 +        m.clear();
  31.288 +    }
  31.289 +
  31.290 +    /**
  31.291 +     * Adds all of the elements in the specified collection to this set.
  31.292 +     *
  31.293 +     * @param c collection containing elements to be added to this set
  31.294 +     * @return {@code true} if this set changed as a result of the call
  31.295 +     * @throws ClassCastException if the elements provided cannot be compared
  31.296 +     *         with the elements currently in the set
  31.297 +     * @throws NullPointerException if the specified collection is null or
  31.298 +     *         if any element is null and this set uses natural ordering, or
  31.299 +     *         its comparator does not permit null elements
  31.300 +     */
  31.301 +    public  boolean addAll(Collection<? extends E> c) {
  31.302 +        // Use linear-time version if applicable
  31.303 +        if (m.size()==0 && c.size() > 0 &&
  31.304 +            c instanceof SortedSet &&
  31.305 +            m instanceof TreeMap) {
  31.306 +            SortedSet<? extends E> set = (SortedSet<? extends E>) c;
  31.307 +            TreeMap<E,Object> map = (TreeMap<E, Object>) m;
  31.308 +            Comparator<? super E> cc = (Comparator<? super E>) set.comparator();
  31.309 +            Comparator<? super E> mc = map.comparator();
  31.310 +            if (cc==mc || (cc != null && cc.equals(mc))) {
  31.311 +                map.addAllForTreeSet(set, PRESENT);
  31.312 +                return true;
  31.313 +            }
  31.314 +        }
  31.315 +        return super.addAll(c);
  31.316 +    }
  31.317 +
  31.318 +    /**
  31.319 +     * @throws ClassCastException {@inheritDoc}
  31.320 +     * @throws NullPointerException if {@code fromElement} or {@code toElement}
  31.321 +     *         is null and this set uses natural ordering, or its comparator
  31.322 +     *         does not permit null elements
  31.323 +     * @throws IllegalArgumentException {@inheritDoc}
  31.324 +     * @since 1.6
  31.325 +     */
  31.326 +    public NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
  31.327 +                                  E toElement,   boolean toInclusive) {
  31.328 +        return new TreeSet<>(m.subMap(fromElement, fromInclusive,
  31.329 +                                       toElement,   toInclusive));
  31.330 +    }
  31.331 +
  31.332 +    /**
  31.333 +     * @throws ClassCastException {@inheritDoc}
  31.334 +     * @throws NullPointerException if {@code toElement} is null and
  31.335 +     *         this set uses natural ordering, or its comparator does
  31.336 +     *         not permit null elements
  31.337 +     * @throws IllegalArgumentException {@inheritDoc}
  31.338 +     * @since 1.6
  31.339 +     */
  31.340 +    public NavigableSet<E> headSet(E toElement, boolean inclusive) {
  31.341 +        return new TreeSet<>(m.headMap(toElement, inclusive));
  31.342 +    }
  31.343 +
  31.344 +    /**
  31.345 +     * @throws ClassCastException {@inheritDoc}
  31.346 +     * @throws NullPointerException if {@code fromElement} is null and
  31.347 +     *         this set uses natural ordering, or its comparator does
  31.348 +     *         not permit null elements
  31.349 +     * @throws IllegalArgumentException {@inheritDoc}
  31.350 +     * @since 1.6
  31.351 +     */
  31.352 +    public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
  31.353 +        return new TreeSet<>(m.tailMap(fromElement, inclusive));
  31.354 +    }
  31.355 +
  31.356 +    /**
  31.357 +     * @throws ClassCastException {@inheritDoc}
  31.358 +     * @throws NullPointerException if {@code fromElement} or
  31.359 +     *         {@code toElement} is null and this set uses natural ordering,
  31.360 +     *         or its comparator does not permit null elements
  31.361 +     * @throws IllegalArgumentException {@inheritDoc}
  31.362 +     */
  31.363 +    public SortedSet<E> subSet(E fromElement, E toElement) {
  31.364 +        return subSet(fromElement, true, toElement, false);
  31.365 +    }
  31.366 +
  31.367 +    /**
  31.368 +     * @throws ClassCastException {@inheritDoc}
  31.369 +     * @throws NullPointerException if {@code toElement} is null
  31.370 +     *         and this set uses natural ordering, or its comparator does
  31.371 +     *         not permit null elements
  31.372 +     * @throws IllegalArgumentException {@inheritDoc}
  31.373 +     */
  31.374 +    public SortedSet<E> headSet(E toElement) {
  31.375 +        return headSet(toElement, false);
  31.376 +    }
  31.377 +
  31.378 +    /**
  31.379 +     * @throws ClassCastException {@inheritDoc}
  31.380 +     * @throws NullPointerException if {@code fromElement} is null
  31.381 +     *         and this set uses natural ordering, or its comparator does
  31.382 +     *         not permit null elements
  31.383 +     * @throws IllegalArgumentException {@inheritDoc}
  31.384 +     */
  31.385 +    public SortedSet<E> tailSet(E fromElement) {
  31.386 +        return tailSet(fromElement, true);
  31.387 +    }
  31.388 +
  31.389 +    public Comparator<? super E> comparator() {
  31.390 +        return m.comparator();
  31.391 +    }
  31.392 +
  31.393 +    /**
  31.394 +     * @throws NoSuchElementException {@inheritDoc}
  31.395 +     */
  31.396 +    public E first() {
  31.397 +        return m.firstKey();
  31.398 +    }
  31.399 +
  31.400 +    /**
  31.401 +     * @throws NoSuchElementException {@inheritDoc}
  31.402 +     */
  31.403 +    public E last() {
  31.404 +        return m.lastKey();
  31.405 +    }
  31.406 +
  31.407 +    // NavigableSet API methods
  31.408 +
  31.409 +    /**
  31.410 +     * @throws ClassCastException {@inheritDoc}
  31.411 +     * @throws NullPointerException if the specified element is null
  31.412 +     *         and this set uses natural ordering, or its comparator
  31.413 +     *         does not permit null elements
  31.414 +     * @since 1.6
  31.415 +     */
  31.416 +    public E lower(E e) {
  31.417 +        return m.lowerKey(e);
  31.418 +    }
  31.419 +
  31.420 +    /**
  31.421 +     * @throws ClassCastException {@inheritDoc}
  31.422 +     * @throws NullPointerException if the specified element is null
  31.423 +     *         and this set uses natural ordering, or its comparator
  31.424 +     *         does not permit null elements
  31.425 +     * @since 1.6
  31.426 +     */
  31.427 +    public E floor(E e) {
  31.428 +        return m.floorKey(e);
  31.429 +    }
  31.430 +
  31.431 +    /**
  31.432 +     * @throws ClassCastException {@inheritDoc}
  31.433 +     * @throws NullPointerException if the specified element is null
  31.434 +     *         and this set uses natural ordering, or its comparator
  31.435 +     *         does not permit null elements
  31.436 +     * @since 1.6
  31.437 +     */
  31.438 +    public E ceiling(E e) {
  31.439 +        return m.ceilingKey(e);
  31.440 +    }
  31.441 +
  31.442 +    /**
  31.443 +     * @throws ClassCastException {@inheritDoc}
  31.444 +     * @throws NullPointerException if the specified element is null
  31.445 +     *         and this set uses natural ordering, or its comparator
  31.446 +     *         does not permit null elements
  31.447 +     * @since 1.6
  31.448 +     */
  31.449 +    public E higher(E e) {
  31.450 +        return m.higherKey(e);
  31.451 +    }
  31.452 +
  31.453 +    /**
  31.454 +     * @since 1.6
  31.455 +     */
  31.456 +    public E pollFirst() {
  31.457 +        Map.Entry<E,?> e = m.pollFirstEntry();
  31.458 +        return (e == null) ? null : e.getKey();
  31.459 +    }
  31.460 +
  31.461 +    /**
  31.462 +     * @since 1.6
  31.463 +     */
  31.464 +    public E pollLast() {
  31.465 +        Map.Entry<E,?> e = m.pollLastEntry();
  31.466 +        return (e == null) ? null : e.getKey();
  31.467 +    }
  31.468 +
  31.469 +    /**
  31.470 +     * Returns a shallow copy of this {@code TreeSet} instance. (The elements
  31.471 +     * themselves are not cloned.)
  31.472 +     *
  31.473 +     * @return a shallow copy of this set
  31.474 +     */
  31.475 +    public Object clone() {
  31.476 +        TreeSet<E> clone = null;
  31.477 +        try {
  31.478 +            clone = (TreeSet<E>) super.clone();
  31.479 +        } catch (CloneNotSupportedException e) {
  31.480 +            throw new InternalError();
  31.481 +        }
  31.482 +
  31.483 +        clone.m = new TreeMap<>(m);
  31.484 +        return clone;
  31.485 +    }
  31.486 +
  31.487 +    /**
  31.488 +     * Save the state of the {@code TreeSet} instance to a stream (that is,
  31.489 +     * serialize it).
  31.490 +     *
  31.491 +     * @serialData Emits the comparator used to order this set, or
  31.492 +     *             {@code null} if it obeys its elements' natural ordering
  31.493 +     *             (Object), followed by the size of the set (the number of
  31.494 +     *             elements it contains) (int), followed by all of its
  31.495 +     *             elements (each an Object) in order (as determined by the
  31.496 +     *             set's Comparator, or by the elements' natural ordering if
  31.497 +     *             the set has no Comparator).
  31.498 +     */
  31.499 +    private void writeObject(java.io.ObjectOutputStream s)
  31.500 +        throws java.io.IOException {
  31.501 +        // Write out any hidden stuff
  31.502 +        s.defaultWriteObject();
  31.503 +
  31.504 +        // Write out Comparator
  31.505 +        s.writeObject(m.comparator());
  31.506 +
  31.507 +        // Write out size
  31.508 +        s.writeInt(m.size());
  31.509 +
  31.510 +        // Write out all elements in the proper order.
  31.511 +        for (E e : m.keySet())
  31.512 +            s.writeObject(e);
  31.513 +    }
  31.514 +
  31.515 +    /**
  31.516 +     * Reconstitute the {@code TreeSet} instance from a stream (that is,
  31.517 +     * deserialize it).
  31.518 +     */
  31.519 +    private void readObject(java.io.ObjectInputStream s)
  31.520 +        throws java.io.IOException, ClassNotFoundException {
  31.521 +        // Read in any hidden stuff
  31.522 +        s.defaultReadObject();
  31.523 +
  31.524 +        // Read in Comparator
  31.525 +        Comparator<? super E> c = (Comparator<? super E>) s.readObject();
  31.526 +
  31.527 +        // Create backing TreeMap
  31.528 +        TreeMap<E,Object> tm;
  31.529 +        if (c==null)
  31.530 +            tm = new TreeMap<>();
  31.531 +        else
  31.532 +            tm = new TreeMap<>(c);
  31.533 +        m = tm;
  31.534 +
  31.535 +        // Read in size
  31.536 +        int size = s.readInt();
  31.537 +
  31.538 +        tm.readTreeSet(size, s, PRESENT);
  31.539 +    }
  31.540 +
  31.541 +    private static final long serialVersionUID = -2479143000061671589L;
  31.542 +}
    32.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    32.2 +++ b/rt/emul/compact/src/main/java/java/util/WeakHashMap.java	Sun Sep 08 11:22:51 2013 +0200
    32.3 @@ -0,0 +1,972 @@
    32.4 +/*
    32.5 + * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
    32.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    32.7 + *
    32.8 + * This code is free software; you can redistribute it and/or modify it
    32.9 + * under the terms of the GNU General Public License version 2 only, as
   32.10 + * published by the Free Software Foundation.  Oracle designates this
   32.11 + * particular file as subject to the "Classpath" exception as provided
   32.12 + * by Oracle in the LICENSE file that accompanied this code.
   32.13 + *
   32.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   32.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   32.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   32.17 + * version 2 for more details (a copy is included in the LICENSE file that
   32.18 + * accompanied this code).
   32.19 + *
   32.20 + * You should have received a copy of the GNU General Public License version
   32.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   32.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   32.23 + *
   32.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   32.25 + * or visit www.oracle.com if you need additional information or have any
   32.26 + * questions.
   32.27 + */
   32.28 +
   32.29 +package java.util;
   32.30 +import java.lang.ref.WeakReference;
   32.31 +import java.lang.ref.ReferenceQueue;
   32.32 +
   32.33 +
   32.34 +/**
   32.35 + * Hash table based implementation of the <tt>Map</tt> interface, with
   32.36 + * <em>weak keys</em>.
   32.37 + * An entry in a <tt>WeakHashMap</tt> will automatically be removed when
   32.38 + * its key is no longer in ordinary use.  More precisely, the presence of a
   32.39 + * mapping for a given key will not prevent the key from being discarded by the
   32.40 + * garbage collector, that is, made finalizable, finalized, and then reclaimed.
   32.41 + * When a key has been discarded its entry is effectively removed from the map,
   32.42 + * so this class behaves somewhat differently from other <tt>Map</tt>
   32.43 + * implementations.
   32.44 + *
   32.45 + * <p> Both null values and the null key are supported. This class has
   32.46 + * performance characteristics similar to those of the <tt>HashMap</tt>
   32.47 + * class, and has the same efficiency parameters of <em>initial capacity</em>
   32.48 + * and <em>load factor</em>.
   32.49 + *
   32.50 + * <p> Like most collection classes, this class is not synchronized.
   32.51 + * A synchronized <tt>WeakHashMap</tt> may be constructed using the
   32.52 + * {@link Collections#synchronizedMap Collections.synchronizedMap}
   32.53 + * method.
   32.54 + *
   32.55 + * <p> This class is intended primarily for use with key objects whose
   32.56 + * <tt>equals</tt> methods test for object identity using the
   32.57 + * <tt>==</tt> operator.  Once such a key is discarded it can never be
   32.58 + * recreated, so it is impossible to do a lookup of that key in a
   32.59 + * <tt>WeakHashMap</tt> at some later time and be surprised that its entry
   32.60 + * has been removed.  This class will work perfectly well with key objects
   32.61 + * whose <tt>equals</tt> methods are not based upon object identity, such
   32.62 + * as <tt>String</tt> instances.  With such recreatable key objects,
   32.63 + * however, the automatic removal of <tt>WeakHashMap</tt> entries whose
   32.64 + * keys have been discarded may prove to be confusing.
   32.65 + *
   32.66 + * <p> The behavior of the <tt>WeakHashMap</tt> class depends in part upon
   32.67 + * the actions of the garbage collector, so several familiar (though not
   32.68 + * required) <tt>Map</tt> invariants do not hold for this class.  Because
   32.69 + * the garbage collector may discard keys at any time, a
   32.70 + * <tt>WeakHashMap</tt> may behave as though an unknown thread is silently
   32.71 + * removing entries.  In particular, even if you synchronize on a
   32.72 + * <tt>WeakHashMap</tt> instance and invoke none of its mutator methods, it
   32.73 + * is possible for the <tt>size</tt> method to return smaller values over
   32.74 + * time, for the <tt>isEmpty</tt> method to return <tt>false</tt> and
   32.75 + * then <tt>true</tt>, for the <tt>containsKey</tt> method to return
   32.76 + * <tt>true</tt> and later <tt>false</tt> for a given key, for the
   32.77 + * <tt>get</tt> method to return a value for a given key but later return
   32.78 + * <tt>null</tt>, for the <tt>put</tt> method to return
   32.79 + * <tt>null</tt> and the <tt>remove</tt> method to return
   32.80 + * <tt>false</tt> for a key that previously appeared to be in the map, and
   32.81 + * for successive examinations of the key set, the value collection, and
   32.82 + * the entry set to yield successively smaller numbers of elements.
   32.83 + *
   32.84 + * <p> Each key object in a <tt>WeakHashMap</tt> is stored indirectly as
   32.85 + * the referent of a weak reference.  Therefore a key will automatically be
   32.86 + * removed only after the weak references to it, both inside and outside of the
   32.87 + * map, have been cleared by the garbage collector.
   32.88 + *
   32.89 + * <p> <strong>Implementation note:</strong> The value objects in a
   32.90 + * <tt>WeakHashMap</tt> are held by ordinary strong references.  Thus care
   32.91 + * should be taken to ensure that value objects do not strongly refer to their
   32.92 + * own keys, either directly or indirectly, since that will prevent the keys
   32.93 + * from being discarded.  Note that a value object may refer indirectly to its
   32.94 + * key via the <tt>WeakHashMap</tt> itself; that is, a value object may
   32.95 + * strongly refer to some other key object whose associated value object, in
   32.96 + * turn, strongly refers to the key of the first value object.  One way
   32.97 + * to deal with this is to wrap values themselves within
   32.98 + * <tt>WeakReferences</tt> before
   32.99 + * inserting, as in: <tt>m.put(key, new WeakReference(value))</tt>,
  32.100 + * and then unwrapping upon each <tt>get</tt>.
  32.101 + *
  32.102 + * <p>The iterators returned by the <tt>iterator</tt> method of the collections
  32.103 + * returned by all of this class's "collection view methods" are
  32.104 + * <i>fail-fast</i>: if the map is structurally modified at any time after the
  32.105 + * iterator is created, in any way except through the iterator's own
  32.106 + * <tt>remove</tt> method, the iterator will throw a {@link
  32.107 + * ConcurrentModificationException}.  Thus, in the face of concurrent
  32.108 + * modification, the iterator fails quickly and cleanly, rather than risking
  32.109 + * arbitrary, non-deterministic behavior at an undetermined time in the future.
  32.110 + *
  32.111 + * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
  32.112 + * as it is, generally speaking, impossible to make any hard guarantees in the
  32.113 + * presence of unsynchronized concurrent modification.  Fail-fast iterators
  32.114 + * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
  32.115 + * Therefore, it would be wrong to write a program that depended on this
  32.116 + * exception for its correctness:  <i>the fail-fast behavior of iterators
  32.117 + * should be used only to detect bugs.</i>
  32.118 + *
  32.119 + * <p>This class is a member of the
  32.120 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
  32.121 + * Java Collections Framework</a>.
  32.122 + *
  32.123 + * @param <K> the type of keys maintained by this map
  32.124 + * @param <V> the type of mapped values
  32.125 + *
  32.126 + * @author      Doug Lea
  32.127 + * @author      Josh Bloch
  32.128 + * @author      Mark Reinhold
  32.129 + * @since       1.2
  32.130 + * @see         java.util.HashMap
  32.131 + * @see         java.lang.ref.WeakReference
  32.132 + */
  32.133 +public class WeakHashMap<K,V>
  32.134 +    extends AbstractMap<K,V>
  32.135 +    implements Map<K,V> {
  32.136 +
  32.137 +    /**
  32.138 +     * The default initial capacity -- MUST be a power of two.
  32.139 +     */
  32.140 +    private static final int DEFAULT_INITIAL_CAPACITY = 16;
  32.141 +
  32.142 +    /**
  32.143 +     * The maximum capacity, used if a higher value is implicitly specified
  32.144 +     * by either of the constructors with arguments.
  32.145 +     * MUST be a power of two <= 1<<30.
  32.146 +     */
  32.147 +    private static final int MAXIMUM_CAPACITY = 1 << 30;
  32.148 +
  32.149 +    /**
  32.150 +     * The load factor used when none specified in constructor.
  32.151 +     */
  32.152 +    private static final float DEFAULT_LOAD_FACTOR = 0.75f;
  32.153 +
  32.154 +    /**
  32.155 +     * The table, resized as necessary. Length MUST Always be a power of two.
  32.156 +     */
  32.157 +    Entry<K,V>[] table;
  32.158 +
  32.159 +    /**
  32.160 +     * The number of key-value mappings contained in this weak hash map.
  32.161 +     */
  32.162 +    private int size;
  32.163 +
  32.164 +    /**
  32.165 +     * The next size value at which to resize (capacity * load factor).
  32.166 +     */
  32.167 +    private int threshold;
  32.168 +
  32.169 +    /**
  32.170 +     * The load factor for the hash table.
  32.171 +     */
  32.172 +    private final float loadFactor;
  32.173 +
  32.174 +    /**
  32.175 +     * Reference queue for cleared WeakEntries
  32.176 +     */
  32.177 +    private final ReferenceQueue<Object> queue = new ReferenceQueue<>();
  32.178 +
  32.179 +    /**
  32.180 +     * The number of times this WeakHashMap has been structurally modified.
  32.181 +     * Structural modifications are those that change the number of
  32.182 +     * mappings in the map or otherwise modify its internal structure
  32.183 +     * (e.g., rehash).  This field is used to make iterators on
  32.184 +     * Collection-views of the map fail-fast.
  32.185 +     *
  32.186 +     * @see ConcurrentModificationException
  32.187 +     */
  32.188 +    int modCount;
  32.189 +
  32.190 +    @SuppressWarnings("unchecked")
  32.191 +    private Entry<K,V>[] newTable(int n) {
  32.192 +        return (Entry<K,V>[]) new Entry[n];
  32.193 +    }
  32.194 +
  32.195 +    /**
  32.196 +     * Constructs a new, empty <tt>WeakHashMap</tt> with the given initial
  32.197 +     * capacity and the given load factor.
  32.198 +     *
  32.199 +     * @param  initialCapacity The initial capacity of the <tt>WeakHashMap</tt>
  32.200 +     * @param  loadFactor      The load factor of the <tt>WeakHashMap</tt>
  32.201 +     * @throws IllegalArgumentException if the initial capacity is negative,
  32.202 +     *         or if the load factor is nonpositive.
  32.203 +     */
  32.204 +    public WeakHashMap(int initialCapacity, float loadFactor) {
  32.205 +        if (initialCapacity < 0)
  32.206 +            throw new IllegalArgumentException("Illegal Initial Capacity: "+
  32.207 +                                               initialCapacity);
  32.208 +        if (initialCapacity > MAXIMUM_CAPACITY)
  32.209 +            initialCapacity = MAXIMUM_CAPACITY;
  32.210 +
  32.211 +        if (loadFactor <= 0 || Float.isNaN(loadFactor))
  32.212 +            throw new IllegalArgumentException("Illegal Load factor: "+
  32.213 +                                               loadFactor);
  32.214 +        int capacity = 1;
  32.215 +        while (capacity < initialCapacity)
  32.216 +            capacity <<= 1;
  32.217 +        table = newTable(capacity);
  32.218 +        this.loadFactor = loadFactor;
  32.219 +        threshold = (int)(capacity * loadFactor);
  32.220 +    }
  32.221 +
  32.222 +    /**
  32.223 +     * Constructs a new, empty <tt>WeakHashMap</tt> with the given initial
  32.224 +     * capacity and the default load factor (0.75).
  32.225 +     *
  32.226 +     * @param  initialCapacity The initial capacity of the <tt>WeakHashMap</tt>
  32.227 +     * @throws IllegalArgumentException if the initial capacity is negative
  32.228 +     */
  32.229 +    public WeakHashMap(int initialCapacity) {
  32.230 +        this(initialCapacity, DEFAULT_LOAD_FACTOR);
  32.231 +    }
  32.232 +
  32.233 +    /**
  32.234 +     * Constructs a new, empty <tt>WeakHashMap</tt> with the default initial
  32.235 +     * capacity (16) and load factor (0.75).
  32.236 +     */
  32.237 +    public WeakHashMap() {
  32.238 +        this.loadFactor = DEFAULT_LOAD_FACTOR;
  32.239 +        threshold = DEFAULT_INITIAL_CAPACITY;
  32.240 +        table = newTable(DEFAULT_INITIAL_CAPACITY);
  32.241 +    }
  32.242 +
  32.243 +    /**
  32.244 +     * Constructs a new <tt>WeakHashMap</tt> with the same mappings as the
  32.245 +     * specified map.  The <tt>WeakHashMap</tt> is created with the default
  32.246 +     * load factor (0.75) and an initial capacity sufficient to hold the
  32.247 +     * mappings in the specified map.
  32.248 +     *
  32.249 +     * @param   m the map whose mappings are to be placed in this map
  32.250 +     * @throws  NullPointerException if the specified map is null
  32.251 +     * @since   1.3
  32.252 +     */
  32.253 +    public WeakHashMap(Map<? extends K, ? extends V> m) {
  32.254 +        this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1, 16),
  32.255 +             DEFAULT_LOAD_FACTOR);
  32.256 +        putAll(m);
  32.257 +    }
  32.258 +
  32.259 +    // internal utilities
  32.260 +
  32.261 +    /**
  32.262 +     * Value representing null keys inside tables.
  32.263 +     */
  32.264 +    private static final Object NULL_KEY = new Object();
  32.265 +
  32.266 +    /**
  32.267 +     * Use NULL_KEY for key if it is null.
  32.268 +     */
  32.269 +    private static Object maskNull(Object key) {
  32.270 +        return (key == null) ? NULL_KEY : key;
  32.271 +    }
  32.272 +
  32.273 +    /**
  32.274 +     * Returns internal representation of null key back to caller as null.
  32.275 +     */
  32.276 +    static Object unmaskNull(Object key) {
  32.277 +        return (key == NULL_KEY) ? null : key;
  32.278 +    }
  32.279 +
  32.280 +    /**
  32.281 +     * Checks for equality of non-null reference x and possibly-null y.  By
  32.282 +     * default uses Object.equals.
  32.283 +     */
  32.284 +    private static boolean eq(Object x, Object y) {
  32.285 +        return x == y || x.equals(y);
  32.286 +    }
  32.287 +
  32.288 +    /**
  32.289 +     * Returns index for hash code h.
  32.290 +     */
  32.291 +    private static int indexFor(int h, int length) {
  32.292 +        return h & (length-1);
  32.293 +    }
  32.294 +
  32.295 +    /**
  32.296 +     * Expunges stale entries from the table.
  32.297 +     */
  32.298 +    private void expungeStaleEntries() {
  32.299 +        for (Object x; (x = queue.poll()) != null; ) {
  32.300 +            synchronized (queue) {
  32.301 +                @SuppressWarnings("unchecked")
  32.302 +                    Entry<K,V> e = (Entry<K,V>) x;
  32.303 +                int i = indexFor(e.hash, table.length);
  32.304 +
  32.305 +                Entry<K,V> prev = table[i];
  32.306 +                Entry<K,V> p = prev;
  32.307 +                while (p != null) {
  32.308 +                    Entry<K,V> next = p.next;
  32.309 +                    if (p == e) {
  32.310 +                        if (prev == e)
  32.311 +                            table[i] = next;
  32.312 +                        else
  32.313 +                            prev.next = next;
  32.314 +                        // Must not null out e.next;
  32.315 +                        // stale entries may be in use by a HashIterator
  32.316 +                        e.value = null; // Help GC
  32.317 +                        size--;
  32.318 +                        break;
  32.319 +                    }
  32.320 +                    prev = p;
  32.321 +                    p = next;
  32.322 +                }
  32.323 +            }
  32.324 +        }
  32.325 +    }
  32.326 +
  32.327 +    /**
  32.328 +     * Returns the table after first expunging stale entries.
  32.329 +     */
  32.330 +    private Entry<K,V>[] getTable() {
  32.331 +        expungeStaleEntries();
  32.332 +        return table;
  32.333 +    }
  32.334 +
  32.335 +    /**
  32.336 +     * Returns the number of key-value mappings in this map.
  32.337 +     * This result is a snapshot, and may not reflect unprocessed
  32.338 +     * entries that will be removed before next attempted access
  32.339 +     * because they are no longer referenced.
  32.340 +     */
  32.341 +    public int size() {
  32.342 +        if (size == 0)
  32.343 +            return 0;
  32.344 +        expungeStaleEntries();
  32.345 +        return size;
  32.346 +    }
  32.347 +
  32.348 +    /**
  32.349 +     * Returns <tt>true</tt> if this map contains no key-value mappings.
  32.350 +     * This result is a snapshot, and may not reflect unprocessed
  32.351 +     * entries that will be removed before next attempted access
  32.352 +     * because they are no longer referenced.
  32.353 +     */
  32.354 +    public boolean isEmpty() {
  32.355 +        return size() == 0;
  32.356 +    }
  32.357 +
  32.358 +    /**
  32.359 +     * Returns the value to which the specified key is mapped,
  32.360 +     * or {@code null} if this map contains no mapping for the key.
  32.361 +     *
  32.362 +     * <p>More formally, if this map contains a mapping from a key
  32.363 +     * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
  32.364 +     * key.equals(k))}, then this method returns {@code v}; otherwise
  32.365 +     * it returns {@code null}.  (There can be at most one such mapping.)
  32.366 +     *
  32.367 +     * <p>A return value of {@code null} does not <i>necessarily</i>
  32.368 +     * indicate that the map contains no mapping for the key; it's also
  32.369 +     * possible that the map explicitly maps the key to {@code null}.
  32.370 +     * The {@link #containsKey containsKey} operation may be used to
  32.371 +     * distinguish these two cases.
  32.372 +     *
  32.373 +     * @see #put(Object, Object)
  32.374 +     */
  32.375 +    public V get(Object key) {
  32.376 +        Object k = maskNull(key);
  32.377 +        int h = HashMap.hash(k.hashCode());
  32.378 +        Entry<K,V>[] tab = getTable();
  32.379 +        int index = indexFor(h, tab.length);
  32.380 +        Entry<K,V> e = tab[index];
  32.381 +        while (e != null) {
  32.382 +            if (e.hash == h && eq(k, e.get()))
  32.383 +                return e.value;
  32.384 +            e = e.next;
  32.385 +        }
  32.386 +        return null;
  32.387 +    }
  32.388 +
  32.389 +    /**
  32.390 +     * Returns <tt>true</tt> if this map contains a mapping for the
  32.391 +     * specified key.
  32.392 +     *
  32.393 +     * @param  key   The key whose presence in this map is to be tested
  32.394 +     * @return <tt>true</tt> if there is a mapping for <tt>key</tt>;
  32.395 +     *         <tt>false</tt> otherwise
  32.396 +     */
  32.397 +    public boolean containsKey(Object key) {
  32.398 +        return getEntry(key) != null;
  32.399 +    }
  32.400 +
  32.401 +    /**
  32.402 +     * Returns the entry associated with the specified key in this map.
  32.403 +     * Returns null if the map contains no mapping for this key.
  32.404 +     */
  32.405 +    Entry<K,V> getEntry(Object key) {
  32.406 +        Object k = maskNull(key);
  32.407 +        int h = HashMap.hash(k.hashCode());
  32.408 +        Entry<K,V>[] tab = getTable();
  32.409 +        int index = indexFor(h, tab.length);
  32.410 +        Entry<K,V> e = tab[index];
  32.411 +        while (e != null && !(e.hash == h && eq(k, e.get())))
  32.412 +            e = e.next;
  32.413 +        return e;
  32.414 +    }
  32.415 +
  32.416 +    /**
  32.417 +     * Associates the specified value with the specified key in this map.
  32.418 +     * If the map previously contained a mapping for this key, the old
  32.419 +     * value is replaced.
  32.420 +     *
  32.421 +     * @param key key with which the specified value is to be associated.
  32.422 +     * @param value value to be associated with the specified key.
  32.423 +     * @return the previous value associated with <tt>key</tt>, or
  32.424 +     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
  32.425 +     *         (A <tt>null</tt> return can also indicate that the map
  32.426 +     *         previously associated <tt>null</tt> with <tt>key</tt>.)
  32.427 +     */
  32.428 +    public V put(K key, V value) {
  32.429 +        Object k = maskNull(key);
  32.430 +        int h = HashMap.hash(k.hashCode());
  32.431 +        Entry<K,V>[] tab = getTable();
  32.432 +        int i = indexFor(h, tab.length);
  32.433 +
  32.434 +        for (Entry<K,V> e = tab[i]; e != null; e = e.next) {
  32.435 +            if (h == e.hash && eq(k, e.get())) {
  32.436 +                V oldValue = e.value;
  32.437 +                if (value != oldValue)
  32.438 +                    e.value = value;
  32.439 +                return oldValue;
  32.440 +            }
  32.441 +        }
  32.442 +
  32.443 +        modCount++;
  32.444 +        Entry<K,V> e = tab[i];
  32.445 +        tab[i] = new Entry<>(k, value, queue, h, e);
  32.446 +        if (++size >= threshold)
  32.447 +            resize(tab.length * 2);
  32.448 +        return null;
  32.449 +    }
  32.450 +
  32.451 +    /**
  32.452 +     * Rehashes the contents of this map into a new array with a
  32.453 +     * larger capacity.  This method is called automatically when the
  32.454 +     * number of keys in this map reaches its threshold.
  32.455 +     *
  32.456 +     * If current capacity is MAXIMUM_CAPACITY, this method does not
  32.457 +     * resize the map, but sets threshold to Integer.MAX_VALUE.
  32.458 +     * This has the effect of preventing future calls.
  32.459 +     *
  32.460 +     * @param newCapacity the new capacity, MUST be a power of two;
  32.461 +     *        must be greater than current capacity unless current
  32.462 +     *        capacity is MAXIMUM_CAPACITY (in which case value
  32.463 +     *        is irrelevant).
  32.464 +     */
  32.465 +    void resize(int newCapacity) {
  32.466 +        Entry<K,V>[] oldTable = getTable();
  32.467 +        int oldCapacity = oldTable.length;
  32.468 +        if (oldCapacity == MAXIMUM_CAPACITY) {
  32.469 +            threshold = Integer.MAX_VALUE;
  32.470 +            return;
  32.471 +        }
  32.472 +
  32.473 +        Entry<K,V>[] newTable = newTable(newCapacity);
  32.474 +        transfer(oldTable, newTable);
  32.475 +        table = newTable;
  32.476 +
  32.477 +        /*
  32.478 +         * If ignoring null elements and processing ref queue caused massive
  32.479 +         * shrinkage, then restore old table.  This should be rare, but avoids
  32.480 +         * unbounded expansion of garbage-filled tables.
  32.481 +         */
  32.482 +        if (size >= threshold / 2) {
  32.483 +            threshold = (int)(newCapacity * loadFactor);
  32.484 +        } else {
  32.485 +            expungeStaleEntries();
  32.486 +            transfer(newTable, oldTable);
  32.487 +            table = oldTable;
  32.488 +        }
  32.489 +    }
  32.490 +
  32.491 +    /** Transfers all entries from src to dest tables */
  32.492 +    private void transfer(Entry<K,V>[] src, Entry<K,V>[] dest) {
  32.493 +        for (int j = 0; j < src.length; ++j) {
  32.494 +            Entry<K,V> e = src[j];
  32.495 +            src[j] = null;
  32.496 +            while (e != null) {
  32.497 +                Entry<K,V> next = e.next;
  32.498 +                Object key = e.get();
  32.499 +                if (key == null) {
  32.500 +                    e.next = null;  // Help GC
  32.501 +                    e.value = null; //  "   "
  32.502 +                    size--;
  32.503 +                } else {
  32.504 +                    int i = indexFor(e.hash, dest.length);
  32.505 +                    e.next = dest[i];
  32.506 +                    dest[i] = e;
  32.507 +                }
  32.508 +                e = next;
  32.509 +            }
  32.510 +        }
  32.511 +    }
  32.512 +
  32.513 +    /**
  32.514 +     * Copies all of the mappings from the specified map to this map.
  32.515 +     * These mappings will replace any mappings that this map had for any
  32.516 +     * of the keys currently in the specified map.
  32.517 +     *
  32.518 +     * @param m mappings to be stored in this map.
  32.519 +     * @throws  NullPointerException if the specified map is null.
  32.520 +     */
  32.521 +    public void putAll(Map<? extends K, ? extends V> m) {
  32.522 +        int numKeysToBeAdded = m.size();
  32.523 +        if (numKeysToBeAdded == 0)
  32.524 +            return;
  32.525 +
  32.526 +        /*
  32.527 +         * Expand the map if the map if the number of mappings to be added
  32.528 +         * is greater than or equal to threshold.  This is conservative; the
  32.529 +         * obvious condition is (m.size() + size) >= threshold, but this
  32.530 +         * condition could result in a map with twice the appropriate capacity,
  32.531 +         * if the keys to be added overlap with the keys already in this map.
  32.532 +         * By using the conservative calculation, we subject ourself
  32.533 +         * to at most one extra resize.
  32.534 +         */
  32.535 +        if (numKeysToBeAdded > threshold) {
  32.536 +            int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);
  32.537 +            if (targetCapacity > MAXIMUM_CAPACITY)
  32.538 +                targetCapacity = MAXIMUM_CAPACITY;
  32.539 +            int newCapacity = table.length;
  32.540 +            while (newCapacity < targetCapacity)
  32.541 +                newCapacity <<= 1;
  32.542 +            if (newCapacity > table.length)
  32.543 +                resize(newCapacity);
  32.544 +        }
  32.545 +
  32.546 +        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
  32.547 +            put(e.getKey(), e.getValue());
  32.548 +    }
  32.549 +
  32.550 +    /**
  32.551 +     * Removes the mapping for a key from this weak hash map if it is present.
  32.552 +     * More formally, if this map contains a mapping from key <tt>k</tt> to
  32.553 +     * value <tt>v</tt> such that <code>(key==null ?  k==null :
  32.554 +     * key.equals(k))</code>, that mapping is removed.  (The map can contain
  32.555 +     * at most one such mapping.)
  32.556 +     *
  32.557 +     * <p>Returns the value to which this map previously associated the key,
  32.558 +     * or <tt>null</tt> if the map contained no mapping for the key.  A
  32.559 +     * return value of <tt>null</tt> does not <i>necessarily</i> indicate
  32.560 +     * that the map contained no mapping for the key; it's also possible
  32.561 +     * that the map explicitly mapped the key to <tt>null</tt>.
  32.562 +     *
  32.563 +     * <p>The map will not contain a mapping for the specified key once the
  32.564 +     * call returns.
  32.565 +     *
  32.566 +     * @param key key whose mapping is to be removed from the map
  32.567 +     * @return the previous value associated with <tt>key</tt>, or
  32.568 +     *         <tt>null</tt> if there was no mapping for <tt>key</tt>
  32.569 +     */
  32.570 +    public V remove(Object key) {
  32.571 +        Object k = maskNull(key);
  32.572 +        int h = HashMap.hash(k.hashCode());
  32.573 +        Entry<K,V>[] tab = getTable();
  32.574 +        int i = indexFor(h, tab.length);
  32.575 +        Entry<K,V> prev = tab[i];
  32.576 +        Entry<K,V> e = prev;
  32.577 +
  32.578 +        while (e != null) {
  32.579 +            Entry<K,V> next = e.next;
  32.580 +            if (h == e.hash && eq(k, e.get())) {
  32.581 +                modCount++;
  32.582 +                size--;
  32.583 +                if (prev == e)
  32.584 +                    tab[i] = next;
  32.585 +                else
  32.586 +                    prev.next = next;
  32.587 +                return e.value;
  32.588 +            }
  32.589 +            prev = e;
  32.590 +            e = next;
  32.591 +        }
  32.592 +
  32.593 +        return null;
  32.594 +    }
  32.595 +
  32.596 +    /** Special version of remove needed by Entry set */
  32.597 +    boolean removeMapping(Object o) {
  32.598 +        if (!(o instanceof Map.Entry))
  32.599 +            return false;
  32.600 +        Entry<K,V>[] tab = getTable();
  32.601 +        Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
  32.602 +        Object k = maskNull(entry.getKey());
  32.603 +        int h = HashMap.hash(k.hashCode());
  32.604 +        int i = indexFor(h, tab.length);
  32.605 +        Entry<K,V> prev = tab[i];
  32.606 +        Entry<K,V> e = prev;
  32.607 +
  32.608 +        while (e != null) {
  32.609 +            Entry<K,V> next = e.next;
  32.610 +            if (h == e.hash && e.equals(entry)) {
  32.611 +                modCount++;
  32.612 +                size--;
  32.613 +                if (prev == e)
  32.614 +                    tab[i] = next;
  32.615 +                else
  32.616 +                    prev.next = next;
  32.617 +                return true;
  32.618 +            }
  32.619 +            prev = e;
  32.620 +            e = next;
  32.621 +        }
  32.622 +
  32.623 +        return false;
  32.624 +    }
  32.625 +
  32.626 +    /**
  32.627 +     * Removes all of the mappings from this map.
  32.628 +     * The map will be empty after this call returns.
  32.629 +     */
  32.630 +    public void clear() {
  32.631 +        // clear out ref queue. We don't need to expunge entries
  32.632 +        // since table is getting cleared.
  32.633 +        while (queue.poll() != null)
  32.634 +            ;
  32.635 +
  32.636 +        modCount++;
  32.637 +        Arrays.fill(table, null);
  32.638 +        size = 0;
  32.639 +
  32.640 +        // Allocation of array may have caused GC, which may have caused
  32.641 +        // additional entries to go stale.  Removing these entries from the
  32.642 +        // reference queue will make them eligible for reclamation.
  32.643 +        while (queue.poll() != null)
  32.644 +            ;
  32.645 +    }
  32.646 +
  32.647 +    /**
  32.648 +     * Returns <tt>true</tt> if this map maps one or more keys to the
  32.649 +     * specified value.
  32.650 +     *
  32.651 +     * @param value value whose presence in this map is to be tested
  32.652 +     * @return <tt>true</tt> if this map maps one or more keys to the
  32.653 +     *         specified value
  32.654 +     */
  32.655 +    public boolean containsValue(Object value) {
  32.656 +        if (value==null)
  32.657 +            return containsNullValue();
  32.658 +
  32.659 +        Entry<K,V>[] tab = getTable();
  32.660 +        for (int i = tab.length; i-- > 0;)
  32.661 +            for (Entry<K,V> e = tab[i]; e != null; e = e.next)
  32.662 +                if (value.equals(e.value))
  32.663 +                    return true;
  32.664 +        return false;
  32.665 +    }
  32.666 +
  32.667 +    /**
  32.668 +     * Special-case code for containsValue with null argument
  32.669 +     */
  32.670 +    private boolean containsNullValue() {
  32.671 +        Entry<K,V>[] tab = getTable();
  32.672 +        for (int i = tab.length; i-- > 0;)
  32.673 +            for (Entry<K,V> e = tab[i]; e != null; e = e.next)
  32.674 +                if (e.value==null)
  32.675 +                    return true;
  32.676 +        return false;
  32.677 +    }
  32.678 +
  32.679 +    /**
  32.680 +     * The entries in this hash table extend WeakReference, using its main ref
  32.681 +     * field as the key.
  32.682 +     */
  32.683 +    private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V> {
  32.684 +        V value;
  32.685 +        final int hash;
  32.686 +        Entry<K,V> next;
  32.687 +
  32.688 +        /**
  32.689 +         * Creates new entry.
  32.690 +         */
  32.691 +        Entry(Object key, V value,
  32.692 +              ReferenceQueue<Object> queue,
  32.693 +              int hash, Entry<K,V> next) {
  32.694 +            super(key, queue);
  32.695 +            this.value = value;
  32.696 +            this.hash  = hash;
  32.697 +            this.next  = next;
  32.698 +        }
  32.699 +
  32.700 +        @SuppressWarnings("unchecked")
  32.701 +        public K getKey() {
  32.702 +            return (K) WeakHashMap.unmaskNull(get());
  32.703 +        }
  32.704 +
  32.705 +        public V getValue() {
  32.706 +            return value;
  32.707 +        }
  32.708 +
  32.709 +        public V setValue(V newValue) {
  32.710 +            V oldValue = value;
  32.711 +            value = newValue;
  32.712 +            return oldValue;
  32.713 +        }
  32.714 +
  32.715 +        public boolean equals(Object o) {
  32.716 +            if (!(o instanceof Map.Entry))
  32.717 +                return false;
  32.718 +            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
  32.719 +            K k1 = getKey();
  32.720 +            Object k2 = e.getKey();
  32.721 +            if (k1 == k2 || (k1 != null && k1.equals(k2))) {
  32.722 +                V v1 = getValue();
  32.723 +                Object v2 = e.getValue();
  32.724 +                if (v1 == v2 || (v1 != null && v1.equals(v2)))
  32.725 +                    return true;
  32.726 +            }
  32.727 +            return false;
  32.728 +        }
  32.729 +
  32.730 +        public int hashCode() {
  32.731 +            K k = getKey();
  32.732 +            V v = getValue();
  32.733 +            return ((k==null ? 0 : k.hashCode()) ^
  32.734 +                    (v==null ? 0 : v.hashCode()));
  32.735 +        }
  32.736 +
  32.737 +        public String toString() {
  32.738 +            return getKey() + "=" + getValue();
  32.739 +        }
  32.740 +    }
  32.741 +
  32.742 +    private abstract class HashIterator<T> implements Iterator<T> {
  32.743 +        private int index;
  32.744 +        private Entry<K,V> entry = null;
  32.745 +        private Entry<K,V> lastReturned = null;
  32.746 +        private int expectedModCount = modCount;
  32.747 +
  32.748 +        /**
  32.749 +         * Strong reference needed to avoid disappearance of key
  32.750 +         * between hasNext and next
  32.751 +         */
  32.752 +        private Object nextKey = null;
  32.753 +
  32.754 +        /**
  32.755 +         * Strong reference needed to avoid disappearance of key
  32.756 +         * between nextEntry() and any use of the entry
  32.757 +         */
  32.758 +        private Object currentKey = null;
  32.759 +
  32.760 +        HashIterator() {
  32.761 +            index = isEmpty() ? 0 : table.length;
  32.762 +        }
  32.763 +
  32.764 +        public boolean hasNext() {
  32.765 +            Entry<K,V>[] t = table;
  32.766 +
  32.767 +            while (nextKey == null) {
  32.768 +                Entry<K,V> e = entry;
  32.769 +                int i = index;
  32.770 +                while (e == null && i > 0)
  32.771 +                    e = t[--i];
  32.772 +                entry = e;
  32.773 +                index = i;
  32.774 +                if (e == null) {
  32.775 +                    currentKey = null;
  32.776 +                    return false;
  32.777 +                }
  32.778 +                nextKey = e.get(); // hold on to key in strong ref
  32.779 +                if (nextKey == null)
  32.780 +                    entry = entry.next;
  32.781 +            }
  32.782 +            return true;
  32.783 +        }
  32.784 +
  32.785 +        /** The common parts of next() across different types of iterators */
  32.786 +        protected Entry<K,V> nextEntry() {
  32.787 +            if (modCount != expectedModCount)
  32.788 +                throw new ConcurrentModificationException();
  32.789 +            if (nextKey == null && !hasNext())
  32.790 +                throw new NoSuchElementException();
  32.791 +
  32.792 +            lastReturned = entry;
  32.793 +            entry = entry.next;
  32.794 +            currentKey = nextKey;
  32.795 +            nextKey = null;
  32.796 +            return lastReturned;
  32.797 +        }
  32.798 +
  32.799 +        public void remove() {
  32.800 +            if (lastReturned == null)
  32.801 +                throw new IllegalStateException();
  32.802 +            if (modCount != expectedModCount)
  32.803 +                throw new ConcurrentModificationException();
  32.804 +
  32.805 +            WeakHashMap.this.remove(currentKey);
  32.806 +            expectedModCount = modCount;
  32.807 +            lastReturned = null;
  32.808 +            currentKey = null;
  32.809 +        }
  32.810 +
  32.811 +    }
  32.812 +
  32.813 +    private class ValueIterator extends HashIterator<V> {
  32.814 +        public V next() {
  32.815 +            return nextEntry().value;
  32.816 +        }
  32.817 +    }
  32.818 +
  32.819 +    private class KeyIterator extends HashIterator<K> {
  32.820 +        public K next() {
  32.821 +            return nextEntry().getKey();
  32.822 +        }
  32.823 +    }
  32.824 +
  32.825 +    private class EntryIterator extends HashIterator<Map.Entry<K,V>> {
  32.826 +        public Map.Entry<K,V> next() {
  32.827 +            return nextEntry();
  32.828 +        }
  32.829 +    }
  32.830 +
  32.831 +    // Views
  32.832 +
  32.833 +    private transient Set<Map.Entry<K,V>> entrySet = null;
  32.834 +
  32.835 +    /**
  32.836 +     * Returns a {@link Set} view of the keys contained in this map.
  32.837 +     * The set is backed by the map, so changes to the map are
  32.838 +     * reflected in the set, and vice-versa.  If the map is modified
  32.839 +     * while an iteration over the set is in progress (except through
  32.840 +     * the iterator's own <tt>remove</tt> operation), the results of
  32.841 +     * the iteration are undefined.  The set supports element removal,
  32.842 +     * which removes the corresponding mapping from the map, via the
  32.843 +     * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
  32.844 +     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
  32.845 +     * operations.  It does not support the <tt>add</tt> or <tt>addAll</tt>
  32.846 +     * operations.
  32.847 +     */
  32.848 +    public Set<K> keySet() {
  32.849 +        Set<K> ks = keySet;
  32.850 +        return (ks != null ? ks : (keySet = new KeySet()));
  32.851 +    }
  32.852 +
  32.853 +    private class KeySet extends AbstractSet<K> {
  32.854 +        public Iterator<K> iterator() {
  32.855 +            return new KeyIterator();
  32.856 +        }
  32.857 +
  32.858 +        public int size() {
  32.859 +            return WeakHashMap.this.size();
  32.860 +        }
  32.861 +
  32.862 +        public boolean contains(Object o) {
  32.863 +            return containsKey(o);
  32.864 +        }
  32.865 +
  32.866 +        public boolean remove(Object o) {
  32.867 +            if (containsKey(o)) {
  32.868 +                WeakHashMap.this.remove(o);
  32.869 +                return true;
  32.870 +            }
  32.871 +            else
  32.872 +                return false;
  32.873 +        }
  32.874 +
  32.875 +        public void clear() {
  32.876 +            WeakHashMap.this.clear();
  32.877 +        }
  32.878 +    }
  32.879 +
  32.880 +    /**
  32.881 +     * Returns a {@link Collection} view of the values contained in this map.
  32.882 +     * The collection is backed by the map, so changes to the map are
  32.883 +     * reflected in the collection, and vice-versa.  If the map is
  32.884 +     * modified while an iteration over the collection is in progress
  32.885 +     * (except through the iterator's own <tt>remove</tt> operation),
  32.886 +     * the results of the iteration are undefined.  The collection
  32.887 +     * supports element removal, which removes the corresponding
  32.888 +     * mapping from the map, via the <tt>Iterator.remove</tt>,
  32.889 +     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
  32.890 +     * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not
  32.891 +     * support the <tt>add</tt> or <tt>addAll</tt> operations.
  32.892 +     */
  32.893 +    public Collection<V> values() {
  32.894 +        Collection<V> vs = values;
  32.895 +        return (vs != null) ? vs : (values = new Values());
  32.896 +    }
  32.897 +
  32.898 +    private class Values extends AbstractCollection<V> {
  32.899 +        public Iterator<V> iterator() {
  32.900 +            return new ValueIterator();
  32.901 +        }
  32.902 +
  32.903 +        public int size() {
  32.904 +            return WeakHashMap.this.size();
  32.905 +        }
  32.906 +
  32.907 +        public boolean contains(Object o) {
  32.908 +            return containsValue(o);
  32.909 +        }
  32.910 +
  32.911 +        public void clear() {
  32.912 +            WeakHashMap.this.clear();
  32.913 +        }
  32.914 +    }
  32.915 +
  32.916 +    /**
  32.917 +     * Returns a {@link Set} view of the mappings contained in this map.
  32.918 +     * The set is backed by the map, so changes to the map are
  32.919 +     * reflected in the set, and vice-versa.  If the map is modified
  32.920 +     * while an iteration over the set is in progress (except through
  32.921 +     * the iterator's own <tt>remove</tt> operation, or through the
  32.922 +     * <tt>setValue</tt> operation on a map entry returned by the
  32.923 +     * iterator) the results of the iteration are undefined.  The set
  32.924 +     * supports element removal, which removes the corresponding
  32.925 +     * mapping from the map, via the <tt>Iterator.remove</tt>,
  32.926 +     * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
  32.927 +     * <tt>clear</tt> operations.  It does not support the
  32.928 +     * <tt>add</tt> or <tt>addAll</tt> operations.
  32.929 +     */
  32.930 +    public Set<Map.Entry<K,V>> entrySet() {
  32.931 +        Set<Map.Entry<K,V>> es = entrySet;
  32.932 +        return es != null ? es : (entrySet = new EntrySet());
  32.933 +    }
  32.934 +
  32.935 +    private class EntrySet extends AbstractSet<Map.Entry<K,V>> {
  32.936 +        public Iterator<Map.Entry<K,V>> iterator() {
  32.937 +            return new EntryIterator();
  32.938 +        }
  32.939 +
  32.940 +        public boolean contains(Object o) {
  32.941 +            if (!(o instanceof Map.Entry))
  32.942 +                return false;
  32.943 +            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
  32.944 +            Entry<K,V> candidate = getEntry(e.getKey());
  32.945 +            return candidate != null && candidate.equals(e);
  32.946 +        }
  32.947 +
  32.948 +        public boolean remove(Object o) {
  32.949 +            return removeMapping(o);
  32.950 +        }
  32.951 +
  32.952 +        public int size() {
  32.953 +            return WeakHashMap.this.size();
  32.954 +        }
  32.955 +
  32.956 +        public void clear() {
  32.957 +            WeakHashMap.this.clear();
  32.958 +        }
  32.959 +
  32.960 +        private List<Map.Entry<K,V>> deepCopy() {
  32.961 +            List<Map.Entry<K,V>> list = new ArrayList<>(size());
  32.962 +            for (Map.Entry<K,V> e : this)
  32.963 +                list.add(new AbstractMap.SimpleEntry<>(e));
  32.964 +            return list;
  32.965 +        }
  32.966 +
  32.967 +        public Object[] toArray() {
  32.968 +            return deepCopy().toArray();
  32.969 +        }
  32.970 +
  32.971 +        public <T> T[] toArray(T[] a) {
  32.972 +            return deepCopy().toArray(a);
  32.973 +        }
  32.974 +    }
  32.975 +}
    33.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    33.2 +++ b/rt/emul/compact/src/main/java/java/util/concurrent/Executor.java	Sun Sep 08 11:22:51 2013 +0200
    33.3 @@ -0,0 +1,141 @@
    33.4 +/*
    33.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    33.6 + *
    33.7 + * This code is free software; you can redistribute it and/or modify it
    33.8 + * under the terms of the GNU General Public License version 2 only, as
    33.9 + * published by the Free Software Foundation.  Oracle designates this
   33.10 + * particular file as subject to the "Classpath" exception as provided
   33.11 + * by Oracle in the LICENSE file that accompanied this code.
   33.12 + *
   33.13 + * This code is distributed in the hope that it will be useful, but WITHOUT
   33.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   33.15 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   33.16 + * version 2 for more details (a copy is included in the LICENSE file that
   33.17 + * accompanied this code).
   33.18 + *
   33.19 + * You should have received a copy of the GNU General Public License version
   33.20 + * 2 along with this work; if not, write to the Free Software Foundation,
   33.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   33.22 + *
   33.23 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   33.24 + * or visit www.oracle.com if you need additional information or have any
   33.25 + * questions.
   33.26 + */
   33.27 +
   33.28 +/*
   33.29 + * This file is available under and governed by the GNU General Public
   33.30 + * License version 2 only, as published by the Free Software Foundation.
   33.31 + * However, the following notice accompanied the original version of this
   33.32 + * file:
   33.33 + *
   33.34 + * Written by Doug Lea with assistance from members of JCP JSR-166
   33.35 + * Expert Group and released to the public domain, as explained at
   33.36 + * http://creativecommons.org/publicdomain/zero/1.0/
   33.37 + */
   33.38 +
   33.39 +package java.util.concurrent;
   33.40 +
   33.41 +/**
   33.42 + * An object that executes submitted {@link Runnable} tasks. This
   33.43 + * interface provides a way of decoupling task submission from the
   33.44 + * mechanics of how each task will be run, including details of thread
   33.45 + * use, scheduling, etc.  An <tt>Executor</tt> is normally used
   33.46 + * instead of explicitly creating threads. For example, rather than
   33.47 + * invoking <tt>new Thread(new(RunnableTask())).start()</tt> for each
   33.48 + * of a set of tasks, you might use:
   33.49 + *
   33.50 + * <pre>
   33.51 + * Executor executor = <em>anExecutor</em>;
   33.52 + * executor.execute(new RunnableTask1());
   33.53 + * executor.execute(new RunnableTask2());
   33.54 + * ...
   33.55 + * </pre>
   33.56 + *
   33.57 + * However, the <tt>Executor</tt> interface does not strictly
   33.58 + * require that execution be asynchronous. In the simplest case, an
   33.59 + * executor can run the submitted task immediately in the caller's
   33.60 + * thread:
   33.61 + *
   33.62 + * <pre>
   33.63 + * class DirectExecutor implements Executor {
   33.64 + *     public void execute(Runnable r) {
   33.65 + *         r.run();
   33.66 + *     }
   33.67 + * }</pre>
   33.68 + *
   33.69 + * More typically, tasks are executed in some thread other
   33.70 + * than the caller's thread.  The executor below spawns a new thread
   33.71 + * for each task.
   33.72 + *
   33.73 + * <pre>
   33.74 + * class ThreadPerTaskExecutor implements Executor {
   33.75 + *     public void execute(Runnable r) {
   33.76 + *         new Thread(r).start();
   33.77 + *     }
   33.78 + * }</pre>
   33.79 + *
   33.80 + * Many <tt>Executor</tt> implementations impose some sort of
   33.81 + * limitation on how and when tasks are scheduled.  The executor below
   33.82 + * serializes the submission of tasks to a second executor,
   33.83 + * illustrating a composite executor.
   33.84 + *
   33.85 + *  <pre> {@code
   33.86 + * class SerialExecutor implements Executor {
   33.87 + *   final Queue<Runnable> tasks = new ArrayDeque<Runnable>();
   33.88 + *   final Executor executor;
   33.89 + *   Runnable active;
   33.90 + *
   33.91 + *   SerialExecutor(Executor executor) {
   33.92 + *     this.executor = executor;
   33.93 + *   }
   33.94 + *
   33.95 + *   public synchronized void execute(final Runnable r) {
   33.96 + *     tasks.offer(new Runnable() {
   33.97 + *       public void run() {
   33.98 + *         try {
   33.99 + *           r.run();
  33.100 + *         } finally {
  33.101 + *           scheduleNext();
  33.102 + *         }
  33.103 + *       }
  33.104 + *     });
  33.105 + *     if (active == null) {
  33.106 + *       scheduleNext();
  33.107 + *     }
  33.108 + *   }
  33.109 + *
  33.110 + *   protected synchronized void scheduleNext() {
  33.111 + *     if ((active = tasks.poll()) != null) {
  33.112 + *       executor.execute(active);
  33.113 + *     }
  33.114 + *   }
  33.115 + * }}</pre>
  33.116 + *
  33.117 + * The <tt>Executor</tt> implementations provided in this package
  33.118 + * implement {@link ExecutorService}, which is a more extensive
  33.119 + * interface.  The {@link ThreadPoolExecutor} class provides an
  33.120 + * extensible thread pool implementation. The {@link Executors} class
  33.121 + * provides convenient factory methods for these Executors.
  33.122 + *
  33.123 + * <p>Memory consistency effects: Actions in a thread prior to
  33.124 + * submitting a {@code Runnable} object to an {@code Executor}
  33.125 + * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
  33.126 + * its execution begins, perhaps in another thread.
  33.127 + *
  33.128 + * @since 1.5
  33.129 + * @author Doug Lea
  33.130 + */
  33.131 +public interface Executor {
  33.132 +
  33.133 +    /**
  33.134 +     * Executes the given command at some time in the future.  The command
  33.135 +     * may execute in a new thread, in a pooled thread, or in the calling
  33.136 +     * thread, at the discretion of the <tt>Executor</tt> implementation.
  33.137 +     *
  33.138 +     * @param command the runnable task
  33.139 +     * @throws RejectedExecutionException if this task cannot be
  33.140 +     * accepted for execution.
  33.141 +     * @throws NullPointerException if command is null
  33.142 +     */
  33.143 +    void execute(Runnable command);
  33.144 +}
    34.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    34.2 +++ b/rt/emul/compact/src/main/java/java/util/logging/Level.java	Sun Sep 08 11:22:51 2013 +0200
    34.3 @@ -0,0 +1,372 @@
    34.4 +/*
    34.5 + * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
    34.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    34.7 + *
    34.8 + * This code is free software; you can redistribute it and/or modify it
    34.9 + * under the terms of the GNU General Public License version 2 only, as
   34.10 + * published by the Free Software Foundation.  Oracle designates this
   34.11 + * particular file as subject to the "Classpath" exception as provided
   34.12 + * by Oracle in the LICENSE file that accompanied this code.
   34.13 + *
   34.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   34.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   34.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   34.17 + * version 2 for more details (a copy is included in the LICENSE file that
   34.18 + * accompanied this code).
   34.19 + *
   34.20 + * You should have received a copy of the GNU General Public License version
   34.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   34.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   34.23 + *
   34.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   34.25 + * or visit www.oracle.com if you need additional information or have any
   34.26 + * questions.
   34.27 + */
   34.28 +
   34.29 +package java.util.logging;
   34.30 +
   34.31 +/**
   34.32 + * The Level class defines a set of standard logging levels that
   34.33 + * can be used to control logging output.  The logging Level objects
   34.34 + * are ordered and are specified by ordered integers.  Enabling logging
   34.35 + * at a given level also enables logging at all higher levels.
   34.36 + * <p>
   34.37 + * Clients should normally use the predefined Level constants such
   34.38 + * as Level.SEVERE.
   34.39 + * <p>
   34.40 + * The levels in descending order are:
   34.41 + * <ul>
   34.42 + * <li>SEVERE (highest value)
   34.43 + * <li>WARNING
   34.44 + * <li>INFO
   34.45 + * <li>CONFIG
   34.46 + * <li>FINE
   34.47 + * <li>FINER
   34.48 + * <li>FINEST  (lowest value)
   34.49 + * </ul>
   34.50 + * In addition there is a level OFF that can be used to turn
   34.51 + * off logging, and a level ALL that can be used to enable
   34.52 + * logging of all messages.
   34.53 + * <p>
   34.54 + * It is possible for third parties to define additional logging
   34.55 + * levels by subclassing Level.  In such cases subclasses should
   34.56 + * take care to chose unique integer level values and to ensure that
   34.57 + * they maintain the Object uniqueness property across serialization
   34.58 + * by defining a suitable readResolve method.
   34.59 + *
   34.60 + * @since 1.4
   34.61 + */
   34.62 +
   34.63 +public class Level implements java.io.Serializable {
   34.64 +    private static java.util.ArrayList<Level> known = new java.util.ArrayList<>();
   34.65 +    private static String defaultBundle = "sun.util.logging.resources.logging";
   34.66 +
   34.67 +    /**
   34.68 +     * @serial  The non-localized name of the level.
   34.69 +     */
   34.70 +    private final String name;
   34.71 +
   34.72 +    /**
   34.73 +     * @serial  The integer value of the level.
   34.74 +     */
   34.75 +    private final int value;
   34.76 +
   34.77 +    /**
   34.78 +     * @serial The resource bundle name to be used in localizing the level name.
   34.79 +     */
   34.80 +    private final String resourceBundleName;
   34.81 +
   34.82 +    /**
   34.83 +     * OFF is a special level that can be used to turn off logging.
   34.84 +     * This level is initialized to <CODE>Integer.MAX_VALUE</CODE>.
   34.85 +     */
   34.86 +    public static final Level OFF = new Level("OFF",Integer.MAX_VALUE, defaultBundle);
   34.87 +
   34.88 +    /**
   34.89 +     * SEVERE is a message level indicating a serious failure.
   34.90 +     * <p>
   34.91 +     * In general SEVERE messages should describe events that are
   34.92 +     * of considerable importance and which will prevent normal
   34.93 +     * program execution.   They should be reasonably intelligible
   34.94 +     * to end users and to system administrators.
   34.95 +     * This level is initialized to <CODE>1000</CODE>.
   34.96 +     */
   34.97 +    public static final Level SEVERE = new Level("SEVERE",1000, defaultBundle);
   34.98 +
   34.99 +    /**
  34.100 +     * WARNING is a message level indicating a potential problem.
  34.101 +     * <p>
  34.102 +     * In general WARNING messages should describe events that will
  34.103 +     * be of interest to end users or system managers, or which
  34.104 +     * indicate potential problems.
  34.105 +     * This level is initialized to <CODE>900</CODE>.
  34.106 +     */
  34.107 +    public static final Level WARNING = new Level("WARNING", 900, defaultBundle);
  34.108 +
  34.109 +    /**
  34.110 +     * INFO is a message level for informational messages.
  34.111 +     * <p>
  34.112 +     * Typically INFO messages will be written to the console
  34.113 +     * or its equivalent.  So the INFO level should only be
  34.114 +     * used for reasonably significant messages that will
  34.115 +     * make sense to end users and system administrators.
  34.116 +     * This level is initialized to <CODE>800</CODE>.
  34.117 +     */
  34.118 +    public static final Level INFO = new Level("INFO", 800, defaultBundle);
  34.119 +
  34.120 +    /**
  34.121 +     * CONFIG is a message level for static configuration messages.
  34.122 +     * <p>
  34.123 +     * CONFIG messages are intended to provide a variety of static
  34.124 +     * configuration information, to assist in debugging problems
  34.125 +     * that may be associated with particular configurations.
  34.126 +     * For example, CONFIG message might include the CPU type,
  34.127 +     * the graphics depth, the GUI look-and-feel, etc.
  34.128 +     * This level is initialized to <CODE>700</CODE>.
  34.129 +     */
  34.130 +    public static final Level CONFIG = new Level("CONFIG", 700, defaultBundle);
  34.131 +
  34.132 +    /**
  34.133 +     * FINE is a message level providing tracing information.
  34.134 +     * <p>
  34.135 +     * All of FINE, FINER, and FINEST are intended for relatively
  34.136 +     * detailed tracing.  The exact meaning of the three levels will
  34.137 +     * vary between subsystems, but in general, FINEST should be used
  34.138 +     * for the most voluminous detailed output, FINER for somewhat
  34.139 +     * less detailed output, and FINE for the  lowest volume (and
  34.140 +     * most important) messages.
  34.141 +     * <p>
  34.142 +     * In general the FINE level should be used for information
  34.143 +     * that will be broadly interesting to developers who do not have
  34.144 +     * a specialized interest in the specific subsystem.
  34.145 +     * <p>
  34.146 +     * FINE messages might include things like minor (recoverable)
  34.147 +     * failures.  Issues indicating potential performance problems
  34.148 +     * are also worth logging as FINE.
  34.149 +     * This level is initialized to <CODE>500</CODE>.
  34.150 +     */
  34.151 +    public static final Level FINE = new Level("FINE", 500, defaultBundle);
  34.152 +
  34.153 +    /**
  34.154 +     * FINER indicates a fairly detailed tracing message.
  34.155 +     * By default logging calls for entering, returning, or throwing
  34.156 +     * an exception are traced at this level.
  34.157 +     * This level is initialized to <CODE>400</CODE>.
  34.158 +     */
  34.159 +    public static final Level FINER = new Level("FINER", 400, defaultBundle);
  34.160 +
  34.161 +    /**
  34.162 +     * FINEST indicates a highly detailed tracing message.
  34.163 +     * This level is initialized to <CODE>300</CODE>.
  34.164 +     */
  34.165 +    public static final Level FINEST = new Level("FINEST", 300, defaultBundle);
  34.166 +
  34.167 +    /**
  34.168 +     * ALL indicates that all messages should be logged.
  34.169 +     * This level is initialized to <CODE>Integer.MIN_VALUE</CODE>.
  34.170 +     */
  34.171 +    public static final Level ALL = new Level("ALL", Integer.MIN_VALUE, defaultBundle);
  34.172 +
  34.173 +    /**
  34.174 +     * Create a named Level with a given integer value.
  34.175 +     * <p>
  34.176 +     * Note that this constructor is "protected" to allow subclassing.
  34.177 +     * In general clients of logging should use one of the constant Level
  34.178 +     * objects such as SEVERE or FINEST.  However, if clients need to
  34.179 +     * add new logging levels, they may subclass Level and define new
  34.180 +     * constants.
  34.181 +     * @param name  the name of the Level, for example "SEVERE".
  34.182 +     * @param value an integer value for the level.
  34.183 +     * @throws NullPointerException if the name is null
  34.184 +     */
  34.185 +    protected Level(String name, int value) {
  34.186 +        this(name, value, null);
  34.187 +    }
  34.188 +
  34.189 +    /**
  34.190 +     * Create a named Level with a given integer value and a
  34.191 +     * given localization resource name.
  34.192 +     * <p>
  34.193 +     * @param name  the name of the Level, for example "SEVERE".
  34.194 +     * @param value an integer value for the level.
  34.195 +     * @param resourceBundleName name of a resource bundle to use in
  34.196 +     *    localizing the given name. If the resourceBundleName is null
  34.197 +     *    or an empty string, it is ignored.
  34.198 +     * @throws NullPointerException if the name is null
  34.199 +     */
  34.200 +    protected Level(String name, int value, String resourceBundleName) {
  34.201 +        if (name == null) {
  34.202 +            throw new NullPointerException();
  34.203 +        }
  34.204 +        this.name = name;
  34.205 +        this.value = value;
  34.206 +        this.resourceBundleName = resourceBundleName;
  34.207 +        synchronized (Level.class) {
  34.208 +            known.add(this);
  34.209 +        }
  34.210 +    }
  34.211 +
  34.212 +    /**
  34.213 +     * Return the level's localization resource bundle name, or
  34.214 +     * null if no localization bundle is defined.
  34.215 +     *
  34.216 +     * @return localization resource bundle name
  34.217 +     */
  34.218 +    public String getResourceBundleName() {
  34.219 +        return resourceBundleName;
  34.220 +    }
  34.221 +
  34.222 +    /**
  34.223 +     * Return the non-localized string name of the Level.
  34.224 +     *
  34.225 +     * @return non-localized name
  34.226 +     */
  34.227 +    public String getName() {
  34.228 +        return name;
  34.229 +    }
  34.230 +
  34.231 +    /**
  34.232 +     * Return the localized string name of the Level, for
  34.233 +     * the current default locale.
  34.234 +     * <p>
  34.235 +     * If no localization information is available, the
  34.236 +     * non-localized name is returned.
  34.237 +     *
  34.238 +     * @return localized name
  34.239 +     */
  34.240 +    public String getLocalizedName() {
  34.241 +        return getName();
  34.242 +    }
  34.243 +
  34.244 +    /**
  34.245 +     * Returns a string representation of this Level.
  34.246 +     *
  34.247 +     * @return the non-localized name of the Level, for example "INFO".
  34.248 +     */
  34.249 +    public final String toString() {
  34.250 +        return name;
  34.251 +    }
  34.252 +
  34.253 +    /**
  34.254 +     * Get the integer value for this level.  This integer value
  34.255 +     * can be used for efficient ordering comparisons between
  34.256 +     * Level objects.
  34.257 +     * @return the integer value for this level.
  34.258 +     */
  34.259 +    public final int intValue() {
  34.260 +        return value;
  34.261 +    }
  34.262 +
  34.263 +    private static final long serialVersionUID = -8176160795706313070L;
  34.264 +
  34.265 +    // Serialization magic to prevent "doppelgangers".
  34.266 +    // This is a performance optimization.
  34.267 +    private Object readResolve() {
  34.268 +        synchronized (Level.class) {
  34.269 +            for (int i = 0; i < known.size(); i++) {
  34.270 +                Level other = known.get(i);
  34.271 +                if (this.name.equals(other.name) && this.value == other.value
  34.272 +                        && (this.resourceBundleName == other.resourceBundleName ||
  34.273 +                            (this.resourceBundleName != null &&
  34.274 +                            this.resourceBundleName.equals(other.resourceBundleName)))) {
  34.275 +                    return other;
  34.276 +                }
  34.277 +            }
  34.278 +            // Woops.  Whoever sent us this object knows
  34.279 +            // about a new log level.  Add it to our list.
  34.280 +            known.add(this);
  34.281 +            return this;
  34.282 +        }
  34.283 +    }
  34.284 +
  34.285 +    /**
  34.286 +     * Parse a level name string into a Level.
  34.287 +     * <p>
  34.288 +     * The argument string may consist of either a level name
  34.289 +     * or an integer value.
  34.290 +     * <p>
  34.291 +     * For example:
  34.292 +     * <ul>
  34.293 +     * <li>     "SEVERE"
  34.294 +     * <li>     "1000"
  34.295 +     * </ul>
  34.296 +     * @param  name   string to be parsed
  34.297 +     * @throws NullPointerException if the name is null
  34.298 +     * @throws IllegalArgumentException if the value is not valid.
  34.299 +     * Valid values are integers between <CODE>Integer.MIN_VALUE</CODE>
  34.300 +     * and <CODE>Integer.MAX_VALUE</CODE>, and all known level names.
  34.301 +     * Known names are the levels defined by this class (e.g., <CODE>FINE</CODE>,
  34.302 +     * <CODE>FINER</CODE>, <CODE>FINEST</CODE>), or created by this class with
  34.303 +     * appropriate package access, or new levels defined or created
  34.304 +     * by subclasses.
  34.305 +     *
  34.306 +     * @return The parsed value. Passing an integer that corresponds to a known name
  34.307 +     * (e.g., 700) will return the associated name (e.g., <CODE>CONFIG</CODE>).
  34.308 +     * Passing an integer that does not (e.g., 1) will return a new level name
  34.309 +     * initialized to that value.
  34.310 +     */
  34.311 +    public static synchronized Level parse(String name) throws IllegalArgumentException {
  34.312 +        // Check that name is not null.
  34.313 +        name.length();
  34.314 +
  34.315 +        // Look for a known Level with the given non-localized name.
  34.316 +        for (int i = 0; i < known.size(); i++) {
  34.317 +            Level l = known.get(i);
  34.318 +            if (name.equals(l.name)) {
  34.319 +                return l;
  34.320 +            }
  34.321 +        }
  34.322 +
  34.323 +        // Now, check if the given name is an integer.  If so,
  34.324 +        // first look for a Level with the given value and then
  34.325 +        // if necessary create one.
  34.326 +        try {
  34.327 +            int x = Integer.parseInt(name);
  34.328 +            for (int i = 0; i < known.size(); i++) {
  34.329 +                Level l = known.get(i);
  34.330 +                if (l.value == x) {
  34.331 +                    return l;
  34.332 +                }
  34.333 +            }
  34.334 +            // Create a new Level.
  34.335 +            return new Level(name, x);
  34.336 +        } catch (NumberFormatException ex) {
  34.337 +            // Not an integer.
  34.338 +            // Drop through.
  34.339 +        }
  34.340 +
  34.341 +        // Finally, look for a known level with the given localized name,
  34.342 +        // in the current default locale.
  34.343 +        // This is relatively expensive, but not excessively so.
  34.344 +        for (int i = 0; i < known.size(); i++) {
  34.345 +            Level l =  known.get(i);
  34.346 +            if (name.equals(l.getLocalizedName())) {
  34.347 +                return l;
  34.348 +            }
  34.349 +        }
  34.350 +
  34.351 +        // OK, we've tried everything and failed
  34.352 +        throw new IllegalArgumentException("Bad level \"" + name + "\"");
  34.353 +    }
  34.354 +
  34.355 +    /**
  34.356 +     * Compare two objects for value equality.
  34.357 +     * @return true if and only if the two objects have the same level value.
  34.358 +     */
  34.359 +    public boolean equals(Object ox) {
  34.360 +        try {
  34.361 +            Level lx = (Level)ox;
  34.362 +            return (lx.value == this.value);
  34.363 +        } catch (Exception ex) {
  34.364 +            return false;
  34.365 +        }
  34.366 +    }
  34.367 +
  34.368 +    /**
  34.369 +     * Generate a hashcode.
  34.370 +     * @return a hashcode based on the level value
  34.371 +     */
  34.372 +    public int hashCode() {
  34.373 +        return this.value;
  34.374 +    }
  34.375 +}
    35.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    35.2 +++ b/rt/emul/compact/src/main/java/java/util/logging/LogRecord.java	Sun Sep 08 11:22:51 2013 +0200
    35.3 @@ -0,0 +1,468 @@
    35.4 +/*
    35.5 + * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
    35.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    35.7 + *
    35.8 + * This code is free software; you can redistribute it and/or modify it
    35.9 + * under the terms of the GNU General Public License version 2 only, as
   35.10 + * published by the Free Software Foundation.  Oracle designates this
   35.11 + * particular file as subject to the "Classpath" exception as provided
   35.12 + * by Oracle in the LICENSE file that accompanied this code.
   35.13 + *
   35.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   35.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   35.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   35.17 + * version 2 for more details (a copy is included in the LICENSE file that
   35.18 + * accompanied this code).
   35.19 + *
   35.20 + * You should have received a copy of the GNU General Public License version
   35.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   35.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   35.23 + *
   35.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   35.25 + * or visit www.oracle.com if you need additional information or have any
   35.26 + * questions.
   35.27 + */
   35.28 +
   35.29 +package java.util.logging;
   35.30 +import java.io.*;
   35.31 +
   35.32 +/**
   35.33 + * LogRecord objects are used to pass logging requests between
   35.34 + * the logging framework and individual log Handlers.
   35.35 + * <p>
   35.36 + * When a LogRecord is passed into the logging framework it
   35.37 + * logically belongs to the framework and should no longer be
   35.38 + * used or updated by the client application.
   35.39 + * <p>
   35.40 + * Note that if the client application has not specified an
   35.41 + * explicit source method name and source class name, then the
   35.42 + * LogRecord class will infer them automatically when they are
   35.43 + * first accessed (due to a call on getSourceMethodName or
   35.44 + * getSourceClassName) by analyzing the call stack.  Therefore,
   35.45 + * if a logging Handler wants to pass off a LogRecord to another
   35.46 + * thread, or to transmit it over RMI, and if it wishes to subsequently
   35.47 + * obtain method name or class name information it should call
   35.48 + * one of getSourceClassName or getSourceMethodName to force
   35.49 + * the values to be filled in.
   35.50 + * <p>
   35.51 + * <b> Serialization notes:</b>
   35.52 + * <ul>
   35.53 + * <li>The LogRecord class is serializable.
   35.54 + *
   35.55 + * <li> Because objects in the parameters array may not be serializable,
   35.56 + * during serialization all objects in the parameters array are
   35.57 + * written as the corresponding Strings (using Object.toString).
   35.58 + *
   35.59 + * <li> The ResourceBundle is not transmitted as part of the serialized
   35.60 + * form, but the resource bundle name is, and the recipient object's
   35.61 + * readObject method will attempt to locate a suitable resource bundle.
   35.62 + *
   35.63 + * </ul>
   35.64 + *
   35.65 + * @since 1.4
   35.66 + */
   35.67 +
   35.68 +public class LogRecord implements java.io.Serializable {
   35.69 +    private static long globalSequenceNumber = 0;
   35.70 +
   35.71 +    /**
   35.72 +     * The default value of threadID will be the current thread's
   35.73 +     * thread id, for ease of correlation, unless it is greater than
   35.74 +     * MIN_SEQUENTIAL_THREAD_ID, in which case we try harder to keep
   35.75 +     * our promise to keep threadIDs unique by avoiding collisions due
   35.76 +     * to 32-bit wraparound.  Unfortunately, LogRecord.getThreadID()
   35.77 +     * returns int, while Thread.getId() returns long.
   35.78 +     */
   35.79 +    private static final int MIN_SEQUENTIAL_THREAD_ID = Integer.MAX_VALUE / 2;
   35.80 +
   35.81 +    /**
   35.82 +     * @serial Logging message level
   35.83 +     */
   35.84 +    private Level level;
   35.85 +
   35.86 +    /**
   35.87 +     * @serial Sequence number
   35.88 +     */
   35.89 +    private long sequenceNumber;
   35.90 +
   35.91 +    /**
   35.92 +     * @serial Class that issued logging call
   35.93 +     */
   35.94 +    private String sourceClassName;
   35.95 +
   35.96 +    /**
   35.97 +     * @serial Method that issued logging call
   35.98 +     */
   35.99 +    private String sourceMethodName;
  35.100 +
  35.101 +    /**
  35.102 +     * @serial Non-localized raw message text
  35.103 +     */
  35.104 +    private String message;
  35.105 +
  35.106 +    /**
  35.107 +     * @serial Thread ID for thread that issued logging call.
  35.108 +     */
  35.109 +    private int threadID;
  35.110 +
  35.111 +    /**
  35.112 +     * @serial Event time in milliseconds since 1970
  35.113 +     */
  35.114 +    private long millis;
  35.115 +
  35.116 +    /**
  35.117 +     * @serial The Throwable (if any) associated with log message
  35.118 +     */
  35.119 +    private Throwable thrown;
  35.120 +
  35.121 +    /**
  35.122 +     * @serial Name of the source Logger.
  35.123 +     */
  35.124 +    private String loggerName;
  35.125 +
  35.126 +    /**
  35.127 +     * @serial Resource bundle name to localized log message.
  35.128 +     */
  35.129 +    private String resourceBundleName;
  35.130 +
  35.131 +    private transient boolean needToInferCaller;
  35.132 +    private transient Object parameters[];
  35.133 +
  35.134 +    /**
  35.135 +     * Returns the default value for a new LogRecord's threadID.
  35.136 +     */
  35.137 +    private int defaultThreadID() {
  35.138 +        return 0;
  35.139 +    }
  35.140 +
  35.141 +    /**
  35.142 +     * Construct a LogRecord with the given level and message values.
  35.143 +     * <p>
  35.144 +     * The sequence property will be initialized with a new unique value.
  35.145 +     * These sequence values are allocated in increasing order within a VM.
  35.146 +     * <p>
  35.147 +     * The millis property will be initialized to the current time.
  35.148 +     * <p>
  35.149 +     * The thread ID property will be initialized with a unique ID for
  35.150 +     * the current thread.
  35.151 +     * <p>
  35.152 +     * All other properties will be initialized to "null".
  35.153 +     *
  35.154 +     * @param level  a logging level value
  35.155 +     * @param msg  the raw non-localized logging message (may be null)
  35.156 +     */
  35.157 +    public LogRecord(Level level, String msg) {
  35.158 +        // Make sure level isn't null, by calling random method.
  35.159 +        level.getClass();
  35.160 +        this.level = level;
  35.161 +        message = msg;
  35.162 +        // Assign a thread ID and a unique sequence number.
  35.163 +        sequenceNumber = globalSequenceNumber++;
  35.164 +        threadID = defaultThreadID();
  35.165 +        millis = System.currentTimeMillis();
  35.166 +        needToInferCaller = true;
  35.167 +   }
  35.168 +
  35.169 +    /**
  35.170 +     * Get the source Logger's name.
  35.171 +     *
  35.172 +     * @return source logger name (may be null)
  35.173 +     */
  35.174 +    public String getLoggerName() {
  35.175 +        return loggerName;
  35.176 +    }
  35.177 +
  35.178 +    /**
  35.179 +     * Set the source Logger's name.
  35.180 +     *
  35.181 +     * @param name   the source logger name (may be null)
  35.182 +     */
  35.183 +    public void setLoggerName(String name) {
  35.184 +        loggerName = name;
  35.185 +    }
  35.186 +
  35.187 +    /**
  35.188 +     * Get the localization resource bundle
  35.189 +     * <p>
  35.190 +     * This is the ResourceBundle that should be used to localize
  35.191 +     * the message string before formatting it.  The result may
  35.192 +     * be null if the message is not localizable, or if no suitable
  35.193 +     * ResourceBundle is available.
  35.194 +     */
  35.195 +//    public ResourceBundle getResourceBundle() {
  35.196 +//        return resourceBundle;
  35.197 +//    }
  35.198 +
  35.199 +    /**
  35.200 +     * Set the localization resource bundle.
  35.201 +     *
  35.202 +     * @param bundle  localization bundle (may be null)
  35.203 +     */
  35.204 +//    public void setResourceBundle(ResourceBundle bundle) {
  35.205 +//        resourceBundle = bundle;
  35.206 +//    }
  35.207 +
  35.208 +    /**
  35.209 +     * Get the localization resource bundle name
  35.210 +     * <p>
  35.211 +     * This is the name for the ResourceBundle that should be
  35.212 +     * used to localize the message string before formatting it.
  35.213 +     * The result may be null if the message is not localizable.
  35.214 +     */
  35.215 +    public String getResourceBundleName() {
  35.216 +        return resourceBundleName;
  35.217 +    }
  35.218 +
  35.219 +    /**
  35.220 +     * Set the localization resource bundle name.
  35.221 +     *
  35.222 +     * @param name  localization bundle name (may be null)
  35.223 +     */
  35.224 +    public void setResourceBundleName(String name) {
  35.225 +        resourceBundleName = name;
  35.226 +    }
  35.227 +
  35.228 +    /**
  35.229 +     * Get the logging message level, for example Level.SEVERE.
  35.230 +     * @return the logging message level
  35.231 +     */
  35.232 +    public Level getLevel() {
  35.233 +        return level;
  35.234 +    }
  35.235 +
  35.236 +    /**
  35.237 +     * Set the logging message level, for example Level.SEVERE.
  35.238 +     * @param level the logging message level
  35.239 +     */
  35.240 +    public void setLevel(Level level) {
  35.241 +        if (level == null) {
  35.242 +            throw new NullPointerException();
  35.243 +        }
  35.244 +        this.level = level;
  35.245 +    }
  35.246 +
  35.247 +    /**
  35.248 +     * Get the sequence number.
  35.249 +     * <p>
  35.250 +     * Sequence numbers are normally assigned in the LogRecord
  35.251 +     * constructor, which assigns unique sequence numbers to
  35.252 +     * each new LogRecord in increasing order.
  35.253 +     * @return the sequence number
  35.254 +     */
  35.255 +    public long getSequenceNumber() {
  35.256 +        return sequenceNumber;
  35.257 +    }
  35.258 +
  35.259 +    /**
  35.260 +     * Set the sequence number.
  35.261 +     * <p>
  35.262 +     * Sequence numbers are normally assigned in the LogRecord constructor,
  35.263 +     * so it should not normally be necessary to use this method.
  35.264 +     */
  35.265 +    public void setSequenceNumber(long seq) {
  35.266 +        sequenceNumber = seq;
  35.267 +    }
  35.268 +
  35.269 +    /**
  35.270 +     * Get the  name of the class that (allegedly) issued the logging request.
  35.271 +     * <p>
  35.272 +     * Note that this sourceClassName is not verified and may be spoofed.
  35.273 +     * This information may either have been provided as part of the
  35.274 +     * logging call, or it may have been inferred automatically by the
  35.275 +     * logging framework.  In the latter case, the information may only
  35.276 +     * be approximate and may in fact describe an earlier call on the
  35.277 +     * stack frame.
  35.278 +     * <p>
  35.279 +     * May be null if no information could be obtained.
  35.280 +     *
  35.281 +     * @return the source class name
  35.282 +     */
  35.283 +    public String getSourceClassName() {
  35.284 +        return sourceClassName;
  35.285 +    }
  35.286 +
  35.287 +    /**
  35.288 +     * Set the name of the class that (allegedly) issued the logging request.
  35.289 +     *
  35.290 +     * @param sourceClassName the source class name (may be null)
  35.291 +     */
  35.292 +    public void setSourceClassName(String sourceClassName) {
  35.293 +        this.sourceClassName = sourceClassName;
  35.294 +        needToInferCaller = false;
  35.295 +    }
  35.296 +
  35.297 +    /**
  35.298 +     * Get the  name of the method that (allegedly) issued the logging request.
  35.299 +     * <p>
  35.300 +     * Note that this sourceMethodName is not verified and may be spoofed.
  35.301 +     * This information may either have been provided as part of the
  35.302 +     * logging call, or it may have been inferred automatically by the
  35.303 +     * logging framework.  In the latter case, the information may only
  35.304 +     * be approximate and may in fact describe an earlier call on the
  35.305 +     * stack frame.
  35.306 +     * <p>
  35.307 +     * May be null if no information could be obtained.
  35.308 +     *
  35.309 +     * @return the source method name
  35.310 +     */
  35.311 +    public String getSourceMethodName() {
  35.312 +        return sourceMethodName;
  35.313 +    }
  35.314 +
  35.315 +    /**
  35.316 +     * Set the name of the method that (allegedly) issued the logging request.
  35.317 +     *
  35.318 +     * @param sourceMethodName the source method name (may be null)
  35.319 +     */
  35.320 +    public void setSourceMethodName(String sourceMethodName) {
  35.321 +        this.sourceMethodName = sourceMethodName;
  35.322 +        needToInferCaller = false;
  35.323 +    }
  35.324 +
  35.325 +    /**
  35.326 +     * Get the "raw" log message, before localization or formatting.
  35.327 +     * <p>
  35.328 +     * May be null, which is equivalent to the empty string "".
  35.329 +     * <p>
  35.330 +     * This message may be either the final text or a localization key.
  35.331 +     * <p>
  35.332 +     * During formatting, if the source logger has a localization
  35.333 +     * ResourceBundle and if that ResourceBundle has an entry for
  35.334 +     * this message string, then the message string is replaced
  35.335 +     * with the localized value.
  35.336 +     *
  35.337 +     * @return the raw message string
  35.338 +     */
  35.339 +    public String getMessage() {
  35.340 +        return message;
  35.341 +    }
  35.342 +
  35.343 +    /**
  35.344 +     * Set the "raw" log message, before localization or formatting.
  35.345 +     *
  35.346 +     * @param message the raw message string (may be null)
  35.347 +     */
  35.348 +    public void setMessage(String message) {
  35.349 +        this.message = message;
  35.350 +    }
  35.351 +
  35.352 +    /**
  35.353 +     * Get the parameters to the log message.
  35.354 +     *
  35.355 +     * @return the log message parameters.  May be null if
  35.356 +     *                  there are no parameters.
  35.357 +     */
  35.358 +    public Object[] getParameters() {
  35.359 +        return parameters;
  35.360 +    }
  35.361 +
  35.362 +    /**
  35.363 +     * Set the parameters to the log message.
  35.364 +     *
  35.365 +     * @param parameters the log message parameters. (may be null)
  35.366 +     */
  35.367 +    public void setParameters(Object parameters[]) {
  35.368 +        this.parameters = parameters;
  35.369 +    }
  35.370 +
  35.371 +    /**
  35.372 +     * Get an identifier for the thread where the message originated.
  35.373 +     * <p>
  35.374 +     * This is a thread identifier within the Java VM and may or
  35.375 +     * may not map to any operating system ID.
  35.376 +     *
  35.377 +     * @return thread ID
  35.378 +     */
  35.379 +    public int getThreadID() {
  35.380 +        return threadID;
  35.381 +    }
  35.382 +
  35.383 +    /**
  35.384 +     * Set an identifier for the thread where the message originated.
  35.385 +     * @param threadID  the thread ID
  35.386 +     */
  35.387 +    public void setThreadID(int threadID) {
  35.388 +        this.threadID = threadID;
  35.389 +    }
  35.390 +
  35.391 +    /**
  35.392 +     * Get event time in milliseconds since 1970.
  35.393 +     *
  35.394 +     * @return event time in millis since 1970
  35.395 +     */
  35.396 +    public long getMillis() {
  35.397 +        return millis;
  35.398 +    }
  35.399 +
  35.400 +    /**
  35.401 +     * Set event time.
  35.402 +     *
  35.403 +     * @param millis event time in millis since 1970
  35.404 +     */
  35.405 +    public void setMillis(long millis) {
  35.406 +        this.millis = millis;
  35.407 +    }
  35.408 +
  35.409 +    /**
  35.410 +     * Get any throwable associated with the log record.
  35.411 +     * <p>
  35.412 +     * If the event involved an exception, this will be the
  35.413 +     * exception object. Otherwise null.
  35.414 +     *
  35.415 +     * @return a throwable
  35.416 +     */
  35.417 +    public Throwable getThrown() {
  35.418 +        return thrown;
  35.419 +    }
  35.420 +
  35.421 +    /**
  35.422 +     * Set a throwable associated with the log event.
  35.423 +     *
  35.424 +     * @param thrown  a throwable (may be null)
  35.425 +     */
  35.426 +    public void setThrown(Throwable thrown) {
  35.427 +        this.thrown = thrown;
  35.428 +    }
  35.429 +
  35.430 +    private static final long serialVersionUID = 5372048053134512534L;
  35.431 +
  35.432 +    /**
  35.433 +     * @serialData Default fields, followed by a two byte version number
  35.434 +     * (major byte, followed by minor byte), followed by information on
  35.435 +     * the log record parameter array.  If there is no parameter array,
  35.436 +     * then -1 is written.  If there is a parameter array (possible of zero
  35.437 +     * length) then the array length is written as an integer, followed
  35.438 +     * by String values for each parameter.  If a parameter is null, then
  35.439 +     * a null String is written.  Otherwise the output of Object.toString()
  35.440 +     * is written.
  35.441 +     */
  35.442 +    private void writeObject(ObjectOutputStream out) throws IOException {
  35.443 +        // We have to call defaultWriteObject first.
  35.444 +        out.defaultWriteObject();
  35.445 +
  35.446 +        // Write our version number.
  35.447 +        out.writeByte(1);
  35.448 +        out.writeByte(0);
  35.449 +        if (parameters == null) {
  35.450 +            out.writeInt(-1);
  35.451 +            return;
  35.452 +        }
  35.453 +        out.writeInt(parameters.length);
  35.454 +        // Write string values for the parameters.
  35.455 +        for (int i = 0; i < parameters.length; i++) {
  35.456 +            if (parameters[i] == null) {
  35.457 +                out.writeObject(null);
  35.458 +            } else {
  35.459 +                out.writeObject(parameters[i].toString());
  35.460 +            }
  35.461 +        }
  35.462 +    }
  35.463 +
  35.464 +
  35.465 +    private boolean isLoggerImplFrame(String cname) {
  35.466 +        // the log record could be created for a platform logger
  35.467 +        return (cname.equals("java.util.logging.Logger") ||
  35.468 +                cname.startsWith("java.util.logging.LoggingProxyImpl") ||
  35.469 +                cname.startsWith("sun.util.logging."));
  35.470 +    }
  35.471 +}
    36.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    36.2 +++ b/rt/emul/compact/src/main/java/java/util/logging/Logger.java	Sun Sep 08 11:22:51 2013 +0200
    36.3 @@ -0,0 +1,1250 @@
    36.4 +/*
    36.5 + * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
    36.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    36.7 + *
    36.8 + * This code is free software; you can redistribute it and/or modify it
    36.9 + * under the terms of the GNU General Public License version 2 only, as
   36.10 + * published by the Free Software Foundation.  Oracle designates this
   36.11 + * particular file as subject to the "Classpath" exception as provided
   36.12 + * by Oracle in the LICENSE file that accompanied this code.
   36.13 + *
   36.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
   36.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   36.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   36.17 + * version 2 for more details (a copy is included in the LICENSE file that
   36.18 + * accompanied this code).
   36.19 + *
   36.20 + * You should have received a copy of the GNU General Public License version
   36.21 + * 2 along with this work; if not, write to the Free Software Foundation,
   36.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   36.23 + *
   36.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   36.25 + * or visit www.oracle.com if you need additional information or have any
   36.26 + * questions.
   36.27 + */
   36.28 +
   36.29 +
   36.30 +package java.util.logging;
   36.31 +
   36.32 +import java.util.HashMap;
   36.33 +import java.util.Map;
   36.34 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
   36.35 +
   36.36 +/**
   36.37 + * A Logger object is used to log messages for a specific
   36.38 + * system or application component.  Loggers are normally named,
   36.39 + * using a hierarchical dot-separated namespace.  Logger names
   36.40 + * can be arbitrary strings, but they should normally be based on
   36.41 + * the package name or class name of the logged component, such
   36.42 + * as java.net or javax.swing.  In addition it is possible to create
   36.43 + * "anonymous" Loggers that are not stored in the Logger namespace.
   36.44 + * <p>
   36.45 + * Logger objects may be obtained by calls on one of the getLogger
   36.46 + * factory methods.  These will either create a new Logger or
   36.47 + * return a suitable existing Logger. It is important to note that
   36.48 + * the Logger returned by one of the {@code getLogger} factory methods
   36.49 + * may be garbage collected at any time if a strong reference to the
   36.50 + * Logger is not kept.
   36.51 + * <p>
   36.52 + * Logging messages will be forwarded to registered Handler
   36.53 + * objects, which can forward the messages to a variety of
   36.54 + * destinations, including consoles, files, OS logs, etc.
   36.55 + * <p>
   36.56 + * Each Logger keeps track of a "parent" Logger, which is its
   36.57 + * nearest existing ancestor in the Logger namespace.
   36.58 + * <p>
   36.59 + * Each Logger has a "Level" associated with it.  This reflects
   36.60 + * a minimum Level that this logger cares about.  If a Logger's
   36.61 + * level is set to <tt>null</tt>, then its effective level is inherited
   36.62 + * from its parent, which may in turn obtain it recursively from its
   36.63 + * parent, and so on up the tree.
   36.64 + * <p>
   36.65 + * The log level can be configured based on the properties from the
   36.66 + * logging configuration file, as described in the description
   36.67 + * of the LogManager class.  However it may also be dynamically changed
   36.68 + * by calls on the Logger.setLevel method.  If a logger's level is
   36.69 + * changed the change may also affect child loggers, since any child
   36.70 + * logger that has <tt>null</tt> as its level will inherit its
   36.71 + * effective level from its parent.
   36.72 + * <p>
   36.73 + * On each logging call the Logger initially performs a cheap
   36.74 + * check of the request level (e.g., SEVERE or FINE) against the
   36.75 + * effective log level of the logger.  If the request level is
   36.76 + * lower than the log level, the logging call returns immediately.
   36.77 + * <p>
   36.78 + * After passing this initial (cheap) test, the Logger will allocate
   36.79 + * a LogRecord to describe the logging message.  It will then call a
   36.80 + * Filter (if present) to do a more detailed check on whether the
   36.81 + * record should be published.  If that passes it will then publish
   36.82 + * the LogRecord to its output Handlers.  By default, loggers also
   36.83 + * publish to their parent's Handlers, recursively up the tree.
   36.84 + * <p>
   36.85 + * Each Logger may have a ResourceBundle name associated with it.
   36.86 + * The named bundle will be used for localizing logging messages.
   36.87 + * If a Logger does not have its own ResourceBundle name, then
   36.88 + * it will inherit the ResourceBundle name from its parent,
   36.89 + * recursively up the tree.
   36.90 + * <p>
   36.91 + * Most of the logger output methods take a "msg" argument.  This
   36.92 + * msg argument may be either a raw value or a localization key.
   36.93 + * During formatting, if the logger has (or inherits) a localization
   36.94 + * ResourceBundle and if the ResourceBundle has a mapping for the msg
   36.95 + * string, then the msg string is replaced by the localized value.
   36.96 + * Otherwise the original msg string is used.  Typically, formatters use
   36.97 + * java.text.MessageFormat style formatting to format parameters, so
   36.98 + * for example a format string "{0} {1}" would format two parameters
   36.99 + * as strings.
  36.100 + * <p>
  36.101 + * When mapping ResourceBundle names to ResourceBundles, the Logger
  36.102 + * will first try to use the Thread's ContextClassLoader.  If that
  36.103 + * is null it will try the SystemClassLoader instead.  As a temporary
  36.104 + * transition feature in the initial implementation, if the Logger is
  36.105 + * unable to locate a ResourceBundle from the ContextClassLoader or
  36.106 + * SystemClassLoader the Logger will also search up the class stack
  36.107 + * and use successive calling ClassLoaders to try to locate a ResourceBundle.
  36.108 + * (This call stack search is to allow containers to transition to
  36.109 + * using ContextClassLoaders and is likely to be removed in future
  36.110 + * versions.)
  36.111 + * <p>
  36.112 + * Formatting (including localization) is the responsibility of
  36.113 + * the output Handler, which will typically call a Formatter.
  36.114 + * <p>
  36.115 + * Note that formatting need not occur synchronously.  It may be delayed
  36.116 + * until a LogRecord is actually written to an external sink.
  36.117 + * <p>
  36.118 + * The logging methods are grouped in five main categories:
  36.119 + * <ul>
  36.120 + * <li><p>
  36.121 + *     There are a set of "log" methods that take a log level, a message
  36.122 + *     string, and optionally some parameters to the message string.
  36.123 + * <li><p>
  36.124 + *     There are a set of "logp" methods (for "log precise") that are
  36.125 + *     like the "log" methods, but also take an explicit source class name
  36.126 + *     and method name.
  36.127 + * <li><p>
  36.128 + *     There are a set of "logrb" method (for "log with resource bundle")
  36.129 + *     that are like the "logp" method, but also take an explicit resource
  36.130 + *     bundle name for use in localizing the log message.
  36.131 + * <li><p>
  36.132 + *     There are convenience methods for tracing method entries (the
  36.133 + *     "entering" methods), method returns (the "exiting" methods) and
  36.134 + *     throwing exceptions (the "throwing" methods).
  36.135 + * <li><p>
  36.136 + *     Finally, there are a set of convenience methods for use in the
  36.137 + *     very simplest cases, when a developer simply wants to log a
  36.138 + *     simple string at a given log level.  These methods are named
  36.139 + *     after the standard Level names ("severe", "warning", "info", etc.)
  36.140 + *     and take a single argument, a message string.
  36.141 + * </ul>
  36.142 + * <p>
  36.143 + * For the methods that do not take an explicit source name and
  36.144 + * method name, the Logging framework will make a "best effort"
  36.145 + * to determine which class and method called into the logging method.
  36.146 + * However, it is important to realize that this automatically inferred
  36.147 + * information may only be approximate (or may even be quite wrong!).
  36.148 + * Virtual machines are allowed to do extensive optimizations when
  36.149 + * JITing and may entirely remove stack frames, making it impossible
  36.150 + * to reliably locate the calling class and method.
  36.151 + * <P>
  36.152 + * All methods on Logger are multi-thread safe.
  36.153 + * <p>
  36.154 + * <b>Subclassing Information:</b> Note that a LogManager class may
  36.155 + * provide its own implementation of named Loggers for any point in
  36.156 + * the namespace.  Therefore, any subclasses of Logger (unless they
  36.157 + * are implemented in conjunction with a new LogManager class) should
  36.158 + * take care to obtain a Logger instance from the LogManager class and
  36.159 + * should delegate operations such as "isLoggable" and "log(LogRecord)"
  36.160 + * to that instance.  Note that in order to intercept all logging
  36.161 + * output, subclasses need only override the log(LogRecord) method.
  36.162 + * All the other logging methods are implemented as calls on this
  36.163 + * log(LogRecord) method.
  36.164 + *
  36.165 + * @since 1.4
  36.166 + */
  36.167 +
  36.168 +
  36.169 +public class Logger {
  36.170 +    private static int offValue = Level.OFF.intValue();
  36.171 +    private static final Map<String,Logger> ALL = new HashMap<>();
  36.172 +    private String name;
  36.173 +
  36.174 +    private volatile int levelValue;  // current effective level value
  36.175 +    private Level levelObject;
  36.176 +    
  36.177 +    /**
  36.178 +     * GLOBAL_LOGGER_NAME is a name for the global logger.
  36.179 +     *
  36.180 +     * @since 1.6
  36.181 +     */
  36.182 +    public static final String GLOBAL_LOGGER_NAME = "global";
  36.183 +
  36.184 +    /**
  36.185 +     * Return global logger object with the name Logger.GLOBAL_LOGGER_NAME.
  36.186 +     *
  36.187 +     * @return global logger object
  36.188 +     * @since 1.7
  36.189 +     */
  36.190 +    public static final Logger getGlobal() {
  36.191 +        return global;
  36.192 +    }
  36.193 +
  36.194 +    /**
  36.195 +     * The "global" Logger object is provided as a convenience to developers
  36.196 +     * who are making casual use of the Logging package.  Developers
  36.197 +     * who are making serious use of the logging package (for example
  36.198 +     * in products) should create and use their own Logger objects,
  36.199 +     * with appropriate names, so that logging can be controlled on a
  36.200 +     * suitable per-Logger granularity. Developers also need to keep a
  36.201 +     * strong reference to their Logger objects to prevent them from
  36.202 +     * being garbage collected.
  36.203 +     * <p>
  36.204 +     * @deprecated Initialization of this field is prone to deadlocks.
  36.205 +     * The field must be initialized by the Logger class initialization
  36.206 +     * which may cause deadlocks with the LogManager class initialization.
  36.207 +     * In such cases two class initialization wait for each other to complete.
  36.208 +     * The preferred way to get the global logger object is via the call
  36.209 +     * <code>Logger.getGlobal()</code>.
  36.210 +     * For compatibility with old JDK versions where the
  36.211 +     * <code>Logger.getGlobal()</code> is not available use the call
  36.212 +     * <code>Logger.getLogger(Logger.GLOBAL_LOGGER_NAME)</code>
  36.213 +     * or <code>Logger.getLogger("global")</code>.
  36.214 +     */
  36.215 +    @Deprecated
  36.216 +    public static final Logger global = new Logger(GLOBAL_LOGGER_NAME);
  36.217 +
  36.218 +    /**
  36.219 +     * Protected method to construct a logger for a named subsystem.
  36.220 +     * <p>
  36.221 +     * The logger will be initially configured with a null Level
  36.222 +     * and with useParentHandlers set to true.
  36.223 +     *
  36.224 +     * @param   name    A name for the logger.  This should
  36.225 +     *                          be a dot-separated name and should normally
  36.226 +     *                          be based on the package name or class name
  36.227 +     *                          of the subsystem, such as java.net
  36.228 +     *                          or javax.swing.  It may be null for anonymous Loggers.
  36.229 +     * @param   resourceBundleName  name of ResourceBundle to be used for localizing
  36.230 +     *                          messages for this logger.  May be null if none
  36.231 +     *                          of the messages require localization.
  36.232 +     * @throws MissingResourceException if the resourceBundleName is non-null and
  36.233 +     *             no corresponding resource can be found.
  36.234 +     */
  36.235 +    protected Logger(String name, String resourceBundleName) {
  36.236 +        this.name = name;
  36.237 +        levelValue = Level.INFO.intValue();
  36.238 +    }
  36.239 +
  36.240 +    // This constructor is used only to create the global Logger.
  36.241 +    // It is needed to break a cyclic dependence between the LogManager
  36.242 +    // and Logger static initializers causing deadlocks.
  36.243 +    private Logger(String name) {
  36.244 +        // The manager field is not initialized here.
  36.245 +        this.name = name;
  36.246 +        levelValue = Level.INFO.intValue();
  36.247 +    }
  36.248 +
  36.249 +    private void checkAccess() throws SecurityException {
  36.250 +        throw new SecurityException();
  36.251 +    }
  36.252 +
  36.253 +    /**
  36.254 +     * Find or create a logger for a named subsystem.  If a logger has
  36.255 +     * already been created with the given name it is returned.  Otherwise
  36.256 +     * a new logger is created.
  36.257 +     * <p>
  36.258 +     * If a new logger is created its log level will be configured
  36.259 +     * based on the LogManager configuration and it will configured
  36.260 +     * to also send logging output to its parent's Handlers.  It will
  36.261 +     * be registered in the LogManager global namespace.
  36.262 +     * <p>
  36.263 +     * Note: The LogManager may only retain a weak reference to the newly
  36.264 +     * created Logger. It is important to understand that a previously
  36.265 +     * created Logger with the given name may be garbage collected at any
  36.266 +     * time if there is no strong reference to the Logger. In particular,
  36.267 +     * this means that two back-to-back calls like
  36.268 +     * {@code getLogger("MyLogger").log(...)} may use different Logger
  36.269 +     * objects named "MyLogger" if there is no strong reference to the
  36.270 +     * Logger named "MyLogger" elsewhere in the program.
  36.271 +     *
  36.272 +     * @param   name            A name for the logger.  This should
  36.273 +     *                          be a dot-separated name and should normally
  36.274 +     *                          be based on the package name or class name
  36.275 +     *                          of the subsystem, such as java.net
  36.276 +     *                          or javax.swing
  36.277 +     * @return a suitable Logger
  36.278 +     * @throws NullPointerException if the name is null.
  36.279 +     */
  36.280 +
  36.281 +    // Synchronization is not required here. All synchronization for
  36.282 +    // adding a new Logger object is handled by LogManager.addLogger().
  36.283 +    public static Logger getLogger(String name) {
  36.284 +        return getLogger(name, null);
  36.285 +    }
  36.286 +
  36.287 +    /**
  36.288 +     * Find or create a logger for a named subsystem.  If a logger has
  36.289 +     * already been created with the given name it is returned.  Otherwise
  36.290 +     * a new logger is created.
  36.291 +     * <p>
  36.292 +     * If a new logger is created its log level will be configured
  36.293 +     * based on the LogManager and it will configured to also send logging
  36.294 +     * output to its parent's Handlers.  It will be registered in
  36.295 +     * the LogManager global namespace.
  36.296 +     * <p>
  36.297 +     * Note: The LogManager may only retain a weak reference to the newly
  36.298 +     * created Logger. It is important to understand that a previously
  36.299 +     * created Logger with the given name may be garbage collected at any
  36.300 +     * time if there is no strong reference to the Logger. In particular,
  36.301 +     * this means that two back-to-back calls like
  36.302 +     * {@code getLogger("MyLogger", ...).log(...)} may use different Logger
  36.303 +     * objects named "MyLogger" if there is no strong reference to the
  36.304 +     * Logger named "MyLogger" elsewhere in the program.
  36.305 +     * <p>
  36.306 +     * If the named Logger already exists and does not yet have a
  36.307 +     * localization resource bundle then the given resource bundle
  36.308 +     * name is used.  If the named Logger already exists and has
  36.309 +     * a different resource bundle name then an IllegalArgumentException
  36.310 +     * is thrown.
  36.311 +     * <p>
  36.312 +     * @param   name    A name for the logger.  This should
  36.313 +     *                          be a dot-separated name and should normally
  36.314 +     *                          be based on the package name or class name
  36.315 +     *                          of the subsystem, such as java.net
  36.316 +     *                          or javax.swing
  36.317 +     * @param   resourceBundleName  name of ResourceBundle to be used for localizing
  36.318 +     *                          messages for this logger. May be <CODE>null</CODE> if none of
  36.319 +     *                          the messages require localization.
  36.320 +     * @return a suitable Logger
  36.321 +     * @throws MissingResourceException if the resourceBundleName is non-null and
  36.322 +     *             no corresponding resource can be found.
  36.323 +     * @throws IllegalArgumentException if the Logger already exists and uses
  36.324 +     *             a different resource bundle name.
  36.325 +     * @throws NullPointerException if the name is null.
  36.326 +     */
  36.327 +
  36.328 +    // Synchronization is not required here. All synchronization for
  36.329 +    // adding a new Logger object is handled by LogManager.addLogger().
  36.330 +    public static Logger getLogger(String name, String resourceBundleName) {
  36.331 +        Logger l = ALL.get(name);
  36.332 +        if (l == null) {
  36.333 +            l = new Logger(name, resourceBundleName);
  36.334 +            ALL.put(name, l);
  36.335 +        }
  36.336 +        return l;
  36.337 +    }
  36.338 +
  36.339 +
  36.340 +    /**
  36.341 +     * Create an anonymous Logger.  The newly created Logger is not
  36.342 +     * registered in the LogManager namespace.  There will be no
  36.343 +     * access checks on updates to the logger.
  36.344 +     * <p>
  36.345 +     * This factory method is primarily intended for use from applets.
  36.346 +     * Because the resulting Logger is anonymous it can be kept private
  36.347 +     * by the creating class.  This removes the need for normal security
  36.348 +     * checks, which in turn allows untrusted applet code to update
  36.349 +     * the control state of the Logger.  For example an applet can do
  36.350 +     * a setLevel or an addHandler on an anonymous Logger.
  36.351 +     * <p>
  36.352 +     * Even although the new logger is anonymous, it is configured
  36.353 +     * to have the root logger ("") as its parent.  This means that
  36.354 +     * by default it inherits its effective level and handlers
  36.355 +     * from the root logger.
  36.356 +     * <p>
  36.357 +     *
  36.358 +     * @return a newly created private Logger
  36.359 +     */
  36.360 +    public static Logger getAnonymousLogger() {
  36.361 +        return getAnonymousLogger(null);
  36.362 +    }
  36.363 +
  36.364 +    /**
  36.365 +     * Create an anonymous Logger.  The newly created Logger is not
  36.366 +     * registered in the LogManager namespace.  There will be no
  36.367 +     * access checks on updates to the logger.
  36.368 +     * <p>
  36.369 +     * This factory method is primarily intended for use from applets.
  36.370 +     * Because the resulting Logger is anonymous it can be kept private
  36.371 +     * by the creating class.  This removes the need for normal security
  36.372 +     * checks, which in turn allows untrusted applet code to update
  36.373 +     * the control state of the Logger.  For example an applet can do
  36.374 +     * a setLevel or an addHandler on an anonymous Logger.
  36.375 +     * <p>
  36.376 +     * Even although the new logger is anonymous, it is configured
  36.377 +     * to have the root logger ("") as its parent.  This means that
  36.378 +     * by default it inherits its effective level and handlers
  36.379 +     * from the root logger.
  36.380 +     * <p>
  36.381 +     * @param   resourceBundleName  name of ResourceBundle to be used for localizing
  36.382 +     *                          messages for this logger.
  36.383 +     *          May be null if none of the messages require localization.
  36.384 +     * @return a newly created private Logger
  36.385 +     * @throws MissingResourceException if the resourceBundleName is non-null and
  36.386 +     *             no corresponding resource can be found.
  36.387 +     */
  36.388 +
  36.389 +    // Synchronization is not required here. All synchronization for
  36.390 +    // adding a new anonymous Logger object is handled by doSetParent().
  36.391 +    public static Logger getAnonymousLogger(String resourceBundleName) {
  36.392 +        return new Logger(null, resourceBundleName);
  36.393 +    }
  36.394 +
  36.395 +    /**
  36.396 +     * Retrieve the localization resource bundle for this
  36.397 +     * logger for the current default locale.  Note that if
  36.398 +     * the result is null, then the Logger will use a resource
  36.399 +     * bundle inherited from its parent.
  36.400 +     *
  36.401 +     * @return localization bundle (may be null)
  36.402 +     */
  36.403 +//    public ResourceBundle getResourceBundle() {
  36.404 +//        return findResourceBundle(getResourceBundleName());
  36.405 +//    }
  36.406 +
  36.407 +    /**
  36.408 +     * Retrieve the localization resource bundle name for this
  36.409 +     * logger.  Note that if the result is null, then the Logger
  36.410 +     * will use a resource bundle name inherited from its parent.
  36.411 +     *
  36.412 +     * @return localization bundle name (may be null)
  36.413 +     */
  36.414 +    public String getResourceBundleName() {
  36.415 +        return null;
  36.416 +    }
  36.417 +
  36.418 +    /**
  36.419 +     * Set a filter to control output on this Logger.
  36.420 +     * <P>
  36.421 +     * After passing the initial "level" check, the Logger will
  36.422 +     * call this Filter to check if a log record should really
  36.423 +     * be published.
  36.424 +     *
  36.425 +     * @param   newFilter  a filter object (may be null)
  36.426 +     * @exception  SecurityException  if a security manager exists and if
  36.427 +     *             the caller does not have LoggingPermission("control").
  36.428 +     */
  36.429 +//    public void setFilter(Filter newFilter) throws SecurityException {
  36.430 +//        checkAccess();
  36.431 +//    }
  36.432 +
  36.433 +    /**
  36.434 +     * Get the current filter for this Logger.
  36.435 +     *
  36.436 +     * @return  a filter object (may be null)
  36.437 +     */
  36.438 +//    public Filter getFilter() {
  36.439 +//        return filter;
  36.440 +//    }
  36.441 +
  36.442 +    /**
  36.443 +     * Log a LogRecord.
  36.444 +     * <p>
  36.445 +     * All the other logging methods in this class call through
  36.446 +     * this method to actually perform any logging.  Subclasses can
  36.447 +     * override this single method to capture all log activity.
  36.448 +     *
  36.449 +     * @param record the LogRecord to be published
  36.450 +     */
  36.451 +    public void log(LogRecord record) {
  36.452 +        if (record.getLevel().intValue() < levelValue) {
  36.453 +            return;
  36.454 +        }
  36.455 +
  36.456 +        consoleLog(
  36.457 +            record.getLevel().toString(), 
  36.458 +            record.getLoggerName(),
  36.459 +            record.getMessage()
  36.460 +        );
  36.461 +    }
  36.462 +    
  36.463 +    @JavaScriptBody(args = { "method", "logger", "msg" }, body = 
  36.464 +        "window.console[method]('[' + logger + ']: ' + msg);"
  36.465 +    )
  36.466 +    private static native void consoleLog(
  36.467 +        String method, String logger, String msg
  36.468 +    );
  36.469 +
  36.470 +    // private support method for logging.
  36.471 +    // We fill in the logger name, resource bundle name, and
  36.472 +    // resource bundle and then call "void log(LogRecord)".
  36.473 +    private void doLog(LogRecord lr) {
  36.474 +        doLog(lr, lr.getResourceBundleName());
  36.475 +    }
  36.476 +    private void doLog(LogRecord lr, String bundleName) {
  36.477 +        lr.setLoggerName(name);
  36.478 +        log(lr);
  36.479 +    }
  36.480 +
  36.481 +
  36.482 +    //================================================================
  36.483 +    // Start of convenience methods WITHOUT className and methodName
  36.484 +    //================================================================
  36.485 +
  36.486 +    /**
  36.487 +     * Log a message, with no arguments.
  36.488 +     * <p>
  36.489 +     * If the logger is currently enabled for the given message
  36.490 +     * level then the given message is forwarded to all the
  36.491 +     * registered output Handler objects.
  36.492 +     * <p>
  36.493 +     * @param   level   One of the message level identifiers, e.g., SEVERE
  36.494 +     * @param   msg     The string message (or a key in the message catalog)
  36.495 +     */
  36.496 +    public void log(Level level, String msg) {
  36.497 +        if (level.intValue() < levelValue || levelValue == offValue) {
  36.498 +            return;
  36.499 +        }
  36.500 +        LogRecord lr = new LogRecord(level, msg);
  36.501 +        doLog(lr);
  36.502 +    }
  36.503 +
  36.504 +    /**
  36.505 +     * Log a message, with one object parameter.
  36.506 +     * <p>
  36.507 +     * If the logger is currently enabled for the given message
  36.508 +     * level then a corresponding LogRecord is created and forwarded
  36.509 +     * to all the registered output Handler objects.
  36.510 +     * <p>
  36.511 +     * @param   level   One of the message level identifiers, e.g., SEVERE
  36.512 +     * @param   msg     The string message (or a key in the message catalog)
  36.513 +     * @param   param1  parameter to the message
  36.514 +     */
  36.515 +    public void log(Level level, String msg, Object param1) {
  36.516 +        if (level.intValue() < levelValue || levelValue == offValue) {
  36.517 +            return;
  36.518 +        }
  36.519 +        LogRecord lr = new LogRecord(level, msg);
  36.520 +        Object params[] = { param1 };
  36.521 +        lr.setParameters(params);
  36.522 +        doLog(lr);
  36.523 +    }
  36.524 +
  36.525 +    /**
  36.526 +     * Log a message, with an array of object arguments.
  36.527 +     * <p>
  36.528 +     * If the logger is currently enabled for the given message
  36.529 +     * level then a corresponding LogRecord is created and forwarded
  36.530 +     * to all the registered output Handler objects.
  36.531 +     * <p>
  36.532 +     * @param   level   One of the message level identifiers, e.g., SEVERE
  36.533 +     * @param   msg     The string message (or a key in the message catalog)
  36.534 +     * @param   params  array of parameters to the message
  36.535 +     */
  36.536 +    public void log(Level level, String msg, Object params[]) {
  36.537 +        if (level.intValue() < levelValue || levelValue == offValue) {
  36.538 +            return;
  36.539 +        }
  36.540 +        LogRecord lr = new LogRecord(level, msg);
  36.541 +        lr.setParameters(params);
  36.542 +        doLog(lr);
  36.543 +    }
  36.544 +
  36.545 +    /**
  36.546 +     * Log a message, with associated Throwable information.
  36.547 +     * <p>
  36.548 +     * If the logger is currently enabled for the given message
  36.549 +     * level then the given arguments are stored in a LogRecord
  36.550 +     * which is forwarded to all registered output handlers.
  36.551 +     * <p>
  36.552 +     * Note that the thrown argument is stored in the LogRecord thrown
  36.553 +     * property, rather than the LogRecord parameters property.  Thus is it
  36.554 +     * processed specially by output Formatters and is not treated
  36.555 +     * as a formatting parameter to the LogRecord message property.
  36.556 +     * <p>
  36.557 +     * @param   level   One of the message level identifiers, e.g., SEVERE
  36.558 +     * @param   msg     The string message (or a key in the message catalog)
  36.559 +     * @param   thrown  Throwable associated with log message.
  36.560 +     */
  36.561 +    public void log(Level level, String msg, Throwable thrown) {
  36.562 +        if (level.intValue() < levelValue || levelValue == offValue) {
  36.563 +            return;
  36.564 +        }
  36.565 +        LogRecord lr = new LogRecord(level, msg);
  36.566 +        lr.setThrown(thrown);
  36.567 +        doLog(lr);
  36.568 +    }
  36.569 +
  36.570 +    //================================================================
  36.571 +    // Start of convenience methods WITH className and methodName
  36.572 +    //================================================================
  36.573 +
  36.574 +    /**
  36.575 +     * Log a message, specifying source class and method,
  36.576 +     * with no arguments.
  36.577 +     * <p>
  36.578 +     * If the logger is currently enabled for the given message
  36.579 +     * level then the given message is forwarded to all the
  36.580 +     * registered output Handler objects.
  36.581 +     * <p>
  36.582 +     * @param   level   One of the message level identifiers, e.g., SEVERE
  36.583 +     * @param   sourceClass    name of class that issued the logging request
  36.584 +     * @param   sourceMethod   name of method that issued the logging request
  36.585 +     * @param   msg     The string message (or a key in the message catalog)
  36.586 +     */
  36.587 +    public void logp(Level level, String sourceClass, String sourceMethod, String msg) {
  36.588 +        if (level.intValue() < levelValue || levelValue == offValue) {
  36.589 +            return;
  36.590 +        }
  36.591 +        LogRecord lr = new LogRecord(level, msg);
  36.592 +        lr.setSourceClassName(sourceClass);
  36.593 +        lr.setSourceMethodName(sourceMethod);
  36.594 +        doLog(lr);
  36.595 +    }
  36.596 +
  36.597 +    /**
  36.598 +     * Log a message, specifying source class and method,
  36.599 +     * with a single object parameter to the log message.
  36.600 +     * <p>
  36.601 +     * If the logger is currently enabled for the given message
  36.602 +     * level then a corresponding LogRecord is created and forwarded
  36.603 +     * to all the registered output Handler objects.
  36.604 +     * <p>
  36.605 +     * @param   level   One of the message level identifiers, e.g., SEVERE
  36.606 +     * @param   sourceClass    name of class that issued the logging request
  36.607 +     * @param   sourceMethod   name of method that issued the logging request
  36.608 +     * @param   msg      The string message (or a key in the message catalog)
  36.609 +     * @param   param1    Parameter to the log message.
  36.610 +     */
  36.611 +    public void logp(Level level, String sourceClass, String sourceMethod,
  36.612 +                                                String msg, Object param1) {
  36.613 +        if (level.intValue() < levelValue || levelValue == offValue) {
  36.614 +            return;
  36.615 +        }
  36.616 +        LogRecord lr = new LogRecord(level, msg);
  36.617 +        lr.setSourceClassName(sourceClass);
  36.618 +        lr.setSourceMethodName(sourceMethod);
  36.619 +        Object params[] = { param1 };
  36.620 +        lr.setParameters(params);
  36.621 +        doLog(lr);
  36.622 +    }
  36.623 +
  36.624 +    /**
  36.625 +     * Log a message, specifying source class and method,
  36.626 +     * with an array of object arguments.
  36.627 +     * <p>
  36.628 +     * If the logger is currently enabled for the given message
  36.629 +     * level then a corresponding LogRecord is created and forwarded
  36.630 +     * to all the registered output Handler objects.
  36.631 +     * <p>
  36.632 +     * @param   level   One of the message level identifiers, e.g., SEVERE
  36.633 +     * @param   sourceClass    name of class that issued the logging request
  36.634 +     * @param   sourceMethod   name of method that issued the logging request
  36.635 +     * @param   msg     The string message (or a key in the message catalog)
  36.636 +     * @param   params  Array of parameters to the message
  36.637 +     */
  36.638 +    public void logp(Level level, String sourceClass, String sourceMethod,
  36.639 +                                                String msg, Object params[]) {
  36.640 +        if (level.intValue() < levelValue || levelValue == offValue) {
  36.641 +            return;
  36.642 +        }
  36.643 +        LogRecord lr = new LogRecord(level, msg);
  36.644 +        lr.setSourceClassName(sourceClass);
  36.645 +        lr.setSourceMethodName(sourceMethod);
  36.646 +        lr.setParameters(params);
  36.647 +        doLog(lr);
  36.648 +    }
  36.649 +
  36.650 +    /**
  36.651 +     * Log a message, specifying source class and method,
  36.652 +     * with associated Throwable information.
  36.653 +     * <p>
  36.654 +     * If the logger is currently enabled for the given message
  36.655 +     * level then the given arguments are stored in a LogRecord
  36.656 +     * which is forwarded to all registered output handlers.
  36.657 +     * <p>
  36.658 +     * Note that the thrown argument is stored in the LogRecord thrown
  36.659 +     * property, rather than the LogRecord parameters property.  Thus is it
  36.660 +     * processed specially by output Formatters and is not treated
  36.661 +     * as a formatting parameter to the LogRecord message property.
  36.662 +     * <p>
  36.663 +     * @param   level   One of the message level identifiers, e.g., SEVERE
  36.664 +     * @param   sourceClass    name of class that issued the logging request
  36.665 +     * @param   sourceMethod   name of method that issued the logging request
  36.666 +     * @param   msg     The string message (or a key in the message catalog)
  36.667 +     * @param   thrown  Throwable associated with log message.
  36.668 +     */
  36.669 +    public void logp(Level level, String sourceClass, String sourceMethod,
  36.670 +                                                        String msg, Throwable thrown) {
  36.671 +        if (level.intValue() < levelValue || levelValue == offValue) {
  36.672 +            return;
  36.673 +        }
  36.674 +        LogRecord lr = new LogRecord(level, msg);
  36.675 +        lr.setSourceClassName(sourceClass);
  36.676 +        lr.setSourceMethodName(sourceMethod);
  36.677 +        lr.setThrown(thrown);
  36.678 +        doLog(lr);
  36.679 +    }
  36.680 +
  36.681 +
  36.682 +    //=========================================================================
  36.683 +    // Start of convenience methods WITH className, methodName and bundle name.
  36.684 +    //=========================================================================
  36.685 +
  36.686 +
  36.687 +    /**
  36.688 +     * Log a message, specifying source class, method, and resource bundle name
  36.689 +     * with no arguments.
  36.690 +     * <p>
  36.691 +     * If the logger is currently enabled for the given message
  36.692 +     * level then the given message is forwarded to all the
  36.693 +     * registered output Handler objects.
  36.694 +     * <p>
  36.695 +     * The msg string is localized using the named resource bundle.  If the
  36.696 +     * resource bundle name is null, or an empty String or invalid
  36.697 +     * then the msg string is not localized.
  36.698 +     * <p>
  36.699 +     * @param   level   One of the message level identifiers, e.g., SEVERE
  36.700 +     * @param   sourceClass    name of class that issued the logging request
  36.701 +     * @param   sourceMethod   name of method that issued the logging request
  36.702 +     * @param   bundleName     name of resource bundle to localize msg,
  36.703 +     *                         can be null
  36.704 +     * @param   msg     The string message (or a key in the message catalog)
  36.705 +     */
  36.706 +
  36.707 +    public void logrb(Level level, String sourceClass, String sourceMethod,
  36.708 +                                String bundleName, String msg) {
  36.709 +        if (level.intValue() < levelValue || levelValue == offValue) {
  36.710 +            return;
  36.711 +        }
  36.712 +        LogRecord lr = new LogRecord(level, msg);
  36.713 +        lr.setSourceClassName(sourceClass);
  36.714 +        lr.setSourceMethodName(sourceMethod);
  36.715 +        doLog(lr, bundleName);
  36.716 +    }
  36.717 +
  36.718 +    /**
  36.719 +     * Log a message, specifying source class, method, and resource bundle name,
  36.720 +     * with a single object parameter to the log message.
  36.721 +     * <p>
  36.722 +     * If the logger is currently enabled for the given message
  36.723 +     * level then a corresponding LogRecord is created and forwarded
  36.724 +     * to all the registered output Handler objects.
  36.725 +     * <p>
  36.726 +     * The msg string is localized using the named resource bundle.  If the
  36.727 +     * resource bundle name is null, or an empty String or invalid
  36.728 +     * then the msg string is not localized.
  36.729 +     * <p>
  36.730 +     * @param   level   One of the message level identifiers, e.g., SEVERE
  36.731 +     * @param   sourceClass    name of class that issued the logging request
  36.732 +     * @param   sourceMethod   name of method that issued the logging request
  36.733 +     * @param   bundleName     name of resource bundle to localize msg,
  36.734 +     *                         can be null
  36.735 +     * @param   msg      The string message (or a key in the message catalog)
  36.736 +     * @param   param1    Parameter to the log message.
  36.737 +     */
  36.738 +    public void logrb(Level level, String sourceClass, String sourceMethod,
  36.739 +                                String bundleName, String msg, Object param1) {
  36.740 +        if (level.intValue() < levelValue || levelValue == offValue) {
  36.741 +            return;
  36.742 +        }
  36.743 +        LogRecord lr = new LogRecord(level, msg);
  36.744 +        lr.setSourceClassName(sourceClass);
  36.745 +        lr.setSourceMethodName(sourceMethod);
  36.746 +        Object params[] = { param1 };
  36.747 +        lr.setParameters(params);
  36.748 +        doLog(lr, bundleName);
  36.749 +    }
  36.750 +
  36.751 +    /**
  36.752 +     * Log a message, specifying source class, method, and resource bundle name,
  36.753 +     * with an array of object arguments.
  36.754 +     * <p>
  36.755 +     * If the logger is currently enabled for the given message
  36.756 +     * level then a corresponding LogRecord is created and forwarded
  36.757 +     * to all the registered output Handler objects.
  36.758 +     * <p>
  36.759 +     * The msg string is localized using the named resource bundle.  If the
  36.760 +     * resource bundle name is null, or an empty String or invalid
  36.761 +     * then the msg string is not localized.
  36.762 +     * <p>
  36.763 +     * @param   level   One of the message level identifiers, e.g., SEVERE
  36.764 +     * @param   sourceClass    name of class that issued the logging request
  36.765 +     * @param   sourceMethod   name of method that issued the logging request
  36.766 +     * @param   bundleName     name of resource bundle to localize msg,
  36.767 +     *                         can be null.
  36.768 +     * @param   msg     The string message (or a key in the message catalog)
  36.769 +     * @param   params  Array of parameters to the message
  36.770 +     */
  36.771 +    public void logrb(Level level, String sourceClass, String sourceMethod,
  36.772 +                                String bundleName, String msg, Object params[]) {
  36.773 +        if (level.intValue() < levelValue || levelValue == offValue) {
  36.774 +            return;
  36.775 +        }
  36.776 +        LogRecord lr = new LogRecord(level, msg);
  36.777 +        lr.setSourceClassName(sourceClass);
  36.778 +        lr.setSourceMethodName(sourceMethod);
  36.779 +        lr.setParameters(params);
  36.780 +        doLog(lr, bundleName);
  36.781 +    }
  36.782 +
  36.783 +    /**
  36.784 +     * Log a message, specifying source class, method, and resource bundle name,
  36.785 +     * with associated Throwable information.
  36.786 +     * <p>
  36.787 +     * If the logger is currently enabled for the given message
  36.788 +     * level then the given arguments are stored in a LogRecord
  36.789 +     * which is forwarded to all registered output handlers.
  36.790 +     * <p>
  36.791 +     * The msg string is localized using the named resource bundle.  If the
  36.792 +     * resource bundle name is null, or an empty String or invalid
  36.793 +     * then the msg string is not localized.
  36.794 +     * <p>
  36.795 +     * Note that the thrown argument is stored in the LogRecord thrown
  36.796 +     * property, rather than the LogRecord parameters property.  Thus is it
  36.797 +     * processed specially by output Formatters and is not treated
  36.798 +     * as a formatting parameter to the LogRecord message property.
  36.799 +     * <p>
  36.800 +     * @param   level   One of the message level identifiers, e.g., SEVERE
  36.801 +     * @param   sourceClass    name of class that issued the logging request
  36.802 +     * @param   sourceMethod   name of method that issued the logging request
  36.803 +     * @param   bundleName     name of resource bundle to localize msg,
  36.804 +     *                         can be null
  36.805 +     * @param   msg     The string message (or a key in the message catalog)
  36.806 +     * @param   thrown  Throwable associated with log message.
  36.807 +     */
  36.808 +    public void logrb(Level level, String sourceClass, String sourceMethod,
  36.809 +                                        String bundleName, String msg, Throwable thrown) {
  36.810 +        if (level.intValue() < levelValue || levelValue == offValue) {
  36.811 +            return;
  36.812 +        }
  36.813 +        LogRecord lr = new LogRecord(level, msg);
  36.814 +        lr.setSourceClassName(sourceClass);
  36.815 +        lr.setSourceMethodName(sourceMethod);
  36.816 +        lr.setThrown(thrown);
  36.817 +        doLog(lr, bundleName);
  36.818 +    }
  36.819 +
  36.820 +
  36.821 +    //======================================================================
  36.822 +    // Start of convenience methods for logging method entries and returns.
  36.823 +    //======================================================================
  36.824 +
  36.825 +    /**
  36.826 +     * Log a method entry.
  36.827 +     * <p>
  36.828 +     * This is a convenience method that can be used to log entry
  36.829 +     * to a method.  A LogRecord with message "ENTRY", log level
  36.830 +     * FINER, and the given sourceMethod and sourceClass is logged.
  36.831 +     * <p>
  36.832 +     * @param   sourceClass    name of class that issued the logging request
  36.833 +     * @param   sourceMethod   name of method that is being entered
  36.834 +     */
  36.835 +    public void entering(String sourceClass, String sourceMethod) {
  36.836 +        if (Level.FINER.intValue() < levelValue) {
  36.837 +            return;
  36.838 +        }
  36.839 +        logp(Level.FINER, sourceClass, sourceMethod, "ENTRY");
  36.840 +    }
  36.841 +
  36.842 +    /**
  36.843 +     * Log a method entry, with one parameter.
  36.844 +     * <p>
  36.845 +     * This is a convenience method that can be used to log entry
  36.846 +     * to a method.  A LogRecord with message "ENTRY {0}", log level
  36.847 +     * FINER, and the given sourceMethod, sourceClass, and parameter
  36.848 +     * is logged.
  36.849 +     * <p>
  36.850 +     * @param   sourceClass    name of class that issued the logging request
  36.851 +     * @param   sourceMethod   name of method that is being entered
  36.852 +     * @param   param1         parameter to the method being entered
  36.853 +     */
  36.854 +    public void entering(String sourceClass, String sourceMethod, Object param1) {
  36.855 +        if (Level.FINER.intValue() < levelValue) {
  36.856 +            return;
  36.857 +        }
  36.858 +        Object params[] = { param1 };
  36.859 +        logp(Level.FINER, sourceClass, sourceMethod, "ENTRY {0}", params);
  36.860 +    }
  36.861 +
  36.862 +    /**
  36.863 +     * Log a method entry, with an array of parameters.
  36.864 +     * <p>
  36.865 +     * This is a convenience method that can be used to log entry
  36.866 +     * to a method.  A LogRecord with message "ENTRY" (followed by a
  36.867 +     * format {N} indicator for each entry in the parameter array),
  36.868 +     * log level FINER, and the given sourceMethod, sourceClass, and
  36.869 +     * parameters is logged.
  36.870 +     * <p>
  36.871 +     * @param   sourceClass    name of class that issued the logging request
  36.872 +     * @param   sourceMethod   name of method that is being entered
  36.873 +     * @param   params         array of parameters to the method being entered
  36.874 +     */
  36.875 +    public void entering(String sourceClass, String sourceMethod, Object params[]) {
  36.876 +        if (Level.FINER.intValue() < levelValue) {
  36.877 +            return;
  36.878 +        }
  36.879 +        String msg = "ENTRY";
  36.880 +        if (params == null ) {
  36.881 +           logp(Level.FINER, sourceClass, sourceMethod, msg);
  36.882 +           return;
  36.883 +        }
  36.884 +        for (int i = 0; i < params.length; i++) {
  36.885 +            msg = msg + " {" + i + "}";
  36.886 +        }
  36.887 +        logp(Level.FINER, sourceClass, sourceMethod, msg, params);
  36.888 +    }
  36.889 +
  36.890 +    /**
  36.891 +     * Log a method return.
  36.892 +     * <p>
  36.893 +     * This is a convenience method that can be used to log returning
  36.894 +     * from a method.  A LogRecord with message "RETURN", log level
  36.895 +     * FINER, and the given sourceMethod and sourceClass is logged.
  36.896 +     * <p>
  36.897 +     * @param   sourceClass    name of class that issued the logging request
  36.898 +     * @param   sourceMethod   name of the method
  36.899 +     */
  36.900 +    public void exiting(String sourceClass, String sourceMethod) {
  36.901 +        if (Level.FINER.intValue() < levelValue) {
  36.902 +            return;
  36.903 +        }
  36.904 +        logp(Level.FINER, sourceClass, sourceMethod, "RETURN");
  36.905 +    }
  36.906 +
  36.907 +
  36.908 +    /**
  36.909 +     * Log a method return, with result object.
  36.910 +     * <p>
  36.911 +     * This is a convenience method that can be used to log returning
  36.912 +     * from a method.  A LogRecord with message "RETURN {0}", log level
  36.913 +     * FINER, and the gives sourceMethod, sourceClass, and result
  36.914 +     * object is logged.
  36.915 +     * <p>
  36.916 +     * @param   sourceClass    name of class that issued the logging request
  36.917 +     * @param   sourceMethod   name of the method
  36.918 +     * @param   result  Object that is being returned
  36.919 +     */
  36.920 +    public void exiting(String sourceClass, String sourceMethod, Object result) {
  36.921 +        if (Level.FINER.intValue() < levelValue) {
  36.922 +            return;
  36.923 +        }
  36.924 +        Object params[] = { result };
  36.925 +        logp(Level.FINER, sourceClass, sourceMethod, "RETURN {0}", result);
  36.926 +    }
  36.927 +
  36.928 +    /**
  36.929 +     * Log throwing an exception.
  36.930 +     * <p>
  36.931 +     * This is a convenience method to log that a method is
  36.932 +     * terminating by throwing an exception.  The logging is done
  36.933 +     * using the FINER level.
  36.934 +     * <p>
  36.935 +     * If the logger is currently enabled for the given message
  36.936 +     * level then the given arguments are stored in a LogRecord
  36.937 +     * which is forwarded to all registered output handlers.  The
  36.938 +     * LogRecord's message is set to "THROW".
  36.939 +     * <p>
  36.940 +     * Note that the thrown argument is stored in the LogRecord thrown
  36.941 +     * property, rather than the LogRecord parameters property.  Thus is it
  36.942 +     * processed specially by output Formatters and is not treated
  36.943 +     * as a formatting parameter to the LogRecord message property.
  36.944 +     * <p>
  36.945 +     * @param   sourceClass    name of class that issued the logging request
  36.946 +     * @param   sourceMethod  name of the method.
  36.947 +     * @param   thrown  The Throwable that is being thrown.
  36.948 +     */
  36.949 +    public void throwing(String sourceClass, String sourceMethod, Throwable thrown) {
  36.950 +        if (Level.FINER.intValue() < levelValue || levelValue == offValue ) {
  36.951 +            return;
  36.952 +        }
  36.953 +        LogRecord lr = new LogRecord(Level.FINER, "THROW");
  36.954 +        lr.setSourceClassName(sourceClass);
  36.955 +        lr.setSourceMethodName(sourceMethod);
  36.956 +        lr.setThrown(thrown);
  36.957 +        doLog(lr);
  36.958 +    }
  36.959 +
  36.960 +    //=======================================================================
  36.961 +    // Start of simple convenience methods using level names as method names
  36.962 +    //=======================================================================
  36.963 +
  36.964 +    /**
  36.965 +     * Log a SEVERE message.
  36.966 +     * <p>
  36.967 +     * If the logger is currently enabled for the SEVERE message
  36.968 +     * level then the given message is forwarded to all the
  36.969 +     * registered output Handler objects.
  36.970 +     * <p>
  36.971 +     * @param   msg     The string message (or a key in the message catalog)
  36.972 +     */
  36.973 +    public void severe(String msg) {
  36.974 +        if (Level.SEVERE.intValue() < levelValue) {
  36.975 +            return;
  36.976 +        }
  36.977 +        log(Level.SEVERE, msg);
  36.978 +    }
  36.979 +
  36.980 +    /**
  36.981 +     * Log a WARNING message.
  36.982 +     * <p>
  36.983 +     * If the logger is currently enabled for the WARNING message
  36.984 +     * level then the given message is forwarded to all the
  36.985 +     * registered output Handler objects.
  36.986 +     * <p>
  36.987 +     * @param   msg     The string message (or a key in the message catalog)
  36.988 +     */
  36.989 +    public void warning(String msg) {
  36.990 +        if (Level.WARNING.intValue() < levelValue) {
  36.991 +            return;
  36.992 +        }
  36.993 +        log(Level.WARNING, msg);
  36.994 +    }
  36.995 +
  36.996 +    /**
  36.997 +     * Log an INFO message.
  36.998 +     * <p>
  36.999 +     * If the logger is currently enabled for the INFO message
 36.1000 +     * level then the given message is forwarded to all the
 36.1001 +     * registered output Handler objects.
 36.1002 +     * <p>
 36.1003 +     * @param   msg     The string message (or a key in the message catalog)
 36.1004 +     */
 36.1005 +    public void info(String msg) {
 36.1006 +        if (Level.INFO.intValue() < levelValue) {
 36.1007 +            return;
 36.1008 +        }
 36.1009 +        log(Level.INFO, msg);
 36.1010 +    }
 36.1011 +
 36.1012 +    /**
 36.1013 +     * Log a CONFIG message.
 36.1014 +     * <p>
 36.1015 +     * If the logger is currently enabled for the CONFIG message
 36.1016 +     * level then the given message is forwarded to all the
 36.1017 +     * registered output Handler objects.
 36.1018 +     * <p>
 36.1019 +     * @param   msg     The string message (or a key in the message catalog)
 36.1020 +     */
 36.1021 +    public void config(String msg) {
 36.1022 +        if (Level.CONFIG.intValue() < levelValue) {
 36.1023 +            return;
 36.1024 +        }
 36.1025 +        log(Level.CONFIG, msg);
 36.1026 +    }
 36.1027 +
 36.1028 +    /**
 36.1029 +     * Log a FINE message.
 36.1030 +     * <p>
 36.1031 +     * If the logger is currently enabled for the FINE message
 36.1032 +     * level then the given message is forwarded to all the
 36.1033 +     * registered output Handler objects.
 36.1034 +     * <p>
 36.1035 +     * @param   msg     The string message (or a key in the message catalog)
 36.1036 +     */
 36.1037 +    public void fine(String msg) {
 36.1038 +        if (Level.FINE.intValue() < levelValue) {
 36.1039 +            return;
 36.1040 +        }
 36.1041 +        log(Level.FINE, msg);
 36.1042 +    }
 36.1043 +
 36.1044 +    /**
 36.1045 +     * Log a FINER message.
 36.1046 +     * <p>
 36.1047 +     * If the logger is currently enabled for the FINER message
 36.1048 +     * level then the given message is forwarded to all the
 36.1049 +     * registered output Handler objects.
 36.1050 +     * <p>
 36.1051 +     * @param   msg     The string message (or a key in the message catalog)
 36.1052 +     */
 36.1053 +    public void finer(String msg) {
 36.1054 +        if (Level.FINER.intValue() < levelValue) {
 36.1055 +            return;
 36.1056 +        }
 36.1057 +        log(Level.FINER, msg);
 36.1058 +    }
 36.1059 +
 36.1060 +    /**
 36.1061 +     * Log a FINEST message.
 36.1062 +     * <p>
 36.1063 +     * If the logger is currently enabled for the FINEST message
 36.1064 +     * level then the given message is forwarded to all the
 36.1065 +     * registered output Handler objects.
 36.1066 +     * <p>
 36.1067 +     * @param   msg     The string message (or a key in the message catalog)
 36.1068 +     */
 36.1069 +    public void finest(String msg) {
 36.1070 +        if (Level.FINEST.intValue() < levelValue) {
 36.1071 +            return;
 36.1072 +        }
 36.1073 +        log(Level.FINEST, msg);
 36.1074 +    }
 36.1075 +
 36.1076 +    //================================================================
 36.1077 +    // End of convenience methods
 36.1078 +    //================================================================
 36.1079 +
 36.1080 +    /**
 36.1081 +     * Set the log level specifying which message levels will be
 36.1082 +     * logged by this logger.  Message levels lower than this
 36.1083 +     * value will be discarded.  The level value Level.OFF
 36.1084 +     * can be used to turn off logging.
 36.1085 +     * <p>
 36.1086 +     * If the new level is null, it means that this node should
 36.1087 +     * inherit its level from its nearest ancestor with a specific
 36.1088 +     * (non-null) level value.
 36.1089 +     *
 36.1090 +     * @param newLevel   the new value for the log level (may be null)
 36.1091 +     * @exception  SecurityException  if a security manager exists and if
 36.1092 +     *             the caller does not have LoggingPermission("control").
 36.1093 +     */
 36.1094 +    public void setLevel(Level newLevel) throws SecurityException {
 36.1095 +        levelValue = newLevel.intValue();
 36.1096 +        levelObject = newLevel;
 36.1097 +    }
 36.1098 +
 36.1099 +    /**
 36.1100 +     * Get the log Level that has been specified for this Logger.
 36.1101 +     * The result may be null, which means that this logger's
 36.1102 +     * effective level will be inherited from its parent.
 36.1103 +     *
 36.1104 +     * @return  this Logger's level
 36.1105 +     */
 36.1106 +    public Level getLevel() {
 36.1107 +        return levelObject;
 36.1108 +    }
 36.1109 +
 36.1110 +    /**
 36.1111 +     * Check if a message of the given level would actually be logged
 36.1112 +     * by this logger.  This check is based on the Loggers effective level,
 36.1113 +     * which may be inherited from its parent.
 36.1114 +     *
 36.1115 +     * @param   level   a message logging level
 36.1116 +     * @return  true if the given message level is currently being logged.
 36.1117 +     */
 36.1118 +    public boolean isLoggable(Level level) {
 36.1119 +        if (level.intValue() < levelValue || levelValue == offValue) {
 36.1120 +            return false;
 36.1121 +        }
 36.1122 +        return true;
 36.1123 +    }
 36.1124 +
 36.1125 +    /**
 36.1126 +     * Get the name for this logger.
 36.1127 +     * @return logger name.  Will be null for anonymous Loggers.
 36.1128 +     */
 36.1129 +    public String getName() {
 36.1130 +        return name;
 36.1131 +    }
 36.1132 +
 36.1133 +    /**
 36.1134 +     * Add a log Handler to receive logging messages.
 36.1135 +     * <p>
 36.1136 +     * By default, Loggers also send their output to their parent logger.
 36.1137 +     * Typically the root Logger is configured with a set of Handlers
 36.1138 +     * that essentially act as default handlers for all loggers.
 36.1139 +     *
 36.1140 +     * @param   handler a logging Handler
 36.1141 +     * @exception  SecurityException  if a security manager exists and if
 36.1142 +     *             the caller does not have LoggingPermission("control").
 36.1143 +     */
 36.1144 +//    public void addHandler(Handler handler) throws SecurityException {
 36.1145 +//        // Check for null handler
 36.1146 +//        handler.getClass();
 36.1147 +//        checkAccess();
 36.1148 +//        handlers.add(handler);
 36.1149 +//    }
 36.1150 +
 36.1151 +    /**
 36.1152 +     * Remove a log Handler.
 36.1153 +     * <P>
 36.1154 +     * Returns silently if the given Handler is not found or is null
 36.1155 +     *
 36.1156 +     * @param   handler a logging Handler
 36.1157 +     * @exception  SecurityException  if a security manager exists and if
 36.1158 +     *             the caller does not have LoggingPermission("control").
 36.1159 +     */
 36.1160 +//    public void removeHandler(Handler handler) throws SecurityException {
 36.1161 +//        checkAccess();
 36.1162 +//        if (handler == null) {
 36.1163 +//            return;
 36.1164 +//        }
 36.1165 +//        handlers.remove(handler);
 36.1166 +//    }
 36.1167 +
 36.1168 +    /**
 36.1169 +     * Get the Handlers associated with this logger.
 36.1170 +     * <p>
 36.1171 +     * @return  an array of all registered Handlers
 36.1172 +     */
 36.1173 +//    public Handler[] getHandlers() {
 36.1174 +//        return handlers.toArray(emptyHandlers);
 36.1175 +//    }
 36.1176 +
 36.1177 +    /**
 36.1178 +     * Specify whether or not this logger should send its output
 36.1179 +     * to its parent Logger.  This means that any LogRecords will
 36.1180 +     * also be written to the parent's Handlers, and potentially
 36.1181 +     * to its parent, recursively up the namespace.
 36.1182 +     *
 36.1183 +     * @param useParentHandlers   true if output is to be sent to the
 36.1184 +     *          logger's parent.
 36.1185 +     * @exception  SecurityException  if a security manager exists and if
 36.1186 +     *             the caller does not have LoggingPermission("control").
 36.1187 +     */
 36.1188 +    public void setUseParentHandlers(boolean useParentHandlers) {
 36.1189 +        checkAccess();
 36.1190 +    }
 36.1191 +
 36.1192 +    /**
 36.1193 +     * Discover whether or not this logger is sending its output
 36.1194 +     * to its parent logger.
 36.1195 +     *
 36.1196 +     * @return  true if output is to be sent to the logger's parent
 36.1197 +     */
 36.1198 +    public boolean getUseParentHandlers() {
 36.1199 +        return true;
 36.1200 +    }
 36.1201 +
 36.1202 +    /**
 36.1203 +     * Return the parent for this Logger.
 36.1204 +     * <p>
 36.1205 +     * This method returns the nearest extant parent in the namespace.
 36.1206 +     * Thus if a Logger is called "a.b.c.d", and a Logger called "a.b"
 36.1207 +     * has been created but no logger "a.b.c" exists, then a call of
 36.1208 +     * getParent on the Logger "a.b.c.d" will return the Logger "a.b".
 36.1209 +     * <p>
 36.1210 +     * The result will be null if it is called on the root Logger
 36.1211 +     * in the namespace.
 36.1212 +     *
 36.1213 +     * @return nearest existing parent Logger
 36.1214 +     */
 36.1215 +    public Logger getParent() {
 36.1216 +        // Note: this used to be synchronized on treeLock.  However, this only
 36.1217 +        // provided memory semantics, as there was no guarantee that the caller
 36.1218 +        // would synchronize on treeLock (in fact, there is no way for external
 36.1219 +        // callers to so synchronize).  Therefore, we have made parent volatile
 36.1220 +        // instead.
 36.1221 +        String n = getName();
 36.1222 +        int at = n.length();
 36.1223 +        for (;;) {
 36.1224 +            int last = n.lastIndexOf('.', at - 1);
 36.1225 +            if (last == -1) {
 36.1226 +                return getGlobal();
 36.1227 +            }
 36.1228 +            Logger p = ALL.get(n.substring(0, last));
 36.1229 +            if (p != null) {
 36.1230 +                return p;
 36.1231 +            }
 36.1232 +            at = last;
 36.1233 +        }
 36.1234 +    }
 36.1235 +
 36.1236 +    /**
 36.1237 +     * Set the parent for this Logger.  This method is used by
 36.1238 +     * the LogManager to update a Logger when the namespace changes.
 36.1239 +     * <p>
 36.1240 +     * It should not be called from application code.
 36.1241 +     * <p>
 36.1242 +     * @param  parent   the new parent logger
 36.1243 +     * @exception  SecurityException  if a security manager exists and if
 36.1244 +     *             the caller does not have LoggingPermission("control").
 36.1245 +     */
 36.1246 +    public void setParent(Logger parent) {
 36.1247 +        if (parent == null) {
 36.1248 +            throw new NullPointerException();
 36.1249 +        }
 36.1250 +        checkAccess();
 36.1251 +    }
 36.1252 +
 36.1253 +}
    37.1 --- a/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/ReaderTest.java	Sun Sep 08 10:58:10 2013 +0200
    37.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/ReaderTest.java	Sun Sep 08 11:22:51 2013 +0200
    37.3 @@ -18,8 +18,10 @@
    37.4  package org.apidesign.bck2brwsr.compact.tck;
    37.5  
    37.6  import java.io.ByteArrayInputStream;
    37.7 +import java.io.ByteArrayOutputStream;
    37.8  import java.io.IOException;
    37.9  import java.io.InputStreamReader;
   37.10 +import java.io.OutputStreamWriter;
   37.11  import java.io.UnsupportedEncodingException;
   37.12  import java.util.Arrays;
   37.13  import org.apidesign.bck2brwsr.vmtest.Compare;
   37.14 @@ -40,7 +42,10 @@
   37.15          };
   37.16          ByteArrayInputStream is = new ByteArrayInputStream(arr);
   37.17          InputStreamReader r = new InputStreamReader(is, "UTF-8");
   37.18 -        
   37.19 +        return readReader(r);        
   37.20 +    }
   37.21 +
   37.22 +    private String readReader(InputStreamReader r) throws IOException {
   37.23          StringBuilder sb = new StringBuilder();
   37.24          for (;;) {
   37.25              int ch = r.read();
   37.26 @@ -52,7 +57,19 @@
   37.27          return sb.toString().toString();
   37.28      }
   37.29      @Compare public String stringToBytes() throws UnsupportedEncodingException {
   37.30 -        return Arrays.toString("\u017dlu\u0165ou\u010dk\u00fd k\u016f\u0148".getBytes("UTF-8"));
   37.31 +        return Arrays.toString(YellowHorse.getBytes("UTF-8"));
   37.32 +    }
   37.33 +    private final String YellowHorse = "\u017dlu\u0165ou\u010dk\u00fd k\u016f\u0148";
   37.34 +    
   37.35 +    @Compare public String readAndWrite() throws Exception {
   37.36 +        ByteArrayOutputStream arr = new ByteArrayOutputStream();
   37.37 +        OutputStreamWriter w = new OutputStreamWriter(arr);
   37.38 +        w.write(YellowHorse);
   37.39 +        w.close();
   37.40 +        
   37.41 +        ByteArrayInputStream is = new ByteArrayInputStream(arr.toByteArray());
   37.42 +        InputStreamReader r = new InputStreamReader(is, "UTF-8");
   37.43 +        return readReader(r);
   37.44      }
   37.45      
   37.46      @Factory public static Object[] create() {
    38.1 --- a/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareHashTest.java	Sun Sep 08 10:58:10 2013 +0200
    38.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareHashTest.java	Sun Sep 08 11:22:51 2013 +0200
    38.3 @@ -30,6 +30,16 @@
    38.4          return "Ahoj".hashCode();
    38.5      }
    38.6      
    38.7 +    @Compare public boolean hashOfIntegerDifferentToOwnHash() {
    38.8 +        Integer i = 120;
    38.9 +        return System.identityHashCode(i) != i.hashCode();
   38.10 +    }
   38.11 +
   38.12 +    @Compare public int hashOfObjectSameAsOwnHash() {
   38.13 +        Object o = new Object();
   38.14 +        return System.identityHashCode(o) - o.hashCode();
   38.15 +    }
   38.16 +    
   38.17      @Compare public int hashRemainsYieldsZero() {
   38.18          Object o = new Object();
   38.19          return o.hashCode() - o.hashCode();
    39.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    39.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/LoggerTest.java	Sun Sep 08 11:22:51 2013 +0200
    39.3 @@ -0,0 +1,43 @@
    39.4 +/**
    39.5 + * Back 2 Browser Bytecode Translator
    39.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    39.7 + *
    39.8 + * This program is free software: you can redistribute it and/or modify
    39.9 + * it under the terms of the GNU General Public License as published by
   39.10 + * the Free Software Foundation, version 2 of the License.
   39.11 + *
   39.12 + * This program is distributed in the hope that it will be useful,
   39.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
   39.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   39.15 + * GNU General Public License for more details.
   39.16 + *
   39.17 + * You should have received a copy of the GNU General Public License
   39.18 + * along with this program. Look for COPYING file in the top folder.
   39.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
   39.20 + */
   39.21 +package org.apidesign.bck2brwsr.tck;
   39.22 +
   39.23 +import java.util.logging.Logger;
   39.24 +import org.apidesign.bck2brwsr.vmtest.Compare;
   39.25 +import org.apidesign.bck2brwsr.vmtest.VMTest;
   39.26 +import org.testng.annotations.Factory;
   39.27 +
   39.28 +/**
   39.29 + *
   39.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
   39.31 + */
   39.32 +public class LoggerTest {
   39.33 +    @Compare public String parentLogger() {
   39.34 +        Logger lx = Logger.getLogger("x");
   39.35 +        assert lx != null;
   39.36 +        assert lx.getName().equals("x") : "Right name: " + lx.getName();
   39.37 +        Logger lxyz = Logger.getLogger("x.y.z");
   39.38 +        assert lxyz != null;
   39.39 +        assert lxyz.getName().equals("x.y.z") : "xyz name: " + lxyz.getName();
   39.40 +        return lxyz.getParent().getName();
   39.41 +    }
   39.42 +    
   39.43 +    @Factory public static Object[] create() {
   39.44 +        return VMTest.create(LoggerTest.class);
   39.45 +    }
   39.46 +}
    40.1 --- a/rt/emul/mini/src/main/java/java/lang/Character.java	Sun Sep 08 10:58:10 2013 +0200
    40.2 +++ b/rt/emul/mini/src/main/java/java/lang/Character.java	Sun Sep 08 11:22:51 2013 +0200
    40.3 @@ -2298,6 +2298,10 @@
    40.4       */
    40.5      @Deprecated
    40.6      public static boolean isSpace(char ch) {
    40.7 +        return isSpaceChar(ch);
    40.8 +    }
    40.9 +
   40.10 +    public static boolean isSpaceChar(int ch) {
   40.11          return (ch <= 0x0020) &&
   40.12              (((((1L << 0x0009) |
   40.13              (1L << 0x000A) |
   40.14 @@ -2307,7 +2311,6 @@
   40.15      }
   40.16  
   40.17  
   40.18 -
   40.19      /**
   40.20       * Determines if the specified character is white space according to Java.
   40.21       * A character is a Java whitespace character if and only if it satisfies
    41.1 --- a/rt/emul/mini/src/main/java/java/lang/Object.java	Sun Sep 08 10:58:10 2013 +0200
    41.2 +++ b/rt/emul/mini/src/main/java/java/lang/Object.java	Sun Sep 08 11:22:51 2013 +0200
    41.3 @@ -125,12 +125,15 @@
    41.4       * @see     java.lang.Object#equals(java.lang.Object)
    41.5       * @see     java.lang.System#identityHashCode
    41.6       */
    41.7 +    public int hashCode() {
    41.8 +        return defaultHashCode();
    41.9 +    }
   41.10      @JavaScriptBody(args = {}, body = 
   41.11          "if (this.$hashCode) return this.$hashCode;\n"
   41.12          + "var h = this.computeHashCode__I();\n"
   41.13          + "return this.$hashCode = h & h;"
   41.14      )
   41.15 -    public native int hashCode();
   41.16 +    final native int defaultHashCode();
   41.17  
   41.18      @JavaScriptBody(args = {}, body = "return Math.random() * Math.pow(2, 32);")
   41.19      native int computeHashCode();