下面列出了怎么用java.nio.file.NoSuchFileException的API类实例代码及写法,或者点击链接到github查看源代码。
private static ContainerProtos.Result translate(Exception cause) {
if (cause instanceof FileNotFoundException ||
cause instanceof NoSuchFileException) {
return UNABLE_TO_FIND_CHUNK;
}
if (cause instanceof IOException) {
return IO_EXCEPTION;
}
if (cause instanceof NoSuchAlgorithmException) {
return NO_SUCH_ALGORITHM;
}
return CONTAINER_INTERNAL_ERROR;
}
/**
* Get the latest snapshot index blob id. Snapshot index blobs are named index-N, where N is
* the next version number from when the index blob was written. Each individual index-N blob is
* only written once and never overwritten. The highest numbered index-N blob is the latest one
* that contains the current snapshots in the repository.
*
* Package private for testing
*/
long latestIndexBlobId() throws IOException {
try {
// First, try listing all index-N blobs (there should only be two index-N blobs at any given
// time in a repository if cleanup is happening properly) and pick the index-N blob with the
// highest N value - this will be the latest index blob for the repository. Note, we do this
// instead of directly reading the index.latest blob to get the current index-N blob because
// index.latest is not written atomically and is not immutable - on every index-N change,
// we first delete the old index.latest and then write the new one. If the repository is not
// read-only, it is possible that we try deleting the index.latest blob while it is being read
// by some other operation (such as the get snapshots operation). In some file systems, it is
// illegal to delete a file while it is being read elsewhere (e.g. Windows). For read-only
// repositories, we read for index.latest, both because listing blob prefixes is often unsupported
// and because the index.latest blob will never be deleted and re-written.
return listBlobsToGetLatestIndexId();
} catch (UnsupportedOperationException e) {
// If its a read-only repository, listing blobs by prefix may not be supported (e.g. a URL repository),
// in this case, try reading the latest index generation from the index.latest blob
try {
return readSnapshotIndexLatestBlob();
} catch (NoSuchFileException nsfe) {
return RepositoryData.EMPTY_REPO_GEN;
}
}
}
/**
* Handle the modified event. We can ignore the changes in watermark files that
* starts with '.' character.
* @throws IOException
*/
private void handleEntryModifyEvent(Path logDir , Path modified ) throws IOException {
Path fullPath = logDir.resolve(modified);
long inode;
try {
inode = SingerUtils.getFileInode(fullPath);
} catch(NoSuchFileException e) {
LOG.warn("Failed to get inode info for " + fullPath, e);
return;
}
List<LogStream> logStreams = LogStreamManager.getLogStreamsFor(logDir, fullPath);
for (LogStream stream : logStreams) {
if (stream != null) {
stream.append(new LogFile(inode), fullPath.toString());
} else if (!modified.toString().startsWith(".")) {
LOG.debug("Found a file {} that is not in any log stream", modified);
}
}
}
public static void main(String[] args) throws Throwable {
try (FileChannel ch = FileChannel.open(BLK_PATH, READ);
RandomAccessFile file = new RandomAccessFile(BLK_FNAME, "r")) {
long size1 = ch.size();
long size2 = file.length();
if (size1 != size2) {
throw new RuntimeException("size differs when retrieved" +
" in different ways: " + size1 + " != " + size2);
}
System.out.println("OK");
} catch (NoSuchFileException nsfe) {
System.err.println("File " + BLK_FNAME + " not found." +
" Skipping test");
} catch (AccessDeniedException ade) {
System.err.println("Access to " + BLK_FNAME + " is denied." +
" Run test as root.");
}
}
@Override
protected void executeAction(DeltaAction action) throws IOException {
LOG.info("GET "+action.getURL());
Id dsRef = Id.fromString(action.httpArgs.datasourceName);
String filenameIRI = determineData(action, dsRef);
ContentType ct = RDFLanguages.guessContentType(filenameIRI) ;
String fn = IRILib.IRIToFilename(filenameIRI);
Path path = Paths.get(fn);
try ( InputStream in = Files.newInputStream(path) ) {
action.response.setStatus(HttpSC.OK_200);
action.response.setContentType(ct.getContentTypeStr());
IOUtils.copy(in, action.response.getOutputStream());
} catch (NoSuchFileException | FileNotFoundException ex) {
throw new DeltaNotFoundException(action.getURL());
}
}
@Test
public void skipsFirstCacheBecauseIgnoredForArchiveMemberPathAbsolutePath() throws IOException {
Config config = ConfigBuilder.createFromText("[project]", "ignore = world.jar");
Path fullPath = Paths.get("world.jar");
ProjectFilesystem filesystem =
TestProjectFilesystems.createProjectFilesystem(tmp.getRoot(), config);
writeJarWithHashes(filesystem, filesystem.resolve(fullPath));
ArchiveMemberPath archiveMemberPath =
ArchiveMemberPath.of(filesystem.resolve(fullPath), Paths.get("Nonexistent.class"));
ProjectFileHashCache innerCache =
DefaultFileHashCache.createDefaultFileHashCache(filesystem, fileHashCacheMode);
StackedFileHashCache cache = new StackedFileHashCache(ImmutableList.of(innerCache));
expectedException.expect(NoSuchFileException.class);
cache.getForArchiveMember(
archiveMemberPath.getArchivePath(), archiveMemberPath.getMemberPath());
}
@Override
public Promise<QueryResult> query(CubeQuery cubeQuery) throws QueryException {
DefiningClassLoader queryClassLoader = getQueryClassLoader(new CubeClassLoaderCache.Key(
new LinkedHashSet<>(cubeQuery.getAttributes()),
new LinkedHashSet<>(cubeQuery.getMeasures()),
cubeQuery.getWhere().getDimensions()));
long queryStarted = eventloop.currentTimeMillis();
return new RequestContext<>().execute(queryClassLoader, cubeQuery)
.whenComplete((queryResult, e) -> {
if (e == null) {
queryTimes.recordValue((int) (eventloop.currentTimeMillis() - queryStarted));
} else {
queryErrors++;
queryLastError = e;
if (e instanceof NoSuchFileException) {
logger.warn("Query failed because of NoSuchFileException. " + cubeQuery.toString(), e);
}
}
});
}
Optional<InputStream> doInputStream() throws IOException {
try {
InputStream is = Files.newInputStream(file, READ);
if (logger.isTraceEnabled()) {
logger.trace("-- doInputStream() - open: {}", Hex.toHexString(checksum()));
IOConsumer<InputStream> callback = u
-> logger.trace("-- doInputStream() - callback close: {}", Hex.toHexString(checksum()));
is = new HookInputStream(is, callback);
}
return Optional.of(is);
} catch (NoSuchFileException ex) {
logger.warn("-- doInputStream() - file was just deleted: {}", ex);
return Optional.empty();
}
}
private <T> T throwException(String segmentName, Exception e) throws StreamSegmentException {
if (e instanceof NoSuchFileException || e instanceof FileNotFoundException) {
throw new StreamSegmentNotExistsException(segmentName);
}
if (e instanceof FileAlreadyExistsException) {
throw new StreamSegmentExistsException(segmentName);
}
if (e instanceof IndexOutOfBoundsException) {
throw new IllegalArgumentException(e.getMessage());
}
if (e instanceof AccessControlException
|| e instanceof AccessDeniedException
|| e instanceof NonWritableChannelException) {
throw new StreamSegmentSealedException(segmentName, e);
}
throw Exceptions.sneakyThrow(e);
}
@Override
public boolean rename(final Path src, final Path dst) throws IOException {
final File srcFile = pathToFile(src);
final File dstFile = pathToFile(dst);
final File dstParent = dstFile.getParentFile();
// Files.move fails if the destination directory doesn't exist
//noinspection ResultOfMethodCallIgnored -- we don't care if the directory existed or was created
dstParent.mkdirs();
try {
Files.move(srcFile.toPath(), dstFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
return true;
}
catch (NoSuchFileException | AccessDeniedException | DirectoryNotEmptyException | SecurityException ex) {
// catch the errors that are regular "move failed" exceptions and return false
return false;
}
}
public static void main(String[] args) throws Throwable {
try (FileChannel ch = FileChannel.open(BLK_PATH, READ);
RandomAccessFile file = new RandomAccessFile(BLK_FNAME, "r")) {
long size1 = ch.size();
long size2 = file.length();
if (size1 != size2) {
throw new RuntimeException("size differs when retrieved" +
" in different ways: " + size1 + " != " + size2);
}
System.out.println("OK");
} catch (NoSuchFileException nsfe) {
System.err.println("File " + BLK_FNAME + " not found." +
" Skipping test");
} catch (AccessDeniedException ade) {
System.err.println("Access to " + BLK_FNAME + " is denied." +
" Run test as root.");
}
}
@Test
public void setLastModifiedTime() throws Exception {
Path root = fs.getRootDirectories().iterator().next();
Path folder = root.resolve("folder");
Files.createDirectory(folder);
Path file = root.resolve("file");
Files.createFile(file);
FileTime someTimeAgo = FileTime.from(365 * 12, TimeUnit.DAYS);
Files.setLastModifiedTime(folder, someTimeAgo);
Files.setLastModifiedTime(file, someTimeAgo);
try {
Files.setLastModifiedTime(root, someTimeAgo);
} catch (NoSuchFileException ex) {
System.err
.println("Unexpected failure of setLastModifiedTime on root");
ex.printStackTrace();
}
}
@Test
@Parameters(method = "getAnyPathParams")
public void throwsIfNamedDirectoryAncestorIsRegularFile(Kind kind, String targetName)
throws ExecutionException, InterruptedException, IOException {
filesystem.createNewFile(Paths.get("file"));
BuildTargetPatternToBuildPackagePathKey key =
key(CanonicalCellName.rootCell(), kind, "file/dir", targetName);
thrown.expect(ExecutionException.class);
thrown.expectCause(
Matchers.anyOf(
IsInstanceOf.instanceOf(NoSuchFileException.class),
IsInstanceOf.instanceOf(NotDirectoryException.class)));
transform("BUCK", key);
}
@SuppressWarnings("resource")
private SFTPAttributesAndOutputStreamPair newOutputStream(Channel channel, SFTPPath path, boolean requireAttributes, OpenOptions options)
throws IOException {
// retrieve the attributes unless create is true and createNew is false, because then the file can be created
SftpATTRS attributes = null;
if (!options.create || options.createNew) {
attributes = findAttributes(channel, path, false);
if (attributes != null && attributes.isDir()) {
throw Messages.fileSystemProvider().isDirectory(path.path());
}
if (!options.createNew && attributes == null) {
throw new NoSuchFileException(path.path());
} else if (options.createNew && attributes != null) {
throw new FileAlreadyExistsException(path.path());
}
}
// else the file can be created if necessary
if (attributes == null && requireAttributes) {
attributes = findAttributes(channel, path, false);
}
OutputStream out = channel.newOutputStream(path.path(), options);
return new SFTPAttributesAndOutputStreamPair(attributes, out);
}
public static int getImageType(Path iviewFileRoot, ImageReader imageReader, int zoomLevel, int x, int y)
throws IOException {
String tileName = new MessageFormat("{0}/{1}/{2}.jpg", Locale.ROOT).format(new Object[] { zoomLevel, y, x });
Path tile = iviewFileRoot.resolve(tileName);
if (Files.exists(tile)) {
try (SeekableByteChannel fileChannel = Files.newByteChannel(tile)) {
ImageInputStream iis = ImageIO.createImageInputStream(fileChannel);
if (iis == null) {
throw new IOException("Could not acquire ImageInputStream from SeekableByteChannel: " + tile);
}
imageReader.setInput(iis, true);
int imageType = MCRImage.getImageType(imageReader);
imageReader.reset();
iis.close();
return imageType;
}
} else {
throw new NoSuchFileException(iviewFileRoot.toString(), tileName, null);
}
}
public static boolean unpackZipNoCheck(String resource, Path target) throws IOException {
try {
if (Files.isDirectory(target))
return false;
Files.deleteIfExists(target);
Files.createDirectory(target);
try (ZipInputStream input = IOHelper.newZipInput(IOHelper.getResourceURL(resource))) {
for (ZipEntry entry = input.getNextEntry(); entry != null; entry = input.getNextEntry()) {
if (entry.isDirectory())
continue; // Skip dirs
// Unpack file
IOHelper.transfer(input, target.resolve(IOHelper.toPath(entry.getName())));
}
}
return true;
} catch (NoSuchFileException e) {
return true;
}
}
private static void addIdentifierForToolchain(
Path toolchainDir, ImmutableMap.Builder<String, AppleToolchain> identifierToToolchainBuilder)
throws IOException {
boolean addedToolchain = false;
String[] potentialPlistNames = {"ToolchainInfo.plist", "Info.plist"};
for (String plistName : potentialPlistNames) {
try {
Optional<AppleToolchain> toolchain = toolchainFromPlist(toolchainDir, plistName);
if (toolchain.isPresent()) {
identifierToToolchainBuilder.put(toolchain.get().getIdentifier(), toolchain.get());
addedToolchain = true;
break;
}
} catch (FileNotFoundException | NoSuchFileException e) {
LOG.debug("Loading toolchain from plist %s for %s failed", plistName, toolchainDir);
}
}
if (!addedToolchain) {
LOG.debug(
"Failed to resolve info about toolchain %s from plist files %s",
toolchainDir.toString(), Arrays.toString(potentialPlistNames));
}
}
public static void main(String[] args) throws Throwable {
try (FileChannel ch = FileChannel.open(BLK_PATH, READ);
RandomAccessFile file = new RandomAccessFile(BLK_FNAME, "r")) {
long size1 = ch.size();
long size2 = file.length();
if (size1 != size2) {
throw new RuntimeException("size differs when retrieved" +
" in different ways: " + size1 + " != " + size2);
}
System.out.println("OK");
} catch (NoSuchFileException nsfe) {
System.err.println("File " + BLK_FNAME + " not found." +
" Skipping test");
} catch (AccessDeniedException ade) {
System.err.println("Access to " + BLK_FNAME + " is denied." +
" Run test as root.");
}
}
public void testOpenReaderAfterDelete() throws IOException {
Path dirFile = createTempDir("deletetest");
Directory dir = newFSDirectory(dirFile);
if (dir instanceof BaseDirectoryWrapper) {
((BaseDirectoryWrapper)dir).setCheckIndexOnClose(false); // we will hit NoSuchFileException in MDW since we nuked it!
}
expectThrowsAnyOf(Arrays.asList(FileNotFoundException.class, NoSuchFileException.class),
() -> DirectoryReader.open(dir)
);
IOUtils.rm(dirFile);
// Make sure we still get a CorruptIndexException (not NPE):
expectThrowsAnyOf(Arrays.asList(FileNotFoundException.class, NoSuchFileException.class),
() -> DirectoryReader.open(dir)
);
dir.close();
}
public static void main(String[] args) throws Throwable {
try (FileChannel ch = FileChannel.open(BLK_PATH, READ);
RandomAccessFile file = new RandomAccessFile(BLK_FNAME, "r")) {
long size1 = ch.size();
long size2 = file.length();
if (size1 != size2) {
throw new RuntimeException("size differs when retrieved" +
" in different ways: " + size1 + " != " + size2);
}
System.out.println("OK");
} catch (NoSuchFileException nsfe) {
System.err.println("File " + BLK_FNAME + " not found." +
" Skipping test");
} catch (AccessDeniedException ade) {
System.err.println("Access to " + BLK_FNAME + " is denied." +
" Run test as root.");
}
}
/**
* {@inheritDoc}
*/
@Override
public FileVisitResult visitFileFailed(final Path file, final IOException ioe) {
if (ioe instanceof FileSystemLoopException) {
log.warn("Detected file system cycle visiting while visiting {}. Skipping.", file);
return FileVisitResult.SKIP_SUBTREE;
} else if (ioe instanceof AccessDeniedException) {
log.warn("Access denied for file {}. Skipping", file);
return FileVisitResult.SKIP_SUBTREE;
} else if (ioe instanceof NoSuchFileException) {
log.warn("File or directory disappeared while visiting {}. Skipping", file);
return FileVisitResult.SKIP_SUBTREE;
} else {
log.error("Got unknown error {} while visiting {}. Terminating visitor", ioe.getMessage(), file, ioe);
// TODO: Not sure if we should do this or skip subtree or just continue and ignore it?
return FileVisitResult.TERMINATE;
}
}
/**
* Ensures that AppendTo is failing on a negative predicate result.
*/
@Test
public void failsIfFileDoesNotExist() {
final File source = new File(
new Randomized(
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'
).asString()
);
new Assertion<>(
"Can't throw exception with proper message",
() -> new AppendTo(source).stream(),
new Throws<>(
source.getPath(),
NoSuchFileException.class
)
).affirm();
}
private RepositoryData getRepositoryData(long indexGen) {
if (indexGen == RepositoryData.EMPTY_REPO_GEN) {
return RepositoryData.EMPTY;
}
try {
final String snapshotsIndexBlobName = INDEX_FILE_PREFIX + Long.toString(indexGen);
RepositoryData repositoryData;
// EMPTY is safe here because RepositoryData#fromXContent calls namedObject
try (InputStream blob = blobContainer().readBlob(snapshotsIndexBlobName);
XContentParser parser = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE, blob)) {
repositoryData = RepositoryData.snapshotsFromXContent(parser, indexGen);
}
return repositoryData;
} catch (NoSuchFileException ex) {
// repository doesn't have an index blob, its a new blank repo
return RepositoryData.EMPTY;
} catch (IOException ioe) {
throw new RepositoryException(metadata.name(), "could not read repository data from index blob", ioe);
}
}
public static void main(String[] args) throws Throwable {
try (FileChannel ch = FileChannel.open(BLK_PATH, READ);
RandomAccessFile file = new RandomAccessFile(BLK_FNAME, "r")) {
long size1 = ch.size();
long size2 = file.length();
if (size1 != size2) {
throw new RuntimeException("size differs when retrieved" +
" in different ways: " + size1 + " != " + size2);
}
System.out.println("OK");
} catch (NoSuchFileException nsfe) {
System.err.println("File " + BLK_FNAME + " not found." +
" Skipping test");
} catch (AccessDeniedException ade) {
System.err.println("Access to " + BLK_FNAME + " is denied." +
" Run test as root.");
}
}
@Override
public long getSize(Path path) throws IOException {
Optional<Pair<ProjectFileHashCache, Path>> found = lookup(path);
if (!found.isPresent()) {
throw new NoSuchFileException(path.toString());
}
return found.get().getFirst().getSize(found.get().getSecond());
}
public Optional<DependencyFileRuleKeyFactory.RuleKeyAndInputs> calculateDepFileRuleKey(
Optional<ImmutableList<String>> depFile, boolean allowMissingInputs) throws IOException {
Preconditions.checkState(useDependencyFileRuleKey());
// Extract the dep file from the last build. If we don't find one, abort.
if (!depFile.isPresent()) {
return Optional.empty();
}
// Build the dep-file rule key. If any inputs are no longer on disk, this means something
// changed and a dep-file based rule key can't be calculated.
ImmutableList<DependencyFileEntry> inputs =
depFile.get().stream()
.map(ObjectMappers.fromJsonFunction(DependencyFileEntry.class))
.collect(ImmutableList.toImmutableList());
try (Scope ignored =
RuleKeyCalculationEvent.scope(
eventBus, RuleKeyCalculationEvent.Type.DEP_FILE, rule.getBuildTarget())) {
return Optional.of(
ruleKeyFactories
.getDepFileRuleKeyFactory()
.build(((SupportsDependencyFileRuleKey) rule), inputs));
} catch (SizeLimiter.SizeLimitException ex) {
return Optional.empty();
} catch (Exception e) {
// TODO(plamenko): fix exception propagation in RuleKeyBuilder
if (allowMissingInputs && Throwables.getRootCause(e) instanceof NoSuchFileException) {
return Optional.empty();
}
throw e;
}
}
@Override
public HashCode getForArchiveMember(Path relativeArchivePath, Path memberPath)
throws IOException {
Optional<Pair<ProjectFileHashCache, Path>> found = lookup(relativeArchivePath);
if (!found.isPresent()) {
throw new NoSuchFileException(relativeArchivePath.toString());
}
return found.get().getFirst().getForArchiveMember(found.get().getSecond(), memberPath);
}
/**
* This implementation opens a NIO file stream for the underlying file.
* @see java.io.FileInputStream
*/
@Override
public InputStream getInputStream() throws IOException {
try {
return Files.newInputStream(this.filePath);
}
catch (NoSuchFileException ex) {
throw new FileNotFoundException(ex.getMessage());
}
}
@Test
public void skipsFirstCacheAbsolutePath() throws IOException {
Path fullPath = Paths.get("some/path");
ProjectFilesystem filesystem =
new FakeProjectFilesystem(CanonicalCellName.rootCell(), tmp.getRoot());
ProjectFileHashCache innerCache =
DefaultFileHashCache.createDefaultFileHashCache(filesystem, fileHashCacheMode);
StackedFileHashCache cache = new StackedFileHashCache(ImmutableList.of(innerCache));
expectedException.expect(NoSuchFileException.class);
cache.get(filesystem.resolve(fullPath));
}
@Override
public void checkAccess(Path file, AccessMode... modes)
throws IOException
{
triggerEx(file, "checkAccess");
// hack
if (modes.length == 0) {
if (Files.exists(unwrap(file)))
return;
else
throw new NoSuchFileException(file.toString());
}
throw new RuntimeException("not implemented yet");
}