📄 commportidentifier.java
字号:
/** * Deregisters a <CODE>CommPortOwnershipListener</CODE> registered using * <CODE>addPortOwnershipListener</CODE> * * @param listener The CommPortOwnershipListener object that was * previously registered using addPortOwnershipListener */ public void removePortOwnershipListener(CommPortOwnershipListener lsnr) { cpoList.remove(lsnr); } void ownershipThreadWaiter() { /******************************************************************* * FILL IN HERE. * Use underlying OS's IPC mechanisms to wait for ownership * event notifications from other Java VMs that may be * running Java Communications API applications. ******************************************************************/ int ret; if (ret) { maskOwnershipEvents = true; switch(ret) { case 0: fireOwnershipEvent(CommPortOwnershipListener.PORT_OWNED); break; case 1: fireOwnershipEvent(CommPortOwnershipListener.PORT_UNOWNED); break; case 2: fireOwnershipEvent( CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED); break; } maskOwnershipEvents = false; } } synchronized void internalClosePort() { owned = false; owner = null; port = null; if (!maskOwnershipEvents) { fireOwnershipEvent(CommPortOwnershipListener.PORT_UNOWNED); } } CommPortIdentifier next; private CommPort port; private CommDriver driver; CommPortIdentifier(String name, CommPort port, int portType,CommDriver dr) { this.name = name; this.port = port; this.portType = portType; this.next = null; this.driver = dr; } void fireOwnershipEvent(int eventType) { /* * Need to clone the ownership listener list, before firing the * event, because the event handlers have a habit of calling * removeOwnershipListener() which causes a deadlock. */ CpoList clone = cpoList.clonelist(); clone.fireOwnershipEvent(eventType); } private static String[] parsePropsFile(InputStream in) { Vector strs = new Vector(); try { byte ba[] = new byte[4096]; int index = 0; boolean skip_to_eol = false; int b; while ((b = in.read()) != -1) { // System.err.println("Read - " + b); switch (b) { case '\t': case ' ': break; case '\r': case '\n': if (index > 0) { String str = new String(ba, 0, 0,index); strs.addElement(str); } index = 0; skip_to_eol = false; break; case '#': skip_to_eol = true; if (index > 0) { String str = new String(ba, 0, 0,index); strs.addElement(str); } index = 0; break; default: if (!skip_to_eol && index < 4096) ba[index++] = (byte) b; } } } catch (Throwable ex) { System.err.println("Caught " + ex + " parsing prop file."); } // Convert to String array if strs has any elements. if (strs.size() > 0) { String strarray[] = new String[strs.size()]; for (int i = 0; i < strs.size(); i++) { strarray[i] = (String) strs.elementAt(i); } return strarray; } else { return null; } } /* for synchronizing access to the list of ports */ static Object lock; static String propfilename; static { System.err.println("****************************************************************************"); System.err.println("Java Communications API - Early Access"); System.err.println( "Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved."); System.err.println("****************************************************************************"); lock = new Object(); /* * Check our property and our properties file for list * of drivers */ String pdrivers; if ((pdrivers = System.getProperty("javax.comm.properties")) != null) { System.err.println("Comm Drivers: " + pdrivers); } propfilename = System.getProperty("java.home") + File.separator + "lib" + File.separator + "javax.comm.properties"; File propfile = new File(propfilename); InputStream fis; try { fis = new BufferedInputStream(new FileInputStream(propfile)); String drivers[] = parsePropsFile(fis); if (drivers != null) { for (int i = 0; i < drivers.length; i++) { if (drivers[i].regionMatches(true, 0, "driver=", 0, 7)) { String drivername = drivers[i].substring(7); drivername.trim(); try { CommDriver driver = (CommDriver) Class.forName(drivername).newInstance(); driver.initialize(); } catch (Throwable th) { System.err.println("Caught " + th + " while loading driver " + drivername); } } } } } catch (Throwable ex) { /* silently ignore any exception */ System.err.println(ex); } } static CommPortIdentifier masterIdList; /* * ownership of this port */ boolean owned; /* this boolean indicates ownership in this VM */ String owner; /* this value describes ownership within this VM */ /** * Opens the communications port using a <CODE>FileDescriptor</CODE> * object on platforms that support this technique. * * @param fd The <CODE>FileDescriptor</CODE> object used to build a <CODE>CommPort</CODE>. * @return a <CODE>CommPort</CODE> object. * @exception UnsupportedCommOperationException is thrown * on platforms which do not support this functionality. */ public CommPort open(FileDescriptor fd) throws UnsupportedCommOperationException { throw new UnsupportedCommOperationException(); }}/** * List of ports. * * @author Jagane Sundar * @version 1.11, 23 Jan 1998 * @see javax.comm.CommPort * @see javax.comm.CommPortIdentifier */class CommPortEnumerator implements Enumeration { private CommPortIdentifier curEntry; /** * Returns the next element of this enumeration. * * @return the next element of this enumeration. */ public Object nextElement() { synchronized (CommPortIdentifier.lock) { if (curEntry != null) { curEntry = curEntry.next; } else { curEntry = CommPortIdentifier.masterIdList; } } return curEntry; } /** * Tests if this enumeration contains more elements. * * @return true if this enumeration contains more elements; false otherwise. */ public boolean hasMoreElements() { synchronized (CommPortIdentifier.lock) { if (curEntry != null) { return curEntry.next == null ? false : true; } else { return CommPortIdentifier.masterIdList == null ? false : true; } } }}/** * Port thread management. * * @author Jagane Sundar * @version 1.11, 23 Jan 1998 * @see javax.comm.CommPort * @see javax.comm.CommPortIdentifier * @see java.lang.thread */class OwnershipEventThread extends Thread { CommPortIdentifier portId; OwnershipEventThread(CommPortIdentifier id) { portId = id; } /** * If this thread was constructed using a separate Runnable run object, * then that Runnable object's run method is called; * otherwise, <CODE>run</CODE> does nothing and returns. */ public void run() { while (!portId.cpoList.isEmpty()) { portId.ownershipThreadWaiter(); } portId.oeThread = null; }}class CpoListEntry { CpoListEntry next; CommPortOwnershipListener listener; CpoListEntry(CommPortOwnershipListener lsnr) { listener = lsnr; next = null; }}class CpoList { CpoListEntry listHead = null; synchronized void add(CommPortOwnershipListener lsnr) { CpoListEntry cur = listHead; while (cur != null) { if (cur.listener == lsnr) { return; } cur = cur.next; } CpoListEntry n = new CpoListEntry(lsnr); n.next = listHead; listHead = n; } synchronized void remove(CommPortOwnershipListener lsnr) { CpoListEntry prev = null; CpoListEntry cur = listHead; while (cur != null) { if (cur.listener == lsnr) { if (prev != null) prev.next = cur.next; else listHead = cur.next; cur.listener = null; cur.next = null; return; } prev = cur; cur = cur.next; } } synchronized CpoList clonelist() { CpoListEntry clonehead = null; CpoListEntry nclone = null; CpoListEntry cur = listHead; while (cur != null) { nclone = new CpoListEntry(cur.listener); nclone.next = clonehead; clonehead = nclone; cur = cur.next; } CpoList ret = new CpoList(); ret.listHead = clonehead; return ret; } synchronized boolean isEmpty() { return listHead == null; } synchronized void fireOwnershipEvent(int eventType) { CpoListEntry cur = listHead; while (cur != null) { cur.listener.ownershipChange(eventType); cur = cur.next; } } synchronized void dump() { CpoListEntry cur = listHead; while (cur != null) { System.err.println(" CpoListEntry - " + cur.listener.toString()); cur = cur.next; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -