Java Code Examples for java.io.FileNotFoundException#initCause()

The following examples show how to use java.io.FileNotFoundException#initCause() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileEncodingQueryDataEditorSupportTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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 ());
}
 
Example 2
Source File: DataEditorSupportTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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 ());
}
 
Example 3
Source File: AssetFilesystem.java    From keemob with MIT License 6 votes vote down vote up
@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;
}
 
Example 4
Source File: AssetFilesystem.java    From keemob with MIT License 6 votes vote down vote up
@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;
}
 
Example 5
Source File: AssetFilesystem.java    From keemob with MIT License 6 votes vote down vote up
@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;
}
 
Example 6
Source File: IoBridge.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example 7
Source File: PrefetchingGcsInputChannelImpl.java    From appengine-gcs-client with Apache License 2.0 6 votes vote down vote up
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());
    }
  }
}
 
Example 8
Source File: StreamUtil.java    From iaf with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 9
Source File: DocumentInfo.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: DocumentInfo.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: DocumentInfo.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 12
Source File: BaseConnectionFileManager.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
protected static FileNotFoundException initFileNotFoundException(IPath path, Throwable cause)
{
	FileNotFoundException e = new FileNotFoundException(path.toPortableString());
	if (cause != null)
	{
		e.initCause(cause);
	}
	return e;
}
 
Example 13
Source File: GoogleCloudStorageExceptions.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
/** 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;
}
 
Example 14
Source File: ExceptionsTest.java    From sis with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 15
Source File: ResourceUtil.java    From metafacture-core with Apache License 2.0 5 votes vote down vote up
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;
}