SlideShare a Scribd company logo
もっと New I/O。



   Java in the Box
   櫻庭 祐一
JSR 51 New I/O APIs

Buffer/Direct Buffer

 Channel

ノンブロッキング I/O

   Charset


 積み残しあり!
                       Mark Reinhold
JSR 51 New I/O APIs
  An API for scalable I/O operations
   Buffer/Directsockets, in the form of
  on both files and Buffer
  either asynchronous requests
   Channel
  or polling

Aノンブロッキングinterface that supports bulk access
 new filesystem I/O
to file attributes (including MIME content types),
escape Charset
         to filesystem-specific APIs,
and a service-provider interface for pluggable filesystem

   積み残しあり!
implementations.



                                             Mark Reinhold
JSR 203 More New I/O APIs

  非同期 I/O

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

  SocketChannel
     のマルチキャスト


                       Alan Bateman
当初 J2SE5.0 向け
なぜファイルシステム ?

 メタデータ
          シンボリックリンク
ファイルの監視
なぜファイルシステム ?
               テ ム
               シ ス
           イ ル
       フ ァ     な ら
     い
 メタデータ
   し       ー ス
 新       ェ
        フ シンボリックリンク
    ン タ     !!
  イ     解 決
    べ て
ファイルの監視
  す
<<factory>>
                      FileSystem
FileSystems



                                         <<interface>>
   Files                   Path
                                         AttributeView




           <<interface>>          <<interface>>
             FileVisitor          WatchService
生成
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();
生成
// シンボリックリンクのターゲット
File file
FileSystem=fileSystem = FileSystems.getDefault();
            ...;
Path target = fileSystem.getPath("realfile.txt");
// file.txt の生成
   シンボリックリンクするファイル
   File から Path
Path path1==file.toPath();
     link
     path   fileSystem.getPath("link.txt"); null);
             fileSystem.getPath("file.txt",

// foo/bar/file.txt の生成
   シンボリックリンクの作成
   Path から File
Files.createSymbolicLink(link, target);
File file2
Path path2 = fileSystem.getPath("foo", "bar", "file.txt");
             path.toFile();
操作 : Files クラス
// ファイルのコピー
String source = ...;
Path source = fileSystem.getPath(source);

String destination = ...;
Path destination = fileSystem.getPath(destination);

Files.copy(source, destination);
操作 : Files クラス
Path source = ...; Path dest = ...;

// 移動
Files.move(source, dest);

// 削除
Files.delete(dest);
Files.deleteIfExists(dest);
操作 : 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);
操作 : Files クラス
Path source = ...;
// ファイルのコピーPath dest = ...;
     path = ...;
// ストリームなどの取得
String source = ...;
   簡易入出力
byte[]
BufferedReader reader = Files.newBufferedReader(path,
// 移動bytes
Path source ==fileSystem.getPath(source);
               Files.readAllBytes(path);
List<String> lines = Files.readAllLines(path,
Files.move(source, dest);       Charset.defaultCharset());
BufferedWriter writer = Files.newBufferedWriter(path,
String destination = ...;    Charset.defaultCharset());
// 削除
Path destination = fileSystem.getPath(destination);
                                Charset.defaultCharset());
Files.write(path, bytes);
InputStream iStream
Files.delete(dest); = Files.newInputStream(path);
Files.write(path, destination);
OutputStream oStream = Files.newOutputStream(path);
Files.deleteIfExists(dest);
Files.copy(source,lines, Charset.defaultCharset());
ByteChannel channel = Files.newByteChannel(path);
メタデータ
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();
ファイルビジター                     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;
    }
});
ファイルの監視
                           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();
}
非同期 I/O
Selector
           AsynchronousChannel
             Future
              CompletionHandler
マルチキャスト
      DatagramChannel
JSR 51 の積み残し
 非同期 I/O
ファイルシステムインタフェース
  マルチキャスト


       JSR 203 More New I/O
ファイルシステムインタフェース
    メタデータ      ファイルビジター
        ファイルの監視
もっと New I/O。




   Java in the Box
   櫻庭 祐一

More Related Content

