KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.iiop;
31
32 import com.caucho.log.Log;
33 import com.caucho.iiop.orb.*;
34 import com.caucho.server.util.CauchoSystem;
35 import com.caucho.util.ByteBuffer;
36 import com.caucho.util.CharBuffer;
37 import com.caucho.util.IntArray;
38 import com.caucho.util.L10N;
39 import com.caucho.vfs.ReadStream;
40 import com.caucho.vfs.TempBuffer;
41
42 import org.omg.CORBA.Principal JavaDoc;
43 import org.omg.CORBA.TCKind JavaDoc;
44 import org.omg.CORBA.TypeCode JavaDoc;
45 import org.omg.CORBA.portable.IndirectionException JavaDoc;
46 import org.omg.SendingContext.RunTime JavaDoc;
47
48 import javax.rmi.CORBA.Util JavaDoc;
49 import javax.rmi.CORBA.ValueHandler JavaDoc;
50 import java.io.EOFException JavaDoc;
51 import java.io.IOException JavaDoc;
52 import java.io.Serializable JavaDoc;
53 import java.util.ArrayList JavaDoc;
54 import java.util.HashMap JavaDoc;
55 import java.util.logging.Logger JavaDoc;
56
57 public class IiopReader extends org.omg.CORBA_2_3.portable.InputStream JavaDoc {
58   protected static final L10N L = new L10N(IiopReader.class);
59   protected static final Logger JavaDoc log = Log.open(IiopReader.class);
60   
61   public static final int MSG_REQUEST = 0;
62   public static final int MSG_REPLY = 1;
63   public static final int MSG_CANCEL_REQUEST = 2;
64   public static final int MSG_LOCATE_REQUEST = 3;
65   public static final int MSG_LOCATE_REPLY = 4;
66   public static final int MSG_CLOSE_CONNECTION = 5;
67   public static final int MSG_ERROR = 6;
68   public static final int MSG_FRAGMENT = 7;
69   
70   public static final int SERVICE_TRANSACTION = 0;
71   public static final int SERVICE_CODE_SET = 1;
72   public static final int SERVICE_CHAIN_BYPASS_CHECK = 2;
73   public static final int SERVICE_CHAIN_BYPASS_INFO = 3;
74   public static final int SERVICE_LOGICAL_THREAD_ID = 4;
75   public static final int SERVICE_BI_DIR_IIOP = 5;
76   public static final int SERVICE_SENDING_CONTEXT_RUN_TIME = 6;
77   public static final int SERVICE_INVOCATION_POLICIES = 7;
78   public static final int SERVICE_FORWARDED_IDENTITY = 8;
79   public static final int SERVICE_UNKNOWN_EXCEPTION_INFO = 9;
80
81   public static final int STATUS_NO_EXCEPTION = 0;
82   public static final int STATUS_USER_EXCEPTION = 1;
83   public static final int STATUS_SYSTEM_EXCEPTION = 2;
84   public static final int STATUS_LOCATION_FORWARD = 3;
85   
86   private ReadStream _rs;
87   private byte []_header = new byte[8];
88   private byte []buf = new byte[16];
89
90   private IntArray _refOffsets = new IntArray();
91   private ArrayList JavaDoc<String JavaDoc> _refIds = new ArrayList JavaDoc<String JavaDoc>();
92   private ArrayList JavaDoc<Class JavaDoc> _refClasses = new ArrayList JavaDoc<Class JavaDoc>();
93   private ArrayList JavaDoc<Serializable JavaDoc> _refValues = new ArrayList JavaDoc<Serializable JavaDoc>();
94
95   private HashMap JavaDoc<Integer JavaDoc,String JavaDoc> _savedStrings = new HashMap JavaDoc<Integer JavaDoc,String JavaDoc>();
96   private int _major;
97   private int _minor;
98   private boolean _isBigEndian;
99   private boolean _hasMoreFragments;
100   private int _flags;
101
102   private TempBuffer _tempBuffer;
103   
104   private byte []_buffer;
105   private int _offset;
106   private int _length;
107   
108   private int _type;
109   
110   private int _fragmentOffset;
111   
112   private int _chunkEnd = -1;
113   private int _chunkDepth = 0;
114
115   private int requestId;
116   private boolean responseExpected;
117   private ByteBuffer objectKey = new ByteBuffer();
118   private CharBuffer _operation = new CharBuffer();
119   private ByteBuffer principal = new ByteBuffer();
120
121   private CharBuffer _cb = new CharBuffer();
122   
123   private ValueHandler JavaDoc _valueHandler = Util.createValueHandler();
124   private RunTime JavaDoc runTime = _valueHandler.getRunTimeCodeBase();
125
126   private ORBImpl _orb;
127
128   public IiopReader()
129   {
130   }
131
132   public IiopReader(ReadStream rs)
133   {
134     init(rs);
135   }
136
137   public void setOrb(ORBImpl orb)
138   {
139     _orb = orb;
140   }
141
142   /**
143    * Initialize the reader with a new underlying stream.
144    *
145    * @param rs the underlying input stream.
146    */

147   public void init(ReadStream rs)
148   {
149     _rs = rs;
150     _major = 0;
151     _minor = 0;
152     _type = 0;
153     requestId = 0;
154     objectKey.clear();
155     _operation.clear();
156     _offset = 0;
157     _length = 0;
158     _fragmentOffset = 0;
159
160     _refOffsets.clear();
161     _refIds.clear();
162     _refClasses.clear();
163     _refValues.clear();
164   }
165
166   public int getMajorVersion()
167   {
168     return _major;
169   }
170
171   public int getMinorVersion()
172   {
173     return _minor;
174   }
175
176   public int getRequestType()
177   {
178     return _type;
179   }
180
181   public int getRequestId()
182   {
183     return requestId;
184   }
185
186   public boolean isBigEndian()
187   {
188     return _isBigEndian;
189   }
190
191   public int getOffset()
192   {
193     return _offset;
194   }
195
196   public boolean isResponseExpected()
197   {
198     return responseExpected;
199   }
200
201   public ByteBuffer getObjectKey()
202   {
203     return objectKey;
204   }
205
206   public CharBuffer getOperation()
207   {
208     return _operation;
209   }
210
211   public void readRequest()
212     throws IOException JavaDoc
213   {
214     if (_tempBuffer == null) {
215       _tempBuffer = TempBuffer.allocate();
216       _buffer = _tempBuffer.getBuffer();
217     }
218     
219     int len = _rs.readAll(_header, 0, _header.length);
220
221     if (_header[0] != 'G' ||
222         _header[1] != 'I' ||
223         _header[2] != 'O' ||
224         _header[3] != 'P') {
225       throw new IOException JavaDoc(L.l("unknown request {0}, {1}, {2}, {3}",
226                 "" + toCh(_header[0]),
227                 "" + toCh(_header[1]),
228                 "" + toCh(_header[2]),
229                 "" + toCh(_header[3])));
230     }
231
232     _major = _header[4];
233     _minor = _header[5];
234
235     if (_major != 1)
236       throw new IOException JavaDoc("unknown major");
237
238     _flags = _header[6];
239     _isBigEndian = (_flags & 1) == 0;
240     _hasMoreFragments = (_flags & 2) == 2;
241     
242     _type = _header[7];
243
244     _rs.readAll(_buffer, 0, 4);
245
246     // ejb/1161
247
_length = readInt(_buffer, 0) + 4;
248     _offset = 4;
249
250     // XXX: ignoring issues with overruns
251
_rs.readAll(_buffer, 4, _length - 4);
252
253     // debug
254
//System.out.println("---");
255
//writeHexGroup(_buffer, 0, _length);
256

257     if (_minor == 0) {
258       switch (_type) {
259       case MSG_REQUEST:
260         readRequest10();
261         break;
262       case MSG_REPLY:
263         readReply10();
264         break;
265       case MSG_ERROR:
266         throw new RuntimeException JavaDoc("MSG_ERROR: unknown protocol error");
267       default:
268         throw new RuntimeException JavaDoc();
269       }
270     }
271     else if (_minor == 1) {
272       switch (_type) {
273       case MSG_REQUEST:
274         readRequest10();
275         break;
276       case MSG_REPLY:
277         readReply10();
278         break;
279       case MSG_ERROR:
280         throw new RuntimeException JavaDoc("MSG_ERROR: unknown protocol error");
281       default:
282         throw new RuntimeException JavaDoc();
283       }
284     }
285     else if (_minor == 2) {
286       switch (_type) {
287       case MSG_REQUEST:
288         readRequest12();
289         break;
290       case MSG_REPLY:
291         readReply12();
292         break;
293       case MSG_ERROR:
294         throw new RuntimeException JavaDoc("MSG_ERROR: unknown protocol error");
295       default:
296         throw new RuntimeException JavaDoc("unknown type: " + _type);
297       }
298     }
299     else
300       throw new IOException JavaDoc("unknown minor");
301   }
302
303   private void readRequest10()
304     throws IOException JavaDoc
305   {
306     readServiceContextList();
307     requestId = readInt();
308     responseExpected = read_octet() != 0;
309     
310     readOctetSequence(objectKey);
311
312     readString(_operation);
313     
314     readOctetSequence(principal);
315   }
316
317   private void readReply10()
318     throws IOException JavaDoc
319   {
320     readServiceContextList();
321
322     int requestId = readInt();
323     int status = readInt();
324
325     switch (status) {
326     case STATUS_NO_EXCEPTION:
327       //debugTail();
328
return;
329         
330     case STATUS_SYSTEM_EXCEPTION:
331       String JavaDoc exceptionId = readString();
332       int minorStatus = readInt();
333       int completionStatus = readInt();
334       throw new IOException JavaDoc("exception: " + exceptionId);
335         
336     case STATUS_USER_EXCEPTION:
337       Object JavaDoc value = read_value();
338       
339       throw new IOException JavaDoc("user exception: " + value);
340         
341     default:
342       throw new IOException JavaDoc("unknown status: " + status);
343     }
344   }
345
346   private void readRequest12()
347     throws IOException JavaDoc
348   {
349     requestId = readInt();
350     int flags = read_octet();
351     responseExpected = flags != 0;
352
353     int disposition = read_long();
354     readOctetSequence(objectKey);
355     readString(_operation);
356
357     readServiceContextList();
358     
359     int frag = _offset % 8;
360     if (frag > 0 && frag < 8) {
361       int delta = 8 - frag;
362
363       if (_length < _offset + delta)
364     delta = _length - _offset;
365
366       if (delta > 0) {
367     _offset += delta;
368       }
369     }
370   }
371
372   private void readReply12()
373     throws IOException JavaDoc
374   {
375     int requestId = readInt();
376     int status = readInt();
377     readServiceContextList();
378
379     switch (status) {
380     case STATUS_NO_EXCEPTION:
381       //debugTail();
382
return;
383         
384     case STATUS_SYSTEM_EXCEPTION:
385       String JavaDoc exceptionId = readString();
386       int minorStatus = readInt();
387       int completionStatus = readInt();
388       throw new IOException JavaDoc("exception: " + exceptionId);
389         
390     case STATUS_USER_EXCEPTION:
391       Object JavaDoc value = read_value();
392
393       if (value instanceof RuntimeException JavaDoc)
394     throw (RuntimeException JavaDoc) value;
395
396       if (value != null)
397     throw new IOException JavaDoc("user exception: " + value + " " + value.getClass().getName());
398       else
399     throw new IOException JavaDoc("user exception: " + value + " " + value.getClass().getName());
400         
401     default:
402       throw new IOException JavaDoc("unknown status: " + status);
403     }
404   }
405
406   private void readServiceContextList()
407     throws IOException JavaDoc
408   {
409     int length = readInt();
410
411     for (int i = 0; i < length; i++) {
412       int serviceId = readInt();
413       int dataLength = readInt();
414
415       if (serviceId == SERVICE_CODE_SET) {
416         int endian = read_octet();
417         int charSet = readInt();
418         int wcharSet = readInt();
419       }
420       else {
421         skip(dataLength);
422       }
423     }
424   }
425
426   private void debugTail()
427     throws IOException JavaDoc
428   {
429     int len = _length;
430     for (int i = 0; i < len; i += 8) {
431       int sublen = _rs.read(buf, 0, 16);
432
433       for (int j = 0; j < 16; j++) {
434         if (j < sublen)
435           printHex(buf[j]);
436         else
437           System.out.print(" ");
438
439         if (j == 7)
440           System.out.print(" - ");
441         else
442           System.out.print(" ");
443       }
444       
445       for (int j = 0; j < 16; j++) {
446         int ch = buf[j] & 0xff;
447         
448         if (j >= sublen)
449           System.out.print("?");
450         else if (ch >= 0x20 && ch < 0x80)
451           System.out.print((char) ch);
452         else
453           System.out.print("?");
454       }
455         
456       System.out.println();
457     }
458     System.out.println();
459   }
460
461   public IOR readIOR()
462     throws IOException JavaDoc
463   {
464     IOR ior = new IOR();
465
466     return ior.read(this);
467   }
468     
469   public Object JavaDoc readObject(Class JavaDoc cl)
470     throws IOException JavaDoc
471   {
472     IOR ior = readIOR();
473     return null;
474   }
475
476   public String JavaDoc readWideString()
477     throws IOException JavaDoc
478   {
479     // XXX: assume ucs-16
480
CharBuffer cb = CharBuffer.allocate();
481     int len = readInt();
482     for (; len > 0; len--) {
483       cb.append((char) read_short());
484     }
485
486     return cb.close();
487   }
488
489   public Serializable JavaDoc read_value()
490   {
491     return read_value((Class JavaDoc) null);
492   }
493     
494   public Serializable JavaDoc read_value(Class JavaDoc type)
495   {
496     try {
497       // ejb/114o tests for chunking
498
int oldChunkEnd = _chunkEnd;
499       
500       // writeHexGroup(16);
501

502       _chunkEnd = -1;
503       align4();
504       int startOffset = _offset - _fragmentOffset;
505       
506       int code = read_long();
507
508       String JavaDoc repId = "";
509       boolean isChunked = false;
510       Serializable JavaDoc value = null;
511
512       if (code == 0)
513     return null;
514       else if (code == 0xffffffff) {
515     _chunkEnd = oldChunkEnd;
516     
517     int start = _offset - _fragmentOffset;
518     int delta = read_long();
519     int target = start + delta;
520     
521     log.fine("INDIRECT:" + delta);
522
523     for (int i = 0; i < _refOffsets.size(); i++) {
524       int refOffset = _refOffsets.get(i);
525
526       if (refOffset == target)
527         return _refValues.get(i);
528     }
529
530     throw new IndirectionException JavaDoc(target);
531       }
532       else if ((code & 0x7fffff00) != 0x7fffff00) {
533     repId = readString(code);
534       }
535       else {
536     isChunked = (code & 8) == 8;
537     boolean hasCodeBase = (code & 1) == 1;
538     int repository = (code & 6);
539
540     log.fine("CHUNKED:");
541       
542     if (hasCodeBase) {
543       readCodeBase();
544     }
545
546     if (repository == 2) {
547       repId = read_string();
548     }
549     else {
550       throw new RuntimeException JavaDoc("Can't cope with repository=" + repository);
551     }
552       }
553
554       try {
555     if (isChunked) {
556       // writeHexGroup(16);
557

558       int chunkLength = readInt();
559
560       _chunkEnd = chunkLength + _offset;
561       _chunkDepth++;
562     }
563
564     // XXX: assume ucs-16
565
if (repId.equals("IDL:omg.org/CORBA/WStringValue:1.0")) {
566       value = read_wstring();
567     }
568     else if (! repId.startsWith("RMI:") && ! repId.startsWith("IDL:")) {
569       log.warning("unknown rep: " + repId + " " + Integer.toHexString(code));
570       throw new UnsupportedOperationException JavaDoc("problem parsing");
571     }
572     else {
573       int p = repId.indexOf(':', 4);
574       if (p < 0)
575         throw new RuntimeException JavaDoc("unknown RMI: " + repId);
576
577       String JavaDoc className = repId.substring(4, p);
578       log.fine("CLASS-NAME: " + className);
579       if (className.equals("javax.rmi.CORBA.ClassDesc")) {
580         return readClass();
581       }
582       else {
583         Class JavaDoc cl = null;
584         
585         try {
586           cl = CauchoSystem.loadClass(className);
587         } catch (ClassNotFoundException JavaDoc e) {
588           e.printStackTrace();
589           throw new RuntimeException JavaDoc(e);
590         }
591
592         int refIndex = _refOffsets.size();
593
594         value = _valueHandler.readValue(this, startOffset,
595                         cl, repId, runTime);
596       }
597     }
598
599     _refOffsets.add(startOffset);
600     _refValues.add(value);
601
602     return value;
603       } finally {
604     if (_chunkDepth > 0) {
605       _chunkDepth--;
606
607       int delta = _chunkEnd - getOffset();
608       _chunkEnd = -1;
609       
610       if (delta > 0) {
611         skip(delta);
612       }
613       
614       int newChunk = readInt();
615
616       if (newChunk >= 0)
617         throw new IllegalStateException JavaDoc(L.l("{0}: expected end of chunk {1}",
618                         getOffset(), newChunk));
619       
620       _chunkDepth = - (newChunk + 1);
621
622       if (_chunkDepth > 0) {
623         newChunk = readInt();
624         //System.out.println("REDO:" + newChunk + " D:" + _chunkDepth);
625
_chunkEnd = _offset + newChunk;
626       }
627     }
628       }
629     } catch (IOException JavaDoc e) {
630       throw new RuntimeException JavaDoc(e);
631     }
632   }
633
634   private Class JavaDoc readClass()
635     throws IOException JavaDoc
636   {
637     String JavaDoc codebase = (String JavaDoc) read_value(String JavaDoc.class);
638     String JavaDoc repId = (String JavaDoc) read_value(String JavaDoc.class);
639
640     log.fine("CODE: " + codebase);
641     log.fine("REP-ID: " + repId);
642
643     if (codebase != null && codebase.startsWith("RMI:")) {
644       String JavaDoc temp = repId;
645       repId = codebase;
646       codebase = temp;
647     }
648
649     return loadClass(repId);
650   }
651
652   private Class JavaDoc loadClass(String JavaDoc repId)
653     throws RuntimeException JavaDoc
654   {
655     if (! repId.startsWith("RMI:"))
656       throw new RuntimeException JavaDoc("unknown RMI: " + repId);
657
658     int p = repId.indexOf(':', 4);
659     if (p < 0)
660       throw new RuntimeException JavaDoc("unknown RMI: " + repId);
661
662     String JavaDoc className = repId.substring(4, p);
663
664     if ("javax.rmi.CORBA.ClassDesc".equals(className))
665       return Class JavaDoc.class;
666       
667     Class JavaDoc cl = null;
668
669     try {
670       Thread JavaDoc thread = Thread.currentThread();
671     
672       return Class.forName(className, false, thread.getContextClassLoader());
673     } catch (ClassNotFoundException JavaDoc e) {
674       throw new RuntimeException JavaDoc(e);
675     }
676   }
677
678   private String JavaDoc readCodeBase()
679   {
680     String JavaDoc codeBase = read_string();
681     return codeBase;
682   }
683
684   public Object JavaDoc read_fault()
685   {
686     int startOffset = _offset;
687     int originalOffset = _rs.getOffset();
688       
689     String JavaDoc repId = read_string();
690         
691     // XXX: assume ucs-16
692
if (repId.equals("IDL:omg.org/CORBA/WStringValue:1.0"))
693       return read_wstring();
694
695     Class JavaDoc cl = null;
696     
697     if (repId.startsWith("RMI:")) {
698       int p = repId.indexOf(':', 4);
699       if (p < 0)
700         throw new RuntimeException JavaDoc("unknown RMI: " + repId);
701
702       String JavaDoc className = repId.substring(4, p);
703
704       try {
705         cl = CauchoSystem.loadClass(className);
706       } catch (ClassNotFoundException JavaDoc e) {
707         throw new RuntimeException JavaDoc(e);
708       }
709       
710       return _valueHandler.readValue(this, _offset, cl, repId, runTime);
711     }
712
713     String JavaDoc className = null;
714
715     if (repId.startsWith("IDL:")) {
716       String JavaDoc tail = repId.substring(4);
717       int p = tail.indexOf(':');
718       if (p > 0)
719         tail = tail.substring(0, p);
720
721       if (tail.startsWith("omg.org/"))
722         tail = "org.omg." + tail.substring("omg.org/".length());
723
724       className = tail.replace('/', '.');
725     }
726     else
727       className = repId;
728     
729     String JavaDoc handler = className + "Handler";
730
731     Class JavaDoc handlerClass = null;
732     try {
733       cl = CauchoSystem.loadClass(className);
734       handlerClass = CauchoSystem.loadClass(handler);
735     } catch (ClassNotFoundException JavaDoc e) {
736     }
737
738     if (cl == null) {
739       int p = className.lastIndexOf('.');
740       className = className.substring(0, p) + "Package" + className.substring(p);
741       handler = className + "Helper";
742
743       try {
744     cl = CauchoSystem.loadClass(className);
745     handlerClass = CauchoSystem.loadClass(handler);
746       } catch (ClassNotFoundException JavaDoc e) {
747       }
748     }
749
750     if (cl != null && handlerClass != null) {
751       java.lang.reflect.Method JavaDoc readHelper = null;
752         
753       try {
754     readHelper = handlerClass.getMethod("read", new Class JavaDoc[] {
755       org.omg.CORBA.portable.InputStream JavaDoc.class
756     });
757       } catch (Exception JavaDoc e) {
758       }
759
760       if (readHelper != null) {
761     try {
762       _offset = startOffset;
763       _rs.setOffset(originalOffset);
764             
765       return readHelper.invoke(null, new Object JavaDoc[] { this });
766     } catch (java.lang.reflect.InvocationTargetException JavaDoc e) {
767       e.printStackTrace();
768     } catch (Exception JavaDoc e) {
769       throw new RuntimeException JavaDoc(String.valueOf(e));
770     }
771       }
772     }
773
774     return new IOException JavaDoc("unknown fault: " + repId);
775   }
776
777   /**
778    * Reads a boolean from the input stream.
779    */

780   public boolean read_boolean()
781   {
782     try {
783       return read() != 0;
784     } catch (IOException JavaDoc e) {
785       throw new RuntimeException JavaDoc(String.valueOf(e));
786     }
787   }
788
789   /**
790    * Reads an 8-bit char from the input stream.
791    */

792   public char read_char()
793   {
794     try {
795       return (char) read();
796     } catch (IOException JavaDoc e) {
797       throw new RuntimeException JavaDoc(String.valueOf(e));
798     }
799   }
800
801   /**
802    * Reads an 16-bit char from the input stream.
803    */

804   public char read_wchar()
805   {
806     try {
807       if (_minor == 2) {
808     read_octet();
809
810     int v = (256 * (read() & 0xff) + (read() & 0xff));
811       
812     return (char) v;
813       }
814       else
815     return (char) read_short();
816     } catch (IOException JavaDoc e) {
817       throw new RuntimeException JavaDoc(e);
818     }
819   }
820   
821   /**
822    * Reads an 16-bit short from the input stream.
823    */

824   public short read_ushort()
825   {
826     return read_short();
827   }
828   
829   /**
830    * Reads a 32-bit integer from the input stream.
831    */

832   public int read_long()
833   {
834     try {
835       int v = readInt();
836
837       return v;
838     } catch (IOException JavaDoc e) {
839       throw new RuntimeException JavaDoc(String.valueOf(e));
840     }
841   }
842   
843   /**
844    * Reads a 32-bit integer from the input stream.
845    */

846   public int read_ulong()
847   {
848     return read_long();
849   }
850   
851   /**
852    * Reads a 64-bit integer from the input stream.
853    */

854   public long read_longlong()
855   {
856     try {
857       return readLong();
858     } catch (IOException JavaDoc e) {
859       throw new RuntimeException JavaDoc(String.valueOf(e));
860     }
861   }
862   
863   /**
864    * Reads a 64-bit integer from the input stream.
865    */

866   public long read_ulonglong()
867   {
868     return read_longlong();
869   }
870
871   /**
872    * Reads an 8-bit byte from the input stream.
873    */

874   public byte read_octet()
875   {
876     try {
877       return (byte) read();
878     } catch (IOException JavaDoc e) {
879       throw new RuntimeException JavaDoc(String.valueOf(e));
880     }
881   }
882   
883   /**
884    * Reads a 32-bit float from the input stream.
885    */

886   public float read_float()
887   {
888     int v = read_long();
889
890     return Float.intBitsToFloat(v);
891   }
892   
893   /**
894    * Reads a 64-bit double from the input stream.
895    */

896   public double read_double()
897   {
898     long v = read_longlong();
899
900     return Double.longBitsToDouble(v);
901   }
902
903
904   /**
905    * Reads a boolean array from the input stream.
906    */

907   public void read_boolean_array(boolean []v, int offset, int length)
908   {
909     for (int i = 0; i < length; i++)
910       v[i + offset] = read_boolean();
911   }
912
913   /**
914    * Reads a char array from the input stream.
915    */

916   public void read_char_array(char []v, int offset, int length)
917   {
918     for (int i = 0; i < length; i++)
919       v[i + offset] = read_char();
920   }
921
922   /**
923    * Reads a string from the input stream.
924    */

925   public String JavaDoc read_string()
926   {
927     CharBuffer cb = _cb;
928     cb.clear();
929
930     int len = read_long();
931
932     if (len < 0) {
933       // ejb/114o
934

935       int delta = read_long();
936       int offset = _offset + delta - 4;
937
938       len = readInt(_buffer, offset);
939
940       offset += 4;
941
942       for (int i = 0; i < len - 1; i++) {
943     cb.append((char) _buffer[offset + i]);
944       }
945
946       return cb.toString();
947     }
948
949     for (int i = 0; i < len - 1; i++)
950       cb.append(read_char());
951
952     int v = read_octet(); // null
953

954     return cb.toString();
955   }
956
957   /**
958    * Reads a string from the input stream.
959    */

960   public String JavaDoc readString(int len)
961   {
962     CharBuffer cb = _cb;
963     cb.clear();
964
965     for (int i = 0; i < len - 1; i++)
966       cb.append(read_char());
967
968     read_octet(); // null
969

970     return cb.toString();
971   }
972
973   /**
974    * Reads a wchar array from the input stream.
975    */

976   public void read_wchar_array(char []v, int offset, int length)
977   {
978     for (int i = 0; i < length; i++) {
979       v[i + offset] = read_wchar();
980     }
981   }
982
983   /**
984    * Reads a string from the input stream.
985    */

986   public String JavaDoc read_wstring()
987   {
988     return read_wstring(read_long());
989   }
990
991   /**
992    * Reads a string from the input stream.
993    */

994   public String JavaDoc read_wstring(int len)
995   {
996     CharBuffer cb = _cb;
997     cb.clear();
998
999     if (_minor == 2) {
1000      for (; len > 1; len -= 2) {
1001        char ch = (char) read_short();
1002        cb.append(ch);
1003      }
1004    }
1005    else {
1006      for (int i = 0; i < len - 1; i++) {
1007        char ch = (char) read_short();
1008        cb.append(ch);
1009      }
1010      read_short();
1011    }
1012
1013    return cb.toString();
1014  }
1015
1016  /**
1017   * Reads a byte array from the input stream.
1018   */

1019  public void read_octet_array(byte []v, int offset, int length)
1020  {
1021    for (int i = 0; i < length; i++)
1022      v[i + offset] = read_octet();
1023  }
1024
1025  /**
1026   * Reads a short array from the input stream.
1027   */

1028  public void read_short_array(short []v, int offset, int length)
1029  {
1030    for (int i = 0; i < length; i++)
1031      v[i + offset] = read_short();
1032  }
1033
1034  /**
1035   * Reads a ushort array from the input stream.
1036   */

1037  public void read_ushort_array(short []v, int offset, int length)
1038  {
1039    for (int i = 0; i < length; i++)
1040      v[i + offset] = read_ushort();
1041  }
1042
1043  /**
1044   * Reads an int array from the input stream.
1045   */

1046  public void read_long_array(int []v, int offset, int length)
1047  {
1048    try {
1049      align4();
1050      for (int i = 0; i < length; i++)
1051    v[i + offset] = read_long();
1052    } catch (IOException JavaDoc e) {
1053      throw new RuntimeException JavaDoc(e);
1054    }
1055  }
1056
1057  /**
1058   * Reads an int array from the input stream.
1059   */

1060  public void read_ulong_array(int []v, int offset, int length)
1061  {
1062    for (int i = 0; i < length; i++)
1063      v[i + offset] = read_ulong();
1064  }
1065
1066  /**
1067   * Reads a long array from the input stream.
1068   */

1069  public void read_longlong_array(long []v, int offset, int length)
1070  {
1071    for (int i = 0; i < length; i++)
1072      v[i + offset] = read_longlong();
1073  }
1074
1075  /**
1076   * Reads a long array from the input stream.
1077   */

1078  public void read_ulonglong_array(long []v, int offset, int length)
1079  {
1080    for (int i = 0; i < length; i++)
1081      v[i + offset] = read_ulonglong();
1082  }
1083
1084  /**
1085   * Reads a float array from the input stream.
1086   */

1087  public void read_float_array(float []v, int offset, int length)
1088  {
1089    for (int i = 0; i < length; i++)
1090      v[i + offset] = read_float();
1091  }
1092
1093  /**
1094   * Reads a double array from the input stream.
1095   */

1096  public void read_double_array(double []v, int offset, int length)
1097  {
1098    try {
1099      align8();
1100      for (int i = 0; i < length; i++) {
1101    v[i + offset] = read_double();
1102      }
1103    } catch (IOException JavaDoc e) {
1104      throw new RuntimeException JavaDoc(e);
1105    }
1106  }
1107
1108  /**
1109   * Reads a CORBA object from the input stream.
1110   */

1111  public Object JavaDoc read_abstract_interface()
1112  {
1113    boolean discriminator = read_boolean();
1114
1115    if (discriminator) {
1116      return read_Object();
1117    }
1118    else
1119      return read_value();
1120  }
1121
1122  /**
1123   * Reads a CORBA object from the input stream.
1124   */

1125  public org.omg.CORBA.Object JavaDoc read_Object()
1126  {
1127    try {
1128      IOR ior = readIOR();
1129      
1130      System.out.println("RO: " + _orb + " " + ior);
1131      if (_orb != null)
1132    return new StubImpl(_orb, ior);
1133      else
1134    return new DummyObjectImpl(ior);
1135    } catch (IOException JavaDoc e) {
1136      throw new RuntimeException JavaDoc(e);
1137    }
1138  }
1139
1140  /**
1141   * Reads a CORBA object from the input stream.
1142   */

1143  public org.omg.CORBA.TypeCode JavaDoc read_TypeCode()
1144  {
1145    int kind = read_long();
1146
1147    try {
1148    switch (kind) {
1149    case TCKind._tk_null:
1150      return TypeCodeImpl.TK_NULL;
1151      
1152    case TCKind._tk_long:
1153      return TypeCodeImpl.TK_LONG;
1154      
1155    case TCKind._tk_octet:
1156      return TypeCodeImpl.TK_OCTET;
1157      
1158    case TCKind._tk_string:
1159      {
1160    TypeCodeImpl typeCode = new TypeCodeImpl(TCKind.tk_string);
1161    typeCode.setLength(read_ulong());
1162    
1163    return typeCode;
1164      }
1165      
1166    case TCKind._tk_wstring:
1167      {
1168    TypeCodeImpl typeCode = new TypeCodeImpl(TCKind.tk_wstring);
1169    typeCode.setLength(read_ulong());
1170    
1171    return typeCode;
1172      }
1173      
1174    case TCKind._tk_sequence:
1175      {
1176    TypeCodeImpl typeCode = TypeCodeImpl.createSequence();
1177    typeCode.setContentType(read_TypeCode());
1178    typeCode.setLength(read_ulong());
1179    
1180    return typeCode;
1181      }
1182      
1183    case TCKind._tk_value:
1184      {
1185    String JavaDoc id = read_string();
1186    String JavaDoc name = read_string();
1187    short modifier = read_short();
1188    TypeCodeImpl typeCode = TypeCodeImpl.createValue(id, name, modifier);
1189    TypeCode JavaDoc base = read_TypeCode();
1190    typeCode.setConcreteBaseType(base);
1191    int count = read_ulong();
1192    for (int i = 0; i < count; i++) {
1193      typeCode.addMember(read_string(), read_TypeCode(), read_short());
1194    }
1195    return typeCode;
1196      }
1197      
1198    case TCKind._tk_value_box:
1199      {
1200    String JavaDoc id = read_string();
1201    String JavaDoc name = read_string();
1202    TypeCodeImpl typeCode = TypeCodeImpl.createValueBox(id, name);
1203    TypeCode JavaDoc contentType = read_TypeCode();
1204    typeCode.setContentType(contentType);
1205    return typeCode;
1206      }
1207      
1208    case TCKind._tk_abstract_interface:
1209      {
1210    String JavaDoc id = read_string();
1211    String JavaDoc name = read_string();
1212    TypeCodeImpl typeCode = TypeCodeImpl.createAbstractInterface(id, name);
1213
1214    return typeCode;
1215      }
1216    
1217    default:
1218      throw new UnsupportedOperationException JavaDoc("unknown typecode kind: " + kind);
1219    }
1220    } finally {
1221      //System.out.println("DONE:" + kind);
1222
}
1223  }
1224
1225  /**
1226   * Reads a CORBA object from the input stream.
1227   */

1228  public org.omg.CORBA.Any JavaDoc read_any()
1229  {
1230    try {
1231      TypeCode JavaDoc typeCode = read_TypeCode();
1232
1233      AnyImpl any = new AnyImpl();
1234
1235      any.read_value(this, typeCode);
1236
1237      return any;
1238    } catch (RuntimeException JavaDoc e) {
1239      throw e;
1240    } catch (Exception JavaDoc e) {
1241      throw new RuntimeException JavaDoc(e);
1242    }
1243  }
1244
1245  /**
1246   * Reads a CORBA object from the input stream.
1247   */

1248  public Principal JavaDoc read_Principal()
1249  {
1250    throw new UnsupportedOperationException JavaDoc();
1251  }
1252
1253  public int read_sequence_length()
1254  {
1255    int length = read_long();
1256    
1257    if (length < 0 || length > 65536)
1258      throw new RuntimeException JavaDoc("sequence too long:" + length);
1259    
1260    return length;
1261  }
1262
1263  public void readOctetSequence(ByteBuffer bb)
1264    throws IOException JavaDoc
1265  {
1266    int len = readInt();
1267    
1268    if (len > 65536)
1269      throw new IOException JavaDoc("too large chunk " + len);
1270    
1271    bb.ensureCapacity(len);
1272    readBytes(bb.getBuffer(), 0, len);
1273    bb.setLength(len);
1274  }
1275
1276  public byte []readBytes()
1277    throws IOException JavaDoc
1278  {
1279    int len = readInt();
1280
1281    if (len > 65536)
1282      throw new IOException JavaDoc("too large chunk " + len);
1283
1284    byte []buf = new byte[len];
1285    System.arraycopy(_buffer, _offset, buf, 0, len);
1286    _offset += len;
1287
1288    return buf;
1289  }
1290
1291  public void readBytes(byte []buf, int off, int len)
1292    throws IOException JavaDoc
1293  {
1294    System.arraycopy(_buffer, _offset, buf, off, len);
1295
1296    _offset += len;
1297  }
1298
1299  public String JavaDoc readString()
1300    throws IOException JavaDoc
1301  {
1302    int len = readInt();
1303
1304    if (len > 65536)
1305      throw new IOException JavaDoc("too large chunk " + len);
1306    
1307    CharBuffer cb = _cb;
1308    cb.clear();
1309    for (int i = 0; i < len - 1; i++) {
1310      int ch = read_octet();
1311      cb.append((char) ch);
1312    }
1313    int ch = read_octet();
1314
1315    return cb.toString();
1316  }
1317
1318  public void readString(CharBuffer cb)
1319    throws IOException JavaDoc
1320  {
1321    int len = readInt();
1322
1323    if (len > 65536)
1324      throw new IOException JavaDoc("too large chunk " + len);
1325
1326    cb.clear();
1327    for (int i = 0; i < len - 1; i++) {
1328      int ch = read();
1329      cb.append((char) ch);
1330    }
1331    
1332    int ch = read();
1333  }
1334  
1335  /**
1336   * Reads an 16-bit short from the input stream.
1337   */

1338  public short read_short()
1339  {
1340    try {
1341      if ((_offset & 1) == 1) {
1342        _offset++;
1343      }
1344    
1345      if (_length <= _offset && _hasMoreFragments || _offset == _chunkEnd) {
1346    handleFragment();
1347      }
1348
1349      int ch1 = _buffer[_offset++] & 0xff;
1350      int ch2 = _buffer[_offset++] & 0xff;
1351
1352      return (short) ((ch1 << 8) + ch2);
1353    } catch (IOException JavaDoc e) {
1354      throw new RuntimeException JavaDoc(String.valueOf(e));
1355    }
1356  }
1357  
1358  /**
1359   * Reads a 32-bit integer from the input stream.
1360   */

1361  public int readInt()
1362    throws IOException JavaDoc
1363  {
1364    align4();
1365    
1366    if (_length <= _offset && _hasMoreFragments || _offset == _chunkEnd)
1367      handleFragment();
1368    
1369    int ch1 = _buffer[_offset++];
1370    int ch2 = _buffer[_offset++];
1371    int ch3 = _buffer[_offset++];
1372    int ch4 = _buffer[_offset++];
1373
1374    return (((ch1 & 0xff) << 24) +
1375            ((ch2 & 0xff) << 16) +
1376            ((ch3 & 0xff) << 8) +
1377            ((ch4 & 0xff)));
1378  }
1379  
1380  /**
1381   * Reads a 32-bit integer from the input stream.
1382   */

1383  public int readInt(byte []buffer, int offset)
1384  {
1385    int ch1 = buffer[offset++];
1386    int ch2 = buffer[offset++];
1387    int ch3 = buffer[offset++];
1388    int ch4 = buffer[offset++];
1389
1390    return (((ch1 & 0xff) << 24) +
1391            ((ch2 & 0xff) << 16) +
1392            ((ch3 & 0xff) << 8) +
1393            ((ch4 & 0xff)));
1394  }
1395  
1396  /**
1397   * Reads a 64-bit integer from the input stream.
1398   */

1399  public long readLong()
1400    throws IOException JavaDoc
1401  {
1402    align8();
1403
1404    int ch1 = _buffer[_offset++];
1405    int ch2 = _buffer[_offset++];
1406    int ch3 = _buffer[_offset++];
1407    int ch4 = _buffer[_offset++];
1408    int ch5 = _buffer[_offset++];
1409    int ch6 = _buffer[_offset++];
1410    int ch7 = _buffer[_offset++];
1411    int ch8 = _buffer[_offset++];
1412
1413    return ((((long) ch1 & 0xffL) << 56) +
1414            (((long) ch2 & 0xffL) << 48) +
1415            (((long) ch3 & 0xffL) << 40) +
1416            (((long) ch4 & 0xffL) << 32) +
1417            (((long) ch5 & 0xffL) << 24) +
1418            (((long) ch6 & 0xffL) << 16) +
1419            (((long) ch7 & 0xffL) << 8) +
1420            (((long) ch8 & 0xffL)));
1421  }
1422
1423  private void align4()
1424    throws IOException JavaDoc
1425  {
1426    int frag = _offset % 4;
1427    if (frag > 0 && frag < 4) {
1428      _offset += 4 - frag;
1429    }
1430
1431    if (_chunkEnd > 0 && _chunkEnd <= _offset)
1432      handleChunk();
1433  }
1434
1435  private void align8()
1436    throws IOException JavaDoc
1437  {
1438    int frag = _offset % 8;
1439    if (frag > 0 && frag < 8) {
1440      _offset += 8 - frag;
1441    }
1442
1443    if (_chunkEnd > 0 && _chunkEnd <= _offset)
1444      handleChunk();
1445  }
1446
1447  /**
1448   * Reads the next byte.
1449   */

1450  public int read()
1451    throws IOException JavaDoc
1452  {
1453    if (_offset == _chunkEnd)
1454      handleChunk();
1455    else if (_chunkEnd > 0 && _chunkEnd < _offset) {
1456      //System.out.println("PAST: " + _offset+ " " + _chunkEnd);
1457
}
1458    
1459    return readImpl();
1460  }
1461
1462  private void handleChunk()
1463    throws IOException JavaDoc
1464  {
1465    while (_offset % 4 != 0) {
1466      _offset++;
1467    }
1468      
1469    int chunkLength = readImpl();
1470
1471    _chunkEnd = getOffset() + chunkLength;
1472  }
1473
1474  /**
1475   * Reads the next byte.
1476   */

1477  private int readImpl()
1478    throws IOException JavaDoc
1479  {
1480    if (_length <= _offset && _hasMoreFragments)
1481      handleFragment();
1482    
1483    return _buffer[_offset++];
1484  }
1485
1486  public void completeRead()
1487    throws IOException JavaDoc
1488  {
1489    _offset = _length;
1490  }
1491
1492  private void handleFragment()
1493    throws IOException JavaDoc
1494  {
1495    if (_length < _offset)
1496      throw new IllegalStateException JavaDoc(L.l("Read {0} past length {1}",
1497                      "" + _offset, "" + _length));
1498    
1499    if (_length <= _offset) {
1500      //System.out.println("FRAG: CHUNK:" + _chunkEnd + " " + _offset);
1501
//System.out.println("FRAG-");
1502
// XXX: IIOP 1.2?
1503

1504      while (_offset % 8 != 0) {
1505    _offset++;
1506    _length++;
1507    
1508    if (_chunkEnd > 0)
1509      _chunkEnd++;
1510      }
1511
1512      int len = _rs.readAll(_header, 0, _header.length);
1513
1514      if (len != _header.length)
1515    throw new EOFException JavaDoc("Unexpected length: " + len);
1516
1517      if (_header[0] != 'G' ||
1518      _header[1] != 'I' ||
1519      _header[2] != 'O' ||
1520      _header[3] != 'P') {
1521
1522    throw new IOException JavaDoc(L.l("unknown request {0},{1},{2},{3}",
1523                  "" + _header[0], "" + _header[1],
1524                  "" + _header[2], "" + _header[3]));
1525      }
1526
1527      _major = _header[4];
1528      _minor = _header[5];
1529
1530      if (_major != 1)
1531    throw new IOException JavaDoc("unknown major");
1532
1533      _flags = _header[6];
1534      _isBigEndian = (_flags & 1) == 0;
1535      _hasMoreFragments = (_flags & 2) == 2;
1536      
1537      _type = _header[7];
1538
1539      _fragmentOffset += 8;
1540
1541      _rs.readAll(_buffer, _length, 4);
1542      int fragLen = readInt(_buffer, _length);
1543    
1544      if (_minor == 2) {
1545    fragLen -= 4;
1546    _rs.readAll(_buffer, _length, 4);
1547    int requestId = readInt(_buffer, _length);
1548    //System.out.println("id: " + requestId);
1549
}
1550      
1551      _rs.readAll(_buffer, _length, fragLen);
1552
1553      writeHexGroup(_buffer, _length, fragLen);
1554      
1555      _length += fragLen;
1556
1557      if (_type != MSG_FRAGMENT)
1558    throw new IOException JavaDoc(L.l("expected Fragment at {0}", "" + _type));
1559    }
1560  }
1561
1562  private void skip(int len)
1563    throws IOException JavaDoc
1564  {
1565    if (_length <= _offset && _hasMoreFragments)
1566      handleFragment();
1567
1568    _offset += len;
1569  }
1570
1571  private void writeHexGroup(byte []buffer, int offset, int length)
1572  {
1573    int end = offset + length;
1574      
1575    while (offset < end) {
1576      int chunkLength = 16;
1577    
1578      for (int j = 0; j < chunkLength; j++) {
1579    System.out.print(" ");
1580    printHex(buffer[offset + j]);
1581      }
1582
1583      System.out.print(" ");
1584      for (int j = 0; j < chunkLength; j++) {
1585    printCh(buffer[offset + j]);
1586      }
1587
1588      offset += chunkLength;
1589    
1590      System.out.println();
1591    }
1592  }
1593
1594  private void printHex(int d)
1595  {
1596    int ch1 = (d >> 4) & 0xf;
1597    int ch2 = d & 0xf;
1598
1599    if (ch1 >= 10)
1600      System.out.print((char) ('a' + ch1 - 10));
1601    else
1602      System.out.print((char) ('0' + ch1));
1603    
1604    if (ch2 >= 10)
1605      System.out.print((char) ('a' + ch2 - 10));
1606    else
1607      System.out.print((char) ('0' + ch2));
1608  }
1609
1610  private void printCh(int d)
1611  {
1612    if (d >= 0x20 && d <= 0x7f)
1613      System.out.print("" + ((char) d));
1614    else
1615      System.out.print(".");
1616  }
1617
1618  private String JavaDoc toCh(int d)
1619  {
1620    if (d >= 0x20 && d <= 0x7f)
1621      return "" + (char) d;
1622    else
1623      return "" + d;
1624  }
1625
1626  private static String JavaDoc toHex(int v)
1627  {
1628    CharBuffer cb = new CharBuffer();
1629    for (int i = 28; i >= 0; i -= 4) {
1630      int h = (v >> i) & 0xf;
1631
1632      if (h >= 10)
1633        cb.append((char) ('a' + h - 10));
1634      else
1635        cb.append(h);
1636    }
1637
1638    return cb.toString();
1639  }
1640}
1641
Popular Tags