com.beetle.framework.util.OtherUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.beetle.framework.util.OtherUtil.java

Source

/*
 * BJAF - Beetle J2EE Application Framework
 * J2EE??
 * ?2003-2015  (www.beetlesoft.net)
 * 
 * ??
 *<http://www.apache.org/licenses/LICENSE-2.0>
 *??????
 *
 * ??
 *  <yuhaodong@gmail.com/>.
 */
package com.beetle.framework.util;

import com.beetle.framework.AppRuntimeException;
import org.apache.commons.codec.binary.Base64;

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

public final class OtherUtil {

    private static Random rand = new Random(System.currentTimeMillis());
    private static String localHostname = "";

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

    /**
     * ?
     * 
     * @param filename
     * @return 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();
            }
        }
        return l;
    }

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

    /**
     * ?ip??char=2
     * 
     * @return
     */
    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();
    }

    public static Random getRandom() {
        return rand;
        // return new Random(System.currentTimeMillis());
    }

    /**
     * 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 final void blockSomeTime(final Object lockObj, long sometime) throws InterruptedException {
        if (Thread.interrupted())
            throw new InterruptedException();
        synchronized (lockObj) {
            long waitTime = sometime;
            long start = System.currentTimeMillis();
            try {
                for (;;) {
                    lockObj.wait(waitTime);
                    waitTime = sometime - (System.currentTimeMillis() - start);
                    if (waitTime <= 0) {
                        break;
                    }
                }
            } catch (InterruptedException ex) {
                lockObj.notify();
                throw ex;
            }
        }
    }

    public static String strBase64Encode(String str) {
        Base64 b64 = new Base64();
        byte[] b = b64.encode(str.getBytes());
        return new String(b);
    }

    public static String strBase64Decode(String base64EncodeStr) {
        Base64 b64 = new Base64();
        byte[] b = b64.decode(base64EncodeStr.getBytes());
        return new String(b);
    }

    public static final String md5AndBase64Encode(String str) {
        byte[] b = md5(str);
        if (b == null) {
            throw new AppRuntimeException("md5 encode err!");
        }
        Base64 b64 = new Base64();
        b = b64.encode(b);
        return new String(b);
    }

    public static final byte[] md5(String str) {

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

    /**
     * ip??
     * 
     * @param ip
     * @return
     */
    public final 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
     * @return
     */
    public final 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;
    }

    private static final int NANO_TO_MILL = 1000000;

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