From: <mrp...@us...> - 2014-03-23 15:13:16
|
Revision: 8009 http://sourceforge.net/p/bigdata/code/8009 Author: mrpersonick Date: 2014-03-23 15:13:12 +0000 (Sun, 23 Mar 2014) Log Message: ----------- trying again with the IPv4 prefix support Modified Paths: -------------- branches/RDR/bigdata-rdf/src/java/com/bigdata/rdf/internal/IVUtility.java branches/RDR/bigdata-rdf/src/java/com/bigdata/rdf/internal/impl/uri/IPAddrIV.java Added Paths: ----------- branches/RDR/bigdata-rdf/src/java/com/bigdata/rdf/internal/Inet4Address.java Modified: branches/RDR/bigdata-rdf/src/java/com/bigdata/rdf/internal/IVUtility.java =================================================================== --- branches/RDR/bigdata-rdf/src/java/com/bigdata/rdf/internal/IVUtility.java 2014-03-23 03:28:20 UTC (rev 8008) +++ branches/RDR/bigdata-rdf/src/java/com/bigdata/rdf/internal/IVUtility.java 2014-03-23 15:13:12 UTC (rev 8009) @@ -544,14 +544,10 @@ * as an IPAddrIV. We need to fix the Extension mechanism for URIs. * Extension is already used above. */ - try { - final byte[] addr = new byte[4]; - System.arraycopy(key, o, addr, 0, 4); - final InetAddress ip = InetAddress.getByAddress(addr); - return new IPAddrIV(ip);//, key[o+4]); - } catch (UnknownHostException ex) { - throw new RuntimeException(ex); - } + final byte[] addr = new byte[5]; + System.arraycopy(key, o, addr, 0, 5); + final Inet4Address ip = new Inet4Address(addr); + return new IPAddrIV(ip); } case XSDByte: { final byte x = key[o];//KeyBuilder.decodeByte(key[o]); Added: branches/RDR/bigdata-rdf/src/java/com/bigdata/rdf/internal/Inet4Address.java =================================================================== --- branches/RDR/bigdata-rdf/src/java/com/bigdata/rdf/internal/Inet4Address.java (rev 0) +++ branches/RDR/bigdata-rdf/src/java/com/bigdata/rdf/internal/Inet4Address.java 2014-03-23 15:13:12 UTC (rev 8009) @@ -0,0 +1,259 @@ +package com.bigdata.rdf.internal; + +import java.math.BigInteger; + +/** + * This class represents an Internet Protocol version 4 (IPv4) address. + * Defined by <a href="http://www.ietf.org/rfc/rfc790.txt"> + * <i>RFC 790: Assigned Numbers</i></a>, + * <a href="http://www.ietf.org/rfc/rfc1918.txt"> + * <i>RFC 1918: Address Allocation for Private Internets</i></a>, + * and <a href="http://www.ietf.org/rfc/rfc2365.txt"><i>RFC 2365: + * Administratively Scoped IP Multicast</i></a> + * + * <h4> <A NAME="format">Textual representation of IP addresses</a> </h4> + * + * Textual representation of IPv4 address used as input to methods + * takes one of the following forms: + * + * <blockquote><table cellpadding=0 cellspacing=0 summary="layout"> + * <tr><td><tt>d.d.d.d</tt></td></tr> + * <tr><td><tt>d.d.d</tt></td></tr> + * <tr><td><tt>d.d</tt></td></tr> + * <tr><td><tt>d</tt></td></tr> + * </table></blockquote> + * + * <p> When four parts are specified, each is interpreted as a byte of + * data and assigned, from left to right, to the four bytes of an IPv4 + * address. + + * <p> When a three part address is specified, the last part is + * interpreted as a 16-bit quantity and placed in the right most two + * bytes of the network address. This makes the three part address + * format convenient for specifying Class B net- work addresses as + * 128.net.host. + * + * <p> When a two part address is supplied, the last part is + * interpreted as a 24-bit quantity and placed in the right most three + * bytes of the network address. This makes the two part address + * format convenient for specifying Class A network addresses as + * net.host. + * + * <p> When only one part is given, the value is stored directly in + * the network address without any byte rearrangement. + * + * <p> For methods that return a textual representation as output + * value, the first form, i.e. a dotted-quad string, is used. + * + * <h4> The Scope of a Multicast Address </h4> + * + * Historically the IPv4 TTL field in the IP header has doubled as a + * multicast scope field: a TTL of 0 means node-local, 1 means + * link-local, up through 32 means site-local, up through 64 means + * region-local, up through 128 means continent-local, and up through + * 255 are global. However, the administrative scoping is preferred. + * Please refer to <a href="http://www.ietf.org/rfc/rfc2365.txt"> + * <i>RFC 2365: Administratively Scoped IP Multicast</i></a> + * @since 1.4 + */ + +public final class Inet4Address { + + + final long address; + + public Inet4Address(final byte addr[]) { + + if (addr == null || addr.length != 5) + throw new IllegalArgumentException(); + + long address = addr[4] & 0xFFl; + address |= ((addr[3] << 8) & 0xFF00l); + address |= ((addr[2] << 16) & 0xFF0000l); + address |= ((addr[1] << 24) & 0xFF000000l); + address |= ((addr[0] << 32) & 0xFF00000000l); + + this.address = address; + + } + + /** + * Returns the raw IP address of this <code>InetAddress</code> + * object. The result is in network byte order: the highest order + * byte of the address is in <code>getAddress()[0]</code>. + * + * @return the raw IP address of this object. + */ + public byte[] getBytes() { + + byte[] addr = new byte[5]; + addr[0] = (byte) ((address >>> 32) & 0xFF); + addr[1] = (byte) ((address >>> 24) & 0xFF); + addr[2] = (byte) ((address >>> 16) & 0xFF); + addr[3] = (byte) ((address >>> 8) & 0xFF); + addr[4] = (byte) (address & 0xFF); + + return addr; + + } + + /** + * Returns the IP address string in textual presentation form. + * + * @return the raw IP address in a string format. + * @since JDK1.0.2 + */ + public String toString() { + return numericToTextFormat(getBytes()); + } + + /** + * Returns a hashcode for this IP address. + * + * @return a hash code value for this IP address. + */ + public int hashCode() { + return BigInteger.valueOf(address).hashCode(); + } + + /** + * Compares this object against the specified object. + * The result is <code>true</code> if and only if the argument is + * not <code>null</code> and it represents the same IP address as + * this object. + * <p> + * Two instances of <code>InetAddress</code> represent the same IP + * address if the length of the byte arrays returned by + * <code>getAddress</code> is the same for both, and each of the + * array components is the same for the byte arrays. + * + * @param obj the object to compare against. + * @return <code>true</code> if the objects are the same; + * <code>false</code> otherwise. + * @see java.net.InetAddress#getAddress() + */ + public boolean equals(Object obj) { + return (obj != null) && (obj instanceof Inet4Address) && + (((Inet4Address)obj).address == address); + } + + // Utilities + /* + * Converts IPv4 binary address into a string suitable for presentation. + * + * @param src a byte array representing an IPv4 numeric address + * @return a String representing the IPv4 address in + * textual representation format + * @since 1.4 + */ + + static String numericToTextFormat(byte[] src) + { + final int netmask = src[4] & 0xff; + + return (src[0] & 0xff) + "." + (src[1] & 0xff) + "." + + (src[2] & 0xff) + "." + (src[3] & 0xff) + + (netmask < 32 ? "/" + netmask : ""); + } + + public static Inet4Address textToAddr(String... s) { + + byte[] res = new byte[5]; + + long val; + try { + switch (s.length) { + case 1: + /* + * When only one part is given, the value is stored directly in + * the network address without any byte rearrangement. + */ + + val = Long.parseLong(s[0]); + if (val < 0 || val > 0xffffffffL) + return null; + res[0] = (byte) ((val >> 24) & 0xff); + res[1] = (byte) (((val & 0xffffff) >> 16) & 0xff); + res[2] = (byte) (((val & 0xffff) >> 8) & 0xff); + res[3] = (byte) (val & 0xff); + res[4] = (byte) (33 & 0xff); + break; + case 2: + /* + * When a two part address is supplied, the last part is + * interpreted as a 24-bit quantity and placed in the right most + * three bytes of the network address. This makes the two part + * address format convenient for specifying Class A network + * addresses as net.host. + */ + + val = Integer.parseInt(s[0]); + if (val < 0 || val > 0xff) + return null; + res[0] = (byte) (val & 0xff); + val = Integer.parseInt(s[1]); + if (val < 0 || val > 0xffffff) + return null; + res[1] = (byte) ((val >> 16) & 0xff); + res[2] = (byte) (((val & 0xffff) >> 8) & 0xff); + res[3] = (byte) (val & 0xff); + res[4] = (byte) (33 & 0xff); + break; + case 3: + /* + * When a three part address is specified, the last part is + * interpreted as a 16-bit quantity and placed in the right most + * two bytes of the network address. This makes the three part + * address format convenient for specifying Class B net- work + * addresses as 128.net.host. + */ + for (int i = 0; i < 2; i++) { + val = Integer.parseInt(s[i]); + if (val < 0 || val > 0xff) + return null; + res[i] = (byte) (val & 0xff); + } + val = Integer.parseInt(s[2]); + if (val < 0 || val > 0xffff) + return null; + res[2] = (byte) ((val >> 8) & 0xff); + res[3] = (byte) (val & 0xff); + res[4] = (byte) (33 & 0xff); + break; + case 4: + /* + * When four parts are specified, each is interpreted as a byte + * of data and assigned, from left to right, to the four bytes + * of an IPv4 address. + */ + for (int i = 0; i < 4; i++) { + val = Integer.parseInt(s[i]); + if (val < 0 || val > 0xff) + return null; + res[i] = (byte) (val & 0xff); + } + res[4] = (byte) (33 & 0xff); + break; + case 5: + /* + * When five parts are specified, each is interpreted as a byte + * of data and assigned, from left to right, to the four bytes + * of an IPv4 address plus the one byte for the netmask. + */ + for (int i = 0; i < 5; i++) { + val = Integer.parseInt(s[i]); + if (val < 0 || val > 0xff) + return null; + res[i] = (byte) (val & 0xff); + } + break; + default: + return null; + } + } catch (NumberFormatException e) { + return null; + } + return new Inet4Address(res); + } + +} Property changes on: branches/RDR/bigdata-rdf/src/java/com/bigdata/rdf/internal/Inet4Address.java ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Modified: branches/RDR/bigdata-rdf/src/java/com/bigdata/rdf/internal/impl/uri/IPAddrIV.java =================================================================== --- branches/RDR/bigdata-rdf/src/java/com/bigdata/rdf/internal/impl/uri/IPAddrIV.java 2014-03-23 03:28:20 UTC (rev 8008) +++ branches/RDR/bigdata-rdf/src/java/com/bigdata/rdf/internal/impl/uri/IPAddrIV.java 2014-03-23 15:13:12 UTC (rev 8009) @@ -29,7 +29,6 @@ import java.io.ObjectOutput; import java.io.ObjectStreamException; import java.io.Serializable; -import java.net.InetAddress; import java.net.UnknownHostException; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -39,19 +38,14 @@ import com.bigdata.btree.BytesUtil.UnsignedByteArrayComparator; import com.bigdata.btree.keys.IKeyBuilder; -import com.bigdata.btree.keys.KeyBuilder; import com.bigdata.io.LongPacker; import com.bigdata.rdf.internal.DTE; -import com.bigdata.rdf.internal.ILexiconConfiguration; import com.bigdata.rdf.internal.IV; -import com.bigdata.rdf.internal.IVUtility; +import com.bigdata.rdf.internal.Inet4Address; import com.bigdata.rdf.internal.VTE; import com.bigdata.rdf.internal.impl.AbstractInlineIV; import com.bigdata.rdf.lexicon.LexiconRelation; -import com.bigdata.rdf.model.BigdataBNode; import com.bigdata.rdf.model.BigdataURI; -import com.bigdata.rdf.model.BigdataValueFactory; -import com.bigdata.rdf.spo.SPOKeyOrder; /** * Internal value representing an inline IP address. Uses the InetAddress @@ -62,7 +56,7 @@ * <p> * {@inheritDoc} */ -public class IPAddrIV<V extends BigdataURI> extends AbstractInlineIV<V, InetAddress> +public class IPAddrIV<V extends BigdataURI> extends AbstractInlineIV<V, Inet4Address> implements Serializable, URI { /** @@ -79,7 +73,7 @@ /** * The inline IP address. */ - private final InetAddress value; + private final Inet4Address value; /** * The cached string representation of this IP. @@ -101,7 +95,7 @@ */ private transient V uri; - public IV<V, InetAddress> clone(final boolean clearCache) { + public IV<V, Inet4Address> clone(final boolean clearCache) { final IPAddrIV<V> tmp = new IPAddrIV<V>(value);//, prefix); @@ -124,7 +118,7 @@ /** * Ctor with internal value specified. */ - public IPAddrIV(final InetAddress value) {//, final byte prefix) { + public IPAddrIV(final Inet4Address value) {//, final byte prefix) { /* * TODO Using XSDBoolean so that we can know how to decode this thing @@ -167,12 +161,25 @@ // log.debug(ip); - this.value = InetAddress.getByName(ip); + final String suffix = matcher.group(4); -// final String suffix = matcher.group(4); - // log.debug(suffix); + + final String[] s; + if (suffix != null) { + + s = new String[5]; + System.arraycopy(ip.split("\\.", -1), 0, s, 0, 4); + s[4] = suffix; + + } else { + + s = ip.split("\\.", -1); + + } + this.value = Inet4Address.textToAddr(s); + // this.prefix = suffix != null ? Byte.valueOf(suffix) : (byte) 33; } else { @@ -188,7 +195,7 @@ /** * Returns the inline value. */ - public InetAddress getInlineValue() throws UnsupportedOperationException { + public Inet4Address getInlineValue() throws UnsupportedOperationException { return value; } @@ -246,13 +253,7 @@ @Override public String getLocalName() { if (hostAddress == null) { - -// if (prefix < 33) { -// hostAddress = value.getHostAddress() + "/" + prefix; -// } else { - hostAddress = value.getHostAddress(); -// } - + hostAddress = value.toString(); } return hostAddress; } @@ -264,9 +265,8 @@ if (this == o) return true; if (o instanceof IPAddrIV) { - final InetAddress value2 = ((IPAddrIV<?>) o).value; -// final byte prefix2 = ((IPAddrIV<?>) o).prefix; - return value.equals(value2);// && prefix == prefix2; + final Inet4Address value2 = ((IPAddrIV<?>) o).value; + return value.equals(value2); } return false; } @@ -303,17 +303,7 @@ private byte[] key() { if (key == null) { - -// final IKeyBuilder kb = KeyBuilder.newInstance(); -// -// kb.append(value.getAddress()); -// -// kb.append(prefix); -// -// key = kb.getKey(); - - key = value.getAddress(); - + key = value.getBytes(); } return key; @@ -358,19 +348,7 @@ } private Object readResolve() throws ObjectStreamException { - - try { - - final InetAddress value = InetAddress.getByAddress(key); - - return new IPAddrIV(value); - - } catch (UnknownHostException ex) { - - throw new RuntimeException(ex); - - } - + return new Inet4Address(key); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |