United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4262187 : add Subnet class

Details
Type:
Enhancement
Submit Date:
1999-08-12
Status:
Closed
Updated Date:
2002-05-27
Project Name:
JDK
Resolved Date:
2002-05-27
Component:
core-libs
OS:
generic
Sub-Component:
java.net
CPU:
generic
Priority:
P4
Resolution:
Duplicate
Affected Versions:
1.2.2,1.4.0
Fixed Versions:

Related Reports
Duplicate:
Duplicate:

Sub Tasks

Description

Name: skT88420			Date: 08/12/99


It would be very convenient if you would include a Subnet class
in the java.net package.  It should have a way to compare
an InetAddress to a Subnet to see if it is in the Subnet.
(Review ID: 93862) 
======================================================================

                                    

Comments
EVALUATION

We can't do this in Merlin time frame. Will reconsider it for Tiger.

yingxian.wang@eng 2001-03-09


4691932 is collecting the bugs/rfes regarded to programmatic access to
network parameters.
                                     
2001-03-09
WORK AROUND



Name: skT88420			Date: 08/12/99


I wrote my own, shown below.

It does not yet have a way to create the subnet from
its string representation, which it should have.
It should also have some address/mask constructors
as well as the address/bitlength constructors.

import java.net.*;

/*****************************************************************************
 * Objects that represent subnets (internet addresses and mask).
 * <P>
 * Subnets are represented as a string in the format "xxx.xxx.xxx.xxx/yy"
 * where "xxx.xxx.xxx.xxx" is the address and "yy" is the bitlength.
 * <P>
 * @author
 *	Dan Lipofsky<BR>
 *	Copyright &copy; 1999 Cycorp, Inc.  All rights reserved.
 *****************************************************************************/
public class Subnet {
  private int address;		// The 4 byte internet address
  private int bitlength;	// The number of meaningful bits
  private int mask;		// The mask used to select the meaningful bits

  /**
   * Constructs a Subnet with the given address and bitlength.
   * This is a private constructor.  We want to hide how the integers
   * correspond to addresses.  Only byte arrays or strings get passed in.
   */
  private Subnet(int address, int bitlength) {
    if (bitlength < 0 || bitlength > 32)
      throw new IllegalArgumentException("Illegal bitlength " + bitlength);
    this.address = address;
    this.bitlength = bitlength;
    this.mask = makeMask(bitlength);
  }
  
  /** Constructs a Subnet with the given address and bitlength. */
  public Subnet(byte addr[], int bitlength) {
    this(arrayToInt(addr), bitlength);
  }

  // Public Methods

  /**
   * Compares two Subnet objects.
   * @return true if address and bitlength are equal.
   */
  public boolean equals(Object obj) {
    if (obj instanceof Subnet) {
	Subnet other = (Subnet)obj;
	return ((address == other.address) && (bitlength == other.bitlength));
    }
    return false;
  }

  /**
   * Determines if an address is in a Subnet.
   * @return true if address is in the Subnet.
   */
  public boolean compareAddressToSubnet(InetAddress a) {
    int tmp_address = arrayToInt(a.getAddress());
    return ( (tmp_address & mask) == (address & mask) );
  }

  /** Converts a Subnet to a string */
  public String toString() {	
    return (String) ( ((address >>> 24) & 0xFF) + "." +
		      ((address >>> 16) & 0xFF) + "." +
		      ((address >>>  8) & 0xFF) + "." +
		      (address & 0xFF)          + "/" +
		      bitlength ) ;
  }

  // Private Methods

  /** Creates a mask from a bitlength.  The most significant bitlength
      bits are 1, while the rest are 0. */
  private static int makeMask(int bitlength) {
    int mask = 0;
    for (int i=0; i < bitlength; i++) {
      mask = (mask << 1) | 1;
    }
    mask = mask << (32 - bitlength);
    return mask;
  }

  /** Converts an array of 4 bytes to an int */
  private static int arrayToInt(byte addr[]) {
    return (int)( addr[3] & 0xFF
		  | ((addr[2] << 8) & 0xFF00)
		  | ((addr[1] << 16) & 0xFF0000)
		  | ((addr[0] << 24) & 0xFF000000) );
  }

  /** Converts an int to an array of 4 bytes */
  private static byte[] intToArray(int address) {
    byte[] addr = new byte[4];
    addr[0] = (byte) ((address >>> 24) & 0xFF);
    addr[1] = (byte) ((address >>> 16) & 0xFF);
    addr[2] = (byte) ((address >>> 8) & 0xFF);
    addr[3] = (byte) (address & 0xFF);
    return addr;
  }

  static public void main(String argv[])
    throws UnknownHostException {
      byte[] address = new byte[] { (byte)207, (byte)207, (byte)8, (byte)0 };
      Subnet s = new Subnet(address, 30);
      InetAddress a1 = InetAddress.getByName("207.207.8.3");
      InetAddress a2 = InetAddress.getByName("207.207.8.4");
      System.out.println(s + " " + a1 + " " + s.compareAddressToSubnet(a1));
      System.out.println(s + " " + a2 + " " + s.compareAddressToSubnet(a2));
  }
}
======================================================================
                                     
2004-06-11



Hardware and Software, Engineered to Work Together