Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/src/main/java/com/github/fge/filesystem/box/attributes/BoxBasicFileAttributesProvider.java b/src/main/java/com/github/fge/filesystem/box/attributes/BoxBasicFileAttributesProvider.java
- index f20b2b6..760cc50 100644
- --- a/src/main/java/com/github/fge/filesystem/box/attributes/BoxBasicFileAttributesProvider.java
- +++ b/src/main/java/com/github/fge/filesystem/box/attributes/BoxBasicFileAttributesProvider.java
- @@ -1,46 +1,60 @@
- package com.github.fge.filesystem.box.attributes;
- +import java.io.IOException;
- +import java.nio.file.attribute.FileTime;
- +import java.nio.file.attribute.GroupPrincipal;
- +import java.nio.file.attribute.PosixFileAttributes;
- +import java.nio.file.attribute.PosixFilePermission;
- +import java.nio.file.attribute.PosixFilePermissions;
- +import java.nio.file.attribute.UserPrincipal;
- +import java.util.HashMap;
- +import java.util.Map;
- +import java.util.Set;
- +
- +import javax.annotation.ParametersAreNonnullByDefault;
- +
- import com.box.sdk.BoxAPIException;
- import com.box.sdk.BoxFolder;
- import com.box.sdk.BoxItem;
- import com.github.fge.filesystem.attributes.provider.BasicFileAttributesProvider;
- -
- -
- import com.github.fge.filesystem.box.exceptions.BoxIOException;
- -
- -import javax.annotation.ParametersAreNonnullByDefault;
- -import java.io.IOException;
- -import java.nio.file.attribute.FileTime;
- -
- @ParametersAreNonnullByDefault
- public final class BoxBasicFileAttributesProvider
- - extends BasicFileAttributesProvider
- + extends BasicFileAttributesProvider implements PosixFileAttributes
- {
- private final BoxItem.Info info;
- private final boolean isFolder;
- + private Map<String, BoxItem.Info> cache = new HashMap<>();
- +
- public BoxBasicFileAttributesProvider(final BoxItem item)
- throws IOException
- {
- - try {
- - info = item.getInfo();
- - } catch (BoxAPIException e) {
- - throw BoxIOException.wrap(e);
- + String key = item.getID();
- + if (cache.containsKey(key)) {
- + info = cache.get(key);
- + } else {
- + try {
- + info = item.getInfo();
- + } catch (BoxAPIException e) {
- + throw BoxIOException.wrap(e);
- + }
- + cache.put(key, info);
- }
- isFolder = item instanceof BoxFolder;
- }
- @Override
- public FileTime lastModifiedTime()
- - {
- - return FileTime.fromMillis(info.getModifiedAt().getTime());
- - }
- + {try {
- + return info.getModifiedAt() != null ? FileTime.fromMillis(info.getModifiedAt().getTime()) : creationTime();
- + } catch (NullPointerException e) {System.err.println("info: "+ info + ", info.getModifiedAt(): " + info.getModifiedAt());throw e;}}
- @Override
- public FileTime creationTime()
- {
- - return FileTime.fromMillis(info.getCreatedAt().getTime());
- + return info.getCreatedAt() != null ? FileTime.fromMillis(info.getCreatedAt().getTime()) : FileTime.fromMillis(0);
- }
- /**
- @@ -75,4 +89,22 @@ public final class BoxBasicFileAttributesProvider
- {
- return info.getSize();
- }
- +
- + /* @see java.nio.file.attribute.PosixFileAttributes#owner() */
- + @Override
- + public UserPrincipal owner() {
- + return null;
- + }
- +
- + /* @see java.nio.file.attribute.PosixFileAttributes#group() */
- + @Override
- + public GroupPrincipal group() {
- + return null;
- + }
- +
- + /* @see java.nio.file.attribute.PosixFileAttributes#permissions() */
- + @Override
- + public Set<PosixFilePermission> permissions() {
- + return isFolder ? PosixFilePermissions.fromString("rwxr-xr-x") : PosixFilePermissions.fromString("rw-r--r--");
- + }
- }
- diff --git a/src/main/java/com/github/fge/filesystem/box/driver/BoxFileSystemDriver.java b/src/main/java/com/github/fge/filesystem/box/driver/BoxFileSystemDriver.java
- index e4261f4..3637ce9 100644
- --- a/src/main/java/com/github/fge/filesystem/box/driver/BoxFileSystemDriver.java
- +++ b/src/main/java/com/github/fge/filesystem/box/driver/BoxFileSystemDriver.java
- @@ -1,21 +1,14 @@
- package com.github.fge.filesystem.box.driver;
- -import com.box.sdk.BoxAPIException;
- -import com.box.sdk.BoxFile;
- -import com.box.sdk.BoxFolder;
- -import com.box.sdk.BoxItem;
- -import com.github.fge.filesystem.box.exceptions.BoxIOException;
- -import com.github.fge.filesystem.box.io.BoxFileInputStream;
- -import com.github.fge.filesystem.box.io.BoxFileOutputStream;
- -import com.github.fge.filesystem.driver.UnixLikeFileSystemDriverBase;
- -import com.github.fge.filesystem.exceptions.IsDirectoryException;
- -import com.github.fge.filesystem.provider.FileSystemFactoryProvider;
- -
- -import javax.annotation.Nonnull;
- -import javax.annotation.ParametersAreNonnullByDefault;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- +import java.nio.ByteBuffer;
- +import java.nio.channels.Channels;
- +import java.nio.channels.NonWritableChannelException;
- +import java.nio.channels.ReadableByteChannel;
- +import java.nio.channels.SeekableByteChannel;
- +import java.nio.channels.WritableByteChannel;
- import java.nio.file.AccessDeniedException;
- import java.nio.file.AccessMode;
- import java.nio.file.CopyOption;
- @@ -26,18 +19,35 @@ import java.nio.file.FileStore;
- import java.nio.file.NoSuchFileException;
- import java.nio.file.OpenOption;
- import java.nio.file.Path;
- +import java.nio.file.StandardOpenOption;
- import java.nio.file.attribute.FileAttribute;
- import java.nio.file.spi.FileSystemProvider;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.EnumSet;
- +import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- +import java.util.Map;
- import java.util.Objects;
- import java.util.Set;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- +import javax.annotation.Nonnull;
- +import javax.annotation.ParametersAreNonnullByDefault;
- +
- +import com.box.sdk.BoxAPIException;
- +import com.box.sdk.BoxFile;
- +import com.box.sdk.BoxFolder;
- +import com.box.sdk.BoxItem;
- +import com.github.fge.filesystem.box.exceptions.BoxIOException;
- +import com.github.fge.filesystem.box.io.BoxFileInputStream;
- +import com.github.fge.filesystem.box.io.BoxFileOutputStream;
- +import com.github.fge.filesystem.driver.UnixLikeFileSystemDriverBase;
- +import com.github.fge.filesystem.exceptions.IsDirectoryException;
- +import com.github.fge.filesystem.provider.FileSystemFactoryProvider;
- +
- /**
- * Box filesystem driver
- *
- @@ -59,10 +69,26 @@ public final class BoxFileSystemDriver
- this.wrapper = Objects.requireNonNull(wrapper);
- }
- + /** */
- + private Map<String, BoxItem> cache = new HashMap<>();
- +
- + private BoxItem getItem(final Path path) throws BoxIOException {
- + String pathString = path.toAbsolutePath().toString();
- + if (cache.containsKey(pathString)) {
- + return cache.get(pathString);
- + } else {
- + BoxItem item = wrapper.getItem(path);
- + if (item != null) {
- + cache.put(pathString, item);
- + }
- + return item;
- + }
- + }
- +
- @Nonnull
- @Override
- public InputStream newInputStream(final Path path,
- - final Set<OpenOption> options)
- + final Set<? extends OpenOption> options)
- throws IOException
- {
- final Path realPath = path.toAbsolutePath();
- @@ -76,14 +102,14 @@ public final class BoxFileSystemDriver
- @Nonnull
- @Override
- public OutputStream newOutputStream(final Path path,
- - final Set<OpenOption> options)
- + final Set<? extends OpenOption> options)
- throws IOException
- {
- final Path realPath = path.toAbsolutePath();
- final OutputStream ret;
- final String target = realPath.toString();
- - final BoxItem item = wrapper.getItem(realPath);
- + final BoxItem item = getItem(realPath);
- final boolean create = item == null;
- if (create) {
- @@ -101,6 +127,8 @@ public final class BoxFileSystemDriver
- return ret;
- }
- + private Map<String, List<Path>> folderCache = new HashMap<>();
- +
- @Nonnull
- @Override
- public DirectoryStream<Path> newDirectoryStream(final Path dir,
- @@ -118,21 +146,30 @@ public final class BoxFileSystemDriver
- * to throw that from within an Iterator, we therefore swallow
- * everything :/
- */
- - final List<Path> list = new ArrayList<>();
- - try {
- - for (final BoxItem.Info info : folder.getChildren("name"))
- - list.add(dir.resolve(info.getName()));
- - } catch (BoxAPIException e) {
- - throw BoxIOException.wrap(e);
- + List<Path> list = null;
- + if (folderCache.containsKey(realPath.toString())) {
- + list = folderCache.get(realPath.toString());
- + } else {
- + list = new ArrayList<>();
- + try {
- + for (final BoxItem.Info info : folder.getChildren("name")) {
- + list.add(dir.resolve(info.getName()));
- + }
- + } catch (BoxAPIException e) {
- + throw BoxIOException.wrap(e);
- + }
- + folderCache.put(realPath.toString(), list);
- }
- + final List<Path> list2 = list;
- +
- //noinspection AnonymousInnerClassWithTooManyMethods
- return new DirectoryStream<Path>()
- {
- @Override
- public Iterator<Path> iterator()
- {
- - return list.iterator();
- + return list2.iterator();
- }
- @Override
- @@ -144,11 +181,110 @@ public final class BoxFileSystemDriver
- }
- @Override
- + public SeekableByteChannel newByteChannel(Path path,
- + Set<? extends OpenOption> options,
- + FileAttribute<?>... attrs) throws IOException {
- + if (options.contains(StandardOpenOption.WRITE) || options.contains(StandardOpenOption.APPEND)) {
- + final WritableByteChannel wbc = Channels.newChannel(newOutputStream(path, options));
- + long leftover = 0;
- + if (options.contains(StandardOpenOption.APPEND)) {
- + BoxItem metadata = getItem(path);
- + if (metadata != null && asFile(metadata).getInfo().getSize() >= 0)
- + leftover = asFile(metadata).getInfo().getSize();
- + }
- + final long offset = leftover;
- + return new SeekableByteChannel() {
- + long written = offset;
- +
- + public boolean isOpen() {
- + return wbc.isOpen();
- + }
- +
- + public long position() throws IOException {
- + return written;
- + }
- +
- + public SeekableByteChannel position(long pos) throws IOException {
- + throw new UnsupportedOperationException();
- + }
- +
- + public int read(ByteBuffer dst) throws IOException {
- + throw new UnsupportedOperationException();
- + }
- +
- + public SeekableByteChannel truncate(long size) throws IOException {
- + throw new UnsupportedOperationException();
- + }
- +
- + public int write(ByteBuffer src) throws IOException {
- + int n = wbc.write(src);
- + written += n;
- + return n;
- + }
- +
- + public long size() throws IOException {
- + return written;
- + }
- +
- + public void close() throws IOException {
- + wbc.close();
- + }
- + };
- + } else {
- + BoxItem metadata = getItem(path);
- + if (isDirectory(metadata))
- + throw new NoSuchFileException(path.toString());
- + final ReadableByteChannel rbc = Channels.newChannel(newInputStream(path, null));
- + final long size = asFile(metadata).getInfo().getSize();
- + return new SeekableByteChannel() {
- + long read = 0;
- +
- + public boolean isOpen() {
- + return rbc.isOpen();
- + }
- +
- + public long position() throws IOException {
- + return read;
- + }
- +
- + public SeekableByteChannel position(long pos) throws IOException {
- + read = pos;
- + return this;
- + }
- +
- + public int read(ByteBuffer dst) throws IOException {
- + int n = rbc.read(dst);
- + if (n > 0) {
- + read += n;
- + }
- + return n;
- + }
- +
- + public SeekableByteChannel truncate(long size) throws IOException {
- + throw new NonWritableChannelException();
- + }
- +
- + public int write(ByteBuffer src) throws IOException {
- + throw new NonWritableChannelException();
- + }
- +
- + public long size() throws IOException {
- + return size;
- + }
- +
- + public void close() throws IOException {
- + rbc.close();
- + }
- + };
- + }
- + }
- +
- + @Override
- public void createDirectory(final Path dir, final FileAttribute<?>... attrs)
- throws IOException
- {
- final Path realPath = dir.toAbsolutePath();
- - final BoxItem item = wrapper.getItem(realPath);
- + final BoxItem item = getItem(realPath);
- //noinspection VariableNotUsedInsideIf
- if (item != null)
- @@ -183,7 +319,7 @@ public final class BoxFileSystemDriver
- */
- final Path srcPath = source.toAbsolutePath();
- final String src = srcPath.toString();
- - final BoxItem srcItem = wrapper.getItem(srcPath);
- + final BoxItem srcItem = getItem(srcPath);
- // TODO! metadata driver, yes, again
- @SuppressWarnings("ConstantConditions")
- @@ -199,7 +335,7 @@ public final class BoxFileSystemDriver
- */
- final Path dstPath = target.toAbsolutePath();
- final String dst = dstPath.toString();
- - final BoxItem dstItem = wrapper.getItem(dstPath);
- + final BoxItem dstItem = getItem(dstPath);
- //noinspection VariableNotUsedInsideIf
- if (dstItem != null)
- @@ -243,7 +379,7 @@ public final class BoxFileSystemDriver
- */
- final Path srcPath = source.toAbsolutePath();
- final String src = srcPath.toString();
- - final BoxItem srcItem = wrapper.getItem(srcPath);
- + final BoxItem srcItem = getItem(srcPath);
- // TODO: within a driver, atomic move of non empty directories are OK
- @SuppressWarnings("ConstantConditions")
- @@ -258,7 +394,7 @@ public final class BoxFileSystemDriver
- * replace it, check that it is either a file or a non empty directory.
- */
- final Path dstPath = target.toAbsolutePath();
- - final BoxItem dstItem = wrapper.getItem(dstPath);
- + final BoxItem dstItem = getItem(dstPath);
- //noinspection VariableNotUsedInsideIf
- if (dstItem != null)
- @@ -286,7 +422,7 @@ public final class BoxFileSystemDriver
- {
- final Path realPath = path.toAbsolutePath();
- final String s = realPath.toString();
- - final BoxItem item = wrapper.getItem(realPath);
- + final BoxItem item = getItem(realPath);
- if (item == null)
- throw new NoSuchFileException(s);
- @@ -306,7 +442,7 @@ public final class BoxFileSystemDriver
- {
- // TODO: when symlinks are supported this may turn out to be wrong
- final Path target = path.toAbsolutePath();
- - final BoxItem item = wrapper.getItem(target);
- + final BoxItem item = getItem(target);
- if (item == null)
- throw new NoSuchFileException(target.toString());
- return item;
- diff --git a/src/main/java/com/github/fge/filesystem/box/opts/BoxFileSystemOptionsFactory.java b/src/main/java/com/github/fge/filesystem/box/opts/BoxFileSystemOptionsFactory.java
- index e69de29..f51af8d 100644
- --- a/src/main/java/com/github/fge/filesystem/box/opts/BoxFileSystemOptionsFactory.java
- +++ b/src/main/java/com/github/fge/filesystem/box/opts/BoxFileSystemOptionsFactory.java
- @@ -0,0 +1,27 @@
- +/*
- + * Copyright (c) 2016 by Naohide Sano, All rights reserved.
- + *
- + * Programmed by Naohide Sano
- + */
- +
- +package com.github.fge.filesystem.box.opts;
- +
- +import java.nio.file.LinkOption;
- +
- +import com.github.fge.filesystem.options.FileSystemOptionsFactory;
- +
- +
- +/**
- + * BoxFileSystemOptionsFactory.
- + *
- + * @author <a href="mailto:umjammer@gmail.com">Naohide Sano</a> (umjammer)
- + * @version 0.00 2016/03/11 umjammer initial version <br>
- + */
- +public class BoxFileSystemOptionsFactory extends FileSystemOptionsFactory {
- +
- + public BoxFileSystemOptionsFactory() {
- + addLinkOption(LinkOption.NOFOLLOW_LINKS);
- + }
- +}
- +
- +/* */
- diff --git a/src/main/java/com/github/fge/filesystem/box/provider/BoxFileSystemFactoryProvider.java b/src/main/java/com/github/fge/filesystem/box/provider/BoxFileSystemFactoryProvider.java
- index 3abb90b..8c9201e 100644
- --- a/src/main/java/com/github/fge/filesystem/box/provider/BoxFileSystemFactoryProvider.java
- +++ b/src/main/java/com/github/fge/filesystem/box/provider/BoxFileSystemFactoryProvider.java
- @@ -1,6 +1,7 @@
- package com.github.fge.filesystem.box.provider;
- import com.github.fge.filesystem.box.attributes.BoxFileAttributesFactory;
- +import com.github.fge.filesystem.box.opts.BoxFileSystemOptionsFactory;
- import com.github.fge.filesystem.provider.FileSystemFactoryProvider;
- public final class BoxFileSystemFactoryProvider
- @@ -9,5 +10,6 @@ public final class BoxFileSystemFactoryProvider
- public BoxFileSystemFactoryProvider()
- {
- setAttributesFactory(new BoxFileAttributesFactory());
- + setOptionsFactory(new BoxFileSystemOptionsFactory());
- }
- }
- diff --git a/src/main/java/com/github/fge/filesystem/box/provider/BoxFileSystemRepository.java b/src/main/java/com/github/fge/filesystem/box/provider/BoxFileSystemRepository.java
- index 2981c88..776e864 100644
- --- a/src/main/java/com/github/fge/filesystem/box/provider/BoxFileSystemRepository.java
- +++ b/src/main/java/com/github/fge/filesystem/box/provider/BoxFileSystemRepository.java
- @@ -1,6 +1,15 @@
- package com.github.fge.filesystem.box.provider;
- +import java.io.IOException;
- +import java.net.URI;
- +import java.nio.file.FileStore;
- +import java.util.Map;
- +
- +import javax.annotation.Nonnull;
- +import javax.annotation.ParametersAreNonnullByDefault;
- +
- import com.box.sdk.BoxAPIConnection;
- +import com.box.sdk.BoxAPIConnectionListener;
- import com.box.sdk.BoxAPIException;
- import com.box.sdk.BoxFolder;
- import com.github.fge.filesystem.box.driver.BoxAPIWrapper;
- @@ -11,13 +20,6 @@ import com.github.fge.filesystem.box.filestore.BoxFileStore;
- import com.github.fge.filesystem.driver.FileSystemDriver;
- import com.github.fge.filesystem.provider.FileSystemRepositoryBase;
- -import javax.annotation.Nonnull;
- -import javax.annotation.ParametersAreNonnullByDefault;
- -import java.io.IOException;
- -import java.net.URI;
- -import java.nio.file.FileStore;
- -import java.util.Map;
- -
- @ParametersAreNonnullByDefault
- public final class BoxFileSystemRepository
- extends FileSystemRepositoryBase
- @@ -41,6 +43,16 @@ public final class BoxFileSystemRepository
- throw new IllegalArgumentException("access token not found");
- final BoxAPIConnection api = new BoxAPIConnection(accessToken);
- + api.addListener(new BoxAPIConnectionListener() {
- + @Override
- + public void onRefresh(BoxAPIConnection api) {
- + System.out.println("refresh tocken" + api.getRefreshToken());
- + }
- + @Override
- + public void onError(BoxAPIConnection api, BoxAPIException error) {
- + error.printStackTrace();
- + }
- + });
- final BoxAPIWrapper wrapper = new DefaultBoxAPIWrapper(api);
- final FileStore store;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement