What methods should go into a java.util.Objects class in JDK 7?

Alan Bateman Alan.Bateman at Sun.COM
Wed Sep 16 16:08:37 UTC 2009


Joel Kamentz wrote:
> :
>     }
>
>     static void close(InputStream s) {
>         if (s == null) return;
>         try { s.close(); }
>         catch (IOException e) {}
>     }
>
>     ... repeat for other stream types as necessary, like ZipFile, RandomAccessFile, etc. because no common inheritance
>   
Just on this one, maybe you are looking for java.io.Closeable? ZipFile 
was updated to implement it a while back. Then again, perhaps Closeable 
is too general because you wouldn't want to do this with output or other 
writable streams (because of possible data loss if the close needs to 
flush buffered bytes and it fails).


> :
>
>         Attempt to convert an URL to a local file, taking into account that it might be wrappered by jar:, that File.toURL doesn't process %20 and the like, etc.
>   
File.toURL is deprecated. You might want to look at File.toURI.


>         Delete files or sub-trees, catching exceptions and instead just return success / failure.
>   
This one comes up a lot and has been addressed in the file system API 
work in jdk7. So if you have a File f you can replace f.delete() with 
f.toPath().delete() and it will do what you want.

Recursive delete is relatively easy too, using Files.walkFileTree. 
There's an example in the javadoc that does this (look in 
java.nio.file.FileVisitor).

>         Copy an InputStream to an OutputStream with supplied byte buffer (or allocate one internally).  General file (or stream or ?) copying utility methods might be useful.
>   
File copy is supported in the file system API. Look at Path.copyTo. It 
has options to indicate if the target file should be replaced, if file 
attributes should be copied, etc.

In the nio repository there are prototype java.io.Inputs and 
java.io.Outputs classes that define static methods for useful/common 
tasks like reading all bytes or lines from a file or input stream. There 
is also a method to copy all bytes from an input stream to an output 
stream. It's on my list to finish this off and get them into jdk7.

-Alan.





More information about the core-libs-dev mailing list