PDF
NIO.2を使って簡単ファイル操作
PPTX
20071030
PDF
Javaセキュアコーディングセミナー東京第3回演習の解説
PDF
第3回 FIC&FCS合同勉強会
PDF
debugging server with strace
PDF
構築手順 Ssis イベントログ取込み 第2版
PDF
Webサーバ勉強会 (160-162:?)
NIO.2を使って簡単ファイル操作
20071030
Javaセキュアコーディングセミナー東京第3回演習の解説
第3回 FIC&FCS合同勉強会
debugging server with strace
構築手順 Ssis イベントログ取込み 第2版
Webサーバ勉強会 (160-162:?)

Similar to もっと New I/O。 (20)

PDF
明日から使える Java SE 7
PDF
2010 icse-an analysis of the variability in forty preprocessor-based software...
PDF
生物データベース論(分散ファイルシステム概論)
PDF
Javaセキュアコーディングセミナー東京第3回講義
PDF
PDF
WDD2012_SC-004
PDF
ffi for rubyists
PDF
Hadoop book-2nd-ch3-update
PDF
File API: Writer & Directories and System
PDF
#02-01 ZFS によるストレージ仮想化 (2012-04-20)
PDF
Brief introduction of Boost.ICL
PDF
Brief introduction of Boost.ICL [PDF]
PDF
TokyoWebMining#18_nukamu
PDF
これからのコンピューティングの変化とJava-JJUG CCC 2015 Fall
PDF
PostgreSQL安定運用のコツ2009 @hbstudy#5
PDF
(工事中) Git の仕組み
PDF
(工事中) Git の仕組み
PDF
AndroidでFeliCaの履歴を読もう
PDF
GCをみればRTSが見えてくる、かも。。。
PDF
What is java_se_7
明日から使える Java SE 7
2010 icse-an analysis of the variability in forty preprocessor-based software...
生物データベース論(分散ファイルシステム概論)
Javaセキュアコーディングセミナー東京第3回講義
WDD2012_SC-004
ffi for rubyists
Hadoop book-2nd-ch3-update
File API: Writer & Directories and System
#02-01 ZFS によるストレージ仮想化 (2012-04-20)
Brief introduction of Boost.ICL
Brief introduction of Boost.ICL [PDF]
TokyoWebMining#18_nukamu
これからのコンピューティングの変化とJava-JJUG CCC 2015 Fall
PostgreSQL安定運用のコツ2009 @hbstudy#5
(工事中) Git の仕組み
(工事中) Git の仕組み
AndroidでFeliCaの履歴を読もう
GCをみればRTSが見えてくる、かも。。。
What is java_se_7
Ad

More from Yuichi Sakuraba (20)

PDF
Vector API - Javaによるベクターコンピューティング
PDF
Oracle Code One - Java KeynoteとJava SE
PDF
Project Loom + Project Panama
PDF
Project Loom - 限定継続と軽量スレッド -
PDF
Oracle Code One 報告会 Java SE Update
PDF
今こそStream API入門
PDF
Oracle Code One 報告会 Java SE Update
PDF
Learn Language 2018 Java Language Update
PDF
Dockerに向けて、Javaもダイエット
PDF
What's New in Java
PDF
Migration Guide to Java SE 10, and also Java SE 11
PDF
琥珀色のJava - Project Amber -
PDF
Moving to Module: Issues & Solutions
PDF
モジュール移行の課題と対策
PDF
Project Jigsawと、ちょっとだけVector API
PDF
Java SE 9の全貌
PDF
Java SEの現在、過去 そして未来
PDF
Java SE 9 のススメ
PDF
Introduction of Project Jigsaw
PDF
Encouragement of Java SE 9
Vector API - Javaによるベクターコンピューティング
Oracle Code One - Java KeynoteとJava SE
Project Loom + Project Panama
Project Loom - 限定継続と軽量スレッド -
Oracle Code One 報告会 Java SE Update
今こそStream API入門
Oracle Code One 報告会 Java SE Update
Learn Language 2018 Java Language Update
Dockerに向けて、Javaもダイエット
What's New in Java
Migration Guide to Java SE 10, and also Java SE 11
琥珀色のJava - Project Amber -
Moving to Module: Issues & Solutions
モジュール移行の課題と対策
Project Jigsawと、ちょっとだけVector API
Java SE 9の全貌
Java SEの現在、過去 そして未来
Java SE 9 のススメ
Introduction of Project Jigsaw
Encouragement of Java SE 9
Ad

