もっと new i/o。

21
もっと New I/O。 Java in the Box 櫻庭 祐一

Upload: skrb

Post on 05-Dec-2014

7.228 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: もっと New I/O。

もっと New I/O。

Java in the Box

櫻庭 祐一

Page 2: もっと New I/O。

JSR 51 New I/O APIs

Mark Reinhold

Buffer/Direct Buffer

Channel

ノンブロッキング I/O

Charset

積み残しあり!

Page 3: もっと New I/O。

JSR 51 New I/O APIs

Mark Reinhold

Buffer/Direct Buffer

Channel

ノンブロッキング I/O

Charset

積み残しあり!

An API for scalable I/O operations

on both files and sockets, in the form of

either

or polling

asynchronous requests

that supports bulk access

to file attributes (including MIME content types),

escape to filesystem-specific APIs,

and a service-provider interface for pluggable filesystem

implementations.

that supports bulk access

to file attributes (including MIME content types),

escape to filesystem-specific APIs,

and a service-provider interface for pluggable filesystem

implementations.

A new filesystem interface

Page 4: もっと New I/O。

JSR 203 More New I/O APIs

Alan Bateman

非同期 I/O

SocketChannel のマルチキャスト

ファイルシステムインタフェース

当初 J2SE5.0 向け

Page 5: もっと New I/O。

なぜファイルシステム ?

メタデータメタデータ

シンボリックリンクシンボリックリンク

ファイルの監視ファイルの監視

Page 6: もっと New I/O。

なぜファイルシステム ?

メタデータメタデータ

シンボリックリンクシンボリックリンク

ファイルの監視ファイルの監視

新しいファイルシステム

インタフェースなら

すべて解決 !!

Page 7: もっと New I/O。

<<factory>>FileSystems FileSystem

PathFiles <<interface>>AttributeView

<<interface>>FileVisitor

<<interface>>WatchService

Page 8: もっと New I/O。

生成

FileSystem fileSystem = FileSystems.getDefault();

// file.txt の生成

Path path1 = fileSystem.getPath("file.txt", null);

// foo/bar/file.txt の生成

Path path2 = fileSystem.getPath("foo", "bar", "file.txt");

Page 9: もっと New I/O。

生成

File file = ...;

// File から Path

Path path = file.toPath();

// Path から File

File file2 = path.toFile();

Page 10: もっと New I/O。

生成

FileSystem fileSystem = FileSystems.getDefault();

// file.txt の生成

Path path1 = fileSystem.getPath("file.txt", null);

// foo/bar/file.txt の生成

Path path2 = fileSystem.getPath("foo", "bar", "file.txt");

File file = ...;

// File から Path

Path path = file.toPath();

// Path から File

File file2 = path.toFile();

// シンボリックリンクのターゲット

Path target = fileSystem.getPath("realfile.txt");

// シンボリックリンクするファイル

Path link = fileSystem.getPath("link.txt");

// シンボリックリンクの作成

Files.createSymbolicLink(link, target);

Page 11: もっと New I/O。

操作 : Files クラス

// ファイルのコピー

String source = ...;

Path source = fileSystem.getPath(source);

String destination = ...;

Path destination = fileSystem.getPath(destination);

Files.copy(source, destination);

Page 12: もっと New I/O。

操作 : Files クラス

Path source = ...; Path dest = ...;

// 移動

Files.move(source, dest);

// 削除

Files.delete(dest);

Files.deleteIfExists(dest);

Page 13: もっと New I/O。

操作 : Files クラス

Path path = ...;

// ストリームなどの取得

BufferedReader reader = Files.newBufferedReader(path,

Charset.defaultCharset());

BufferedWriter writer = Files.newBufferedWriter(path,

Charset.defaultCharset());

InputStream iStream = Files.newInputStream(path);

OutputStream oStream = Files.newOutputStream(path);

ByteChannel channel = Files.newByteChannel(path);

Page 14: もっと New I/O。

操作 : Files クラス

// ファイルのコピー

String source = ...;

Path source = fileSystem.getPath(source);

String destination = ...;

Path destination = fileSystem.getPath(destination);

Files.copy(source, destination);

Path source = ...; Path dest = ...;

// 移動

Files.move(source, dest);

// 削除

Files.delete(dest);

Files.deleteIfExists(dest);

Path path = ...;

// ストリームなどの取得

