KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > iiop > StreamMessageWriter


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.iiop;
30
31 import com.caucho.vfs.WriteStream;
32
33 import java.io.IOException JavaDoc;
34
35 public class StreamMessageWriter extends MessageWriter {
36   private WriteStream _out;
37
38   private byte []_buffer;
39   private int _bufferLength;
40   
41   private int _offset;
42   private int _align;
43   private int _length;
44
45   private int _version;
46
47   public StreamMessageWriter()
48   {
49   }
50
51   public StreamMessageWriter(WriteStream out)
52   {
53     init(out);
54   }
55   
56   /**
57    * initialize the writer.
58    */

59   public void init(WriteStream out)
60   {
61     _out = out;
62     _buffer = _out.getBuffer();
63     _bufferLength = _buffer.length;
64     _align = 0;
65     
66     _buffer[0] = 'G';
67     _buffer[1] = 'I';
68     _buffer[2] = 'O';
69     _buffer[3] = 'P';
70   }
71
72   /**
73    * Starts a 1.0 message.
74    */

75   public void start10Message(int type)
76   {
77     _version = 0;
78     
79     _offset = 0;
80     _align = 0;
81     _length = 12;
82
83     _buffer[4] = 1;
84     _buffer[5] = 0;
85     _buffer[6] = 0;
86     _buffer[7] = (byte) type;
87   }
88
89   /**
90    * Starts a 1.1 message.
91    */

92   public void start11Message(int type)
93   {
94     _version = 1;
95     
96     _offset = 0;
97     _length = 12;
98     _align = 0;
99
100     _buffer[4] = 1;
101     _buffer[5] = 1;
102     _buffer[6] = 0;
103     _buffer[7] = (byte) type;
104   }
105
106   /**
107    * Starts a 1.2 message.
108    */

109   public void start12Message(int type, int requestId)
110   {
111     _version = 2;
112     
113     _offset = 4;
114     _length = 16;
115     _align = 0;
116
117     _buffer[4] = 1;
118     _buffer[5] = 2;
119     _buffer[6] = 0;
120     _buffer[7] = (byte) type;
121
122     _buffer[12] = (byte) (requestId >> 24);
123     _buffer[13] = (byte) (requestId >> 16);
124     _buffer[14] = (byte) (requestId >> 8);
125     _buffer[15] = (byte) (requestId);
126   }
127
128   /**
129    * Returns the offset.
130    */

131   public int getOffset()
132   {
133     // flush if nearly full to deal with the IIOP 1.2 message header
134
if (_bufferLength <= _length + 4)
135       flushBuffer();
136
137     return _offset;
138   }
139   
140   /**
141    * Writes a byte.
142    */

143   public void write(int v)
144   {
145     if (_bufferLength <= _length)
146       flushBuffer();
147
148     _offset++;
149     _buffer[_length++] = (byte) v;
150   }
151   
152   /**
153    * Writes data
154    */

155   public void write(byte []buffer, int offset, int length)
156   {
157     while (length > 0) {
158       int sublen = _bufferLength - _length;
159       
160       if (length < sublen)
161     sublen = length;
162
163       System.arraycopy(buffer, offset, _buffer, _length, sublen);
164
165       _offset += sublen;
166       _length += sublen;
167       length -= sublen;
168
169       if (length > 0)
170     flushBuffer();
171     }
172   }
173   
174   /**
175    * Writes a short
176    */

177   public final void writeShort(int v)
178   {
179     if (_bufferLength <= _length + 1)
180       flushBuffer();
181
182     _offset += 2;
183
184     _buffer[_length++] = (byte) (v >> 8);
185     _buffer[_length++] = (byte) (v);
186   }
187   
188   /**
189    * Writes an integer.
190    */

191   public void writeInt(int v)
192   {
193     if (_bufferLength <= _length + 3)
194       flushBuffer();
195
196     _offset += 4;
197     
198     _buffer[_length++] = (byte) (v >> 24);
199     _buffer[_length++] = (byte) (v >> 16);
200     _buffer[_length++] = (byte) (v >> 8);
201     _buffer[_length++] = (byte) (v);
202   }
203   
204   /**
205    * Aligns to a specified value.
206    */

207   public void align(int v)
208   {
209     int delta = v - _length % v;
210
211     if (delta == v)
212       return;
213
214     _offset += delta;
215
216     for (; delta > 0; delta--)
217       _buffer[_length++] = 0;
218   }
219
220   /**
221    * Flushes the buffer.
222    */

223   public void flushBuffer()
224   {
225     try {
226       int size = _length - 12;
227
228       _buffer[6] = 2; // fragmented
229

230       _buffer[8] = (byte) (size >> 24);
231       _buffer[9] = (byte) (size >> 16);
232       _buffer[10] = (byte) (size >> 8);
233       _buffer[11] = (byte) (size >> 0);
234
235       if (_version == 2) {
236     _length += (8 - _length % 8) % 8;
237       }
238
239       _out.setBufferOffset(_length);
240       _out.flushBuffer();
241
242       _buffer[7] = IiopReader.MSG_FRAGMENT;
243
244       if (_version == 2) {
245     _offset += 4;
246     _length = 16;
247       }
248       else
249     _length = 12;
250     } catch (IOException JavaDoc e) {
251       throw new RuntimeException JavaDoc(e);
252     }
253   }
254
255   /**
256    * Completes the response.
257    */

258   public void close()
259     throws IOException JavaDoc
260   {
261     int size = _length - 12;
262
263     _buffer[6] = 0; // last message
264

265     _buffer[8] = (byte) (size >> 24);
266     _buffer[9] = (byte) (size >> 16);
267     _buffer[10] = (byte) (size >> 8);
268     _buffer[11] = (byte) (size >> 0);
269
270     /*
271     if (_version == 2) {
272       int delta = (8 - _length % 8) % 8;
273
274       for (int i = 0; i < delta; i++)
275     _buffer[_length + i] = (byte) 0xaa;
276       
277       _length += delta;
278     }
279     */

280
281     // debugData();
282

283     _out.setBufferOffset(_length);
284     _out.flushBuffer();
285
286     _length = 0;
287   }
288
289   public void debugData()
290   {
291     for (int tail = 0; tail < _length; tail += 16) {
292       for (int j = 0; j < 16; j++) {
293     System.out.print(" ");
294     
295     if (tail + j < _length)
296       printHex(_buffer[tail + j]);
297     else
298       System.out.print(" ");
299       }
300
301       System.out.print(" ");
302       for (int j = 0; j < 16; j++) {
303     if (tail + j < _length)
304       printCh(_buffer[tail + j]);
305     else
306       System.out.print(" ");
307       }
308     
309       System.out.println();
310     }
311     
312     System.out.println();
313   }
314
315   private void printHex(int d)
316   {
317     int ch1 = (d >> 4) & 0xf;
318     int ch2 = d & 0xf;
319
320     if (ch1 >= 10)
321       System.out.print((char) ('a' + ch1 - 10));
322     else
323       System.out.print((char) ('0' + ch1));
324     
325     if (ch2 >= 10)
326       System.out.print((char) ('a' + ch2 - 10));
327     else
328       System.out.print((char) ('0' + ch2));
329   }
330
331   private void printCh(int d)
332   {
333     if (d >= 0x20 && d <= 0x7f)
334       System.out.print("" + ((char) d));
335     else
336       System.out.print(".");
337   }
338
339   private String JavaDoc toCh(int d)
340   {
341     if (d >= 0x20 && d <= 0x7f)
342       return "" + (char) d;
343     else
344       return "" + d;
345   }
346
347   private static String JavaDoc toHex(int v)
348   {
349     String JavaDoc s = "";
350       
351     for (int i = 28; i >= 0; i -= 4) {
352       int h = (v >> i) & 0xf;
353
354       if (h >= 10)
355         s += ((char) ('a' + h - 10));
356       else
357         s += (h);
358     }
359
360     return s;
361   }
362 }
363
Popular Tags