IIOPAddressBase.java revision 608:7e06bf1dcb09
118334Speter/*
2117395Skan * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
3169689Skan * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4132718Skan *
518334Speter * This code is free software; you can redistribute it and/or modify it
690075Sobrien * under the terms of the GNU General Public License version 2 only, as
752284Sobrien * published by the Free Software Foundation.  Oracle designates this
890075Sobrien * particular file as subject to the "Classpath" exception as provided
990075Sobrien * by Oracle in the LICENSE file that accompanied this code.
1090075Sobrien *
1190075Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1252284Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1390075Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1490075Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1590075Sobrien * accompanied this code).
1690075Sobrien *
1752284Sobrien * You should have received a copy of the GNU General Public License version
1852284Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
1990075Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20169689Skan *
21169689Skan * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2252284Sobrien * or visit www.oracle.com if you need additional information or have any
2352284Sobrien * questions.
2418334Speter */
2518334Speter
2618334Speterpackage com.sun.corba.se.impl.ior.iiop ;
2718334Speter
2818334Speterimport org.omg.CORBA.BAD_PARAM ;
2918334Speter
30169689Skanimport org.omg.CORBA_2_3.portable.InputStream ;
31169689Skanimport org.omg.CORBA_2_3.portable.OutputStream ;
3218334Speter
3318334Speterimport com.sun.corba.se.spi.ior.iiop.IIOPAddress ;
3418334Speter
3518334Speter/**
3618334Speter * @author
3718334Speter */
3818334Speterabstract class IIOPAddressBase implements IIOPAddress
3918334Speter{
4018334Speter    // Ports are marshalled as shorts on the wire.  The IDL
4118334Speter    // type is unsigned short, which lacks a convenient representation
42117395Skan    // in Java in the 32768-65536 range.  So, we treat ports as
4318334Speter    // ints throught this code, except that marshalling requires a
4418334Speter    // scaling conversion.  intToShort and shortToInt are provided
4518334Speter    // for this purpose.
4618334Speter    protected short intToShort( int value )
4718334Speter    {
4818334Speter        if (value > 32767)
49117395Skan            return (short)(value - 65536) ;
5018334Speter        return (short)value ;
5118334Speter    }
5218334Speter
5318334Speter    protected int shortToInt( short value )
5418334Speter    {
5518334Speter        if (value < 0)
5650397Sobrien            return value + 65536 ;
5750397Sobrien        return value ;
5818334Speter    }
5918334Speter
6018334Speter    public void write( OutputStream os )
6118334Speter    {
6218334Speter        os.write_string( getHost() ) ;
6318334Speter        int port = getPort() ;
6418334Speter        os.write_short( intToShort( port ) ) ;
6518334Speter    }
6618334Speter
6718334Speter    public boolean equals( Object obj )
6818334Speter    {
6918334Speter        if (!(obj instanceof IIOPAddress))
7018334Speter            return false ;
71169689Skan
72169689Skan        IIOPAddress other = (IIOPAddress)obj ;
73169689Skan
74169689Skan        return getHost().equals(other.getHost()) &&
75169689Skan            (getPort() == other.getPort()) ;
76169689Skan    }
77169689Skan
78169689Skan    public int hashCode()
79169689Skan    {
80169689Skan        return getHost().hashCode() ^ getPort() ;
81169689Skan    }
82169689Skan
83169689Skan    public String toString()
84169689Skan    {
85169689Skan        return "IIOPAddress[" + getHost() + "," + getPort() + "]" ;
86169689Skan    }
87169689Skan}
88169689Skan