diff options
author | jfrijters <jfrijters> | 2011-07-12 16:12:32 +0400 |
---|---|---|
committer | jfrijters <jfrijters> | 2011-07-12 16:12:32 +0400 |
commit | 257720a90dbb90dafe63fd227a05fbad6cb401a2 (patch) | |
tree | 3b5bec62c67ba64d40f20085c6d321d1be23bf06 /openjdk/sun | |
parent | cb00def97dc48440d6d4db45452dbeef8aba0f30 (diff) |
More java.nio.file functionality.
Diffstat (limited to 'openjdk/sun')
-rw-r--r-- | openjdk/sun/nio/fs/NetFileSystem.java | 6 | ||||
-rw-r--r-- | openjdk/sun/nio/fs/NetFileSystemProvider.java | 158 | ||||
-rw-r--r-- | openjdk/sun/nio/fs/NetPath.java | 35 |
3 files changed, 192 insertions, 7 deletions
diff --git a/openjdk/sun/nio/fs/NetFileSystem.java b/openjdk/sun/nio/fs/NetFileSystem.java index 1e9946bd..fca727b3 100644 --- a/openjdk/sun/nio/fs/NetFileSystem.java +++ b/openjdk/sun/nio/fs/NetFileSystem.java @@ -29,10 +29,14 @@ import java.io.IOException; import java.nio.file.*; import java.nio.file.attribute.*; import java.nio.file.spi.FileSystemProvider; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; import java.util.Set; final class NetFileSystem extends FileSystem { + private static final Set<String> attributes = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList("basic"))); private final NetFileSystemProvider provider; private final String separator = Character.toString(cli.System.IO.Path.DirectorySeparatorChar); @@ -78,7 +82,7 @@ final class NetFileSystem extends FileSystem public Set<String> supportedFileAttributeViews() { - throw new NotYetImplementedError(); + return attributes; } public Path getPath(String first, String... more) diff --git a/openjdk/sun/nio/fs/NetFileSystemProvider.java b/openjdk/sun/nio/fs/NetFileSystemProvider.java index ca588f86..9eb8d1a9 100644 --- a/openjdk/sun/nio/fs/NetFileSystemProvider.java +++ b/openjdk/sun/nio/fs/NetFileSystemProvider.java @@ -25,6 +25,13 @@ package sun.nio.fs; import ikvm.internal.NotYetImplementedError; +import cli.System.IO.FileMode; +import cli.System.IO.FileShare; +import cli.System.IO.FileStream; +import cli.System.IO.FileOptions; +import cli.System.Security.AccessControl.FileSystemRights; +import com.sun.nio.file.ExtendedOpenOption; +import java.io.FileDescriptor; import java.io.IOException; import java.net.URI; import java.nio.channels.*; @@ -33,6 +40,7 @@ import java.nio.file.attribute.*; import java.nio.file.spi.FileSystemProvider; import java.util.Map; import java.util.Set; +import sun.nio.ch.FileChannelImpl; final class NetFileSystemProvider extends AbstractFileSystemProvider { @@ -58,9 +66,155 @@ final class NetFileSystemProvider extends AbstractFileSystemProvider throw new NotYetImplementedError(); } - public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException + public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> opts, FileAttribute<?>... attrs) throws IOException { - throw new NotYetImplementedError(); + NetPath npath = NetPath.from(path); + if (attrs.length != 0) + { + throw new NotYetImplementedError(); + } + int mode = FileMode.Open; + int rights = 0; + int share = FileShare.ReadWrite | FileShare.Delete; + int options = FileOptions.None; + boolean read = false; + boolean write = false; + boolean append = false; + boolean sparse = false; + for (OpenOption opt : opts) + { + if (opt instanceof StandardOpenOption) + { + switch ((StandardOpenOption)opt) + { + case APPEND: + append = true; + write = true; + mode = FileMode.Append; + rights |= FileSystemRights.AppendData; + break; + case CREATE: + mode = FileMode.Create; + break; + case CREATE_NEW: + mode = FileMode.CreateNew; + break; + case DELETE_ON_CLOSE: + options |= FileOptions.DeleteOnClose; + break; + case DSYNC: + options |= FileOptions.WriteThrough; + break; + case READ: + read = true; + rights |= FileSystemRights.Read; + break; + case SPARSE: + sparse = true; + break; + case SYNC: + options |= FileOptions.WriteThrough; + break; + case TRUNCATE_EXISTING: + mode = FileMode.Truncate; + break; + case WRITE: + write = true; + rights |= FileSystemRights.Write; + break; + default: + throw new UnsupportedOperationException(); + } + } + else if (opt instanceof ExtendedOpenOption) + { + switch ((ExtendedOpenOption)opt) + { + case NOSHARE_READ: + share &= ~FileShare.Read; + break; + case NOSHARE_WRITE: + share &= ~FileShare.Write; + break; + case NOSHARE_DELETE: + share &= ~FileShare.Delete; + break; + default: + throw new UnsupportedOperationException(); + } + } + else if (opt == null) + { + throw new NullPointerException(); + } + else + { + throw new UnsupportedOperationException(); + } + } + + if (!read && !write) + { + read = true; + rights |= FileSystemRights.Read; + } + + if (read && append) + { + throw new IllegalArgumentException("READ + APPEND not allowed"); + } + + if (append && mode == FileMode.Truncate) + { + throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed"); + } + + if (mode == FileMode.CreateNew && sparse) + { + throw new UnsupportedOperationException(); + } + + FileStream fs; + try + { + if (false) throw new cli.System.ArgumentException(); + if (false) throw new cli.System.IO.FileNotFoundException(); + if (false) throw new cli.System.IO.DirectoryNotFoundException(); + if (false) throw new cli.System.PlatformNotSupportedException(); + if (false) throw new cli.System.IO.IOException(); + if (false) throw new cli.System.Security.SecurityException(); + if (false) throw new cli.System.UnauthorizedAccessException(); + fs = new FileStream(npath.path, FileMode.wrap(mode), FileSystemRights.wrap(rights), FileShare.wrap(share), 8, FileOptions.wrap(options)); + } + catch (cli.System.ArgumentException x) + { + throw new FileSystemException(npath.path, null, x.getMessage()); + } + catch (cli.System.IO.FileNotFoundException _) + { + throw new NoSuchFileException(npath.path); + } + catch (cli.System.IO.DirectoryNotFoundException _) + { + throw new NoSuchFileException(npath.path); + } + catch (cli.System.PlatformNotSupportedException x) + { + throw new UnsupportedOperationException(x.getMessage()); + } + catch (cli.System.IO.IOException x) + { + throw new FileSystemException(npath.path, null, x.getMessage()); + } + catch (cli.System.Security.SecurityException _) + { + throw new AccessDeniedException(npath.path); + } + catch (cli.System.UnauthorizedAccessException _) + { + throw new AccessDeniedException(npath.path); + } + return FileChannelImpl.open(FileDescriptor.fromStream(fs), read, write, null, append); } public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException diff --git a/openjdk/sun/nio/fs/NetPath.java b/openjdk/sun/nio/fs/NetPath.java index 30cdbe5b..8d55e60f 100644 --- a/openjdk/sun/nio/fs/NetPath.java +++ b/openjdk/sun/nio/fs/NetPath.java @@ -37,7 +37,7 @@ final class NetPath extends AbstractPath { private static final char[] invalid = cli.System.IO.Path.GetInvalidFileNameChars(); private final NetFileSystem fs; - private final String path; + final String path; NetPath(NetFileSystem fs, String path) { @@ -117,7 +117,11 @@ final class NetPath extends AbstractPath path += '\\'; } } - else if (path.length() > 0 && path.charAt(path.length() - 1) == cli.System.IO.Path.DirectorySeparatorChar) + else if (path.length() == 3 && path.charAt(1) == ':' && WINDOWS) + { + // don't remove trailing backslash + } + else if (path.length() > 1 && path.charAt(path.length() - 1) == cli.System.IO.Path.DirectorySeparatorChar) { path = path.substring(0, path.length() - 1); } @@ -148,7 +152,12 @@ final class NetPath extends AbstractPath public Path getParent() { - throw new NotYetImplementedError(); + String parent = cli.System.IO.Path.GetDirectoryName(path); + if (parent == null || parent.length() == 0) + { + return null; + } + return new NetPath(fs, parent); } public int getNameCount() @@ -183,7 +192,16 @@ final class NetPath extends AbstractPath public Path resolve(Path other) { - throw new NotYetImplementedError(); + NetPath nother = NetPath.from(other); + if (nother.isAbsolute()) + { + return other; + } + if (nother.path.length() == 0) + { + return this; + } + return new NetPath(fs, cli.System.IO.Path.Combine(path, nother.path)); } public Path relativize(Path other) @@ -236,4 +254,13 @@ final class NetPath extends AbstractPath { return path; } + + static NetPath from(Path path) + { + if (!(path instanceof NetPath)) + { + throw new ProviderMismatchException(); + } + return (NetPath)path; + } } |