RubyArray writeAry = null;
if (!write.isNil()) {
writeAry = write.convertToArray();
for (i = 0; i < writeAry.size(); i++) {
RubyIO write_io = TypeConverter.ioGetIO(runtime, writeAry.eltOk(i)).GetWriteIO();
fptr = write_io.getOpenFileChecked();
fdSetWrite(context, fptr.fd(), writeAry.size());
}
}
RubyArray exceptAry = null;
if (!except.isNil()) {
// This does not actually register anything because we do not have a way to select for error on JDK.
// We make the calls for their side effects.
exceptAry = except.convertToArray();
for (i = 0; i < exceptAry.size(); i++) {
RubyIO io = TypeConverter.ioGetIO(runtime, exceptAry.eltOk(i));
RubyIO write_io = io.GetWriteIO();
fptr = io.getOpenFileChecked();
if (io != write_io) {
fptr = write_io.getOpenFileChecked();
}
}
}
int n = threadFdSelect(context);
if (n == 0 && pendingReadFDs == null && n == 0 && unselectableReadFDs == null && unselectableWriteFDs == null) return context.nil; /* returns nil on timeout */
res = RubyArray.newArray(runtime, 3);
res.push(runtime.newArray(Math.min(n, maxReadReadySize())));
res.push(runtime.newArray(Math.min(n, maxWriteReadySize())));
// we never add anything for error since JDK does not provide a way to select for error
res.push(runtime.newArray(0));
if (readKeyList != null) {
list = (RubyArray) res.eltOk(0);
for (i = 0; i < readAry.size(); i++) {
IRubyObject obj = readAry.eltOk(i);
RubyIO io = TypeConverter.ioGetIO(runtime, obj);
fptr = io.getOpenFileChecked();
if (fdIsSet(readKeyList, fptr.fd(), READ_ACCEPT_OPS) || (pendingReadFDs != null && pendingReadFDs.contains(fptr.fd()))) {
list.push(obj);
}
}
}
if (unselectableReadFDs != null) {
list = (RubyArray) res.eltOk(0);
for (i = 0; i < readAry.size(); i++) {
IRubyObject obj = readAry.eltOk(i);
RubyIO io = TypeConverter.ioGetIO(runtime, obj);
fptr = io.getOpenFileChecked();
if (unselectableReadFDs.contains(fptr.fd())) {
list.push(obj);
}
}
}
if (writeKeyList != null) {
list = (RubyArray) res.eltOk(1);
for (i = 0; i < writeAry.size(); i++) {
IRubyObject obj = writeAry.eltOk(i);
RubyIO io = TypeConverter.ioGetIO(runtime, obj);
RubyIO write_io = io.GetWriteIO();
fptr = write_io.getOpenFileChecked();
if (fdIsSet(writeKeyList, fptr.fd(), WRITE_CONNECT_OPS)) {
list.push(obj);
}
}
}
if (unselectableWriteFDs != null) {
list = (RubyArray) res.eltOk(1);
for (i = 0; i < writeAry.size(); i++) {
IRubyObject obj = writeAry.eltOk(i);
RubyIO io = TypeConverter.ioGetIO(runtime, obj);
fptr = io.getOpenFileChecked();
if (unselectableWriteFDs.contains(fptr.fd())) {
list.push(obj);
}
}
}
if (errorKeyList != null) {
list = (RubyArray) res.eltOk(2);
for (i = 0; i < exceptAry.size(); i++) {
IRubyObject obj = exceptAry.eltOk(i);
RubyIO io = TypeConverter.ioGetIO(runtime, obj);
RubyIO write_io = io.GetWriteIO();
fptr = io.getOpenFileChecked();
if (errorKeyList.contains(fptr.fd())) {
list.push(obj);
} else if (io != write_io) {
fptr = write_io.getOpenFileChecked();
if (errorKeyList.contains(fptr.fd())) {
list.push(obj);
}
}
}