001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1999-2004 Gerald Brose
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: *
020: */
021: package org.jacorb.orb.portableInterceptor;
022:
023: import org.omg.IOP.CodecPackage.*;
024: import org.omg.IOP.CodecFactoryPackage.UnknownEncoding;
025: import org.omg.IOP.Codec;
026: import org.omg.IOP.Encoding;
027: import org.omg.IOP.ENCODING_CDR_ENCAPS;
028: import org.omg.CORBA.*;
029:
030: import org.jacorb.orb.CDRInputStream;
031: import org.jacorb.orb.CDROutputStream;
032:
033: /**
034: * This class represents a codec for encoding CDR encapsulations for the supported
035: * GIOP versions 1.0-1.2
036: *
037: * See PI SPec p.10-77ff
038: *
039: * @author Nicolas Noffke
040: * @version $Id: CodecImpl.java,v 1.4 2006/07/07 10:55:57 alphonse.bendt Exp $
041: */
042:
043: public class CodecImpl extends org.omg.CORBA.LocalObject implements
044: Codec {
045: private final ORB orb;
046: private final int giopMinor;
047:
048: public CodecImpl(ORB orb, Encoding enc) throws UnknownEncoding {
049: super ();
050:
051: if (enc.format != ENCODING_CDR_ENCAPS.value) {
052: throw new UnknownEncoding();
053: }
054:
055: if (enc.major_version != 1 || enc.minor_version > 2) {
056: throw new UnknownEncoding();
057: }
058:
059: this .orb = orb;
060: this .giopMinor = enc.minor_version;
061: }
062:
063: // implementation of org.omg.IOP.CodecOperations interface
064:
065: public Any decode(byte[] data) throws FormatMismatch {
066: final CDRInputStream in = new CDRInputStream(orb, data);
067:
068: try {
069: in.setGIOPMinor(giopMinor);
070:
071: in.openEncapsulatedArray();
072: Any result = in.read_any();
073:
074: //not necessary, since stream is never used again
075: //in.closeEncapsulation();
076:
077: return result;
078: } finally {
079: in.close();
080: }
081: }
082:
083: public Any decode_value(byte[] data, TypeCode tc)
084: throws FormatMismatch, TypeMismatch {
085: final CDRInputStream in = new CDRInputStream(orb, data);
086:
087: try {
088: in.setGIOPMinor(giopMinor);
089:
090: in.openEncapsulatedArray();
091: Any result = orb.create_any();
092: result.read_value(in, tc);
093:
094: //not necessary, since stream is never used again
095: //in.closeEncasupaltion();
096:
097: return result;
098: } finally {
099: in.close();
100: }
101: }
102:
103: public byte[] encode(Any data) throws InvalidTypeForEncoding {
104: final CDROutputStream out = new CDROutputStream(orb);
105: try {
106: out.setGIOPMinor(giopMinor);
107:
108: out.beginEncapsulatedArray();
109: out.write_any(data);
110:
111: /*
112: closing must not be done, since it will patch the
113: array with a size!
114: try
115: {
116: out.endEncapsulation();
117: }
118: catch (java.io.IOException e)
119: {
120: }
121: */
122:
123: /*
124: * We have to copy anyway since we need an exact-sized array.
125: * Closing afterwards, to return buffer to BufferManager.
126: */
127: byte[] result = out.getBufferCopy();
128:
129: return result;
130: } finally {
131: out.close();
132: }
133: }
134:
135: public byte[] encode_value(Any data) throws InvalidTypeForEncoding {
136: final CDROutputStream out = new CDROutputStream(orb);
137:
138: try {
139: out.setGIOPMinor(giopMinor);
140:
141: out.beginEncapsulatedArray();
142: data.write_value(out);
143:
144: /*
145: closing must not be done, since it will patch the
146: array with a size!
147:
148: try
149: {
150: out.endEncapsulation();
151: }
152: catch (java.io.IOException e)
153: {
154: }
155: */
156:
157: /*
158: * We have to copy anyway since we need an exact-sized array.
159: * Closing afterwards, to return buffer to BufferManager.
160: */
161: byte[] result = out.getBufferCopy();
162:
163: return result;
164: } finally {
165: out.close();
166: }
167: }
168:
169: } // CodecImpl
|