首页

关于camel-netty源码包中SubnetUtils网络链接工具类对转数组、范围检查及格式化等操作

标签:camel-netty,网络工具类,apache,util     发布时间:2018-04-02   

一、前言

关于camel-netty4-2.21.0.jar包中的org.apache.camel.component.netty4.util.SubnetUtils网络工具类,对ip地址有效验证、范围检查rangeCheck、获取网络地址getNetworkAddress、获取网路广播地址getBroadcastAddress、获取所有IP地址getAllAddresses等。

二、源码说明

package org.apache.camel.component.netty4.util;@b@@b@import java.util.regex.Matcher;@b@import java.util.regex.Pattern;@b@@b@public class SubnetUtils@b@{@b@  private static final String IP_ADDRESS = "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})";@b@  private static final String SLASH_FORMAT = "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,3})";@b@  private static final Pattern ADDRESS_PATTERN = Pattern.compile("(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})");@b@  private static final Pattern CIDR_PATTERN = Pattern.compile("(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,3})");@b@  private static final int NBITS = 32;@b@  private int netmask;@b@  private int address;@b@  private int network;@b@  private int broadcast;@b@  private boolean inclusiveHostCount;@b@@b@  public SubnetUtils(String cidrNotation)@b@  {@b@    calculate(cidrNotation);@b@  }@b@@b@  public SubnetUtils(String address, String mask)@b@  {@b@    calculate(toCidrNotation(address, mask));@b@  }@b@@b@  public boolean isInclusiveHostCount()@b@  {@b@    return this.inclusiveHostCount;@b@  }@b@@b@  public void setInclusiveHostCount(boolean inclusiveHostCount)@b@  {@b@    this.inclusiveHostCount = inclusiveHostCount;@b@  }@b@@b@  public final SubnetInfo getInfo()@b@  {@b@    return new SubnetInfo(this, null);@b@  }@b@@b@  private void calculate(String mask)@b@  {@b@    Matcher matcher = CIDR_PATTERN.matcher(mask);@b@@b@    if (matcher.matches()) {@b@      this.address = matchAddress(matcher);@b@@b@      int cidrPart = rangeCheck(Integer.parseInt(matcher.group(5)), 0, 32);@b@      for (int j = 0; j < cidrPart; ++j) {@b@        this.netmask |= 1 << 31 - j;@b@      }@b@@b@      this.network = (this.address & this.netmask);@b@@b@      this.broadcast = (this.network | this.netmask ^ 0xFFFFFFFF);@b@    } else {@b@      throw new IllegalArgumentException(new StringBuilder().append("Could not parse [").append(mask).append("]").toString());@b@    }@b@  }@b@@b@  private int toInteger(String address)@b@  {@b@    Matcher matcher = ADDRESS_PATTERN.matcher(address);@b@    if (matcher.matches())@b@      return matchAddress(matcher);@b@@b@    throw new IllegalArgumentException(new StringBuilder().append("Could not parse [").append(address).append("]").toString());@b@  }@b@@b@  private int matchAddress(Matcher matcher)@b@  {@b@    int addr = 0;@b@    for (int i = 1; i <= 4; ++i) {@b@      int n = rangeCheck(Integer.parseInt(matcher.group(i)), 0, 255);@b@      addr |= (n & 0xFF) << 8 * (4 - i);@b@    }@b@    return addr;@b@  }@b@@b@  private int[] toArray(int val)@b@  {@b@    int[] ret = new int[4];@b@    for (int j = 3; j >= 0; --j)@b@      ret[j] |= val >>> 8 * (3 - j) & 0xFF;@b@@b@    return ret;@b@  }@b@@b@  private String format(int[] octets)@b@  {@b@    StringBuilder str = new StringBuilder();@b@    for (int i = 0; i < octets.length; ++i) {@b@      str.append(octets[i]);@b@      if (i != octets.length - 1)@b@        str.append(".");@b@    }@b@@b@    return str.toString();@b@  }@b@@b@  private int rangeCheck(int value, int begin, int end)@b@  {@b@    if ((value >= begin) && (value <= end)) {@b@      return value;@b@    }@b@@b@    throw new IllegalArgumentException(new StringBuilder().append("Value [").append(value).append("] not in range [").append(begin).append(",").append(end).append("]").toString());@b@  }@b@@b@  int pop(int x)@b@  {@b@    x -= (x >>> 1 & 0x55555555);@b@    x = (x & 0x33333333) + (x >>> 2 & 0x33333333);@b@    x = x + (x >>> 4) & 0xF0F0F0F;@b@    x += (x >>> 8);@b@    x += (x >>> 16);@b@    return (x & 0x3F);@b@  }@b@@b@  private String toCidrNotation(String addr, String mask)@b@  {@b@    return new StringBuilder().append(addr).append("/").append(pop(toInteger(mask))).toString();@b@  }@b@@b@  public final class SubnetInfo@b@  {@b@    private static final long UNSIGNED_INT_MASK = 4294967295L;@b@@b@    private int netmask()@b@    {@b@      return SubnetUtils.access$000(this.this$0); }@b@@b@    private int network() {@b@      return SubnetUtils.access$100(this.this$0); }@b@@b@    private int address() {@b@      return SubnetUtils.access$200(this.this$0); }@b@@b@    private int broadcast() {@b@      return SubnetUtils.access$300(this.this$0);@b@    }@b@@b@    private long networkLong()@b@    {@b@      return (SubnetUtils.access$100(this.this$0) & 0xFFFFFFFF); }@b@@b@    private long broadcastLong() {@b@      return (SubnetUtils.access$300(this.this$0) & 0xFFFFFFFF);@b@    }@b@@b@    private int low() {@b@      return @b@        ((broadcastLong() - networkLong() > 1L) ? network() + 1 : (this.this$0.isInclusiveHostCount()) ? network() : @b@        0);@b@    }@b@@b@    private int high() {@b@      return @b@        ((broadcastLong() - networkLong() > 1L) ? broadcast() - 1 : (this.this$0.isInclusiveHostCount()) ? broadcast() : @b@        0);@b@    }@b@@b@    public boolean isInRange()@b@    {@b@      Matcher matcher = SubnetUtils.access$400().matcher(address);@b@      if (matcher.matches())@b@        return isInRange(SubnetUtils.access$500(this.this$0, address));@b@@b@      return false;@b@    }@b@@b@    private boolean isInRange()@b@    {@b@      long addLong = address & 0xFFFFFFFF;@b@      long lowLong = low() & 0xFFFFFFFF;@b@      long highLong = high() & 0xFFFFFFFF;@b@      return ((addLong >= lowLong) && (addLong <= highLong));@b@    }@b@@b@    public String getBroadcastAddress() {@b@      return SubnetUtils.access$700(this.this$0, SubnetUtils.access$600(this.this$0, broadcast()));@b@    }@b@@b@    public String getNetworkAddress() {@b@      return SubnetUtils.access$700(this.this$0, SubnetUtils.access$600(this.this$0, network()));@b@    }@b@@b@    public String getNetmask() {@b@      return SubnetUtils.access$700(this.this$0, SubnetUtils.access$600(this.this$0, netmask()));@b@    }@b@@b@    public String getAddress() {@b@      return SubnetUtils.access$700(this.this$0, SubnetUtils.access$600(this.this$0, address()));@b@    }@b@@b@    public String getLowAddress()@b@    {@b@      return SubnetUtils.access$700(this.this$0, SubnetUtils.access$600(this.this$0, low()));@b@    }@b@@b@    public String getHighAddress()@b@    {@b@      return SubnetUtils.access$700(this.this$0, SubnetUtils.access$600(this.this$0, high()));@b@    }@b@@b@    @Deprecated@b@    public int getAddressCount()@b@    {@b@      long countLong = getAddressCountLong();@b@      if (countLong > 2147483647L) {@b@        throw new RuntimeException("Count is larger than an integer: " + countLong);@b@      }@b@@b@      return (int)countLong;@b@    }@b@@b@    public long getAddressCountLong()@b@    {@b@      long b = broadcastLong();@b@      long n = networkLong();@b@      long count = b - n + ((this.this$0.isInclusiveHostCount()) ? 1 : -1);@b@      return ((count < 0L) ? 0L : count);@b@    }@b@@b@    public int asInteger() {@b@      return SubnetUtils.access$500(this.this$0, address);@b@    }@b@@b@    public String getCidrSignature() {@b@      return SubnetUtils.access$800(this.this$0, @b@        SubnetUtils.access$700(this.this$0, @b@        SubnetUtils.access$600(this.this$0, @b@        address())), @b@        SubnetUtils.access$700(this.this$0, @b@        SubnetUtils.access$600(this.this$0, @b@        netmask())));@b@    }@b@@b@    public String[] getAllAddresses()@b@    {@b@      int ct = getAddressCount();@b@      String[] addresses = new String[ct];@b@      if (ct == 0)@b@        return addresses;@b@@b@      int j = 0;@b@      for (int add = low(); add <= high(); ++add) {@b@        addresses[j] = SubnetUtils.access$700(this.this$0, SubnetUtils.access$600(this.this$0, add));@b@        ++j;@b@      }@b@      return addresses;@b@    }@b@@b@    public String toString()@b@    {@b@      StringBuilder buf = new StringBuilder();@b@      buf.append("CIDR Signature:\t[").append(getCidrSignature()).append("]")@b@        .append(" Netmask: [")@b@        .append(getNetmask()).append("]\n")@b@        .append("Network:\t[")@b@        .append(getNetworkAddress()).append("]\n")@b@        .append("Broadcast:\t[")@b@        .append(getBroadcastAddress()).append("]\n")@b@        .append("First Address:\t[")@b@        .append(getLowAddress()).append("]\n")@b@        .append("Last Address:\t[")@b@        .append(getHighAddress()).append("]\n")@b@        .append("# Addresses:\t[")@b@        .append(getAddressCount()).append("]\n");@b@      return buf.toString();@b@    }@b@  }@b@}