}
@JRubyMethod(required = 1, optional = 1, visibility = PRIVATE, compat = RUBY1_8)
public IRubyObject initialize(IRubyObject[] args, Block unusedBlock) {
int argCount = args.length;
ModeFlags modes;
int fileno = RubyNumeric.fix2int(args[0]);
try {
ChannelDescriptor descriptor = ChannelDescriptor.getDescriptorByFileno(getRuntime().getFilenoExtMap(fileno));
if (descriptor == null) {
throw getRuntime().newErrnoEBADFError();
}
descriptor.checkOpen();
if (argCount == 2) {
if (args[1] instanceof RubyFixnum) {
modes = new ModeFlags(RubyFixnum.fix2long(args[1]));
} else {
modes = getIOModes(getRuntime(), args[1].convertToString().toString());
}
} else {
// use original modes
modes = descriptor.getOriginalModes();
}
if (openFile.isOpen()) {
// JRUBY-4650: Make sure we clean up the old data,
// if it's present.
openFile.cleanup(getRuntime(), false);
}
openFile.setMode(modes.getOpenFileFlags());
openFile.setMainStream(fdopen(descriptor, modes));
} catch (BadDescriptorException ex) {
throw getRuntime().newErrnoEBADFError();
} catch (InvalidValueException ive) {