< prev index next >

## src/java.base/share/classes/java/io/File.java

Print this page

       1 /*
       2  * Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
       3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       4  *
       5  * This code is free software; you can redistribute it and/or modify it
       6  * under the terms of the GNU General Public License version 2 only, as
       7  * published by the Free Software Foundation.  Oracle designates this
       8  * particular file as subject to the "Classpath" exception as provided
       9  * by Oracle in the LICENSE file that accompanied this code.
      10  *
      11  * This code is distributed in the hope that it will be useful, but WITHOUT
      12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14  * version 2 for more details (a copy is included in the LICENSE file that
      15  * accompanied this code).
      16  *
      17  * You should have received a copy of the GNU General Public License version
      18  * 2 along with this work; if not, write to the Free Software Foundation,
      19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
      20  *
      21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
      22  * or visit www.oracle.com if you need additional information or have any
    2214     public int hashCode() {
    2215         return fs.hashCode(this);
    2216     }
    2217
    2218     /**
    2219      * Returns the pathname string of this abstract pathname.  This is just the
    2220      * string returned by the {@link #getPath} method.
    2221      *
    2222      * @return  The string form of this abstract pathname
    2223      */
    2224     public String toString() {
    2225         return getPath();
    2226     }
    2227
    2228     /**
    2229      * WriteObject is called to save this filename.
    2230      * The separator character is saved also so it can be replaced
    2231      * in case the path is reconstituted on a different host type.
    2232      *
    2233      * @serialData  Default fields followed by separator character.

    2234      */
    2235     @java.io.Serial
    2236     private synchronized void writeObject(java.io.ObjectOutputStream s)
    2237         throws IOException
    2238     {
    2239         s.defaultWriteObject();
    2240         s.writeChar(separatorChar); // Add the separator character
    2241     }
    2242
    2243     /**
    2244      * readObject is called to restore this filename.
    2245      * The original separator character is read.  If it is different
    2246      * than the separator character on this system, then the old separator
    2247      * is replaced by the local separator.

    2248      */
    2249     @java.io.Serial
    2250     private synchronized void readObject(java.io.ObjectInputStream s)
    2251          throws IOException, ClassNotFoundException
    2252     {
    2253         ObjectInputStream.GetField fields = s.readFields();
    2254         String pathField = (String)fields.get("path", null);
    2255         char sep = s.readChar(); // read the previous separator char
    2256         if (sep != separatorChar)
    2257             pathField = pathField.replace(sep, separatorChar);
    2258         String path = fs.normalize(pathField);
    2259         UNSAFE.putReference(this, PATH_OFFSET, path);
    2260         UNSAFE.putIntVolatile(this, PREFIX_LENGTH_OFFSET, fs.prefixLength(path));
    2261     }
    2262
    2263     private static final jdk.internal.misc.Unsafe UNSAFE
    2264             = jdk.internal.misc.Unsafe.getUnsafe();
    2265     private static final long PATH_OFFSET
    2266             = UNSAFE.objectFieldOffset(File.class, "path");
    2267     private static final long PREFIX_LENGTH_OFFSET
       1 /*
       2  * Copyright (c) 1994, 2020, Oracle and/or its affiliates. All rights reserved.
       3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       4  *
       5  * This code is free software; you can redistribute it and/or modify it
       6  * under the terms of the GNU General Public License version 2 only, as
       7  * published by the Free Software Foundation.  Oracle designates this
       8  * particular file as subject to the "Classpath" exception as provided
       9  * by Oracle in the LICENSE file that accompanied this code.
      10  *
      11  * This code is distributed in the hope that it will be useful, but WITHOUT
      12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14  * version 2 for more details (a copy is included in the LICENSE file that
      15  * accompanied this code).
      16  *
      17  * You should have received a copy of the GNU General Public License version
      18  * 2 along with this work; if not, write to the Free Software Foundation,
      19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
      20  *
      21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
      22  * or visit www.oracle.com if you need additional information or have any
    2214     public int hashCode() {
    2215         return fs.hashCode(this);
    2216     }
    2217
    2218     /**
    2219      * Returns the pathname string of this abstract pathname.  This is just the
    2220      * string returned by the {@link #getPath} method.
    2221      *
    2222      * @return  The string form of this abstract pathname
    2223      */
    2224     public String toString() {
    2225         return getPath();
    2226     }
    2227
    2228     /**
    2229      * WriteObject is called to save this filename.
    2230      * The separator character is saved also so it can be replaced
    2231      * in case the path is reconstituted on a different host type.
    2232      *
    2233      * @serialData  Default fields followed by separator character.
    2234      *
    2235      * @param  s the {@code ObjectOutputStream} to which data is written
    2236      * @throws IOException if an I/O error occurs
    2237      */
    2238     @java.io.Serial
    2239     private synchronized void writeObject(java.io.ObjectOutputStream s)
    2240         throws IOException
    2241     {
    2242         s.defaultWriteObject();
    2243         s.writeChar(separatorChar); // Add the separator character
    2244     }
    2245
    2246     /**
    2247      * readObject is called to restore this filename.
    2248      * The original separator character is read.  If it is different
    2249      * than the separator character on this system, then the old separator
    2250      * is replaced by the local separator.
    2251      *
    2252      * @param  s the {@code ObjectInputStream} from which data is read
    2253      * @throws IOException if an I/O error occurs
    2254      * @throws ClassNotFoundException if a serialized class cannot be loaded
    2255      */
    2256     @java.io.Serial
    2257     private synchronized void readObject(java.io.ObjectInputStream s)
    2258          throws IOException, ClassNotFoundException
    2259     {
    2260         ObjectInputStream.GetField fields = s.readFields();
    2261         String pathField = (String)fields.get("path", null);
    2262         char sep = s.readChar(); // read the previous separator char
    2263         if (sep != separatorChar)
    2264             pathField = pathField.replace(sep, separatorChar);
    2265         String path = fs.normalize(pathField);
    2266         UNSAFE.putReference(this, PATH_OFFSET, path);
    2267         UNSAFE.putIntVolatile(this, PREFIX_LENGTH_OFFSET, fs.prefixLength(path));
    2268     }
    2269
    2270     private static final jdk.internal.misc.Unsafe UNSAFE
    2271             = jdk.internal.misc.Unsafe.getUnsafe();
    2272     private static final long PATH_OFFSET
    2273             = UNSAFE.objectFieldOffset(File.class, "path");
    2274     private static final long PREFIX_LENGTH_OFFSET

< prev index next >