Questions on the NIO.2 API
Martijn Verburg
martijnverburg at gmail.com
Mon Jan 31 08:34:02 PST 2011
Hi all,
On Mon, Jan 31, 2011 at 4:05 PM, Dr Andrew John Hughes <
gnu_andrew at member.fsf.org> wrote:
> On 31 January 2011 15:57, Rémi Forax <forax at univ-mlv.fr> wrote:
> > Le 31/01/2011 16:30, Martijn Verburg a écrit :
> >>
> >> Hi all,
> >>
> >> I've finally come back to this Chapter and am thinking about the various
> >> rough code samples I've written (running b127 on Windows XP SP3 32bit).
> My
> >> next code sample simply takes the reader through some of the file system
> api
> >> in order to explore some of the basics.
> >>
> >> import java.io.IOException;
> >> import java.nio.file.*;
> >>
> >> public class Listing_2_1
> >> {
> >> public static void main(String[] args) throws IOException
> >> {
> >> try (FileSystem fileSystem = FileSystems.getDefault())
> >> {
> >> Path timeSheetFile =
> >> fileSystem.getPath("C:/projects/timesheet.txt");
> >> Path backupDir = fileSystem.getPath("H:/projects/");
> >> Path backupFile =
> >> fileSystem.getPath("H:/projects/timesheet.txt");
> >>
> >> timeSheetFile.checkAccess(AccessMode.READ);
> >> backupDir.checkAccess(AccessMode.WRITE);
> >>
> >> FileStore backupFileStore = backupDir.getFileStore();
> >> if (!backupFileStore.type().equals("NTFS"))
> >> {
> >> throw new IOException("Suspicious storage type");
> >> }
> >>
> >> CopyOption copyOptions = StandardCopyOption.REPLACE_EXISTING;
> >> timeSheetFile.copyTo(backupFile, copyOptions);
> >> }
> >> }
> >> }
> >>
> >> The code example is a pretty patsy one, but one gotcha in particular was
> >> noticed by earlier reviewers.
> >>
> >> The FileStore object's type is simply a String as opposed to a constant,
> >> enum or other defined type. This makes it very hard to rely on for
> decision
> >> making (as per the if statement I have above). I assume this is a
> >> limitation of being able to consistently detect the underlying storage
> type?
> >
> > Yes, the problem is that the string can be easily misspelled.
> > I wondering if it worth an API like this:
> >
> > interface FileStoreType {
> > public String getName();
> > }
> >
> > public enum StandardFileStoreType implements FileStoreType {
> > EXT3, EXT4, FAT32, NTFS;
> > ...
> > }
> >
> > public class FileStoreTypes {
> > public static FileStoreType getFileStoreType(final String name) {
> > // StandardFileStoreType.valueOf(name)
> > // otherwise new FileStoreType() {
> > // public String getName() {
> > // return name;
> > // }
> > //};
> > }
> > }
> >
> > In that case, the code can be rewrite to:
> > backupFileStore.type().equals(StandardFileStoreType.NTFS)) { ... }
> >
> >>
> >> Thanks again all.
> >> Martijn
> >> (@java7developer - twitter)
> >>
> >
> > Rémi
> >
> >
> >
>
> What happens if the file store uses a type not defined in the enum?
> It may be possible to misspell a String, but it's worse if the API is
> outdated before it even gets released because it only lists certain
> filesystems.
> --
> Andrew :-)
Free Java Software Engineer
> Red Hat, Inc. (http://www.redhat.com)
>
> Support Free Java!
> Contribute to GNU Classpath and the OpenJDK
> http://www.gnu.org/software/classpath
> http://openjdk.java.net
>
> PGP Key: 94EFD9D8 (http://subkeys.pgp.net)
> Fingerprint: F8EF F1EA 401E 2E60 15FA 7927 142C 2591 94EF D9D8
>
Perhaps a compromise? Have a certain defined set of StandardFileStoreTypes,
yet still allow more to be added on the fly? Perhaps something a little like
how CopyOptions are dealt with? OK, I'm probably starting to get out of my
depth here :)
Cheers,
Martijn
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/nio-dev/attachments/20110131/7387d413/attachment-0001.html
More information about the nio-dev
mailing list