com.titilink.camel.rest.util.OtherUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.titilink.camel.rest.util.OtherUtil.java

Source

/**
 * Copyright 2005-2015 titilink
 *
 * The contents of this file are subject to the terms of one of the following
 * open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
 * 1.0 (the "Licenses"). You can select the license that you prefer but you may
 * not use this file except in compliance with one of these Licenses.
 *
 * You can obtain a copy of the Apache 2.0 license at
 * http://www.opensource.org/licenses/apache-2.0
 *
 * You can obtain a copy of the LGPL 3.0 license at
 * http://www.opensource.org/licenses/lgpl-3.0
 *
 * You can obtain a copy of the LGPL 2.1 license at
 * http://www.opensource.org/licenses/lgpl-2.1
 *
 * You can obtain a copy of the CDDL 1.0 license at
 * http://www.opensource.org/licenses/cddl1
 *
 * You can obtain a copy of the EPL 1.0 license at
 * http://www.opensource.org/licenses/eclipse-1.0
 *
 * See the Licenses for the specific language governing permissions and
 * limitations under the Licenses.
 *
 * Alternatively, you can obtain a royalty free commercial license with less
 * limitations, transferable or non-transferable, directly at
 * https://github.com/titilink/titilink-framework
 *
 * titilink is a registered trademark of titilink.inc
 */
package com.titilink.camel.rest.util;

import org.apache.commons.codec.binary.Base64;

import java.io.File;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * <p>
 * @author by kam
 * @date 2015/05/01
 * @since v1.0.0
 */
public final class OtherUtil {

    private static Random rand = new Random(OtherUtil.getCurrentTime());

    private static String localHostname = "";

    private static final int NANO_TO_MILL = 1000000;

    /**
     * ??255
     */
    private static final int HEX_255 = 0xFF;

    /**
     * ??2
     */
    private static final int DECIMAL_2 = 2;

    /**
     * ??16
     */
    private static final int DECIMAL_16 = 16;

    /**
     * ?????
     *
     * @param filename --???
     * @return String  --????
     */
    public static String removePath(String filename) {
        int i = filename.lastIndexOf('/');
        if (i == -1) {
            return filename;
        } else {
            return filename.substring(i + 1);
        }
    }

    /**
     * ??
     *
     * @param filename --??
     * @return long -- 0-????0
     */
    public static long getFileLastModified(String filename) {
        long l = 0;
        File f = new File(filename);
        if (f.exists()) {
            try {
                l = f.lastModified();
            } catch (SecurityException se) {
                l = 0;
                se.printStackTrace(); //NOPMD
            }
        }
        return l;
    }

    /**
     * ???
     *
     * @return String --??
     */
    public static String getLocalHostName() {
        if (localHostname.equals("")) {
            try {
                localHostname = InetAddress.getLocalHost().getHostName();
            } catch (UnknownHostException e) {
                localHostname = "UnknownHost";
            }
        }
        return localHostname;
    }

