summaryrefslogtreecommitdiff
path: root/src/main/java/io
diff options
context:
space:
mode:
authorSzymon Szukalski <szymon.szukalski@gmail.com>2012-10-01 13:35:00 +1000
committerSzymon Szukalski <szymon.szukalski@gmail.com>2012-10-01 13:35:00 +1000
commit6a16552fd38a91076169466328763cf541027e31 (patch)
tree826659a21331ef9e9cc9b0ee48f27456caa3a0c1 /src/main/java/io
initial commit
Diffstat (limited to 'src/main/java/io')
-rw-r--r--src/main/java/io/skas/melbjvm/nio2/App.java34
-rw-r--r--src/main/java/io/skas/melbjvm/nio2/FileReader.java76
-rw-r--r--src/main/java/io/skas/melbjvm/nio2/Nio2MetaDataLoggingUtils.java115
-rw-r--r--src/main/java/io/skas/melbjvm/nio2/Watcher.java53
4 files changed, 278 insertions, 0 deletions
diff --git a/src/main/java/io/skas/melbjvm/nio2/App.java b/src/main/java/io/skas/melbjvm/nio2/App.java
new file mode 100644
index 0000000..df6e962
--- /dev/null
+++ b/src/main/java/io/skas/melbjvm/nio2/App.java
@@ -0,0 +1,34 @@
+package io.skas.melbjvm.nio2;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+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");
+
+ 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/FileReader.java b/src/main/java/io/skas/melbjvm/nio2/FileReader.java
new file mode 100644
index 0000000..56882ec
--- /dev/null
+++ b/src/main/java/io/skas/melbjvm/nio2/FileReader.java
@@ -0,0 +1,76 @@
+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<Integer, ByteBuffer> {
+
+ 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
new file mode 100644
index 0000000..3c2451d
--- /dev/null
+++ b/src/main/java/io/skas/melbjvm/nio2/Nio2MetaDataLoggingUtils.java
@@ -0,0 +1,115 @@
+package io.skas.melbjvm.nio2;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.nio.file.FileStore;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.*;
+import java.util.Set;
+
+public class Nio2MetaDataLoggingUtils {
+
+ public static final Logger LOG = LoggerFactory.getLogger(Nio2MetaDataLoggingUtils.class);
+
+ public static final long BYTES_IN_MEGABYTE = 1048576L;
+
+ 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";
+
+ public static void logBasicFileAttributes(Path path) throws IOException {
+ final FileStore fileStore = Files.getFileStore(path);
+
+ if (fileStore.supportsFileAttributeView(BASIC_VIEW)) {
+ 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());
+ LOG.info(" isSymbolicLink? {}", basicFileAttributes.isSymbolicLink());
+ } else {
+ LOG.info("the file store in which [{}] resides doesn't support the [{}] file attribute view", path, BASIC_VIEW);
+ }
+ }
+
+ public static void logDosFileAttributes(Path path) throws IOException {
+ final FileStore fileStore = Files.getFileStore(path);
+
+ if (fileStore.supportsFileAttributeView(DOS_VIEW)) {
+ DosFileAttributes dosFileAttributes = Files.readAttributes(path, DosFileAttributes.class);
+
+ LOG.info("Dos file attributes for path [{}]", path);
+
+ LOG.info(" isArchive? {}", dosFileAttributes.isArchive());
+ LOG.info(" isHidden? {}", dosFileAttributes.isHidden());
+ LOG.info(" isReadOnly? {}", dosFileAttributes.isReadOnly());
+ LOG.info(" isSystem? {}", dosFileAttributes.isSystem());
+ } else {
+ LOG.info("the file store in which [{}] resides doesn't support the [{}] file attribute view", path, DOS_VIEW);
+ }
+ }
+
+ public static void logPosixFileAttributes(Path path) throws IOException {
+ final FileStore fileStore = Files.getFileStore(path);
+
+ if (fileStore.supportsFileAttributeView(POSIX_VIEW)) {
+ PosixFileAttributes posixFileAttributes = Files.readAttributes(path, PosixFileAttributes.class);
+
+ final Set<PosixFilePermission> permissions = posixFileAttributes.permissions();
+
+ LOG.info("Posix file attributes for path [{}]", path);
+
+ LOG.info(" owner? {}", posixFileAttributes.owner());
+ LOG.info(" group? {}", posixFileAttributes.group());
+ LOG.info(" owner permissions? {}",
+ (permissions.contains(PosixFilePermission.OWNER_READ) ? "r" : "-") +
+ (permissions.contains(PosixFilePermission.OWNER_WRITE) ? "w" : "-") +
+ (permissions.contains(PosixFilePermission.OWNER_EXECUTE) ? "x" : "-"));
+ LOG.info(" group permissions? {}",
+ (permissions.contains(PosixFilePermission.GROUP_READ) ? "r" : "-") +
+ (permissions.contains(PosixFilePermission.GROUP_WRITE) ? "w" : "-") +
+ (permissions.contains(PosixFilePermission.GROUP_EXECUTE) ? "x" : "-"));
+ LOG.info(" others permissions? {}",
+ (permissions.contains(PosixFilePermission.OTHERS_READ) ? "r" : "-") +
+ (permissions.contains(PosixFilePermission.OTHERS_WRITE) ? "w" : "-") +
+ (permissions.contains(PosixFilePermission.OTHERS_EXECUTE) ? "x" : "-"));
+ } else {
+ LOG.info("the file store in which [{}] resides doesn't support the [{}] file attribute view", path, POSIX_VIEW);
+ }
+ }
+
+ 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 {
+ LOG.info("the file store in which [{}] resides doesn't support the [{}] file attribute view", path, OWNER_VIEW);
+ }
+ }
+
+ public static void logFileStoreAttributes(Path path) throws IOException {
+ final FileStore fileStore = Files.getFileStore(path);
+
+ LOG.info("File store attributes for path [{}]", path);
+
+ LOG.info(" name: {}", fileStore.name());
+ LOG.info(" type: {}", fileStore.type());
+ LOG.info(" total space: {}", fileStore.getTotalSpace() / BYTES_IN_MEGABYTE);
+ LOG.info(" total unallocated: {}", fileStore.getUnallocatedSpace() / BYTES_IN_MEGABYTE);
+ LOG.info(" total space: {}", fileStore.getUsableSpace() / BYTES_IN_MEGABYTE);
+ }
+
+}
diff --git a/src/main/java/io/skas/melbjvm/nio2/Watcher.java b/src/main/java/io/skas/melbjvm/nio2/Watcher.java
new file mode 100644
index 0000000..2947ef7
--- /dev/null
+++ b/src/main/java/io/skas/melbjvm/nio2/Watcher.java
@@ -0,0 +1,53 @@
+package io.skas.melbjvm.nio2;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.nio.file.*;
+
+public class Watcher {
+
+ private static final Logger LOG = LoggerFactory.getLogger(Watcher.class);
+
+ public void watchDirectory(Path watchedPath) throws IOException, InterruptedException {
+
+ try (WatchService watchService = FileSystems.getDefault().newWatchService()) {
+
+ final WatchKey key = watchedPath.register(
+ watchService,
+ StandardWatchEventKinds.ENTRY_CREATE,
+ StandardWatchEventKinds.ENTRY_MODIFY,
+ StandardWatchEventKinds.ENTRY_DELETE);
+
+ while (true) {
+
+ watchService.take();
+
+ for (WatchEvent<?> watchEvent : key.pollEvents()) {
+ final WatchEvent.Kind<?> kind = watchEvent.kind();
+
+ // handle OVERFLOW event
+ if (kind == StandardWatchEventKinds.OVERFLOW) {
+ continue;
+ }
+
+ final WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent;
+ Path newFile = watchedPath.resolve(watchEventPath.context());
+
+ if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
+ new FileReader(newFile);
+ }
+ }
+
+ //reset the key
+ boolean valid = key.reset();
+
+ //exit loop if the key is not valid (if the directory was deleted, for example)
+ if (!valid) {
+ break;
+ }
+ }
+ }
+ }
+}