package org.jeffpiazza.derby;

import jssc.*;
import java.io.*;
import java.util.*;

public class PortIterator implements Iterator<SerialPort> {

  public PortIterator(String[] portnames) {
    this.portNames = portnames;
    this.index = 0;
  }

  public PortIterator(String portname) {
    this(new String[] { portname });
  }

  public PortIterator(File[] files) {
    this(mapPathNames(files));
  }

  public PortIterator() {
    this(defaultPortNames());
  }

  public boolean hasNext() { return index < portNames.length; }

  public SerialPort next() { return new SerialPort(portNames[index++]); }

  public void remove() { throw new UnsupportedOperationException(); }

  private static String[] mapPathNames(File[] files) {
    String[] pathnames = new String[files.length];
    for (int i = 0; i < files.length; ++i) {
      pathnames[i] = files[i].getPath();
    }
    return pathnames;
  }

  private static String[] defaultPortNames() {
    if (isWindows()) {
      return SerialPortList.getPortNames();
    } else {
      return mapPathNames(new File("/dev").listFiles(new FilenameFilter() {
          public boolean accept(File dir, String name) {
            return name.startsWith("tty.") && !name.equals("tty")
            // Mac devices: /dev/tty.KeySerial1, /dev/tty.USA19Hfd12P1.1
            /* && (name.contains("usb") || name.contains("USB")) */;
          }
        }));
    }
  }

  private static boolean isWindows() {
    return System.getProperty("os.name").toLowerCase().contains("win");
  }

  String[] portNames;
  int index;
}

@@ -1004,24 +1004,575 @@
 ortNames();%0A
+    %7D else if (isLinux()) %7B%0A      return mapPathNames(new File(%22/dev%22).listFiles(new FilenameFilter() %7B%0A          public boolean accept(File dir, String name) %7B%0A            File sys = new File(new File(%22/sys/class/tty%22), name);%0A            if (!sys.exists()) %7B%0A                return false;%0A            %7D%0A            File device = new File(sys, %22device%22);%0A            if (!device.exists()) %7B%0A                return false;%0A            %7D%0A            File driver = new File(device, %22driver%22);%0A            return driver.exists();%0A          %7D%0A        %7D));%0A
     %7D else %7B
@@ -2065,16 +2065,130 @@
 );%0A  %7D%0A%0A
+  private static boolean isLinux() %7B%0A    return System.getProperty(%22os.name%22).toLowerCase().equals(%22linux%22);%0A  %7D%0A%0A
   String

Update defaultPortNames() function for Linux.