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);
}
}
FileDescriptor fileDescriptor;
FileChannel fileChannel;
boolean isInAppendMode;
if (flags.isWritable() && !flags.isReadable()) {
FileOutputStream fos = new FileOutputStream(theFile, flags.isAppendable());
fileChannel = fos.getChannel();
fileDescriptor = fos.getFD();
isInAppendMode = true;
} else {
RandomAccessFile raf = new RandomAccessFile(theFile, flags.toJavaModeString());
fileChannel = raf.getChannel();
fileDescriptor = raf.getFD();
isInAppendMode = false;
}
// 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,
// perm is > 0, and only if the file was created in this call.
if (posix != null && perm > 0) {
posix.chmod(theFile.getPath(), perm);
}
}
try {
if (flags.isTruncate()) fileChannel.truncate(0);