Java Number Convert numToIPv4(StringBuilder host)

Here you can find the source of numToIPv4(StringBuilder host)

Description

num To I Pv

License

Open Source License

Declaration

public static String numToIPv4(StringBuilder host) 

Method Source Code

//package com.java2s;
/*/*w  w  w.jav a  2  s  .  c  o m*/
 * Copyright (c) NASK, NCSC
 * 
 * This file is part of HoneySpider Network 2.0.
 * 
 * This is a free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
    
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
    
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static String numToIPv4(StringBuilder host) {
        int i = findFirstMatch(host, ":/?", 0);
        if (i < 0) {
            i = host.length();
        }
        return numToIPv4(host, 0, i);
    }

    public static String numToIPv4(StringBuilder sb, int start, int endInd) {
        int end = findFirstMatch(sb, ":/?", start, endInd);
        if (end < 0) {
            end = endInd;
        }
        long ip;
        try {
            ip = Long.decode(sb.substring(start, end));
            if (ip <= 0xffffff) { //disallow 0.x.x.x format
                return null;
            }
        } catch (NumberFormatException e) {
            return null;
        }
        StringBuilder tmp = new StringBuilder();
        for (int i = 0; i < 3; i++) {
            tmp.insert(0, ip & 0xff);
            tmp.insert(0, '.');
            ip = ip >> 8;
        }
        tmp.insert(0, ip & 0xff);
        sb.replace(start, end, tmp.toString());
        return tmp.toString();

    }

    public static int findFirstMatch(StringBuilder sb, String chars, int begIndex) {
        return findFirstMatch(sb, chars, begIndex, sb.length());
    }

    public static int findFirstMatch(StringBuilder sb, String chars, int startIndex, int endIndex) {
        if (sb.length() < endIndex) {
            endIndex = sb.length();
        }
        if (startIndex >= endIndex) {
            return -1;
        }
        char tab[] = chars.toCharArray();
        int first = endIndex;
        for (int i = 0; i < tab.length; i++) {
            for (int j = startIndex; j < first; j++) {
                if (sb.codePointAt(j) == tab[i]) {
                    first = j;
                }
            }
        }
        if (first < endIndex) {
            return first;
        }
        return -1;
    }

    public static int findFirstMatch(StringBuilder sb, String[] ss, int startIndex) {
        return findFirstMatch(sb, ss, startIndex, sb.length());
    }

    public static int findFirstMatch(StringBuilder sb, String[] ss, int startIndex, int endIndex) {
        int first = endIndex;
        for (int i = 0; i < ss.length; i++) {
            int tmp = sb.indexOf(ss[i], startIndex);
            if (tmp >= 0 && tmp < first && tmp + ss[i].length() < endIndex) {
                first = tmp;
            }
        }
        if (first < endIndex) {
            return first;
        }
        return -1;
    }
}

Related

  1. getMoneyConversion(int money, int divisor)
  2. numToBytes(byte[] buffer, long value)
  3. numToBytes(byte[] buffer, long value, int radix)
  4. numToBytes(long value, byte[] b, int off, int len)
  5. numToInt(Number n)
  6. numToLetter(String input)
  7. numToPowerOf(int num, int toPowerOf)
  8. numToStringWithOrder(long count)
  9. numToWords(long num)