KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > JtInputStream


1
2 package Jt;
3 import java.util.*;
4 import java.lang.reflect.*;
5 import java.beans.*;
6 import java.io.*;
7
8 /**
9   * Handles input streams.
10   */

11
12
13 public class JtInputStream extends JtObject {
14
15
16   InputStream inputStream = null;
17   //boolean createdir = true;
18
byte[] buffer;
19   public final static int BUFFER_SIZE = 1024; // Buffer size (read_file)
20
public final static int MAX_LENGTH = 1024 * 50; // Max length(read_file)
21
int bufferSize = BUFFER_SIZE;
22
23   public JtInputStream() {
24   }
25
26    /**
27     * Specifies the input stream.
28     * @param inputStream input stream
29     */

30
31    public void setInputStream(Object JavaDoc inputStream) {
32     this.inputStream = (InputStream) inputStream;
33    }
34
35    /**
36     * Returns the input stream.
37     */

38
39    public Object JavaDoc getInputStream() {
40     return inputStream;
41    }
42
43   /**
44     * Return the buffer size.
45     */

46
47   public int getBufferSize () {
48     return bufferSize;
49   }
50
51   /**
52     * Sets the buffer size. The default value for this attribute is 1024.
53     * @param bufferSize buffer size
54     */

55
56   public void setBufferSize (int bufferSize) {
57     this.bufferSize = bufferSize;
58   }
59
60
61   // open operation
62

63 /*
64   public void open () {
65
66      try {
67     ostream = new FileOutputStream (name);
68      } catch (Exception e) {
69     handleException (e);
70      }
71   }
72 */

73
74   byte [] getBuffer ()
75   {
76     return (buffer);
77   }
78
79
80   // read_file: read from input stream
81

82   private void read_stream (Object JavaDoc 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       //istream = new FileInputStream (name);
101

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         // send messages to the reply_to object
124

125         if (reply_to != null)
126           this.sendMessage (reply_to, msg);
127       }
128
129       // istream.close (); check
130

131     } catch (Exception JavaDoc e) {
132       handleException (e);
133     }
134
135
136 /*
137     handleTrace ("read_file:");
138     for (i = 0; i < offset; i++)
139       System.out.print (new Character ((char) buffer[i]));
140 */

141     return;
142   }
143
144   // write operation
145

146 /*
147   void write (byte buffer[], int len) {
148
149      if (ostream == null)
150         return;
151
152      try {
153         ostream.write (buffer, 0, len);
154      } catch (Exception e) {
155         handleException (e);
156      }
157
158   }
159 */

160
161   // Destroy operation
162

163 /*
164   public void destroy () {
165      if (ostream != null)
166     close ();
167
168   }
169 */

170   
171   // close operation
172

173   void closeStream () {
174
175      if (inputStream == null)
176        return;
177
178      try {
179        inputStream.close ();
180      } catch (Exception JavaDoc e) {
181        handleException (e);
182      }
183   }
184
185
186   /**
187     * Process object messages.
188    * <ul>
189    * <li> JtREAD - Reads from the input stream, one buffer of data at a time.
190    * Each buffer is sent to the object specified by msgReplyTo (JtMessage).
191    * <li> JtCLOSE - Closes the input stream.
192    * </ul>
193    * @param message Jt Message
194    */

195
196
197   public Object JavaDoc processMessage (Object JavaDoc message) {
198
199    String JavaDoc msgid = null;
200    byte buffer[];
201    //JtBuffer buf;
202
File file;
203    JtMessage e = (JtMessage) message;
204    Object JavaDoc reply_to;
205
206      if (e == null)
207     return null;
208
209      msgid = (String JavaDoc) e.getMsgId ();
210
211      if (msgid == null)
212     return null;
213
214
215      if (msgid.equals ("JtREAD")) {
216         reply_to = (Object JavaDoc) 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   /**
233     * Unit tests the messages processed by JtInputStream.
234     */

235
236   public static void main(String JavaDoc[] 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 JavaDoc e) {
248       e.printStackTrace ();
249     }
250
251     //main.setObjTrace (1);
252
//main.setLogFile ("log.txt");
253

254
255     // Create JtInputStream using test.txt
256

257     main.createObject ("Jt.JtInputStream", "istream");
258     main.setValue ("istream", "inputStream", istream);
259     main.setValue ("istream", "bufferSize", "2048");
260
261
262     // Create output file (output)
263

264     f = (JtObject) main.createObject ("Jt.JtFile", "file");
265     main.setValue ("file", "name", "output");
266
267     main.sendMessage ("file", new JtMessage ("JtOPEN"));
268
269     // Read input stream, one buffer at a time. Send buffers to a file
270

271     msg = new JtMessage ("JtREAD");
272     msg.setMsgReplyTo (f);
273
274     main.sendMessage ("istream", msg);
275     //main.setValue ("message", "msgId", "JtCLOSE");
276
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