📄 externalmessageio.java
字号:
/** * ExternalMessageIO.java * * * Created: Fri Apr 17 22:14:01 1998 * * @author Steven Zeil * @version */package edu.odu.cs.zeil.AlgAE;import edu.odu.cs.zeil.AlgAE.Application;import edu.odu.cs.zeil.AlgAE.Debug;import edu.odu.cs.zeil.AlgAE.Message;import edu.odu.cs.zeil.AlgAE.MessageIO;import edu.odu.cs.zeil.AlgAE.ParameterizedMessage;import java.io.BufferedReader;import java.io.InputStream;import java.io.PrintWriter;import java.io.Reader;import java.util.Hashtable;import java.util.Vector;public class ExternalMessageIO implements MessageIO{ private java.io.Reader inStream; private PrintWriter outStream; private Application application; public ExternalMessageIO(java.io.Reader istream, PrintWriter ostream, Application appl) { inStream = istream; outStream = ostream; application = appl; } /** * Send a message */ public void send (ParameterizedMessage pmsg) { if (outStream != null) { outStream.print (pmsg); outStream.print ("\n"); if (pmsg.getMessage().isForcing()) outStream.flush(); } } /** * Read a message **/ public ParameterizedMessage read() { String line; ParameterizedMessage pmsg = null; boolean keepTrying = true; while (keepTrying) { keepTrying = false; try { line = getLine(); Debug.show (Debug.tokenizer, "ExtMsgIO.read.line: /" + line + "/"); String kind; Vector params = new Vector(); if (line != null) { line.trim(); int lineSize = line.length(); if (lineSize > 0) { // Extract the message kind int blankPos = line.indexOf(' '); if (blankPos >= 0) kind = line.substring(0, blankPos); else kind = line; if (!kind.equals("//")) { if (blankPos < 0) blankPos = lineSize; // Extract the message parameters while (blankPos < lineSize) { int lexemeStart = blankPos; // Skip over any blanks while ((lexemeStart < lineSize) && (line.charAt(lexemeStart) == ' ')) ++lexemeStart; if (lexemeStart < lineSize) { if (line.charAt(lexemeStart) == '"') { // Parameter is a string. int lexemeEnd = lexemeStart + 1; while ((lexemeEnd < lineSize) && (line.charAt(lexemeEnd) != '"')) ++lexemeEnd; params.addElement (ParameterizedMessage.decode (line.substring(lexemeStart+1, lexemeEnd))); blankPos = lexemeEnd + 1; } else { // Parameter should be an integer blankPos = line.indexOf(' ', lexemeStart+1); if (blankPos < 0) blankPos = lineSize; String lexeme = line.substring(lexemeStart, blankPos); try { params.addElement (new Integer(lexeme)); } catch (NumberFormatException e) { Debug.error ("Communications error\n" + "Bad message parameter: " + lexeme + " in\n" + line); } } } else blankPos = lexemeStart; } // Build and queue up the message. Message msg = application.msgs.get (kind); if (msg == null) Debug.error ("Communications error\n" + "Unregistered message: " + kind); else { pmsg = new ParameterizedMessage(msg,params); Debug.show (Debug.tokenizer, "read " + pmsg); } } } else keepTrying = true; } } catch (java.io.IOException e) { Debug.error ("Communications error\nIOException raised"); pmsg = null; } } return pmsg; } public void close() { try { inStream.close(); } catch (Throwable e) { } try { if (outStream != null) outStream.close(); } catch (Throwable e) { } } private String getLine() throws java.io.IOException { StringBuffer s = new StringBuffer (); while (true) { int ch = inStream.read(); //Debug.show (Debug.tokenizer, "Read char " + (char)ch + " (" + ch + ")"); if (ch < ' ') break; else s.append ((char)ch); } return s.toString(); } } // ExternalMessageIO
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -