下面列出了怎么用java.nio.file.ProviderMismatchException的API类实例代码及写法,或者点击链接到github查看源代码。
private BuckUnixPath toUnixPath(Path obj) {
if (obj == null) {
throw new NullPointerException();
}
if (obj instanceof BuckUnixPath) {
return (BuckUnixPath) obj;
}
FileSystem otherFs = obj.getFileSystem();
if (!fs.equals(otherFs) && !fs.getDefaultFileSystem().equals(otherFs)) {
throw new ProviderMismatchException(
"Unable to convert Path to BuckUnixPath because file systems do not match");
}
return BuckUnixPath.of(fs, obj.toString());
}
@Override
public void move(Path srcPath, SecureDirectoryStream<Path> targetDir, Path targetPath)
throws IOException {
checkOpen();
JimfsPath checkedSrcPath = checkPath(srcPath);
JimfsPath checkedTargetPath = checkPath(targetPath);
if (!(targetDir instanceof JimfsSecureDirectoryStream)) {
throw new ProviderMismatchException(
"targetDir isn't a secure directory stream associated with this file system");
}
JimfsSecureDirectoryStream checkedTargetDir = (JimfsSecureDirectoryStream) targetDir;
view.copy(
checkedSrcPath,
checkedTargetDir.view,
checkedTargetPath,
ImmutableSet.<CopyOption>of(),
true);
}
@Override
public JimfsPath resolve(Path other) {
JimfsPath otherPath = checkPath(other);
if (otherPath == null) {
throw new ProviderMismatchException(other.toString());
}
if (isEmptyPath() || otherPath.isAbsolute()) {
return otherPath;
}
if (otherPath.isEmptyPath()) {
return this;
}
return pathService.createPath(
root, ImmutableList.<Name>builder().addAll(names).addAll(otherPath.names).build());
}
@Override
public JimfsPath resolveSibling(Path other) {
JimfsPath otherPath = checkPath(other);
if (otherPath == null) {
throw new ProviderMismatchException(other.toString());
}
if (otherPath.isAbsolute()) {
return otherPath;
}
JimfsPath parent = getParent();
if (parent == null) {
return otherPath;
}
return parent.resolve(other);
}
@Override
public WatchKey register(
WatchService watcher, WatchEvent.Kind<?>[] events, WatchEvent.Modifier... modifiers)
throws IOException {
if (watcher == null) {
throw new NullPointerException();
}
if (!(watcher instanceof AbstractWatchService)) {
throw new ProviderMismatchException();
}
return ((AbstractWatchService) watcher).register(this, Arrays.asList(events));
}
@Override
public Iterable<FileBlockLocation> getFileBlockLocations(FileAttributes file, long start, long len) throws IOException {
if (!(file instanceof FileStatusWrapper)) {
throw new ProviderMismatchException();
}
final FileStatus status = ((FileStatusWrapper) file).getFileStatus();
try (WaitRecorder recorder = OperatorStats.getWaitRecorder(operatorStats)) {
return toFileBlockLocations(() -> underlyingFs.getFileBlockLocations(status, start, len));
} catch (FSError e) {
throw propagateFSError(e);
}
}
@Override
public Iterable<FileBlockLocation> getFileBlockLocations(FileAttributes file, long start, long len) throws IOException {
if (!(file instanceof HadoopFileStatusWrapper)) {
throw new ProviderMismatchException();
}
final FileStatus status = ((HadoopFileStatusWrapper) file).getFileStatus();
try (WaitRecorder recorder = OperatorStats.getWaitRecorder(operatorStats)) {
return toFileBlockLocations(() -> underlyingFs.getFileBlockLocations(status, start, len));
} catch(FSError e) {
throw propagateFSError(e);
}
}
@Override
public Iterable<FileBlockLocation> getFileBlockLocations(FileAttributes file, long start, long len) throws IOException {
if (!(file instanceof HadoopFileStatusWrapper)) {
throw new ProviderMismatchException();
}
final FileStatus status = ((HadoopFileStatusWrapper) file).getFileStatus();
try (WaitRecorder recorder = OperatorStats.getWaitRecorder(operatorStats)) {
return toFileBlockLocations(() -> underlyingFs.getFileBlockLocations(status, start, len));
} catch(FSError e) {
throw propagateFSError(e);
}
}
public static MCRPath toMCRPath(final Path other) {
if (other == null) {
throw new NullPointerException();
}
if (!(other instanceof MCRPath)) {
throw new ProviderMismatchException("other is not an instance of MCRPath: " + other.getClass());
}
return (MCRPath) other;
}
static MCRPath checkPathAbsolute(Path path) {
MCRPath mcrPath = MCRPath.toMCRPath(path);
if (!(Objects.requireNonNull(mcrPath.getFileSystem(), "'path' requires a associated filesystem.")
.provider() instanceof MCRFileSystemProvider)) {
throw new ProviderMismatchException("Path does not match to this provider: " + path);
}
if (!mcrPath.isAbsolute()) {
throw new InvalidPathException(mcrPath.toString(), "'path' must be absolute.");
}
return mcrPath;
}
static MCRPath checkPathAbsolute(Path path) {
MCRPath mcrPath = MCRPath.toMCRPath(path);
if (!(Objects.requireNonNull(mcrPath.getFileSystem(), "'path' requires a associated filesystem.")
.provider() instanceof MCRFileSystemProvider)) {
throw new ProviderMismatchException("Path does not match to this provider: " + path);
}
if (!mcrPath.isAbsolute()) {
throw new InvalidPathException(mcrPath.toString(), "'path' must be absolute.");
}
return mcrPath;
}
/** Override this to customize the unboxing of Path
* from various operations
*/
protected Path toDelegate(Path path) {
if (path instanceof FilterPath) {
FilterPath fp = (FilterPath) path;
if (fp.fileSystem != fileSystem) {
throw new ProviderMismatchException("mismatch, expected: " + fileSystem.provider().getClass() + ", got: " + fp.fileSystem.provider().getClass());
}
return fp.delegate;
} else {
throw new ProviderMismatchException("mismatch, expected: FilterPath, got: " + path.getClass());
}
}
protected Path toDelegate(Path path) {
if (path instanceof FilterPath) {
FilterPath fp = (FilterPath) path;
if (fp.fileSystem != fileSystem) {
throw new ProviderMismatchException("mismatch, expected: " + fileSystem.provider().getClass() + ", got: " + fp.fileSystem.provider().getClass());
}
return fp.delegate;
} else {
throw new ProviderMismatchException("mismatch, expected: FilterPath, got: " + path.getClass());
}
}
private static SFTPPath toSFTPPath(Path path) {
Objects.requireNonNull(path);
if (path instanceof SFTPPath) {
return (SFTPPath) path;
}
throw new ProviderMismatchException();
}
/**
* Send a keep-alive signal for an SFTP file system.
*
* @param fs The SFTP file system to send a keep-alive signal for.
* @throws ProviderMismatchException If the given file system is not an SFTP file system (not created by an {@code SFTPFileSystemProvider}).
* @throws IOException If an I/O error occurred.
*/
public static void keepAlive(FileSystem fs) throws IOException {
if (fs instanceof SFTPFileSystem) {
((SFTPFileSystem) fs).keepAlive();
return;
}
throw new ProviderMismatchException();
}
private Path requireValidPath(Path path) {
Objects.requireNonNull(path);
Objects.requireNonNull(fileSystem); // to allow this method to be used in constructor.
if (!path.getFileSystem().provider().equals(fileSystem.provider())) {
throw new ProviderMismatchException("Expected file system provider " + fileSystem.provider() +
" but " + path + " had " + path.getFileSystem().provider());
}
return path;
}
@Nonnull
public static String getFile(URI uri) throws ProviderMismatchException {
checkScheme(uri);
String fragment = uri.getFragment();
if(fragment == null)
fragment = "";
if(!fragment.startsWith("/"))
fragment = "/" + fragment;
if(fragment.length() > 1 && fragment.endsWith("/"))
fragment = fragment.substring(0, fragment.length() - 1);
return fragment;
}
static Path dismantle(Path mantle) {
if (mantle == null)
throw new NullPointerException();
if (!(mantle instanceof EncryptedFileSystemPath))
throw new ProviderMismatchException();
return ((EncryptedFileSystemPath) mantle).subFSPath;
}
@Override
public WatchKey register(WatchService watcher,
WatchEvent.Kind<?>[] events,
WatchEvent.Modifier... modifiers)
throws IOException {
if (watcher == null)
throw new NullPointerException();
if (!(watcher instanceof AbstractWatchService))
throw new ProviderMismatchException();
return ((AbstractWatchService) watcher).register(this, events, modifiers);
}
@Nonnull
public static String getFile(URI uri) throws ProviderMismatchException {
checkScheme(uri);
String fragment = uri.getFragment();
if(fragment == null)
fragment = "";
if(!fragment.startsWith("/"))
fragment = "/" + fragment;
if(fragment.length() > 1 && fragment.endsWith("/"))
fragment = fragment.substring(0, fragment.length() - 1);
return fragment;
}
static Path dismantle(Path mantle) {
if (mantle == null)
throw new NullPointerException();
if (!(mantle instanceof EncryptedFileSystemPath))
throw new ProviderMismatchException();
return ((EncryptedFileSystemPath) mantle).subFSPath;
}
private HadoopPath checkPath(Path pathToCheck) {
if (pathToCheck == null) {
throw new NullPointerException();
}
if (!(pathToCheck instanceof HadoopPath)) {
throw new ProviderMismatchException();
}
return (HadoopPath) pathToCheck;
}
private static final HadoopPath toHadoopPath(Path path) {
if (path == null) {
throw new NullPointerException();
}
if (!(path instanceof HadoopPath)) {
throw new ProviderMismatchException();
}
return (HadoopPath) path;
}
/** {@inheritDoc} */
@Override
public WatchKey register( WatchService watcher, Kind<?>[] events, Modifier... modifiers ) throws IOException {
if ( watcher == null ) {
throw new NullPointerException();
}
if ( !(watcher instanceof UnixSshFileSystemWatchService) ) {
throw new ProviderMismatchException();
}
if ( !getFileSystem().provider().readAttributes( this, BasicFileAttributes.class ).isDirectory() ) {
throw new NotDirectoryException( this.toString() );
}
getFileSystem().provider().checkAccess( this, AccessMode.READ );
return ((UnixSshFileSystemWatchService) watcher).register( this, events, modifiers );
}
private static JimfsPath checkPath(Path path) {
if (path instanceof JimfsPath) {
return (JimfsPath) path;
}
throw new ProviderMismatchException(
"path " + path + " is not associated with a Jimfs file system");
}
private static JimfsPath checkPath(Path path) {
if (path instanceof JimfsPath) {
return (JimfsPath) path;
}
throw new ProviderMismatchException(
"path " + path + " is not associated with a Jimfs file system");
}
@Test
public void testKeepAliveWithNonFTPFileSystem() {
@SuppressWarnings("resource")
FileSystem defaultFileSystem = FileSystems.getDefault();
assertThrows(ProviderMismatchException.class, () -> SFTPFileSystemProvider.keepAlive(defaultFileSystem));
}
@Test
public void testKeepAliveWithNullFTPFileSystem() {
assertThrows(ProviderMismatchException.class, () -> SFTPFileSystemProvider.keepAlive(null));
}
static void checkScheme(URI uri) throws ProviderMismatchException {
if(!GitFileSystemProvider.GFS.equalsIgnoreCase(uri.getScheme()))
throw new ProviderMismatchException(uri.getScheme());
}
@Nullable
public static String getSession(URI uri) throws ProviderMismatchException {
checkScheme(uri);
return parseQuery(uri.getQuery(), Collections.singleton(SID_KEY)).get(SID_KEY);
}