KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > portableInterceptor > Codec_CDR_1_0_Impl


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1999-2004 Gerald Brose
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */

21 package org.jacorb.orb.portableInterceptor;
22
23 import org.omg.IOP.CodecPackage.*;
24 import org.omg.IOP.Codec JavaDoc;
25 import org.omg.CORBA.*;
26
27 import org.jacorb.orb.CDRInputStream;
28 import org.jacorb.orb.CDROutputStream;
29 /**
30  * This class represents a codec for encoding ENCODING_CDR_ENCAPS 1.0.
31  *
32  * See PI SPec p.10-77ff
33  *
34  * @author Nicolas Noffke
35  * @version $Id: Codec_CDR_1_0_Impl.java,v 1.11 2004/05/06 12:40:00 nicolas Exp $
36  */

37
38 public class Codec_CDR_1_0_Impl
39     extends org.omg.CORBA.LocalObject JavaDoc
40     implements Codec JavaDoc
41 {
42
43     private ORB orb = null;
44
45     public Codec_CDR_1_0_Impl(ORB orb)
46     {
47         this.orb = orb;
48     }
49
50     // implementation of org.omg.IOP.CodecOperations interface
51

52     public Any decode(byte[] data)
53         throws FormatMismatch
54     {
55         CDRInputStream in = new CDRInputStream(orb, data);
56
57         in.openEncapsulatedArray();
58         Any result = in.read_any();
59
60         //not necessary, since stream is never used again
61
//in.closeEncapsulation();
62

63         return result;
64     }
65
66
67     public Any decode_value(byte[] data, TypeCode tc)
68         throws FormatMismatch, TypeMismatch
69     {
70         CDRInputStream in = new CDRInputStream(orb, data);
71
72         in.openEncapsulatedArray();
73         Any result = orb.create_any();
74         result.read_value(in, tc);
75
76         //not necessary, since stream is never used again
77
//in.closeEncasupaltion();
78

79         return result;
80     }
81
82     public byte[] encode(Any data)
83         throws InvalidTypeForEncoding
84     {
85         CDROutputStream out = new CDROutputStream(orb);
86
87         out.beginEncapsulatedArray();
88         out.write_any(data);
89
90         /*
91           closing must not be done, since it will patch the
92           array with a size!
93         try
94         {
95             out.endEncapsulation();
96         }
97         catch (java.io.IOException e)
98         {
99         }
100         */

101
102         /*
103          * We have to copy anyway since we need an exact-sized array.
104          * Closing afterwards, to return buffer to BufferManager.
105          */

106         byte[] result = out.getBufferCopy();
107         out.close();
108
109         return result;
110     }
111
112     public byte[] encode_value(Any data)
113         throws InvalidTypeForEncoding
114     {
115
116         CDROutputStream out = new CDROutputStream(orb);
117
118         out.beginEncapsulatedArray();
119         data.write_value(out);
120
121         /*
122           closing must not be done, since it will patch the
123           array with a size!
124
125         try
126         {
127             out.endEncapsulation();
128         }
129         catch (java.io.IOException e)
130         {
131         }
132         */

133
134         /*
135          * We have to copy anyway since we need an exact-sized array.
136          * Closing afterwards, to return buffer to BufferManager.
137          */

138         byte[] result = out.getBufferCopy();
139         out.close();
140
141         return result;
142     }
143
144 } // Codec_CDR_1_0_Impl
145
Popular Tags