KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > server > framework > AceInputStream


1 package com.quikj.server.framework;
2
3 import java.io.*;
4
5 public class AceInputStream
6 {
7     public AceInputStream(InputStream stream)
8     {
9         System.out.println("New AceInputStream()");
10         reader = new InputStreamReader(stream);
11     }
12     
13     public int read(char[] b, int offset, int length)
14     throws IOException
15     {
16         System.out.println ("In read() : " + buffer.length());
17         if (buffer.length() > 0) // if there is some data
18
{
19             // copy the buffer
20
char[] buf = buffer.toString().toCharArray();
21             
22             int i = 0;
23             for (i = 0; (i < buf.length) && (i < length); i++)
24             {
25                 b[offset+i] = buf[i];
26             }
27             
28             buffer = new StringBuffer JavaDoc();
29             if (i < buf.length) // the length requested is less than what is there in the buffer
30
{
31                 String JavaDoc remaining = new String JavaDoc(buf, i, buf.length - i);
32                 buffer.append(remaining);
33                 //System.out.println ("In buffer: " + buffer.toString());
34
}
35             
36             System.out.println ("read returning --" + new String JavaDoc(buf, offset, i)
37             + "-- Remaning in buffer --"
38             //+ buffer.toString()
39
+ "--");
40             return i; // return the length
41
}
42         else // nothing in the buffer
43
{
44             char[] buf = new char[1000];
45             int rcvd = reader.read(buf, 0, buf.length);
46             
47             if (rcvd >= 0)
48             {
49                 int i = 0;
50                 for (i = 0; (i < rcvd) && (i < length); i++)
51                 {
52                     b[offset+i] = buf[i];
53                 }
54                 
55                 if (i < rcvd) // the length requested is less than what is there in the buffer
56
{
57                     String JavaDoc remaining = new String JavaDoc(buf, i, rcvd - i);
58                     buffer = new StringBuffer JavaDoc(remaining);
59                     //System.out.println ("In buffer: " + buffer.toString());
60
}
61
62                 System.out.println ("read returning --" + new String JavaDoc(buf, offset, i)
63                 + "-- Remaning in buffer --"
64                 //+ buffer.toString()
65
+ "--");
66                 return i;
67             }
68             else
69             {
70                 streamClosed = true;
71                 return rcvd;
72             }
73         }
74     }
75     
76     public int read(char[] b)
77     throws IOException
78     {
79         return read(b, 0, b.length);
80     }
81     
82     public String JavaDoc readLine()
83     throws IOException
84     {
85         System.out.println ("In readLine() : " + buffer.length());
86         
87         if (buffer.length() > 0) // if there is some remaining data
88
{
89             String JavaDoc to_send = handleReadlineBuffer();
90             
91             if (to_send != null) // if there is already a line in the buffer
92
{
93                 return to_send; // send it
94
}
95             else // if there is no data to send (no CR-LF)
96
{
97                 if (streamClosed == true)
98                 {
99                     // send whatever is in the buffer
100
to_send = buffer.toString();
101                     
102                     buffer = new StringBuffer JavaDoc();
103                     return to_send;
104                 }
105             }
106         }
107         else if (streamClosed == true)
108         {
109             return null;
110         }
111         
112         // read from the stream
113
char[] buf = new char[1000];
114         
115         while (true)
116         {
117             int length_read = reader.read(buf, 0, buf.length);
118             if (length_read < 0) // the stream has been closed
119
{
120                 streamClosed = true;
121                 if (buffer.length() > 0) // there is something in the buffer
122
{
123                     String JavaDoc to_send = handleReadlineBuffer();
124                     if (to_send != null) // if there is already a line in the buffer
125
{
126                         return to_send; // send it
127
}
128                     else
129                     {
130                         // send whatever is in the buffer
131
to_send = buffer.toString();
132                         
133                         buffer = new StringBuffer JavaDoc();
134                         return to_send;
135                     }
136                 }
137                 else
138                 {
139                     return null;
140                 }
141             }
142             else
143             {
144                 buffer.append(buf, 0, length_read);
145                 String JavaDoc to_send = handleReadlineBuffer();
146                 if (to_send != null) // if a line has been read
147
{
148                     return to_send; // send it
149
}
150             }
151         }
152     }
153     
154     private String JavaDoc handleReadlineBuffer()
155     {
156         char[] buf = buffer.toString().toCharArray();
157         int length = buf.length;
158         
159         boolean cr_rcvd = false;
160         for (int i = 0; i < length; i++)
161         {
162             if (buf[i] == '\015')
163             {
164                 cr_rcvd = true;
165             }
166             else if (buf[i] == '\012')
167             {
168                 if (cr_rcvd == true)
169                 {
170                     // CR-LF sequence received
171

172                     i++;
173                     // copy the remaining for next time
174
if (i < length)
175                     {
176                         String JavaDoc remaining = new String JavaDoc(buf, i, length - i);
177                         buffer = new StringBuffer JavaDoc(remaining);
178                         //System.out.println(" In buffer: " + buffer.toString());
179
}
180                     else
181                     {
182                         buffer = new StringBuffer JavaDoc();
183                     }
184                     
185                     String JavaDoc to_send = new String JavaDoc(buf, 0, i - 2);
186                     System.out.println ("handlereadlinebuffer() returning --"
187                     + to_send + "-- Still in buffer --"
188                     //+ buffer.toString()
189
+ "--");
190                     return to_send;
191                 }
192                 else
193                 {
194                     cr_rcvd = false;
195                 }
196             }
197             else
198             {
199                 cr_rcvd = false;
200             }
201         }
202         
203         return null;
204     }
205     
206     public boolean available()
207     {
208         boolean ret = false;
209         
210         if (buffer.length() > 0)
211         {
212             ret = true;
213         }
214         
215         return ret;
216     }
217     
218     public int availableBufferSize()
219     {
220         return buffer.length();
221     }
222     
223     private InputStreamReader reader;
224     private StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
225     private boolean streamClosed = false;
226 }
227
228
229
230
231
232
233
234
235
236
Popular Tags