    /**
     * ?ip??char=2
     *
     * @return String --ip?
     */
    public static String getLocalHostIps() {
        StringBuffer sb = new StringBuffer();
        final char flag = 2;
        try {
            Enumeration<?> netInterfaces = NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
                Enumeration<InetAddress> ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    InetAddress inetAddress = ips.nextElement();
                    String ip = inetAddress.getHostAddress();
                    if (!inetAddress.isLoopbackAddress() && ip.indexOf(":") == -1) {
                        sb.append(ip).append(flag);
                    }
                }
            }
        } catch (Exception e) {
            return "";
        }
        return sb.toString();
    }

    /**
     * ??
     *
     * @return Random --?
     */
    public static Random getRandom() {
        return rand;
        // return new Random(OtherUtil.getCurrentTime());
    }

    /**
     * Generates pseudo-random long from specific range. Generated number is
     * great or equals to min parameter value and less then max parameter value.
     *
     * @param min lower (inclusive) boundary
     * @param max higher (exclusive) boundary
     * @return pseudo-random value
     */

    public static long randomLong(long min, long max) {
        return min + (long) (Math.random() * (max - min));
    }

    /**
     * 
     *
     * @param arg --
     */
    public static void clearArray(Object arg[]) {
        if (arg == null) {
            return;
        }
        for (int i = 0; i < arg.length; i++) {
            arg[i] = null;
        }
        // arg = null;
    }

    /**
     * Generates pseudo-random integer from specific range. Generated number is
     * great or equals to min parameter value and less then max parameter value.
     *
     * @param min lower (inclusive) boundary
     * @param max higher (exclusive) boundary
     * @return pseudo-random value
     */
    public static int randomInt(int min, int max) {
        return min + (int) (Math.random() * (max - min));
    }

    /**
     * ?sleep?object.wait
     *
     * @param lockObj  ?
     * @param sometime ??
     * @throws InterruptedException --
     */
    public static void blockSomeTime(final Object lockObj, long sometime) throws InterruptedException {
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }

        synchronized (lockObj) {
            long waitTime = sometime;
            long start = OtherUtil.getCurrentTime();
            try {
                for (;;) {
                    if (waitTime > 0) {
                        lockObj.wait(waitTime);
                    } else {
                        break;
                    }

                    waitTime = sometime - (OtherUtil.getCurrentTime() - start);
                }
            } catch (InterruptedException ex) {
                lockObj.notifyAll();
                throw ex;
            }
        }
    }

    /**
     * BASE64?
     *
     * @param str --?
     * @return --??
     */
    public static String strBase64Encode(String str) {
        Base64 b64 = new Base64();
        byte[] b = b64.encode(str.getBytes());
        return new String(b);
    }

    /**
     * ?BASE64?
     *
     * @param base64EncodeStr --?BASE64?
     * @return String          --??
     */
    public static String strBase64Decode(String base64EncodeStr) {
        Base64 b64 = new Base64();
        byte[] b = b64.decode(base64EncodeStr.getBytes());
        return new String(b);
    }

    /**
     * ?MD5????BASE64?
     *
     * @param str --??
     * @return --??
     */
    public static String md5AndBase64Encode(String str) {
        byte[] b = md5(str);
        if (b == null) {
            throw new RuntimeException("md5 encode err!");
        }
        Base64 b64 = new Base64();
        b = b64.encode(b);
        return new String(b);
    }

    /**
     * ?MD5?
     *
     * @param str --??
     * @return --??
     */
    public static byte[] md5(String str) {

        java.security.MessageDigest digest;
        try {
            digest = java.security.MessageDigest.getInstance("MD5");
            return digest.digest(str.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * ?
     *
     * @param info --?
     */
    public static void systemOut(String info) {
        //???
        PrintStream ps = System.out;
        ps.println(info);
    }

    /**
     * ?
     */
    public static void jvmGC() {
        //???
        Runtime rt = Runtime.getRuntime();
        rt.gc();
    }

    /**
     * ?
     *
     * @param o --?
     */
    public static void emptyBlock(Object o) {
        //???
        ; //NOPMD
    }

    /**
     * ip??
     *
     * @param ip --IP?
     * @return boolean  --??true?false
     */
    public static boolean checkip(String ip) {
        Pattern patt = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
        Matcher mat = patt.matcher(ip);
        return mat.matches();
    }

    /**
     * ?dns??ip?
     *
     * @param dnsip --DNS??
     * @return String[] --IP?
     */
    public static String[] getDnsIPs(String dnsip) {
        String ips[];
        try {
            InetAddress ias[] = InetAddress.getAllByName(dnsip);
            ips = new String[ias.length];
            for (int i = 0; i < ias.length; i++) {
                ips[i] = ias[i].getHostAddress();
                ias[i] = null;
            }
        } catch (UnknownHostException e) {
            ips = null;
        }
        return ips;
    }

    /**
     * ???
     *
     * @return long --?
     */
    public static long getCurrentTime() {
        return System.nanoTime() / NANO_TO_MILL;
    }

    /**
     * ???
     *
     * @param buf 
     * @return 
     */
    public static String parseByte2HexStr(byte[] buf) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & HEX_255);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase(Locale.US));
        }
        return sb.toString();

    }

    /**
     * 16?
     *
     * @param hexStr 
     * @return 
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) {
            return new byte[0];
        }
        byte[] result = new byte[hexStr.length() / DECIMAL_2];
        for (int i = 0; i < hexStr.length() / DECIMAL_2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * DECIMAL_2, i * DECIMAL_2 + 1), DECIMAL_16);
            int low = Integer.parseInt(hexStr.substring(i * DECIMAL_2 + 1, i * DECIMAL_2 + DECIMAL_2), DECIMAL_16);
            result[i] = (byte) (high * DECIMAL_16 + low);
        }
        return result;
    }

}