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