Java IP Address to Long ip2long(String ip)

Here you can find the source of ip2long(String ip)

Description

iplong

License

Apache License

Declaration

public static long ip2long(String ip) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;

import java.io.IOException;

import java.io.StringReader;

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static long ip2long(String ip) {
        long ipNumber = 0;
        String[] ips = ip.split("[.]");
        for (int i = 0; i < 4; ++i) {
            ipNumber = ipNumber << 8 | Integer.parseInt(ips[i]);
        }//from  w ww . j av a2 s  . c  om
        return ipNumber;
    }

    public static String[] split(String s, String delimiter) {
        if (s == null || delimiter == null) {
            return new String[0];
        }

        if (!s.endsWith(delimiter)) {
            s += delimiter;
        }

        s = s.trim();

        if (s.equals(delimiter)) {
            return new String[0];
        }

        List<String> nodeValues = new ArrayList<String>();

        if (delimiter.equals("\n") || delimiter.equals("\r")) {
            try {
                BufferedReader br = new BufferedReader(new StringReader(s));

                String line = null;

                while ((line = br.readLine()) != null) {
                    nodeValues.add(line);
                }

                br.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        } else {
            int offset = 0;
            int pos = s.indexOf(delimiter, offset);

            while (pos != -1) {
                nodeValues.add(s.substring(offset, pos));

                offset = pos + delimiter.length();
                pos = s.indexOf(delimiter, offset);
            }
        }

        return (String[]) nodeValues.toArray(new String[0]);
    }

    public static boolean[] split(String s, String delimiter, boolean x) {
        String[] array = split(s, delimiter);
        boolean[] newArray = new boolean[array.length];

        for (int i = 0; i < array.length; i++) {
            boolean value = x;

            try {
                value = Boolean.valueOf(array[i]).booleanValue();
            } catch (Exception e) {
            }

            newArray[i] = value;
        }

        return newArray;
    }

    public static double[] split(String s, String delimiter, double x) {
        String[] array = split(s, delimiter);
        double[] newArray = new double[array.length];

        for (int i = 0; i < array.length; i++) {
            double value = x;

            try {
                value = Double.parseDouble(array[i]);
            } catch (Exception e) {
            }

            newArray[i] = value;
        }

        return newArray;
    }

    public static float[] split(String s, String delimiter, float x) {
        String[] array = split(s, delimiter);
        float[] newArray = new float[array.length];

        for (int i = 0; i < array.length; i++) {
            float value = x;

            try {
                value = Float.parseFloat(array[i]);
            } catch (Exception e) {
            }

            newArray[i] = value;
        }

        return newArray;
    }

    public static int[] split(String s, String delimiter, int x) {
        String[] array = split(s, delimiter);
        int[] newArray = new int[array.length];

        for (int i = 0; i < array.length; i++) {
            int value = x;

            try {
                value = Integer.parseInt(array[i]);
            } catch (Exception e) {
            }

            newArray[i] = value;
        }

        return newArray;
    }

    public static long[] split(String s, String delimiter, long x) {
        String[] array = split(s, delimiter);
        long[] newArray = new long[array.length];

        for (int i = 0; i < array.length; i++) {
            long value = x;

            try {
                value = Long.parseLong(array[i]);
            } catch (Exception e) {
            }

            newArray[i] = value;
        }

        return newArray;
    }

    public static short[] split(String s, String delimiter, short x) {
        String[] array = split(s, delimiter);
        short[] newArray = new short[array.length];

        for (int i = 0; i < array.length; i++) {
            short value = x;

            try {
                value = Short.parseShort(array[i]);
            } catch (Exception e) {
            }

            newArray[i] = value;
        }

        return newArray;
    }
}

Related

  1. ip2Long(final String ip)
  2. ip2long(String ip)
  3. ip2long(String ip)
  4. ip2long(String ip)
  5. ip2Long(String ip)
  6. ip2long(String ip)
  7. ip2long(String ip)
  8. ip2Long(String ip)
  9. ip2Long(String ipAddress)