path = path.substring("classpath:/".length());
InputStream is = classLoader.getResourceAsStream(path);
// FIXME: don't use RubyIO for this
return new ChannelDescriptor(Channels.newChannel(is), flags);
} else {
JRubyFile theFile = JRubyFile.create(cwd,path);
if (theFile.isDirectory() && flags.isWritable()) {
throw new DirectoryAsFileException();
}
if (flags.isCreate()) {
if (theFile.exists() && flags.isExclusive()) {
throw new FileExistsException(path);
}
try {
fileCreated = theFile.createNewFile();
} catch (IOException ioe) {
// See JRUBY-4380.
// MRI behavior: raise Errno::ENOENT in case
// when the directory for the file doesn't exist.
// Java in such cases just throws IOException.
File parent = theFile.getParentFile();
if (parent != null && parent != theFile && !parent.exists()) {
throw new FileNotFoundException(path);
} else if (!theFile.canWrite()) {
throw new PermissionDeniedException(path);
} else {
// for all other IO errors, just re-throw the original exception
throw ioe;
}
}
} else {
if (!theFile.exists()) {
throw new FileNotFoundException(path);
}
}
// We always open this rw since we can only open it r or rw.
RandomAccessFile file = new RandomAccessFile(theFile, flags.toJavaModeString());
// call chmod after we created the RandomAccesFile
// because otherwise, the file could be read-only
if (fileCreated) {
// attempt to set the permissions, if we have been passed a POSIX instance,
// and only if the file was created in this call.
if (posix != null && perm != -1) {
posix.chmod(theFile.getPath(), perm);
}
}
if (flags.isTruncate()) file.setLength(0L);