< prev index next >

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

Print this page




2200         return fs.hashCode(this);
2201     }
2202 
2203     /**
2204      * Returns the pathname string of this abstract pathname.  This is just the
2205      * string returned by the {@link #getPath} method.
2206      *
2207      * @return  The string form of this abstract pathname
2208      */
2209     public String toString() {
2210         return getPath();
2211     }
2212 
2213     /**
2214      * WriteObject is called to save this filename.
2215      * The separator character is saved also so it can be replaced
2216      * in case the path is reconstituted on a different host type.
2217      *
2218      * @serialData  Default fields followed by separator character.
2219      */

2220     private synchronized void writeObject(java.io.ObjectOutputStream s)
2221         throws IOException
2222     {
2223         s.defaultWriteObject();
2224         s.writeChar(separatorChar); // Add the separator character
2225     }
2226 
2227     /**
2228      * readObject is called to restore this filename.
2229      * The original separator character is read.  If it is different
2230      * than the separator character on this system, then the old separator
2231      * is replaced by the local separator.
2232      */

2233     private synchronized void readObject(java.io.ObjectInputStream s)
2234          throws IOException, ClassNotFoundException
2235     {
2236         ObjectInputStream.GetField fields = s.readFields();
2237         String pathField = (String)fields.get("path", null);
2238         char sep = s.readChar(); // read the previous separator char
2239         if (sep != separatorChar)
2240             pathField = pathField.replace(sep, separatorChar);
2241         String path = fs.normalize(pathField);
2242         UNSAFE.putReference(this, PATH_OFFSET, path);
2243         UNSAFE.putIntVolatile(this, PREFIX_LENGTH_OFFSET, fs.prefixLength(path));
2244     }
2245 
2246     private static final jdk.internal.misc.Unsafe UNSAFE
2247             = jdk.internal.misc.Unsafe.getUnsafe();
2248     private static final long PATH_OFFSET
2249             = UNSAFE.objectFieldOffset(File.class, "path");
2250     private static final long PREFIX_LENGTH_OFFSET
2251             = UNSAFE.objectFieldOffset(File.class, "prefixLength");
2252 
2253     /** use serialVersionUID from JDK 1.0.2 for interoperability */

2254     private static final long serialVersionUID = 301077366599181567L;
2255 
2256     // -- Integration with java.nio.file --
2257 
2258     private transient volatile Path filePath;
2259 
2260     /**
2261      * Returns a {@link Path java.nio.file.Path} object constructed from
2262      * this abstract path. The resulting {@code Path} is associated with the
2263      * {@link java.nio.file.FileSystems#getDefault default-filesystem}.
2264      *
2265      * <p> The first invocation of this method works as if invoking it were
2266      * equivalent to evaluating the expression:
2267      * <blockquote><pre>
2268      * {@link java.nio.file.FileSystems#getDefault FileSystems.getDefault}().{@link
2269      * java.nio.file.FileSystem#getPath getPath}(this.{@link #getPath getPath}());
2270      * </pre></blockquote>
2271      * Subsequent invocations of this method return the same {@code Path}.
2272      *
2273      * <p> If this abstract pathname is the empty abstract pathname then this




2200         return fs.hashCode(this);
2201     }
2202 
2203     /**
2204      * Returns the pathname string of this abstract pathname.  This is just the
2205      * string returned by the {@link #getPath} method.
2206      *
2207      * @return  The string form of this abstract pathname
2208      */
2209     public String toString() {
2210         return getPath();
2211     }
2212 
2213     /**
2214      * WriteObject is called to save this filename.
2215      * The separator character is saved also so it can be replaced
2216      * in case the path is reconstituted on a different host type.
2217      *
2218      * @serialData  Default fields followed by separator character.
2219      */
2220     @java.io.Serial
2221     private synchronized void writeObject(java.io.ObjectOutputStream s)
2222         throws IOException
2223     {
2224         s.defaultWriteObject();
2225         s.writeChar(separatorChar); // Add the separator character
2226     }
2227 
2228     /**
2229      * readObject is called to restore this filename.
2230      * The original separator character is read.  If it is different
2231      * than the separator character on this system, then the old separator
2232      * is replaced by the local separator.
2233      */
2234     @java.io.Serial
2235     private synchronized void readObject(java.io.ObjectInputStream s)
2236          throws IOException, ClassNotFoundException
2237     {
2238         ObjectInputStream.GetField fields = s.readFields();
2239         String pathField = (String)fields.get("path", null);
2240         char sep = s.readChar(); // read the previous separator char
2241         if (sep != separatorChar)
2242             pathField = pathField.replace(sep, separatorChar);
2243         String path = fs.normalize(pathField);
2244         UNSAFE.putReference(this, PATH_OFFSET, path);
2245         UNSAFE.putIntVolatile(this, PREFIX_LENGTH_OFFSET, fs.prefixLength(path));
2246     }
2247 
2248     private static final jdk.internal.misc.Unsafe UNSAFE
2249             = jdk.internal.misc.Unsafe.getUnsafe();
2250     private static final long PATH_OFFSET
2251             = UNSAFE.objectFieldOffset(File.class, "path");
2252     private static final long PREFIX_LENGTH_OFFSET
2253             = UNSAFE.objectFieldOffset(File.class, "prefixLength");
2254 
2255     /** use serialVersionUID from JDK 1.0.2 for interoperability */
2256     @java.io.Serial
2257     private static final long serialVersionUID = 301077366599181567L;
2258 
2259     // -- Integration with java.nio.file --
2260 
2261     private transient volatile Path filePath;
2262 
2263     /**
2264      * Returns a {@link Path java.nio.file.Path} object constructed from
2265      * this abstract path. The resulting {@code Path} is associated with the
2266      * {@link java.nio.file.FileSystems#getDefault default-filesystem}.
2267      *
2268      * <p> The first invocation of this method works as if invoking it were
2269      * equivalent to evaluating the expression:
2270      * <blockquote><pre>
2271      * {@link java.nio.file.FileSystems#getDefault FileSystems.getDefault}().{@link
2272      * java.nio.file.FileSystem#getPath getPath}(this.{@link #getPath getPath}());
2273      * </pre></blockquote>
2274      * Subsequent invocations of this method return the same {@code Path}.
2275      *
2276      * <p> If this abstract pathname is the empty abstract pathname then this


< prev index next >