From: Marcus <the...@us...> - 2004-03-22 12:28:09
|
Update of /cvsroot/junk/junk/WEB-INF/classes/junk/util In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2539 Modified Files: IpSubnet.java Log Message: cosmetics Index: IpSubnet.java =================================================================== RCS file: /cvsroot/junk/junk/WEB-INF/classes/junk/util/IpSubnet.java,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -C2 -d -r1.1.1.1 -r1.2 *** IpSubnet.java 17 Nov 2003 19:39:59 -0000 1.1.1.1 --- IpSubnet.java 22 Mar 2004 12:17:52 -0000 1.2 *************** *** 2,8 **** * juNK - a file search system for smb shares * ! * Copyright 2003 by Marcus Proest (theevilflow at users dot sf dot net) * ! * This file is part of junk (java usefull net kollektor). * * junk is free software; you can redistribute it and/or modify --- 2,10 ---- * juNK - a file search system for smb shares * ! * Copyright 2004 by ! * Marcus Proest (theevilflow at users dot sf dot net) ! * Uwe van Heeschh (tyron_e at users dot sf dot net) * ! * This file is part of junk (java useful net kollektor). * * junk is free software; you can redistribute it and/or modify *************** *** 21,24 **** --- 23,27 ---- * */ + package junk.util; *************** *** 33,42 **** */ public class IpSubnet implements Iterator, Cloneable { ! /** number of ip's to compute */ ! private long num; /** counter */ private long now; /** 1st byte of the next ip address */ private short a; --- 36,48 ---- */ public class IpSubnet implements Iterator, Cloneable { ! /** original ipstring passed to the constructor */ ! private String ipstring; /** counter */ private long now; + /** number of ip's to compute */ + private long num; + /** 1st byte of the next ip address */ private short a; *************** *** 51,57 **** private short d; - /** original ipstring passed to the constructor */ - private String ipstring; - /** Creates a new instance of IPRange. * @param ipstring The targeted range of ip addresses in the form --- 57,60 ---- *************** *** 66,135 **** } ! /** resets the ip range. ! * you should not do this, it is better to get a new object via ! * the clone method. ! */ ! public void reset() { ! init(); ! } ! ! /** initializes the object data. */ ! private void init() { ! /* ! * this regex determines a string like "nnn.nnn.nnn.nnn/nn". ! * it will not check for bad values. This will be done later. ! */ ! final String regex = "([0-9]{1,3}[.]{1}){3}[0-9]{1,3}[/]{1}[0-9]{1,2}"; ! ! if (!this.ipstring.matches(regex)) { ! throw new IllegalArgumentException("bad ipstring"); ! } ! ! this.now = 0; ! ! String[] str = this.ipstring.split("[./]"); //syntax is "a.b.c.d/s" ! ! int ips = 32 - Integer.parseInt(str[4]); ! ! if (ips < 0) { ! throw new IllegalArgumentException("bad network"); ! } ! ! ! /* ! * number of addresses is 2^s ! * 2 are subtracted because we may not use ! * the first and the last address ! * (network and broadcast) ! */ ! this.num = (long) Math.pow(2, ips) - 2; ! ! ! //apply the ip byte values ! this.a = Short.parseShort(str[0]); ! this.b = Short.parseShort(str[1]); ! this.c = Short.parseShort(str[2]); ! this.d = Short.parseShort(str[3]); ! ! ! // check for bad values ! checkRangeOfValues(a); ! checkRangeOfValues(b); ! checkRangeOfValues(c); ! checkRangeOfValues(d); ! ! // a * 256 ^ 3 + b * 256 ^ 2 + c * 256 ^ 1 + d * 256 ^ 0 ! //double nA = (a * 16777216) + (b * 65536) + (c * 256) + d; ! //double over = nA % (this.num + 2); ! } ! ! /** checks whether the number is an unsigned byte. ! * @param i value to be checked */ ! private void checkRangeOfValues(int i) { ! if ((i > 255) || (i < 0)) { ! throw new IllegalArgumentException( ! "ip segment must be between 0 and 255"); ! } } --- 69,77 ---- } ! /** Clones the subnet. ! * @return a new, resetted instance of IpSubnet */ ! public Object clone() { ! return new IpSubnet(this.ipstring); } *************** *** 154,158 **** */ public Object next() throws NoSuchElementException { ! if (num <= now) { throw new NoSuchElementException(); } --- 96,100 ---- */ public Object next() throws NoSuchElementException { ! if (!this.hasNext()) { throw new NoSuchElementException(); } *************** *** 160,164 **** now++; ! d++; // increase d in advance, to skip the first address if (d > 255) { //leaving range of values for d --- 102,106 ---- now++; ! d++; // increase d in advance, to skip the network address if (d > 255) { //leaving range of values for d *************** *** 178,183 **** /* if the previous checks worked, this will ! * NEVER happen ... maybe throw sth.? */ } } --- 120,127 ---- /* if the previous checks worked, this will ! * NEVER happen */ + throw new InternalError("Error generating IP address. " + + "This should NEVER happen."); } } *************** *** 196,204 **** } ! /** Clones the subnet. ! * @return a new, resetted instance of IpSubnet */ ! public Object clone() { ! return new IpSubnet(this.ipstring); } --- 140,149 ---- } ! /** resets the ip range. ! * you should not do this, it is better to get a new object via ! * the clone method. */ ! public void reset() { ! init(); } *************** *** 210,212 **** return this.ipstring; } ! } \ No newline at end of file --- 155,212 ---- return this.ipstring; } ! ! /** checks whether the number is an unsigned byte. ! * @param i value to be checked ! */ ! private void checkRangeOfValues(int i) { ! if ((i > 255) || (i < 0)) { ! throw new IllegalArgumentException( ! "ip segment must be between 0 and 255"); ! } ! } ! ! /** initializes the object data. */ ! private void init() { ! // This regex determines a string like "nnn.nnn.nnn.nnn/nn". ! // It will not check for bad values. This will be done later. ! final String regex = "([0-9]{1,3}[.]{1}){3}[0-9]{1,3}[/]{1}[0-9]{1,2}"; ! ! if (!this.ipstring.matches(regex)) { ! throw new IllegalArgumentException("bad ipstring"); ! } ! ! this.now = 0; ! ! String[] str = this.ipstring.split("[./]"); //syntax is "a.b.c.d/s" ! ! int ips = 32 - Integer.parseInt(str[4]); ! ! if (ips < 0) { ! throw new IllegalArgumentException("bad network"); ! } ! ! /* ! * number of addresses is 2^s ! * 2 are subtracted because we may not use ! * the first and the last address ! * (network and broadcast) ! */ ! this.num = (long) Math.pow(2, ips) - 2; ! ! //apply the ip byte values ! this.a = Short.parseShort(str[0]); ! this.b = Short.parseShort(str[1]); ! this.c = Short.parseShort(str[2]); ! this.d = Short.parseShort(str[3]); ! ! // check for bad values ! this.checkRangeOfValues(a); ! this.checkRangeOfValues(b); ! this.checkRangeOfValues(c); ! this.checkRangeOfValues(d); ! ! // a * 256 ^ 3 + b * 256 ^ 2 + c * 256 ^ 1 + d * 256 ^ 0 ! //double nA = (a * 16777216) + (b * 65536) + (c * 256) + d; ! //double over = nA % (this.num + 2); ! } ! } |