Annotation of OpenXM/src/OpenMath/ORG/openxm/tam/OpenXMstream.java, Revision 1.3
1.1 tam 1: /**
1.3 ! tam 2: * $OpenXM: OpenXM/src/OpenMath/ORG/openxm/tam/OpenXMstream.java,v 1.2 2000/10/11 08:32:15 ohara Exp $
1.1 tam 3: */
4: package ORG.openxm.tam;
5:
6: import java.io.*;
7: import java.net.*;
8:
9: /**
10: * OpenXM $B%a%C%;!<%8$NAw<u?.$r9T$J$&%/%i%9(B.
11: * $B%3%s%H%m!<%k(B, $B%G!<%?$=$l$>$l$K0l$D$:$DI,MW$H$J$k(B.
12: * $B$3$N%/%i%9$G$O(B, $B@\B3;~$K%P%$%H%*!<%@$N8r49$r9T$J$$(B,
13: * 32bit $B@0?t$NAw<u?.;~$K%P%$%H%*!<%@$NJQ49$r9T$J$&(B.
14: * $B8=:_$O%M%C%H%o!<%/%P%$%H%*!<%@$N$_<BAu$5$l$F$$$k(B.
15: */
16: public class OpenXMstream{
17: private int serial = 0;
1.3 ! tam 18: //private Socket socket = null;
1.1 tam 19: private InputStream is = null;
20: private OutputStream os = null;
21: private int order = OX_BYTE_NETWORK_BYTE_ORDER;
22: private CMO_MATHCAP mathcap = null;
23: private ByteArrayOutputStream buffer = new ByteArrayOutputStream();
24:
25: /**
26: * $B%M%C%H%o!<%/%P%$%H%*!<%@$rI=$9(B.
27: * $B%P%$%H%*!<%@$N7hDj;~$KMQ$$$k(B.
28: */
29: final public static int OX_BYTE_NETWORK_BYTE_ORDER = 0;
30:
31: /**
32: * $B%j%H%k%$%s%G%#%"%s$rI=$9(B.
33: * $B%P%$%H%*!<%@$N7hDj;~$KMQ$$$k(B.
34: */
35: final public static int OX_BYTE_LITTLE_INDIAN = 1;
36:
37: /**
38: * $B%S%C%0%$%s%G%#%"%s$rI=$9(B.
39: * $B%P%$%H%*!<%@$N7hDj;~$KMQ$$$k(B.
40: */
41: final public static int OX_BYTE_BIG_INDIAN = 0xff;
42:
43: /**
1.3 ! tam 44: * InputStream is $B$H(B OutputStream os $B$rMQ$$$F(B OpenXM $BDL?.$r9T$J$&(B.
! 45: */
! 46: public OpenXMstream(InputStream is,OutputStream os) throws IOException{
! 47: this.is = is;
! 48: this.os = os;
! 49: }
! 50:
! 51: /**
1.1 tam 52: * $B%^%7%sL>(B host $B$N%]!<%HHV9f(B Port $B$X(B TCP $B@\B3$r9T$J$$(B,
1.3 ! tam 53: * $B@\B3$r9T$J$&(B. $B8=:_$O8_49@-$N$?$a$K;D$7$F$"$k(B.
1.1 tam 54: */
55: public OpenXMstream(String host,int Port) throws IOException{
56: this(host,Port,false);
57: }
58:
59: /**
60: * $B%^%7%sL>(B host $B$N%]!<%HHV9f(B Port $B$X(B TCP $B@\B3$r9T$J$$(B,
61: * $B@\B3$r9T$J$&(B. reverse $B$,(B true $B$N$H$-!"(B TCP $B%=%1%C%H$r(B
1.3 ! tam 62: * $B:n@.$7(B, $B@\B3$rBT$D(B. $B8=:_$O8_49@-$N$?$a$K;D$7$F$"$k(B.
1.1 tam 63: */
64: public OpenXMstream(String host,int Port,boolean reverse) throws IOException{
1.3 ! tam 65: Socket socket;
! 66:
1.1 tam 67: // create socket
68: if(!reverse){
69: socket = new Socket(host,Port);
70: }else{
71: socket = new ServerSocket(Port,50,InetAddress.getByName(host)).accept();
72: }
73:
74: //is =new DebugInputStream(new BufferedInputStream(socket.getInputStream()));
1.3 ! tam 75: is= new BufferedInputStream(socket.getInputStream());
1.1 tam 76: os = socket.getOutputStream();
77: }
78:
79: /**
80: * $B%P%$%H%*!<%@$N7hDj$r9T$J$&(B. $B8=:_$O%M%C%H%o!<%/%P%$%H%*!<%@!<$N$_(B.
81: */
1.3 ! tam 82: public int exchangeByteOrder(int order) throws IOException{
1.1 tam 83: // send byte order
84: os.write(OX_BYTE_NETWORK_BYTE_ORDER);
85: os.flush();
86:
87: // receive byte order
1.2 ohara 88: //System.err.println("get: "+is.read());
1.1 tam 89: is.read();
90:
1.3 ! tam 91: this.order = OX_BYTE_NETWORK_BYTE_ORDER;
1.1 tam 92:
1.3 ! tam 93: return this.order;
! 94: }
! 95:
! 96: /**
! 97: * $B%P%$%H%*!<%@$N7hDj$r9T$J$&(B. $B8=:_$O%M%C%H%o!<%/%P%$%H%*!<%@!<$N$_(B.
! 98: * $B?d>)$5$l$J$$(B.
! 99: */
! 100: public int sendByteOrder() throws IOException{
! 101: return exchangeByteOrder(OX_BYTE_NETWORK_BYTE_ORDER);
1.1 tam 102: }
103:
104: /**
105: * $B@\B3$rJD$8$k(B.
106: */
107: public void close() throws IOException{
108: is.close();
109: os.close();
110: }
111:
112: /**
113: * 1 $B%P%$%H$NAw?.$r9T$J$&(B.
114: */
115: public void writeByte(int b) throws IOException{
116: buffer.write(b);
117: }
118:
119: /**
120: * 32 bit integer $B$NAw?.$r9T$J$&(B.
121: * $B$3$N%a%=%C%I$O(B, $B%P%$%H%*!<%@$NJQ49$r<+F0$G9T$J$&(B.
122: */
123: public void writeInt(int i) throws IOException{
124: new DataOutputStream(buffer).writeInt(i);
125: }
126:
127: /**
128: * 1 $B%P%$%H$N<u?.$r9T$J$&(B.
129: */
130: public byte readByte() throws IOException{
131: return (byte)is.read();
132: }
133:
134: /**
135: * 32 bit integer $B$N<u?.$r9T$J$&(B.
136: * $B$3$N%a%=%C%I$O(B, $B%P%$%H%*!<%@$NJQ49$r<+F0$G9T$J$&(B.
137: */
138: public int readInt() throws IOException{
139: return new DataInputStream(is).readInt();
140: }
141:
142: /**
143: * OpenXM $B%a%C%;!<%8$NAw?.$r9T$J$&(B.
144: */
145: public void write(OXmessage message) throws IOException,MathcapViolation{
146: buffer.reset();
147: message.write(this);
148: buffer.writeTo(os);
149: os.flush();
150: }
151:
152: /**
153: * OpenXM $B%a%C%;!<%8$NAw?.$r9T$J$&(B.
154: * $B%\%G%#$N$_$G$h$/(B, $B%X%C%@$O<+F0$GIU2C$5$l$k(B.
155: */
156: public void send(OXbody object) throws IOException,MathcapViolation{
157: this.write(new OXmessage(serial++,object));
158: }
159:
160: /**
161: * OXtag $B$KAjEv$9$k(B mathcap $B$rF@$k(B.
162: * mathcap $B$,@_Dj$5$l$F$$$J$1$l$P(B, null $B$,JV$5$l$k(B.
163: */
164: public CMO[] getMathcap(int OXtag) throws IOException,MathcapViolation{
165: if(mathcap != null){
166: CMO[] list =((CMO_LIST)mathcap.getList().getElements()[2]).getElements();
167:
168: for(int i=0;i<list.length;i++){
169: CMO[] datacap = ((CMO_LIST)list[i]).getElements();
170:
171: if(((CMO_INT32)datacap[0]).intValue() == OXtag){
172: return datacap;
173: }
174: }
175: }
176:
177: return null;
178: }
179:
180: /**
181: * OX_SYNC_BALL $B$rAw?.$9$k(B. $B8=:_$OL$<BAu(B.
182: */
183: public void sendOX_SYNC_BALL() throws IOException,MathcapViolation{
184: this.send(null);
185: }
186:
187: /**
188: * OpenXM $B%a%C%;!<%8$r<u?.$9$k(B.
189: */
190: public OXmessage receive() throws IOException{
191: return new OXmessage(this);
192: }
193:
194: /**
195: * mathcap $B$r@_Dj$9$k(B.
196: * $B0J8e(B, $BAw?.$9$k:]$K(B mathcap $B$KE,9g$7$F$$$k$+$I$&$+%A%'%C%/$,9T$J$o$l$k(B.
197: */
198: public void setMathCap(CMO_MATHCAP mathcap){
199: this.mathcap = mathcap;
200: }
201: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>