下面列出了java.io.FileNotFoundException#initCause ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public java.io.InputStream getInputStream () throws java.io.FileNotFoundException {
if (openStreams < 0) {
FileNotFoundException e = new FileNotFoundException("Already exists output stream");
if (previousStream != null) {
e.initCause(previousStream);
}
throw e;
}
class IS extends ByteArrayInputStream {
public IS(byte[] arr) {
super(arr);
openStreams++;
}
@Override
public void close() throws IOException {
openStreams--;
super.close();
}
}
previousStream = new Exception("Input");
return new IS(RUNNING.content.getBytes ());
}
public java.io.InputStream getInputStream () throws java.io.FileNotFoundException {
if (openStreams < 0) {
FileNotFoundException e = new FileNotFoundException("Already exists output stream");
if (previousStream != null) {
e.initCause(previousStream);
}
throw e;
}
class IS extends ByteArrayInputStream {
public IS(byte[] arr) {
super(arr);
openStreams++;
}
@Override
public void close() throws IOException {
openStreams--;
super.close();
}
}
previousStream = new Exception("Input");
return new IS(RUNNING.content.getBytes ());
}
@Override
public LocalFilesystemURL[] listChildren(LocalFilesystemURL inputURL) throws FileNotFoundException {
String pathNoSlashes = inputURL.path.substring(1);
if (pathNoSlashes.endsWith("/")) {
pathNoSlashes = pathNoSlashes.substring(0, pathNoSlashes.length() - 1);
}
String[] files;
try {
files = listAssets(pathNoSlashes);
} catch (IOException e) {
FileNotFoundException fnfe = new FileNotFoundException();
fnfe.initCause(e);
throw fnfe;
}
LocalFilesystemURL[] entries = new LocalFilesystemURL[files.length];
for (int i = 0; i < files.length; ++i) {
entries[i] = localUrlforFullPath(new File(inputURL.path, files[i]).getPath());
}
return entries;
}
@Override
public LocalFilesystemURL[] listChildren(LocalFilesystemURL inputURL) throws FileNotFoundException {
String pathNoSlashes = inputURL.path.substring(1);
if (pathNoSlashes.endsWith("/")) {
pathNoSlashes = pathNoSlashes.substring(0, pathNoSlashes.length() - 1);
}
String[] files;
try {
files = listAssets(pathNoSlashes);
} catch (IOException e) {
FileNotFoundException fnfe = new FileNotFoundException();
fnfe.initCause(e);
throw fnfe;
}
LocalFilesystemURL[] entries = new LocalFilesystemURL[files.length];
for (int i = 0; i < files.length; ++i) {
entries[i] = localUrlforFullPath(new File(inputURL.path, files[i]).getPath());
}
return entries;
}
@Override
public LocalFilesystemURL[] listChildren(LocalFilesystemURL inputURL) throws FileNotFoundException {
String pathNoSlashes = inputURL.path.substring(1);
if (pathNoSlashes.endsWith("/")) {
pathNoSlashes = pathNoSlashes.substring(0, pathNoSlashes.length() - 1);
}
String[] files;
try {
files = listAssets(pathNoSlashes);
} catch (IOException e) {
FileNotFoundException fnfe = new FileNotFoundException();
fnfe.initCause(e);
throw fnfe;
}
LocalFilesystemURL[] entries = new LocalFilesystemURL[files.length];
for (int i = 0; i < files.length; ++i) {
entries[i] = localUrlforFullPath(new File(inputURL.path, files[i]).getPath());
}
return entries;
}
/**
* java.io only throws FileNotFoundException when opening files, regardless of what actually
* went wrong. Additionally, java.io is more restrictive than POSIX when it comes to opening
* directories: POSIX says read-only is okay, but java.io doesn't even allow that. We also
* have an Android-specific hack to alter the default permissions.
*/
public static FileDescriptor open(String path, int flags) throws FileNotFoundException {
FileDescriptor fd = null;
try {
// On Android, we don't want default permissions to allow global access.
int mode = ((flags & O_ACCMODE) == O_RDONLY) ? 0 : 0600;
fd = Libcore.os.open(path, flags, mode);
// Posix open(2) fails with EISDIR only if you ask for write permission.
// Java disallows reading directories too.
if (isDirectory(path)) {
throw new ErrnoException("open", EISDIR);
}
return fd;
} catch (ErrnoException errnoException) {
try {
if (fd != null) {
IoUtils.close(fd);
}
} catch (IOException ignored) {
}
FileNotFoundException ex = new FileNotFoundException(path + ": " + errnoException.getMessage());
ex.initCause(errnoException);
throw ex;
}
}
private void waitForFetch() throws IOException, InterruptedException {
Preconditions.checkState(pendingFetch != null, "%s: no fetch pending", this);
Preconditions.checkState(!current.hasRemaining(), "%s: current has remaining", this);
try {
GcsFileMetadata gcsFileMetadata = pendingFetch.get();
flipToNextBlockAndPrefetch(gcsFileMetadata.getLength());
} catch (ExecutionException e) {
if (e.getCause() instanceof BadRangeException) {
eofHit = true;
current = EMPTY_BUFFER;
next = null;
pendingFetch = null;
} else if (e.getCause() instanceof FileNotFoundException) {
FileNotFoundException toThrow = new FileNotFoundException(e.getMessage());
toThrow.initCause(e);
throw toThrow;
} else if (e.getCause() instanceof IOException) {
log.log(Level.WARNING, this + ": IOException fetching block", e);
requestBlock();
throw new IOException(this + ": Prefetch failed, prefetching again", e.getCause());
} else {
throw new RuntimeException(this + ": Unknown cause of ExecutionException", e.getCause());
}
}
}
public static OutputStream getOutputStream(Object target) throws IOException {
if (target instanceof OutputStream) {
return (OutputStream) target;
}
if (target instanceof String) {
String filename=(String)target;
if (StringUtils.isEmpty(filename)) {
throw new IOException("target string cannot be empty but must contain a filename");
}
try {
return new FileOutputStream(filename);
} catch (FileNotFoundException e) {
FileNotFoundException fnfe = new FileNotFoundException("cannot create file ["+filename+"]");
fnfe.initCause(e);
throw fnfe;
}
}
return null;
}
public static FileNotFoundException asFileNotFoundException(Throwable t)
throws FileNotFoundException {
if (t instanceof FileNotFoundException) {
throw (FileNotFoundException) t;
}
final FileNotFoundException fnfe = new FileNotFoundException(t.getMessage());
fnfe.initCause(t);
throw fnfe;
}
public static FileNotFoundException asFileNotFoundException(Throwable t)
throws FileNotFoundException {
if (t instanceof FileNotFoundException) {
throw (FileNotFoundException) t;
}
final FileNotFoundException fnfe = new FileNotFoundException(t.getMessage());
fnfe.initCause(t);
throw fnfe;
}
public static FileNotFoundException asFileNotFoundException(Throwable t)
throws FileNotFoundException {
if (t instanceof FileNotFoundException) {
throw (FileNotFoundException) t;
}
final FileNotFoundException fnfe = new FileNotFoundException(t.getMessage());
fnfe.initCause(t);
throw fnfe;
}
protected static FileNotFoundException initFileNotFoundException(IPath path, Throwable cause)
{
FileNotFoundException e = new FileNotFoundException(path.toPortableString());
if (cause != null)
{
e.initCause(cause);
}
return e;
}
/** Creates FileNotFoundException with suitable message for a GCS bucket or object. */
public static FileNotFoundException createFileNotFoundException(
String bucketName, String objectName, @Nullable IOException cause) {
checkArgument(!isNullOrEmpty(bucketName), "bucketName must not be null or empty");
FileNotFoundException fileNotFoundException =
new FileNotFoundException(
String.format(
"Item not found: '%s'. Note, it is possible that the live version"
+ " is still available but the requested generation is deleted.",
StringPaths.fromComponents(bucketName, nullToEmpty(objectName))));
if (cause != null) {
fileNotFoundException.initCause(cause);
}
return fileNotFoundException;
}
/**
* Tests {@link Exceptions#formatChainedMessages(Locale, String, Throwable)}.
*/
@Test
public void testFormatChainedMessages() {
final String lineSeparator = System.lineSeparator();
final FileNotFoundException cause = new FileNotFoundException("MisingFile.txt");
cause.initCause(new IOException("Disk is not mounted."));
final Exception e = new Exception("Can not find “MisingFile.txt”.", cause);
/*
* The actual sequence of messages (with their cause is):
*
* Can not find “MisingFile.txt”
* MisingFile.txt
* Disk is not mounted.
*
* But the second line shall be omitted because it duplicates the first line.
*/
String message = Exceptions.formatChainedMessages(Locale.ENGLISH, null, e);
assertEquals("Can not find “MisingFile.txt”." + lineSeparator +
"Caused by IOException: Disk is not mounted.",
message);
/*
* Test again with a header.
*/
message = Exceptions.formatChainedMessages(Locale.ENGLISH, "Error while creating the data store.", e);
assertEquals("Error while creating the data store." + lineSeparator +
"Caused by Exception: Can not find “MisingFile.txt”." + lineSeparator +
"Caused by IOException: Disk is not mounted.",
message);
}
private static void throwFileNotFoundException(final String name,
final Throwable t) throws FileNotFoundException {
final FileNotFoundException e = new FileNotFoundException(
"No file, resource or URL found: " + name);
if (t != null) {
e.initCause(t);
}
throw e;
}