From 9c632725afb73bbf0966e52ca50c6d66c12d853b Mon Sep 17 00:00:00 2001 From: Szymon Szukalski Date: Tue, 2 Oct 2012 21:11:19 +1000 Subject: Re-wrote AsynchronousFileReader. Various clean-ups. --- src/main/java/io/skas/melbjvm/nio2/App.java | 19 ++--- .../skas/melbjvm/nio2/AsynchronousFileReader.java | 81 ++++++++++++++++++++++ src/main/java/io/skas/melbjvm/nio2/FileReader.java | 76 -------------------- .../melbjvm/nio2/Nio2MetaDataLoggingUtils.java | 8 +-- src/main/java/io/skas/melbjvm/nio2/Watcher.java | 11 +-- 5 files changed, 101 insertions(+), 94 deletions(-) create mode 100644 src/main/java/io/skas/melbjvm/nio2/AsynchronousFileReader.java delete mode 100644 src/main/java/io/skas/melbjvm/nio2/FileReader.java diff --git a/src/main/java/io/skas/melbjvm/nio2/App.java b/src/main/java/io/skas/melbjvm/nio2/App.java index df6e962..a586247 100644 --- a/src/main/java/io/skas/melbjvm/nio2/App.java +++ b/src/main/java/io/skas/melbjvm/nio2/App.java @@ -7,28 +7,29 @@ import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; +/** + * @author Szymon Szukalski [szymon.szukalski@gmail.com] + */ public class App { public static final Logger LOG = LoggerFactory.getLogger(App.class); public static void main(String[] args) throws IOException { - final Path path = Paths.get("/tmp/doit"); + final Path path = Paths.get("/tmp", "doit"); + + Nio2MetaDataLoggingUtils.logFileStoreAttributes(path); + Nio2MetaDataLoggingUtils.logBasicFileAttributes(path); + Nio2MetaDataLoggingUtils.logDosFileAttributes(path); + Nio2MetaDataLoggingUtils.logPosixFileAttributes(path); + Nio2MetaDataLoggingUtils.logFileOwnerAttributes(path); Watcher watcher = new Watcher(); try { - Nio2MetaDataLoggingUtils.logFileStoreAttributes(path); - Nio2MetaDataLoggingUtils.logBasicFileAttributes(path); - Nio2MetaDataLoggingUtils.logDosFileAttributes(path); - Nio2MetaDataLoggingUtils.logPosixFileAttributes(path); - Nio2MetaDataLoggingUtils.logFileOwnerAttributes(path); - watcher.watchDirectory(path); - } catch (IOException | InterruptedException ex) { LOG.error(ex.getMessage()); } } - } diff --git a/src/main/java/io/skas/melbjvm/nio2/AsynchronousFileReader.java b/src/main/java/io/skas/melbjvm/nio2/AsynchronousFileReader.java new file mode 100644 index 0000000..ff7c82b --- /dev/null +++ b/src/main/java/io/skas/melbjvm/nio2/AsynchronousFileReader.java @@ -0,0 +1,81 @@ +package io.skas.melbjvm.nio2; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.AsynchronousFileChannel; +import java.nio.channels.CompletionHandler; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; + +/** + * @author Szymon Szukalski [szymon.szukalski@gmail.com] + */ +public class AsynchronousFileReader implements CompletionHandler { + + public static final Logger LOG = LoggerFactory.getLogger(AsynchronousFileReader.class); + public static final int BYTES_IN_MEGABYTE = 1048576; + + + private Long position; + private Path path; + private ByteBuffer buffer; + private AsynchronousFileChannel asynchronousFileChannel; + + public AsynchronousFileReader(Path path) { + this.position = 0L; + this.path = path; + this.buffer = ByteBuffer.allocate(BYTES_IN_MEGABYTE); + + LOG.info("reading {}...", path); + + this.openChannel(); + this.readChannel(0); + } + + private void openChannel() { + try { + this.asynchronousFileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void closeChannel() { + try { + this.asynchronousFileChannel.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void readChannel(long position) { + asynchronousFileChannel.read(buffer, position, buffer, this); + } + + @Override + public void completed(Integer result, ByteBuffer buffer) { + if (result < 0) { + closeChannel(); + LOG.debug("read: {} megabytes", position / BYTES_IN_MEGABYTE); + } else { + position += result; + if (buffer.hasRemaining()) { + readChannel(position); + } else { + buffer.flip(); + // Do something with the content of the buffer + buffer.clear(); + this.readChannel(position); + } + } + } + + @Override + public void failed(Throwable exc, ByteBuffer buffer) { + LOG.error("read failed: {}", exc.getMessage()); + exc.printStackTrace(); + } +} diff --git a/src/main/java/io/skas/melbjvm/nio2/FileReader.java b/src/main/java/io/skas/melbjvm/nio2/FileReader.java deleted file mode 100644 index 56882ec..0000000 --- a/src/main/java/io/skas/melbjvm/nio2/FileReader.java +++ /dev/null @@ -1,76 +0,0 @@ -package io.skas.melbjvm.nio2; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.channels.AsynchronousFileChannel; -import java.nio.channels.CompletionHandler; -import java.nio.file.Path; -import java.nio.file.StandardOpenOption; - -/** - * @author Szymon Szukalski [szymon.szukalski@portlandrisk.com] - */ -public class FileReader implements CompletionHandler { - - public static final Logger LOG = LoggerFactory.getLogger(FileReader.class); - - private Long fileSize; - private Path path; - private ByteBuffer buffer; - private AsynchronousFileChannel asynchronousFileChannel; - - public FileReader(Path path) { - this.fileSize = 0L; - this.path = path; - this.buffer = ByteBuffer.allocate(1048576); - LOG.info("reading {}...", path); - - this.openChannel(); - this.readChannel(0); - } - - private void openChannel() { - try { - this.asynchronousFileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ); - } catch (IOException e) { - e.printStackTrace(); - } - } - - private void readChannel(long position) { - asynchronousFileChannel.read(buffer, position, buffer, this); - } - - private void closeChannel() { - try { - this.asynchronousFileChannel.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - @Override - public void completed(Integer result, ByteBuffer attachment) { - if (result != -1) { - this.fileSize += result; - attachment.flip(); - attachment.clear(); - this.readChannel(this.fileSize); - } else { - - LOG.info("... read in: {} bytes from {}", fileSize, path); - attachment.flip(); - attachment.clear(); - this.closeChannel(); - } - } - - @Override - public void failed(Throwable exc, ByteBuffer attachment) { - LOG.error("read failed: {}", exc.getMessage()); - exc.printStackTrace(); - } -} diff --git a/src/main/java/io/skas/melbjvm/nio2/Nio2MetaDataLoggingUtils.java b/src/main/java/io/skas/melbjvm/nio2/Nio2MetaDataLoggingUtils.java index 3c2451d..0899d60 100644 --- a/src/main/java/io/skas/melbjvm/nio2/Nio2MetaDataLoggingUtils.java +++ b/src/main/java/io/skas/melbjvm/nio2/Nio2MetaDataLoggingUtils.java @@ -10,6 +10,9 @@ import java.nio.file.Path; import java.nio.file.attribute.*; import java.util.Set; +/** + * @author Szymon Szukalski [szymon.szukalski@gmail.com] + */ public class Nio2MetaDataLoggingUtils { public static final Logger LOG = LoggerFactory.getLogger(Nio2MetaDataLoggingUtils.class); @@ -18,8 +21,6 @@ public class Nio2MetaDataLoggingUtils { public static final String BASIC_VIEW = "basic"; public static final String OWNER_VIEW = "owner"; - public static final String USER_VIEW = "user"; - public static final String UNIX_VIEW = "unix"; public static final String DOS_VIEW = "dos"; public static final String POSIX_VIEW = "posix"; @@ -30,7 +31,6 @@ public class Nio2MetaDataLoggingUtils { BasicFileAttributes basicFileAttributes = Files.readAttributes(path, BasicFileAttributes.class); LOG.info("Basic file attributes for path [{}]", path); - LOG.info(" isDirectory? {}", basicFileAttributes.isDirectory()); LOG.info(" isOther? {}", basicFileAttributes.isOther()); LOG.info(" isRegularFile? {}", basicFileAttributes.isRegularFile()); @@ -88,11 +88,9 @@ public class Nio2MetaDataLoggingUtils { public static void logFileOwnerAttributes(Path path) throws IOException { final FileStore fileStore = Files.getFileStore(path); - if (fileStore.supportsFileAttributeView(OWNER_VIEW)) { final FileOwnerAttributeView fileAttributeView = Files.getFileAttributeView(path, FileOwnerAttributeView.class); LOG.info("Owner file attributes for path [{}]", path); - LOG.info(" principal: {}", fileAttributeView.getOwner()); } else { diff --git a/src/main/java/io/skas/melbjvm/nio2/Watcher.java b/src/main/java/io/skas/melbjvm/nio2/Watcher.java index 2947ef7..1938c9f 100644 --- a/src/main/java/io/skas/melbjvm/nio2/Watcher.java +++ b/src/main/java/io/skas/melbjvm/nio2/Watcher.java @@ -6,6 +6,9 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.nio.file.*; +/** + * @author Szymon Szukalski [szymon.szukalski@gmail.com] + */ public class Watcher { private static final Logger LOG = LoggerFactory.getLogger(Watcher.class); @@ -35,15 +38,15 @@ public class Watcher { final WatchEvent watchEventPath = (WatchEvent) watchEvent; Path newFile = watchedPath.resolve(watchEventPath.context()); - if (kind == StandardWatchEventKinds.ENTRY_MODIFY) { - new FileReader(newFile); + if (kind == StandardWatchEventKinds.ENTRY_CREATE) { + new AsynchronousFileReader(newFile); } } - //reset the key + // reset the key boolean valid = key.reset(); - //exit loop if the key is not valid (if the directory was deleted, for example) + // exit loop if the key is not valid (if the directory was deleted, for example) if (!valid) { break; } -- cgit v1.2.3