Here are the examples of the java api com.llamalab.safs.Path taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
98 Examples
19
Source : UnixPath.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
private static void checkPath(Path path) {
if (!(path instanceof UnixPath))
throw (path == null) ? new NullPointerException() : new ProviderMismatchException();
}
19
Source : UnixPath.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@SuppressWarnings("NullableProblems")
@Override
public int compareTo(Path other) {
return path.compareTo(other.toString());
}
19
Source : UnixPath.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
public Path resolveSibling(String other) {
final Path parent = getParent();
return (parent != null) ? parent.resolve(other) : fs.getPath(other);
}
19
Source : UnixPath.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
public Path resolveSibling(Path other) {
checkPath(other);
final Path parent = getParent();
return (parent != null) ? parent.resolve(other) : other;
}
19
Source : UnixPath.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
public final boolean endsWith(Path other) {
return fs.equals(other.getFileSystem()) && endsWithSanitized(((UnixPath) other).path);
}
19
Source : UnixPath.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
// clreplaced RelativizeHelper
@Override
public Path resolve(Path other) {
checkPath(other);
if (other.isAbsolute())
return other;
if (((UnixPath) other).isEmpty())
return this;
return fs.getPath(path, ((UnixPath) other).path);
}
19
Source : UnixPath.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public final boolean startsWith(Path other) {
return fs.equals(other.getFileSystem()) && startsWithSanitized(((UnixPath) other).path);
}
19
Source : FileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
public boolean deleteIfExists(Path path) throws IOException {
try {
delete(path);
return true;
} catch (NoSuchFileException e) {
return false;
}
}
19
Source : FileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
/*
public void createSymbolicLink (Path link, Path target) throws IOException {
throw new UnsupportedOperationException();
}
*/
public Path readSymbolicLink(Path link) throws IOException {
throw new UnsupportedOperationException();
}
19
Source : JavaFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public boolean isSameFile(Path path1, Path path2) throws IOException {
if (path1.equals(path2))
return true;
return getPathType().isInstance(path1) && getPathType().isInstance(path2) && path1.getFileSystem().equals(path2.getFileSystem()) && isSameFile(path1.toFile(), path2.toFile());
}
19
Source : JavaFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public void delete(Path path) throws IOException {
checkPath(path);
delete(path.toFile(), false);
}
19
Source : JavaFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public InputStream newInputStream(Path path, OpenOption... options) throws IOException {
checkPath(path);
return newInputStream(path.toFile(), (options.length == 0) ? DEFAULT_NEW_INPUT_STREAM_OPTIONS : new SearchSet<OpenOption>(options));
}
19
Source : JavaFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
/**
* May be overridden with a faster implementation.
*/
protected boolean isSymbolicLink(Path path) {
return isSymbolicLink(path.toFile());
}
19
Source : JavaFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public DirectoryStream<Path> newDirectoryStream(final Path dir, final DirectoryStream.Filter<? super Path> filter) throws IOException {
checkPath(dir);
if (filter == null)
throw new NullPointerException("filter");
try {
final File dirFile = dir.toFile();
final String[] files = dirFile.list();
if (files == null) {
if (dirFile.exists() && !dirFile.canRead())
throw new AccessDeniedException(dir.toString());
throw new NotDirectoryException(dir.toString());
}
return new AbstractDirectoryStream<Path>() {
private int index;
@Override
protected Path advance() throws IOException {
while (index < files.length) {
final Path entry = dir.resolve(files[index++]);
if (filter.accept(entry))
return entry;
}
return null;
}
};
} catch (IOException e) {
throw toProperException(e, dir.toString(), null);
}
}
19
Source : JavaFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException {
checkPath(dir);
createDirectory(dir.toFile(), attrs);
}
19
Source : JavaFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public boolean isHidden(Path path) throws IOException {
checkPath(path);
return ((UnixPath) path).isHidden();
}
19
Source : JavaFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public FileStore getFileStore(Path path) throws IOException {
// TODO: possible?
throw new UnsupportedOperationException();
}
19
Source : JavaFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
checkPath(source);
checkPath(target);
transfer(source, target, true, new SearchSet<CopyOption>(options));
}
19
Source : JavaFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
/*
private RandomAccessFile newRandomAccessFile (File file, Set<? extends OpenOption> options) throws IOException {
try {
if (!options.contains(StandardOpenOption.WRITE))
return new RandomAccessFile(file, toModeString(options));
final RandomAccessFile raf = new RandomAccessFile(file, toModeString(options));
if (options.contains(StandardOpenOption.TRUNCATE_EXISTING)) {
try {
raf.setLength(0);
}
catch (IOException e) {
Utils.closeQuietly(raf);
throw e;
}
}
return raf;
}
catch (IOException e) {
throw toProperException(e, file.toString(), null);
}
}
*/
/*
@Override
public void createSymbolicLink (Path link, Path target) throws IOException {
checkPaths(link, target);
createSymbolicLink(link.toFile(), target.toFile());
}
protected void createSymbolicLink (File link, File target) throws IOException {
final Process process = Runtime.getRuntime().exec(new String[] { "ln", "-s", target.toString(), link.toString() });
try {
if (0 != process.waitFor()) {
if (target.exists())
throw new FileAlreadyExistsException(target.toString());
else
throw new IOException("ln command failure");
}
}
catch (InterruptedException e) {
throw (IOException)new InterruptedIOException().initCause(e);
}
finally {
process.destroy();
}
}
*/
@Override
public Path readSymbolicLink(Path link) throws IOException {
final Path parent = link.getParent();
if (parent != null)
link = parent.toRealPath().resolve(link.getFileName());
final Path real = link.toRealPath();
if (real.equals(link.toAbsolutePath()))
throw new NotLinkException(link.toString());
return real;
}
19
Source : JavaFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public OutputStream newOutputStream(Path path, OpenOption... options) throws IOException {
checkPath(path);
return newOutputStream(path.toFile(), (options.length == 0) ? DEFAULT_NEW_OUTPUT_STREAM_OPTIONS : new SearchSet<OpenOption>(options));
}
19
Source : JavaFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
checkPath(path);
return newByteChannel(path.toFile(), options, attrs);
}
19
Source : JavaFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public void copy(Path source, Path target, CopyOption... options) throws IOException {
checkPath(source);
checkPath(target);
transfer(source, target, false, new SearchSet<CopyOption>(options));
}
19
Source : JavaFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
protected void setAttributes(Path path, Set<? extends FileAttribute<?>> attrs, LinkOption... options) throws IOException {
checkPath(path);
for (final FileAttribute<?> attr : attrs) {
if (attr instanceof BasicFileAttributeValue) {
switch(((BasicFileAttributeValue) attr).type()) {
case lastModifiedTime:
setLastModifiedTime(path.toFile(), (FileTime) attr.value());
continue;
}
}
throw new UnsupportedOperationException("Attribute: " + attr.name());
}
}
19
Source : JavaFileSystem.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
/**
* Only support {@link com.llamalab.safs.unix.UnixPath}.
*/
public clreplaced JavaFileSystem extends AbstractUnixFileSystem implements DefaultFileSystem {
protected volatile Path cacheDirectory;
protected volatile Path currentDirectory;
public JavaFileSystem(FileSystemProvider provider) {
super(provider);
}
@Override
public final void close() throws IOException {
throw new UnsupportedOperationException();
}
@Override
public final boolean isOpen() {
return true;
}
@Override
public boolean isReadOnly() {
return false;
}
@Override
public Path getCacheDirectory() {
if (cacheDirectory == null)
cacheDirectory = getPathSanitized(System.getProperty("java.io.tmpdir"));
return cacheDirectory;
}
public final Path getCurrentDirectory() {
if (currentDirectory == null)
currentDirectory = getPathSanitized(System.getProperty("user.dir"));
return currentDirectory;
}
@Override
protected Path toRealPath(Path path, LinkOption... options) throws IOException {
final File file = path.toFile();
if (!file.exists())
throw new NoSuchFileException(path.toString());
for (final LinkOption option : options) {
if (LinkOption.NOFOLLOW_LINKS == option)
return path.toAbsolutePath().normalize();
}
return getPathSanitized(file.getCanonicalPath());
}
}
19
Source : DefaultJavaFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public FileSystem newFileSystem(Path path, Map<String, ?> env) throws IOException {
checkPath(path);
throw new FileSystemAlreadyExistsException();
}
19
Source : Utils.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
public static String getFileExtension(Path path) {
final Path fileName = path.getFileName();
if (fileName != null) {
final String name = fileName.toString();
final int i = name.lastIndexOf('.');
if (i > 0 && i < name.length() - 1)
return name.substring(i + 1).toLowerCase(Locale.US);
}
return null;
}
19
Source : AbstractFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
protected void checkPath(Path path) {
if (!getPathType().isInstance(path))
throw (path == null) ? new NullPointerException() : new ProviderMismatchException();
if (path.getFileSystem().provider() != this)
throw new ProviderMismatchException();
}
19
Source : PrimaryFileStore.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
final clreplaced PrimaryFileStore extends AndroidFileStore {
private final Path path;
public PrimaryFileStore(Path path) {
this.path = path;
}
@Override
public String name() {
return PRIMARY_NAME;
}
@Override
public Path path() {
return path;
}
@Override
public boolean isPrimary() {
return true;
}
@Override
public boolean isEmulated() {
return Environment.isExternalStorageEmulated();
}
@Override
public boolean isRemovable() {
return Environment.isExternalStorageRemovable();
}
@Override
public String state() {
return Environment.getExternalStorageState();
}
}
19
Source : AndroidWatchService.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
private void cancel(AndroidWatchKey key) {
final Path path = (Path) key.watchable();
synchronized (observers) {
final PathObserver observer = observers.get(path);
if (observer != null && !observer.cancel(key))
observers.remove(path);
}
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public Path readSymbolicLink(Path link) throws IOException {
if (Build.VERSION_CODES.LOLLIPOP > Build.VERSION.SDK_INT)
return readSymbolicLink(link);
checkPath(link);
final AndroidFileSystem fs = (AndroidFileSystem) link.getFileSystem();
try {
return fs.getPath(Os.readlink(link.toString()));
} catch (RuntimeException e) {
// BUG: https://code.google.com/p/android/issues/detail?id=209129
throw e;
} catch (Exception e) {
throw toProperException((ErrnoException) e, link.toString(), null);
}
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private String getMimeType(AndroidFileSystem fs, Path path, Uri uri) throws IOException {
final Cursor cursor;
try {
cursor = fs.getContentResolver().query(uri, MIME_TYPE_PROJECTION, null, null, null);
} catch (IllegalArgumentException e) {
// BUG: DoreplacedentProviders throws undoreplacedented exceptions
return null;
}
try {
// noinspection ConstantConditions
if (!cursor.moveToNext())
return null;
final String mimeType = cursor.getString(MIME_TYPE_COLUMN_MIME_TYPE);
return (mimeType != null) ? mimeType : "application/octet-stream";
} finally {
Utils.closeQuietly(cursor);
}
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@SuppressWarnings("unchecked")
@Override
public <A extends BasicFileAttributes> A readAttributes(Path path, Clreplaced<A> type, LinkOption... options) throws IOException {
checkPath(path);
if (BasicFileAttributes.clreplaced != type)
throw new UnsupportedOperationException("Unsupported type: " + type);
if (Build.VERSION_CODES.LOLLIPOP <= Build.VERSION.SDK_INT) {
final AndroidFileSystem fs = (AndroidFileSystem) path.getFileSystem();
final Uri uri = fs.getTreeDoreplacedentUri(fs.toDoreplacedentPath(path));
if (uri != null)
return (A) readBasicFileAttributes(fs, path, uri, options);
try {
for (final LinkOption option : options) {
if (LinkOption.NOFOLLOW_LINKS == option)
return (A) new StatBasicFileAttributes(Os.lstat(path.toString()));
}
return (A) new StatBasicFileAttributes(Os.stat(path.toString()));
} catch (RuntimeException e) {
// BUG: https://code.google.com/p/android/issues/detail?id=209129
throw e;
} catch (Exception e) {
throw toProperException((ErrnoException) e, path.toString(), null);
}
}
return (A) readBasicFileAttributes(path.toFile(), options);
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
/**
* @throws FileStoreNotFoundException if the file isn't found.
*/
@Override
public FileStore getFileStore(Path path) throws IOException {
checkPath(path);
path = path.toRealPath();
for (final FileStore store : path.getFileSystem().getFileStores()) {
if (path.startsWith(((AndroidFileStore) store).path()))
return store;
}
throw new FileStoreNotFoundException(path.toString());
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public FileSystem newFileSystem(Path path, Map<String, ?> env) throws IOException {
synchronized (this) {
if (fileSystem != null)
throw new FileSystemAlreadyExistsException();
fileSystem = new AndroidFileSystem(this);
}
return fileSystem;
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Uri createDoreplacedent(AndroidFileSystem fs, Path path, Uri parentUri, String name, String mimeType) throws IOException {
// if (DEBUG) Log.d(TAG, "createDoreplacedent: path="+path+", parentUri="+parentUri+", name="+name);
Uri uri;
try {
uri = DoreplacedentsContract.createDoreplacedent(fs.getContentResolver(), parentUri, mimeType, name);
} catch (RuntimeException e) {
uri = null;
}
if (uri == null)
throw new FileSystemException(path.toString(), null, "Failed to create doreplacedent");
final Path created = fs.getPath(uri);
if (created != null && !isSameFile(path.toFile(), created.toFile())) {
try {
DoreplacedentsContract.deleteDoreplacedent(fs.getContentResolver(), uri);
} catch (RuntimeException e) {
// BUG: DoreplacedentProviders throws undoreplacedented exceptions
}
throw new FileAlreadyExistsException(path.toString());
}
return uri;
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
private InputStream newInputStream(Path path, Set<? extends OpenOption> options) throws IOException {
if (options.contains(StandardOpenOption.WRITE))
throw new IllegalArgumentException();
return new ParcelFileDescriptor.AutoCloseInputStream(newParcelFileDescriptor(path, options));
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public void copy(Path source, Path target, CopyOption... options) throws IOException {
if (Build.VERSION_CODES.LOLLIPOP <= Build.VERSION.SDK_INT)
transfer(source, target, false, new SearchSet<>(options));
else
super.copy(source, target, options);
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
protected void setAttributes(Path path, Set<? extends FileAttribute<?>> attrs, LinkOption... options) throws IOException {
checkPath(path);
for (final FileAttribute<?> attr : attrs) {
if (attr instanceof BasicFileAttributeValue) {
if (Build.VERSION_CODES.LOLLIPOP <= Build.VERSION.SDK_INT) {
final AndroidFileSystem fs = (AndroidFileSystem) path.getFileSystem();
if (fs.getTreeDoreplacedentUri(fs.toDoreplacedentPath(path)) != null)
throw new UnsupportedOperationException("Doreplacedent attributes are immutable");
// TODO: http://man7.org/linux/man-pages/man3/futimes.3.html
}
switch(((BasicFileAttributeValue) attr).type()) {
case lastModifiedTime:
setLastModifiedTime(path.toFile(), (FileTime) attr.value());
continue;
}
}
throw new UnsupportedOperationException("Attribute: " + attr.name());
}
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
// TODO: symbolic links
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void transfer(Path source, Path target, boolean move, Set<CopyOption> options) throws IOException {
checkPath(source);
checkPath(target);
final AndroidFileSystem fs = (AndroidFileSystem) source.getFileSystem();
final File sourceFile = source.toFile();
final UnixPath sourceDoc = (UnixPath) fs.toDoreplacedentPath(source);
final Uri sourceUri = fs.getTreeDoreplacedentUri(sourceDoc);
final BasicFileAttributes sourceAttrs;
if (sourceUri != null)
sourceAttrs = readBasicFileAttributes(fs, source, sourceUri);
else
sourceAttrs = readBasicFileAttributes(sourceFile);
final UnixPath targetDoc = (UnixPath) fs.toDoreplacedentPath(target);
if (sourceDoc.equals(targetDoc))
return;
final File targetFile = target.toFile();
final String targetName = targetDoc.getFileName().toString();
final Path targetParent = targetDoc.getParent();
if (targetParent == null)
throw new FileSystemException(target.toString(), null, "Target is root");
// atomic
if (move && options.contains(StandardCopyOption.ATOMIC_MOVE)) {
if (sourceUri != null) {
if (targetParent.equals(sourceDoc.getParent()) && renameDoreplacedent(fs, sourceUri, targetName) != null)
return;
} else {
if (sourceFile.renameTo(targetFile))
return;
}
throw new AtomicMoveNotSupportedException(source.toString(), target.toString(), "Rename failed");
}
// delete target
Uri targetUri = fs.getTreeDoreplacedentUri(targetDoc);
if (options.contains(StandardCopyOption.REPLACE_EXISTING)) {
if (targetUri == null)
delete(targetFile, true);
else
deleteDoreplacedent(fs, target, targetUri, true);
} else {
if ((targetUri != null) ? exists(fs, targetUri) : targetFile.exists())
throw new FileAlreadyExistsException(target.toString());
}
// rename
if (move) {
if (sourceUri != null) {
if (targetParent.equals(sourceDoc.getParent()) && renameDoreplacedent(fs, sourceUri, targetName) != null)
return;
} else {
if (sourceFile.renameTo(targetFile))
return;
}
}
// transfer
final Uri targetParentUri = fs.getTreeDoreplacedentUri(targetParent);
if (sourceAttrs.isDirectory()) {
if (move && (sourceUri != null ? exists(fs, AndroidFileSystem.childrenOf(sourceUri)) : isNonEmptyDirectory(sourceFile)))
throw new DirectoryNotEmptyException(source.toString());
if (targetParentUri != null)
targetUri = createDoreplacedent(fs, target, targetParentUri, targetName, DoreplacedentsContract.Doreplacedent.MIME_TYPE_DIR);
else
createDirectory(targetFile);
} else {
if (targetParentUri != null) {
targetUri = createDoreplacedent(fs, target, targetParentUri, targetName, null);
copyDoreplacedent(fs, sourceFile, targetFile, targetUri);
} else
copyFile(sourceFile, targetFile);
}
try {
if (targetUri == null && options.contains(StandardCopyOption.COPY_ATTRIBUTES))
setLastModifiedTime(targetFile, sourceAttrs.lastModifiedTime());
if (move) {
if (sourceUri != null)
deleteDoreplacedent(fs, source, sourceUri, false);
else
delete(sourceFile, false);
}
} catch (IOException | RuntimeException e) {
try {
if (targetUri != null)
deleteDoreplacedent(fs, target, targetUri, true);
else
delete(targetFile, true);
} catch (Throwable t) {
// ignore
}
throw e;
}
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
// TODO: LinkOption.NOFOLLOW_LINKS
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private ParcelFileDescriptor newParcelFileDescriptor(Path path, Set<? extends OpenOption> options) throws IOException {
checkPath(path);
try {
if (options.contains(AndroidOpenOption.NODOreplacedENT)) {
// non-doreplacedent
return openParcelFileDescriptor(path.toFile(), options);
}
final AndroidFileSystem fs = (AndroidFileSystem) path.getFileSystem();
final UnixPath doc = (UnixPath) fs.toDoreplacedentPath(path);
Uri uri = fs.getTreeDoreplacedentUri(doc);
if (uri == null) {
// non-doreplacedent
return openParcelFileDescriptor(path.toFile(), options);
}
// doreplacedent
if (options.contains(StandardOpenOption.WRITE)) {
if (exists(fs, uri)) {
if (options.contains(StandardOpenOption.CREATE_NEW))
throw new FileAlreadyExistsException(path.toString());
} else {
if (!options.contains(StandardOpenOption.CREATE_NEW) && !options.contains(StandardOpenOption.CREATE))
throw new NoSuchFileException(path.toString());
final Uri parentUri = fs.getTreeDoreplacedentUri(doc.getParent());
if (parentUri == null)
return ParcelFileDescriptor.open(path.toFile(), toModeFlags(options));
uri = createDoreplacedent(fs, path, parentUri, doc.getFileName().toString(), null);
}
}
try {
return fs.getContentResolver().openFileDescriptor(uri, toModeString(options));
} catch (RuntimeException e) {
// BUG: DoreplacedentProvider throws undoreplacedented exceptions.
throw new NoSuchFileException(path.toString());
}
} catch (IOException e) {
throw toProperException(e, path.toString(), null);
}
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public DirectoryStream<Path> newDirectoryStream(final Path dir, final DirectoryStream.Filter<? super Path> filter) throws IOException {
if (Build.VERSION_CODES.LOLLIPOP > Build.VERSION.SDK_INT)
return super.newDirectoryStream(dir, filter);
checkPath(dir);
if (filter == null)
throw new NullPointerException("filter");
final AndroidFileSystem fs = (AndroidFileSystem) dir.getFileSystem();
final Uri uri = fs.getTreeDoreplacedentUri(dir);
if (uri == null)
return super.newDirectoryStream(dir, filter);
final Cursor cursor;
try {
cursor = fs.getContentResolver().query(AndroidFileSystem.childrenOf(uri), BASIC_NEW_DIRECTORY_STREAM_PROJECTION, null, null, null);
} catch (RuntimeException e) {
if (DoreplacedentsContract.Doreplacedent.MIME_TYPE_DIR.equals(getMimeType(fs, dir, uri)))
throw new NotDirectoryException(dir.toString());
throw new FileSystemException(dir.toString(), null, "Failed to list directory doreplacedent");
}
return new AbstractDirectoryStream<Path>() {
@Override
protected Path advance() throws IOException {
// noinspection ConstantConditions
while (cursor.moveToNext()) {
final String displayName = cursor.getString(BASIC_NEW_DIRECTORY_STREAM_COLUMN_DISPLAY_NAME);
if (displayName != null && !displayName.isEmpty()) {
final Path entry = dir.resolve(displayName);
if (filter.accept(entry))
return entry;
}
}
return null;
}
@SuppressLint("NewApi")
@Override
protected void implCloseStream() throws IOException {
Utils.closeQuietly(cursor);
}
};
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
public ParcelFileDescriptor newParcelFileDescriptor(Path path, OpenOption... options) throws IOException {
if (Build.VERSION_CODES.LOLLIPOP > Build.VERSION.SDK_INT) {
checkPath(path);
return newParcelFileDescriptor(path.toFile(), new SearchSet<>(options));
}
return newParcelFileDescriptor(path, new SearchSet<>(options));
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
boolean isFileStoreMounted(Path path) throws IOException {
final String state = getFileStoreState(path);
return Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public void move(Path source, Path target, CopyOption... options) throws IOException {
if (Build.VERSION_CODES.LOLLIPOP <= Build.VERSION.SDK_INT)
transfer(source, target, true, new SearchSet<>(options));
else
super.move(source, target, options);
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
private String getFileStoreState(Path path) throws IOException {
checkPath(path);
if (Build.VERSION_CODES.LOLLIPOP <= Build.VERSION.SDK_INT)
return Environment.getExternalStorageState(path.toFile());
if (Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT) {
// noinspection deprecation
return Environment.getStorageState(path.toFile());
}
final AndroidFileSystem fs = (AndroidFileSystem) path.getFileSystem();
try {
return fs.getVolumeState(path);
} catch (Throwable t) {
if (!path.toRealPath().startsWith(fs.getExternalStorageDirectory()))
throw new IllegalArgumentException();
return Environment.getExternalStorageState();
}
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
if (Build.VERSION_CODES.LOLLIPOP > Build.VERSION.SDK_INT)
return super.newByteChannel(path, options, attrs);
return new SeekableByteChannelWrapper(newParcelFileDescriptor(path, options), toModeFlags(options));
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
/**
* Ensured NOT to delete a non-empty directory, which DoreplacedentsContract.deleteDoreplacedent does!
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void deleteDoreplacedent(AndroidFileSystem fs, Path path, Uri uri, boolean ifExists) throws IOException {
final String mimeType = getMimeType(fs, path, uri);
if (DoreplacedentsContract.Doreplacedent.MIME_TYPE_DIR.equals(mimeType) && exists(fs, AndroidFileSystem.childrenOf(uri)))
throw new DirectoryNotEmptyException(path.toString());
boolean success;
try {
success = DoreplacedentsContract.deleteDoreplacedent(fs.getContentResolver(), uri);
} catch (RuntimeException e) {
// BUG: DoreplacedentProviders throws undoreplacedented exceptions
success = false;
}
if (!success) {
if (mimeType != null)
throw new FileSystemException(path.toString(), null, "Failed to delete doreplacedent");
if (!ifExists)
throw new NoSuchFileException(path.toString());
}
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException {
checkPath(dir);
if (Build.VERSION_CODES.LOLLIPOP <= Build.VERSION.SDK_INT) {
final AndroidFileSystem fs = (AndroidFileSystem) dir.getFileSystem();
final UnixPath doc = (UnixPath) fs.toDoreplacedentPath(dir);
final Uri parentUri = fs.getTreeDoreplacedentUri(doc.getParent());
if (parentUri != null) {
createDoreplacedent(fs, dir, parentUri, doc.getFileName().toString(), DoreplacedentsContract.Doreplacedent.MIME_TYPE_DIR);
return;
}
}
createDirectory(dir.toFile(), attrs);
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
boolean isFileStoreEmulated(Path path) throws IOException {
checkPath(path);
path = path.toRealPath();
if (Build.VERSION_CODES.LOLLIPOP <= Build.VERSION.SDK_INT)
return Environment.isExternalStorageEmulated(path.toFile());
final AndroidFileSystem fs = (AndroidFileSystem) path.getFileSystem();
if (!path.toRealPath().startsWith(fs.getExternalStorageDirectory()))
throw new IllegalArgumentException();
return Environment.isExternalStorageEmulated();
}
19
Source : AndroidFileSystemProvider.java
with Apache License 2.0
from henrik-lindqvist
with Apache License 2.0
from henrik-lindqvist
@Override
public OutputStream newOutputStream(Path path, OpenOption... options) throws IOException {
if (Build.VERSION_CODES.LOLLIPOP > Build.VERSION.SDK_INT)
return super.newOutputStream(path, options);
return newOutputStream(path, (options.length == 0) ? DEFAULT_NEW_OUTPUT_STREAM_OPTIONS : new SearchSet<>(options));
}
See More Examples