Refactored select(2) in Java to share similar code.