java i/o history from jse1.0 jse7 - mathunipdabujari/pcd1819/java_nio2.pdf · 5555 comparison of...
TRANSCRIPT
5454
Java I/O history from JSE1.0 – JSE7
I/O in Java 1.1
NIO (JSR-51) in JDK1.4
NIO.2 (JSR-203) in JSE7
5555
Comparison of I/O features in Releases
File System part of the NIO.2 is platform specific
File tree walk
File Operations (Copy, Delete, Move, etc.)
Symbolic links support
Support for file attributes in NIO.2
Support for handling file permissions
Java 7
All File System were treated the same
No API for walking over the file system
No API for file operations
No support for symbolic links
No support for reading and writing attributes
No support for dealing with permissions
Before Java 7
What NIO.2 providesCurrent lacking IO features
5656
Comparison of I/O features in Releases
Proper Exception handling
Support for change notification
Provider SPI to add new file system support
Asynchronous I/O
What NIO.2 provides
Java 7
No proper exception handling
No watch API to handle file system changes
No SPI for adding support for new file systems
No Asynchronous I/O support
Current lacking IO features
Before Java 7
5757
The NIO.2 Architecture
Classes, interfaces reside in java.nioNew set of main classes, interfaces
FileSystems factory, FileSysteminterface: The file system doorway
Path: The object representing an existing or non-existing file or directory in the file system.FileStore: Represents underlying storage devices, partitions and so
on.WatchService: Watching a directory or a file in the file systemAttributes and related classes:
Reading and writing attributes
Permissions and related classes: Reading and writing permissionsetc
A FileSystem
A FileSystemsProvider
WatchService
Attibutes
Path
FileStore
Etc
Through FileSystems
Factory class
5858
How the NIO.2 works
FileSystem fs = FileSystems.getDefault();
Path p = fs.getPath("/home/abujari/someFile.txt");
try {
Files.deleteIfExists(p);
} catch (IOException ex) {
//handle it
}
Basically everything start with FileSystems and
FileSystem object
5959
Basic File operations with Path
Copying and moving Files
Attributes state after copying / moving; creating files or directories.
Overwriting the target file
Move can be atomic
FileSystem fs = FileSystems.getDefault();
Path p = fs.getPath("/home/abujari/simple.txt");
Path newCopy = fs.getPath("/home/masoud/copy.txt");
p.copyTo(newCopy,
StandardCopyOption.COPY_ATTRIBUTES,
StandardCopyOption.REPLACE_EXISTING);
newCopy.moveTo(fs.getPath("/home/abujari/moved.txt"),
StandardCopyOption.ATOMIC_MOVE);
6060
Symbolic links support
• Support for
• Creating and Resolving Symbolic Links
• Creating hard links
FileSystem fs = FileSystems.getDefault();
Path actualFile = fs.getPath("/home/masoud/simple");
//creating a symbolic link
Path symLinkFile = fs.getPath("/home/masoud/simple.sym");
symLinkFile.createSymbolicLink(actualFile);
System.out.println("Target: "+symLinkFile.readSymbolicLink());
//Creating hard link
Path linkFile = fs.getPath("/home/masoud/simple.link");
linkFile.createLink(actualFile);
6161
Walking The File System Tree
• Files.walkFileTree
* Walks a file tree from a given starting path down to the given depth
* Invoke FileVisitor methods for each file/directory
PreVisitDirectory PreVisitDirectoryFailed VisitFile
VisitFileFailed postVisitDirectory
public class SimpleVisitor {
public static void main(String args[]) throws IOException {
FileSystem fs = FileSystems.getDefault();Path p = fs.getPath("/home/abujari/");MyVisit v = new MyVisit();Files.walkFileTree(p, EnumSet.allOf(FileVisitOption.class), 2, v);
}}
class MyVisit extends SimpleFileVisitor<Path> {
@Overridepublic FileVisitResult visitFile(Path file, BasicFileAttributes attrs)throws IOException {
System.out.println(file.getFileName());return super.visitFile(file, attrs);
}}
6262
• Reading and Writing File attributes
• Different views for different file systems
– Dos View: DosFileAttributeView
– Posix view: PosixFileAttributeView
– ACL view: AclFileAttributeView
– User defined attributes view: UserDefinedFileAttributeView
– Basic view: BasicFileAttributeView: Common for All FS
• Each view has a corresponding *FileAttributes object holding the attributes for a file system entry.
• Checking for supported attributes using FileSystem.supportedFileAttributeViews()
File attributes and metadata
6363
File attributes and metadata: basic
attributes
BasicFileAttributes: Common to all FS and contains
basic attributes. Creation Time Last Access Time Modification Time SizeFile Type: isDirectory,IsFile, IsSymbolicLink,IsOtherFile Key
FileSystem fs = FileSystems.getDefault(); Path p = fs.getPath("/home/abujari/simple");
BasicFileAttributeView bav= Files.getFileAttributeView(p, BasicFileAttributeView.class,LinkOption.NOFOLLOW_LINKS);
BasicFileAttributes ba =bav.readAttributes();
System.out.println(p.toString() + " last access: " + ba.lastAccessTime()); System.out.println(p.toString() + " last modified: " + ba.lastModifiedTime());
6464
File attributes and metadata: Platform
specific Attributes
PosixFileAttributeView Extends BasicFileAttributeView, contain Posix specificattributes.
All Basic attributes Retrieving group Retrieving owner Retrieving permission
FileSystem fs = FileSystems.getDefault();Path p = fs.getPath("/home/masoud/photo.png");
Set<String> supportedViews = fs.supportedFileAttributeViews();
if (supportedViews.contains("unix")) {
PosixFileAttributes pat = Attributes.readPosixFileAttributes(p, LinkOption.NOFOLLOW_LINKS);
System.out.println(pat.group().getName()); System.out.println(pat.owner().
}
basic unix dos acl owner
user etc.
6565
• Using Attributes utility class methods:
• Attributes.readBasicFileAttributes(...);
• Attributes.readDosFileAttributes(...);
• Attributes.readPosixFileAttributes(...)
• Bulk read using Path.readAttributes(...) method:
Map<String, ?> att = (Map<String, String>) p.readAttributes("posix:owner,basic:*");
•Using Path and AttributeViews:
Path.getFileAttributeView(Class<V> type, LinkOption...
options)
Different ways for retrieving file attributes
Comma separated list of [View name: Attribute name] Default view is Basic View.For example:basic:lastModifiedTime
basic:size
posix:group
dos:hidden
posix:*
fileKey → with no view name
BasicFileAttributeView DosFileAttributeView
PosixFileAttributeView
6666
File attributes and metadata:
•
•
•
Managing POSIX files permissions
PosixFilePermission: A posix permission
PosixFilePermissions: Utility class to convert permissions
from between OS notation and a set of PosixFilePermission
Attributes: To read file attributes
PosixFileAttributes: includes posix file permissions
FileSystem fs = FileSystems.getDefault();
Path p = fs.getPath("/home/abujari/script.sh");
PosixFileAttributes patts = Attributes.readPosixFileAttributes(p, LinkOption.NOFOLLOW_LINKS);Set<PosixFilePermission> st = patts.permissions();
System.out.println(PosixFilePermissions.toString(st));
6767
FileStore features
•The FileStore: Represent the file system
underlying storage devices, pools, partitions, and
so on.
FileSystem fs = FileSystems.getFileSystem(new URI("File:///"));
Path p = fs.getPath("/home/abujari");
FileStore fstore = p.getFileStore();
FileStoreSpaceAttributes attrs = Attributes.readFileStoreSpaceAttributes(fstore); long total = attrs.totalSpace() / (1024*1024);long used = (attrs.totalSpace() - attrs.unallocatedSpace()) / (1024*1024); long avail = attrs.usableSpace() /(1024*1024);System.out.println(" device size(MB)used(MB) available(MB) "); System.out.format("%-20s %12d %12d %12d%n", fstore, total, used, avail);
We can use FileSystem.getFileStores() to get all underlying file stores.
6868
File system change notification
• How does it works:
• Use underlying FS facilities when available.
• Retreat to rudimentary polling if underlying FS does not provide change notification.
• API consists of four elements:
• Watchable: Path implements Watchable interface so the watch service works
over it.
• A set of event types: whether we want to receive creation, deletion, … events. In our case we will use StandardWatchEventKind which implements the WatchEvent.Kind<T>.
• The Watcher: This is the entity watching watchable. In our case the watcher (
java.nio.file.WatchService) watches the File System for changes.
• Watch Key:
6969
Change notification: Sample Code
public static void main(String args[]) {FileSystem fs = FileSystems.getDefault(); WatchService ws = fs.newWatchService();
Path path = fs.getPath("/home/abujari/Pictures"); path.register(ws, StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE);
WatchKey key = ws.take(); //wait!List<WatchEvent<?>> events = key.pollEvents();
for (WatchEvent object : events) {if (object.kind() == StandardWatchEventKinds.ENTRY_DELETE)
System.out.println("Delete: " +path.getFileName()+"/"+ object.context().toString());
if (object.kind() == StandardWatchEventKinds.ENTRY_CREATE) System.out.println("Created: " +path.getFileName()+"/"+
object.context().toString());}
7070
(Non-)Blocking and Asynchronous I/O
Prior to Java SE 1.4 : Everything was blocking and and use of threads was inevitable. ( which causes scalability issues and performance overhead)
Java SE 1.4: Brings the JSR-51 or NIO with non-blocking IO; Introduction of channels and selectors. Non-Blocking not Asynchronous.
Java SE 7: Addition of asynchronous IO to the pack; included inside java.nio.channels
7171
Asynchronous I/O for sockets and Files
•What is Asynchronous I/O
• No blocking, no quick pass over the buffer, simple manual
check backs or call back handlers (completion handlers).
•Why Asynchronous I/O
• More scalability, benefiting from underlying OS IO features.
•How Asynchronous I/O works
• Using java.util.concurrent.Future: Manual check
backs.
• Using java.nio.channels.CompletionHandler:
Callback handler for completion or failure of IO operation
7272
AsynchronousChannel
AsynchronousByteChannel
AsynchronousFileChannel
Asynchronous read/writeNo global file position/offset
Each read/write specifies position in file Access different parts of file concurrently
Open method specifies options: READ, WRITE, APPEND...
AsynchronousSocketChannel
Asynchronous connectAsynchronous read/writeAsynchronous scatter/gather (multiple buffers)Read/write operations support timeout: failed method invoked with timeout exceptionImplements NetworkChannel for binding, setting socket options, etc.
AsynchronousServerSocketChannel
Asynchronous accept handler invoked when connection accepted Result is AsynchronousSocketConnection
Implements NetworkChannel for binding, setting socket options, etc.
AsynchronousDatagramChannel
Asynchronous read/write (connected)•Asynchronous receive/send (unconnected)
Implements NetworkChannel for binding, setting socket options, etc.
Implements MulticastChannel to join multicast groups
Different types of Async. Channels