Example #1
0
        /// <summary>
        /// <code>readObject</code> for custom serialization.
        ///
        /// <para>This method reads this object's serialized form for this class
        /// as follows:
        ///
        /// </para>
        /// <para>The <code>readUTF</code> method is invoked on <code>in</code>
        /// to read the external ref type name for the <code>RemoteRef</code>
        /// instance to be filled in to this object's <code>ref</code> field.
        /// If the string returned by <code>readUTF</code> has length zero,
        /// the <code>readObject</code> method is invoked on <code>in</code>,
        /// and than the value returned by <code>readObject</code> is cast to
        /// <code>RemoteRef</code> and this object's <code>ref</code> field is
        /// set to that value.
        /// Otherwise, this object's <code>ref</code> field is set to a
        /// <code>RemoteRef</code> instance that is created of an
        /// implementation-specific class corresponding to the external ref
        /// type name returned by <code>readUTF</code>, and then
        /// the <code>readExternal</code> method is invoked on
        /// this object's <code>ref</code> field.
        ///
        /// </para>
        /// <para>If the external ref type name is
        /// <code>"UnicastRef"</code>, <code>"UnicastServerRef"</code>,
        /// <code>"UnicastRef2"</code>, <code>"UnicastServerRef2"</code>,
        /// or <code>"ActivatableRef"</code>, a corresponding
        /// implementation-specific class must be found, and its
        /// <code>readExternal</code> method must read the serial data
        /// for that external ref type name as specified to be written
        /// in the <b>serialData</b> documentation for this class.
        /// If the external ref type name is any other string (of non-zero
        /// length), a <code>ClassNotFoundException</code> will be thrown,
        /// unless the implementation provides an implementation-specific
        /// class corresponding to that external ref type name, in which
        /// case this object's <code>ref</code> field will be set to an
        /// instance of that implementation-specific class.
        /// </para>
        /// </summary>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException
        private void ReadObject(java.io.ObjectInputStream @in)
        {
            String refClassName = @in.ReadUTF();

            if (refClassName == null || refClassName.Length() == 0)
            {
                /*
                 * No reference class name specified, so construct
                 * remote reference from its serialized form.
                 */
                @ref = (RemoteRef)@in.ReadObject();
            }
            else
            {
                /*
                 * Built-in reference class specified, so delegate to
                 * internal reference class to initialize its fields from
                 * its external form.
                 */
                String internalRefClassName = RemoteRef_Fields.PackagePrefix + "." + refClassName;
                Class  refClass             = Class.ForName(internalRefClassName);
                try
                {
                    @ref = (RemoteRef)refClass.NewInstance();

                    /*
                     * If this step fails, assume we found an internal
                     * class that is not meant to be a serializable ref
                     * type.
                     */
                }
                catch (InstantiationException e)
                {
                    throw new ClassNotFoundException(internalRefClassName, e);
                }
                catch (IllegalAccessException e)
                {
                    throw new ClassNotFoundException(internalRefClassName, e);
                }
                catch (ClassCastException e)
                {
                    throw new ClassNotFoundException(internalRefClassName, e);
                }
                @ref.ReadExternal(@in);
            }
        }
        /// <summary>
        /// Restores this object from a stream (i.e., deserializes it).
        /// </summary>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void readObject(java.io.ObjectInputStream ois) throws java.io.IOException, ClassNotFoundException
        private void ReadObject(java.io.ObjectInputStream ois)
        {
            CertificateFactory cf;
            Dictionary <String, CertificateFactory> cfs = null;

            ois.DefaultReadObject();

            if (Type == null)
            {
                throw new NullPointerException("type can't be null");
            }

            // process any new-style certs in the stream (if present)
            int size = ois.ReadInt();

            if (size > 0)
            {
                // we know of 3 different cert types: X.509, PGP, SDSI, which
                // could all be present in the stream at the same time
                cfs        = new Dictionary <String, CertificateFactory>(3);
                this.Certs = new java.security.cert.Certificate[size];
            }

            for (int i = 0; i < size; i++)
            {
                // read the certificate type, and instantiate a certificate
                // factory of that type (reuse existing factory if possible)
                String certType = ois.ReadUTF();
                if (cfs.ContainsKey(certType))
                {
                    // reuse certificate factory
                    cf = cfs[certType];
                }
                else
                {
                    // create new certificate factory
                    try
                    {
                        cf = CertificateFactory.GetInstance(certType);
                    }
                    catch (CertificateException)
                    {
                        throw new ClassNotFoundException("Certificate factory for " + certType + " not found");
                    }
                    // store the certificate factory so we can reuse it later
                    cfs[certType] = cf;
                }
                // parse the certificate
                sbyte[] encoded = null;
                try
                {
                    encoded = new sbyte[ois.ReadInt()];
                }
                catch (OutOfMemoryError)
                {
                    throw new IOException("Certificate too big");
                }
                ois.ReadFully(encoded);
                ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
                try
                {
                    this.Certs[i] = cf.GenerateCertificate(bais);
                }
                catch (CertificateException ce)
                {
                    throw new IOException(ce.Message);
                }
                bais.Close();
            }
        }