Source for org.omg.IOP.TaggedProfileHelper

   1: /* TaggedProfileHelper.java --
   2:    Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package org.omg.IOP;
  40: 
  41: import gnu.CORBA.CDR.cdrBufInput;
  42: import gnu.CORBA.CDR.cdrBufOutput;
  43: 
  44: import org.omg.CORBA.Any;
  45: import org.omg.CORBA.BAD_OPERATION;
  46: import org.omg.CORBA.MARSHAL;
  47: import org.omg.CORBA.ORB;
  48: import org.omg.CORBA.StructMember;
  49: import org.omg.CORBA.TCKind;
  50: import org.omg.CORBA.TypeCode;
  51: import org.omg.CORBA.portable.InputStream;
  52: import org.omg.CORBA.portable.OutputStream;
  53: 
  54: import java.io.IOException;
  55: 
  56: /**
  57:  * A helper operations for the structure {@link TaggedProfile}.
  58:  *
  59:  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  60:  */
  61: public abstract class TaggedProfileHelper
  62: {
  63:   /**
  64:    * The cached typecode value, computed only once.
  65:    */
  66:   private static TypeCode typeCode;
  67: 
  68:   /**
  69:    * Create the TaggedProfile typecode (structure, named "TaggedProfile"). The
  70:    * typecode states that the structure contains the following fields: tag,
  71:    * profile_data.
  72:    */
  73:   public static TypeCode type()
  74:   {
  75:     if (typeCode == null)
  76:       {
  77:         ORB orb = ORB.init();
  78:         StructMember[] members = new StructMember[2];
  79: 
  80:         TypeCode field;
  81: 
  82:         field = orb.create_alias_tc("IDL:omg.org/IOP/ProfileId:1.0",
  83:                                     "ProfileId",
  84:                                     orb.get_primitive_tc(TCKind.tk_ulong));
  85:         members[0] = new StructMember("tag", field, null);
  86: 
  87:         field = orb.create_sequence_tc(0, orb.get_primitive_tc(TCKind.tk_octet));
  88:         members[1] = new StructMember("profile_data", field, null);
  89:         typeCode = orb.create_struct_tc(id(), "TaggedProfile", members);
  90:       }
  91:     return typeCode;
  92:   }
  93: 
  94:   /**
  95:    * Insert the TaggedProfile into the given Any. This method uses the
  96:    * TaggedProfileHolder.
  97:    *
  98:    * @param any the Any to insert into.
  99:    * @param that the TaggedProfile to insert.
 100:    */
 101:   public static void insert(Any any, TaggedProfile that)
 102:   {
 103:     any.insert_Streamable(new TaggedProfileHolder(that));
 104:   }
 105: 
 106:   /**
 107:    * Extract the TaggedProfile from given Any. This method uses the
 108:    * TaggedProfileHolder.
 109:    *
 110:    * @throws BAD_OPERATION if the passed Any does not contain TaggedProfile.
 111:    */
 112:   public static TaggedProfile extract(Any any)
 113:   {
 114:     try
 115:       {
 116:         return ((TaggedProfileHolder) any.extract_Streamable()).value;
 117:       }
 118:     catch (ClassCastException cex)
 119:       {
 120:         BAD_OPERATION bad = new BAD_OPERATION("TaggedProfile expected");
 121:         bad.initCause(cex);
 122:         throw bad;
 123:       }
 124:   }
 125: 
 126:   /**
 127:    * Get the TaggedProfile repository id.
 128:    *
 129:    * @return "IDL:omg.org/IOP/TaggedProfile:1.0", always.
 130:    */
 131:   public static String id()
 132:   {
 133:     return "IDL:omg.org/IOP/TaggedProfile:1.0";
 134:   }
 135: 
 136:   /**
 137:    * Read the structure from the CDR intput stream.
 138:    *
 139:    * @param input a org.omg.CORBA.portable stream to read from.
 140:    */
 141:   public static TaggedProfile read(InputStream input)
 142:   {
 143:     TaggedProfile value = new TaggedProfile();
 144:     value.tag = input.read_long();
 145: 
 146:     if (input instanceof cdrBufInput)
 147:       {
 148:         // Highly probable.
 149:         value.profile_data = ((cdrBufInput) input).read_sequence();
 150:       }
 151:     else
 152:       {
 153:         value.profile_data = new byte[input.read_long()];
 154:         for (int i0 = 0; i0 < value.profile_data.length; i0++)
 155:           value.profile_data[i0] = input.read_octet();
 156:       }
 157:     return value;
 158:   }
 159: 
 160:   /**
 161:    * Write the structure to the CDR output stream.
 162:    *
 163:    * @param output a org.omg.CORBA.portable stream stream to write into.
 164:    * @param value a value to write.
 165:    */
 166:   public static void write(OutputStream output, TaggedProfile value)
 167:   {
 168:     output.write_long(value.tag);
 169: 
 170:     if (output instanceof cdrBufOutput)
 171:       {
 172:         // Highly probable.
 173:         output.write_long(value.profile_data.length);
 174:         try
 175:           {
 176:             output.write(value.profile_data);
 177:           }
 178:         catch (IOException e)
 179:           {
 180:             MARSHAL m = new MARSHAL();
 181:             m.initCause(e);
 182:             throw m;
 183:           }
 184:       }
 185:     else
 186:       {
 187:         output.write_long(value.profile_data.length);
 188:         for (int i0 = 0; i0 < value.profile_data.length; i0++)
 189:           output.write_octet(value.profile_data[i0]);
 190:       }
 191:   }