BufferedReader reader = Files.newBufferedReader(path,

Charset.defaultCharset());

BufferedWriter writer = Files.newBufferedWriter(path,

Charset.defaultCharset());

InputStream iStream = Files.newInputStream(path);

OutputStream oStream = Files.newOutputStream(path);

ByteChannel channel = Files.newByteChannel(path);

Path path = ...;

// 簡易入出力

byte[] bytes = Files.readAllBytes(path);

List<String> lines = Files.readAllLines(path,

Charset.defaultCharset());

Files.write(path, bytes);

Files.write(path, lines, Charset.defaultCharset());

Page 15: もっと New I/O。

メタデータ

Path path = ...;

// 直接取得

FileTime creationTime

= (FileTime)Files.getAttribute(path, "creationTime");

// AttributeView を介して取得

BasicFileAttributeView view

= Files.getFileAttributeView(path,

BasicFileAttributeView.class);

BasicFileAttributes attributes = view.readAttributes();

FileTime lastAccessTime = attributes.lastAccessTime();

Path path = ...;

// 直接取得

FileTime creationTime

= (FileTime)Files.getAttribute(path, "creationTime");

// AttributeView を介して取得

BasicFileAttributeView view

= Files.getFileAttributeView(path,

BasicFileAttributeView.class);

BasicFileAttributes attributes = view.readAttributes();

FileTime lastAccessTime = attributes.lastAccessTime();

Page 16: もっと New I/O。

メタデータ

Path path = ...;

// 直接取得

FileTime creationTime

= (FileTime)Files.getAttribute(path, "creationTime");

// AttributeView を介して取得

BasicFileAttributeView view

= Files.getFileAttributeView(path,

BasicFileAttributeView.class);

BasicFileAttributes attributes = view.readAttributes();

FileTime lastAccessTime = attributes.lastAccessTime();

Path path = ...;

// 直接取得

FileTime creationTime

= (FileTime)Files.getAttribute(path, "creationTime");

// AttributeView を介して取得

BasicFileAttributeView view

= Files.getFileAttributeView(path,

BasicFileAttributeView.class);

BasicFileAttributes attributes = view.readAttributes();

FileTime lastAccessTime = attributes.lastAccessTime();

Page 17: もっと New I/O。

ファイルビジター Vistor パターンを使用してファイルツリーを探索

Path startDir = ...;

Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

public FileVisitResult visitFile(Path file,

BasicFileAttributes attrs) throws IOException {

System.out.println("Visit File: " + file);

return FileVisitResult.CONTINUE;

}

public FileVisitResult preVisitDirectory(Path dir,

BasicFileAttributes attrs) throws IOException {

System.out.println("Visit Directory: " + dir);

return FileVisitResult.CONTINUE;

}

});

Page 18: もっと New I/O。

ファイルの監視WatchService

Path dir = fileSystem.getPath(...);

WatchService service = fileSystem.newWatchService();

dir.register(service,

StandardWatchEventKind.ENTRY_MODIFY);

for (;;) {

WatchKey key = service.take();

for (WatchEvent<?> event : key.pollEvents()) {

System.out.println(event.kind()

+ " " + event.context());

}

key.reset();

}

Path dir = fileSystem.getPath(...);

WatchService service = fileSystem.newWatchService();

dir.register(service,

StandardWatchEventKind.ENTRY_MODIFY);

for (;;) {

WatchKey key = service.take();

for (WatchEvent<?> event : key.pollEvents()) {

System.out.println(event.kind()

+ " " + event.context());

}

key.reset();

}

Path dir = fileSystem.getPath(...);

WatchService service = fileSystem.newWatchService();

dir.register(service,

StandardWatchEventKind.ENTRY_MODIFY);

for (;;) {

WatchKey key = service.take();

for (WatchEvent<?> event : key.pollEvents()) {

System.out.println(event.kind()

+ " " + event.context());

}

key.reset();

}

Page 19: もっと New I/O。

非同期 I/O

Selector

AsynchronousChannelFuture

CompletionHandler

マルチキャスト

DatagramChannel

Page 20: もっと New I/O。

JSR 51 の積み残し

非同期 I/O

ファイルシステムインタフェース

マルチキャスト

JSR 203 More New I/O

ファイルシステムインタフェース

メタデータ ファイルビジター

ファイルの監視

Page 21: もっと New I/O。

もっと New I/O。

Java in the Box

櫻庭 祐一