KickJava   Java API By Example, From Geeks To Geeks.

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


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.CodecFactoryPackage.UnknownEncoding JavaDoc;
25 import org.omg.IOP.Codec JavaDoc;
26 import org.omg.IOP.Encoding JavaDoc;
27 import org.omg.IOP.ENCODING_CDR_ENCAPS JavaDoc;
28 import org.omg.CORBA.*;
29
30 import org.jacorb.orb.CDRInputStream;
31 import org.jacorb.orb.CDROutputStream;
32
33 /**
34  * This class represents a codec for encoding CDR encapsulations for the supported
35  * GIOP versions 1.0-1.2
36  *
37  * See PI SPec p.10-77ff
38  *
39  * @author Nicolas Noffke
40  * @version $Id: CodecImpl.java,v 1.2 2004/04/28 12:37:28 brose Exp $
41  */

42
43 public class CodecImpl
44     extends org.omg.CORBA.LocalObject JavaDoc
45     implements Codec JavaDoc
46 {
47     private ORB orb = null;
48     int giopMinor = 0;
49     
50     public CodecImpl(ORB orb, Encoding JavaDoc enc)
51         throws UnknownEncoding JavaDoc
52     {
53         if (enc.format != ENCODING_CDR_ENCAPS.value)
54             throw new UnknownEncoding JavaDoc();
55
56         if (enc.major_version != 1 || enc.minor_version > 2 )
57             throw new UnknownEncoding JavaDoc();
58        
59         this.orb = orb;
60         this.giopMinor = (int)enc.minor_version;
61     }
62
63     // implementation of org.omg.IOP.CodecOperations interface
64

65     public Any decode(byte[] data)
66         throws FormatMismatch
67     {
68         CDRInputStream in = new CDRInputStream(orb, data);
69         in.setGIOPMinor( giopMinor );
70
71         in.openEncapsulatedArray();
72         Any result = in.read_any();
73
74         //not necessary, since stream is never used again
75
//in.closeEncapsulation();
76

77         return result;
78     }
79
80
81     public Any decode_value(byte[] data, TypeCode tc)
82         throws FormatMismatch, TypeMismatch
83     {
84         CDRInputStream in = new CDRInputStream(orb, data);
85         in.setGIOPMinor( giopMinor );
86
87         in.openEncapsulatedArray();
88         Any result = orb.create_any();
89         result.read_value(in, tc);
90
91         //not necessary, since stream is never used again
92
//in.closeEncasupaltion();
93

94         return result;
95     }
96
97     public byte[] encode(Any data)
98         throws InvalidTypeForEncoding
99     {
100         CDROutputStream out = new CDROutputStream(orb);
101         out.setGIOPMinor( giopMinor );
102
103         out.beginEncapsulatedArray();
104         out.write_any(data);
105
106         /*
107           closing must not be done, since it will patch the
108           array with a size!
109         try
110         {
111             out.endEncapsulation();
112         }
113         catch (java.io.IOException e)
114         {
115         }
116         */

117
118         /*
119          * We have to copy anyway since we need an exact-sized array.
120          * Closing afterwards, to return buffer to BufferManager.
121          */

122         byte[] result = out.getBufferCopy();
123         out.close();
124
125         return result;
126     }
127
128     public byte[] encode_value(Any data)
129         throws InvalidTypeForEncoding
130     {
131         CDROutputStream out = new CDROutputStream(orb);
132         out.setGIOPMinor( giopMinor );
133
134         out.beginEncapsulatedArray();
135         data.write_value(out);
136
137         /*
138           closing must not be done, since it will patch the
139           array with a size!
140
141         try
142         {
143             out.endEncapsulation();
144         }
145         catch (java.io.IOException e)
146         {
147         }
148         */

149
150         /*
151          * We have to copy anyway since we need an exact-sized array.
152          * Closing afterwards, to return buffer to BufferManager.
153          */

154         byte[] result = out.getBufferCopy();
155         out.close();
156
157         return result;
158     }
159
160 } // CodecImpl
161
Popular Tags