1 2 package Jt; 3 import java.util.*; 4 import java.lang.reflect.*; 5 import java.beans.*; 6 import java.io.*; 7 8 11 12 13 public class JtInputStream extends JtObject { 14 15 16 InputStream inputStream = null; 17 byte[] buffer; 19 public final static int BUFFER_SIZE = 1024; public final static int MAX_LENGTH = 1024 * 50; int bufferSize = BUFFER_SIZE; 22 23 public JtInputStream() { 24 } 25 26 30 31 public void setInputStream(Object inputStream) { 32 this.inputStream = (InputStream) inputStream; 33 } 34 35 38 39 public Object getInputStream() { 40 return inputStream; 41 } 42 43 46 47 public int getBufferSize () { 48 return bufferSize; 49 } 50 51 55 56 public void setBufferSize (int bufferSize) { 57 this.bufferSize = bufferSize; 58 } 59 60 61 63 73 74 byte [] getBuffer () 75 { 76 return (buffer); 77 } 78 79 80 82 private void read_stream (Object reply_to) 83 { 84 InputStream istream; 85 File file; 86 int len, i; 87 JtMessage msg; 88 JtBuffer buff = new JtBuffer (); 89 90 if (inputStream == null) 91 return; 92 93 istream = inputStream; 94 95 byte buf [] = new byte [bufferSize]; 96 97 byte[] buffer1; 98 99 try { 100 102 handleTrace ("read_stream:available:" + 103 istream.available ()); 104 105 while ((len = istream.read (buf, 0, bufferSize)) > 0) 106 { 107 108 handleTrace ("read_stream:" + len); 109 buffer1 = new byte [len]; 110 111 i = 0; 112 while (i < len) { 113 buffer1[i] = buf[i]; 114 i++; 115 } 116 117 buff.setBuffer (buffer1); 118 119 msg = new JtMessage (); 120 msg.setMsgId ("JtDATA_BUFFER"); 121 msg.setMsgContent (buff); 122 123 125 if (reply_to != null) 126 this.sendMessage (reply_to, msg); 127 } 128 129 131 } catch (Exception e) { 132 handleException (e); 133 } 134 135 136 141 return; 142 } 143 144 146 160 161 163 170 171 173 void closeStream () { 174 175 if (inputStream == null) 176 return; 177 178 try { 179 inputStream.close (); 180 } catch (Exception e) { 181 handleException (e); 182 } 183 } 184 185 186 195 196 197 public Object processMessage (Object message) { 198 199 String msgid = null; 200 byte buffer[]; 201 File file; 203 JtMessage e = (JtMessage) message; 204 Object reply_to; 205 206 if (e == null) 207 return null; 208 209 msgid = (String ) e.getMsgId (); 210 211 if (msgid == null) 212 return null; 213 214 215 if (msgid.equals ("JtREAD")) { 216 reply_to = (Object ) e.getMsgReplyTo (); 217 read_stream (reply_to); 218 return (null); 219 } 220 221 if (msgid.equals ("JtCLOSE") || msgid.equals ("JtREMOVE")) { 222 closeStream (); 223 return (null); 224 } 225 226 handleError ("JtInputStream.processMessage: invalid message id:" + msgid); 227 return (null); 228 229 } 230 231 232 235 236 public static void main(String [] args) { 237 238 JtObject main = new JtObject (); 239 JtMessage msg; 240 File tmp; 241 JtFile jfile; 242 FileInputStream istream = null; 243 JtObject f; 244 245 try { 246 istream = new FileInputStream ("test.txt"); 247 } catch (Exception e) { 248 e.printStackTrace (); 249 } 250 251 254 255 257 main.createObject ("Jt.JtInputStream", "istream"); 258 main.setValue ("istream", "inputStream", istream); 259 main.setValue ("istream", "bufferSize", "2048"); 260 261 262 264 f = (JtObject) main.createObject ("Jt.JtFile", "file"); 265 main.setValue ("file", "name", "output"); 266 267 main.sendMessage ("file", new JtMessage ("JtOPEN")); 268 269 271 msg = new JtMessage ("JtREAD"); 272 msg.setMsgReplyTo (f); 273 274 main.sendMessage ("istream", msg); 275 main.sendMessage ("file", new JtMessage ("JtCLOSE")); 277 278 main.sendMessage ("istream", new JtMessage ("JtCLOSE")); 279 main.removeObject ("istream"); 280 281 282 283 } 284 285 } 286 287 288
| Popular Tags
|