もっと New I/O。

  • 1. もっと New I/O。 Java in the Box 櫻庭 祐一
  • 2. JSR 51 New I/O APIs Buffer/Direct Buffer Channel ノンブロッキング I/O Charset 積み残しあり! Mark Reinhold
  • 3. JSR 51 New I/O APIs An API for scalable I/O operations Buffer/Directsockets, in the form of on both files and Buffer either asynchronous requests Channel or polling Aノンブロッキングinterface that supports bulk access new filesystem I/O to file attributes (including MIME content types), escape Charset to filesystem-specific APIs, and a service-provider interface for pluggable filesystem 積み残しあり! implementations. Mark Reinhold
  • 4. JSR 203 More New I/O APIs 非同期 I/O ファイルシステムインタフェース SocketChannel のマルチキャスト Alan Bateman 当初 J2SE5.0 向け
  • 5. なぜファイルシステム ? メタデータ シンボリックリンク ファイルの監視
  • 6. なぜファイルシステム ? テ ム シ ス イ ル フ ァ な ら い メタデータ し ー ス 新 ェ フ シンボリックリンク ン タ !! イ 解 決 べ て ファイルの監視 す
  • 7. <<factory>> FileSystem FileSystems <<interface>> Files Path AttributeView <<interface>> <<interface>> FileVisitor WatchService
  • 8. 生成 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");
  • 9. 生成 File file = ...; // File から Path Path path = file.toPath(); // Path から File File file2 = path.toFile();
  • 10. 生成 // シンボリックリンクのターゲット File file FileSystem=fileSystem = FileSystems.getDefault(); ...; Path target = fileSystem.getPath("realfile.txt"); // file.txt の生成 シンボリックリンクするファイル File から Path Path path1==file.toPath(); link path fileSystem.getPath("link.txt"); null); fileSystem.getPath("file.txt", // foo/bar/file.txt の生成 シンボリックリンクの作成 Path から File Files.createSymbolicLink(link, target); File file2 Path path2 = fileSystem.getPath("foo", "bar", "file.txt"); path.toFile();
  • 11. 操作 : Files クラス // ファイルのコピー String source = ...; Path source = fileSystem.getPath(source); String destination = ...; Path destination = fileSystem.getPath(destination); Files.copy(source, destination);
  • 12. 操作 : Files クラス Path source = ...; Path dest = ...; // 移動 Files.move(source, dest); // 削除 Files.delete(dest); Files.deleteIfExists(dest);
  • 13. 操作 : 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);
  • 14. 操作 : Files クラス Path source = ...; // ファイルのコピーPath dest = ...; path = ...; // ストリームなどの取得 String source = ...; 簡易入出力 byte[] BufferedReader reader = Files.newBufferedReader(path, // 移動bytes Path source ==fileSystem.getPath(source); Files.readAllBytes(path); List<String> lines = Files.readAllLines(path, Files.move(source, dest); Charset.defaultCharset()); BufferedWriter writer = Files.newBufferedWriter(path, String destination = ...; Charset.defaultCharset()); // 削除 Path destination = fileSystem.getPath(destination); Charset.defaultCharset()); Files.write(path, bytes); InputStream iStream Files.delete(dest); = Files.newInputStream(path); Files.write(path, destination); OutputStream oStream = Files.newOutputStream(path); Files.deleteIfExists(dest); Files.copy(source,lines, Charset.defaultCharset()); ByteChannel channel = Files.newByteChannel(path);
  • 15. メタデータ 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();
  • 16. メタデータ 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();
  • 17. ファイルビジター 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; } });
  • 18. ファイルの監視 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(); }
  • 19. 非同期 I/O Selector AsynchronousChannel Future CompletionHandler マルチキャスト DatagramChannel
  • 20. JSR 51 の積み残し 非同期 I/O ファイルシステムインタフェース マルチキャスト JSR 203 More New I/O ファイルシステムインタフェース メタデータ ファイルビジター ファイルの監視
  • 21. もっと New I/O。 Java in the Box 櫻庭 祐一