🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #19558

Re: Net transfer of native data types

#19558
From: Greg Matthews
Date: Fri, 23 Jan 1998 00:00
80 lines
3637 bytes
Has anyone had problems with using readObject/writeObject with regard to
different hashCode()'s being generated on the source and target VMs? If so,
what did you do to deal with it? For example, if you have some typesafe
constants defined then after they have been transferred to a new VM using
writeObject through a socket then their hashCode() is different to the
hashCode() generated on the source machine.

The effect of this is that anything that relies on the hashCode to match things
up no longer works properly. If the destination VM puts something into a
Dictionary using a typesafe constant as the key then it can't be retrieved from
the Dictionary using the same typesafe constant that was passed from another VM
through a socket using writeObject. Also, equals() and == do not work for
comparisons.

To work around this we've been looking at having all typesafe constants
subclass a new class called BaseTypeSafe. This has a constructor that accepts
an int, and overrides the equals() method and the hashCode() method. The
hashCode() method returns the int value that was called in the constructor. The
equals() method checks if the object being compared is an instance of
BaseTypeSafe then it just compares the hashCode() and returns true if they
match.

A new set of typesafe constants would be defined as follows:

public class MyTypeSafe extends BaseTypeSafe {

    public NewTypeSafe( int val ) {
        super( val );
    }

    // The actual int constructor values are pretty much irrelevant. They just
    // serve to differentiate constants within a class.
    public static final MyTypeSafe newConst1 = new MyTypeSafe(1);
    public static final MyTypeSafe newConst2 = new MyTypeSafe(2);
}

The benefit is that you can still pass constants between VMs and use them in a
typesafe manner.

Anyone have any comments on any drawbacks of implementing equals() and
hashCode() in BaseTypeSafe or any side effects that this might cause ?

Regards,
Greg Matthews
gmatthews@flex.com.au


Brian Burton wrote:

> Read the documentation for java.io.ObjectOutputStream.
>
> ++Brian
>
> Shahed Ali wrote:
> >
> > I had written a function in delphi which would transfer
> > int/byte/long/string data across
> > a network, by taking the address of the variable which held the data, and
> > transferring the content of memory from that address ownwards for the
> > number of bytes that
> > were required to transfer the data type.
> >
> > Any ideas on how I may do this in Java ?
> >
> > I am currently using writeInt , writeLong etc etc. But I have to write a
> > seprate function to transfer
> > each data type.
> >
> > Any ideas on how to do this more elegantly ??
> >
> > PS. I can only use sockets, and RMI / CORBA etc are out of the question.
> >
> > Regards
> > Shahed.
>
> --
> Brian Burton                        Custom Software Development
> Burton Computer Corporation         Java, C++, Orbix, Tuxedo
> http://www.burton-computer.com      OO Consulting and Mentoring
Technical Details
Message-ID: <34C80F9B.1DDB83FC@flex.com.au>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <01bd1a99$1b2e9cc0$0de3aac2@GIT07.al-rostamani.co.ae> <34C3F755.3612354E@burton-computer.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,377a67f7e75051dc
X-Google-Attributes: gidf74b3,public
From: Greg Matthews <gmatthews@flex.com.au>
Subject: Re: Net transfer of native data types
Date: 1998/01/23
Message-ID: <34C80F9B.1DDB83FC@flex.com.au>
X-Deja-AN: 318498313
Content-Transfer-Encoding: 7bit
References: <01bd1a99$1b2e9cc0$0de3aac2@GIT07.al-rostamani.co.ae> <34C3F755.3612354E@burton-computer.com>
Content-Type: text/plain; charset=us-ascii
Organization: None
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language