⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringutils.java

📁 java版源代码,里面包含很多源代码,大家可以看看.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.trulytech.mantis.util;

import java.security.*;
import java.text.*;
import java.util.Random;
import java.util.ArrayList;

/**
 * <p>Title: Mantis</p>
 * <p>Description: 字符处理类</p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author Wang Xian
 * @version 1.2
 */

public class StringUtils {

  /**
   * HashCode.
   */
  private static MessageDigest digest = null;

  /**
   * 生成Hash编码(MD5编码)
   * @param data 输入字符
   * @return Hash编码
   */
  public synchronized static final String hash(String data) {

    return hash(data.getBytes());

  }

  /**
   * 生成Hash编码(MD5编码)
   * @param data byte[]
   * @return String
   */
  public synchronized static final String hash(byte[] data) {
    if (digest == null) {
      try {
        digest = MessageDigest.getInstance("MD5");
      }
      catch (NoSuchAlgorithmException nsae) {
        System.err.println("Failed to load the MD5 MessageDigest. " +
                           "Jive will be unable to function normally.");
        nsae.printStackTrace();
      }
    }

    digest.update(data);
    return encodeHex(digest.digest());

  }

  /**
   * Turns an array of bytes into a String representing each byte as an
   * unsigned hex number.
   * <p>
   * Method by Santeri Paavolainen, Helsinki Finland 1996<br>
   * (c) Santeri Paavolainen, Helsinki Finland 1996<br>
   * Distributed under LGPL.
   *
   * @param bytes an array of bytes to convert to a hex-string
   * @return generated hex string
   */
  public static final String encodeHex(byte[] bytes) {
    StringBuffer buf = new StringBuffer(bytes.length * 2);
    int i;

    for (i = 0; i < bytes.length; i++) {
      if ( ( (int) bytes[i] & 0xff) < 0x10) {
        buf.append("0");
      }
      buf.append(Long.toString( (int) bytes[i] & 0xff, 16));
    }
    return buf.toString();
  }

  /**
   * Turns a hex encoded string into a byte array. It is specifically meant
   * to "reverse" the toHex(byte[]) method.
   *
   * @param hex a hex encoded String to transform into a byte array.
   * @return a byte array representing the hex String[
   */
  public static final byte[] decodeHex(String hex) {
    char[] chars = hex.toCharArray();
    byte[] bytes = new byte[chars.length / 2];
    int byteCount = 0;
    for (int i = 0; i < chars.length; i += 2) {
      byte newByte = 0x00;
      newByte |= hexCharToByte(chars[i]);
      newByte <<= 4;
      newByte |= hexCharToByte(chars[i + 1]);
      bytes[byteCount] = newByte;
      byteCount++;
    }
    return bytes;
  }

  /**
   * Returns the the byte value of a hexadecmical char (0-f). It's assumed
   * that the hexidecimal chars are lower case as appropriate.
   *
   * @param ch a hexedicmal character (0-f)
   * @return the byte value of the character (0x00-0x0F)
   */
  private static final byte hexCharToByte(char ch) {
    switch (ch) {
      case '0':
        return 0x00;
      case '1':
        return 0x01;
      case '2':
        return 0x02;
      case '3':
        return 0x03;
      case '4':
        return 0x04;
      case '5':
        return 0x05;
      case '6':
        return 0x06;
      case '7':
        return 0x07;
      case '8':
        return 0x08;
      case '9':
        return 0x09;
      case 'a':
        return 0x0A;
      case 'b':
        return 0x0B;
      case 'c':
        return 0x0C;
      case 'd':
        return 0x0D;
      case 'e':
        return 0x0E;
      case 'f':
        return 0x0F;
    }
    return 0x00;
  }

//*********************************************************************
   //* Base64 - a simple base64 encoder and decoder.
    //*
     //*     Copyright (c) 1999, Bob Withers - bwit@pobox.com
      //*
       //* This code may be freely used for any purpose, either personal
        //* or commercial, provided the authors copyright notice remains
         //* intact.
          //*********************************************************************

           /**
            * Base64编码
            *
            * @param data a String to encode.
            * @return a base64 encoded String.
            */
           public static String encodeBase64(String data) {
             return encodeBase64(data.getBytes());
           }

  /**
   * Encodes a byte array into a base64 String.
   *
   * @param data a byte array to encode.
   * @return a base64 encode String.
   */
  public static String encodeBase64(byte[] data) {
    int c;
    int len = data.length;
    StringBuffer ret = new StringBuffer( ( (len / 3) + 1) * 4);
    for (int i = 0; i < len; ++i) {
      c = (data[i] >> 2) & 0x3f;
      ret.append(cvt.charAt(c));
      c = (data[i] << 4) & 0x3f;
      if (++i < len)
        c |= (data[i] >> 4) & 0x0f;

      ret.append(cvt.charAt(c));
      if (i < len) {
        c = (data[i] << 2) & 0x3f;
        if (++i < len)
          c |= (data[i] >> 6) & 0x03;

        ret.append(cvt.charAt(c));
      }
      else {
        ++i;
        ret.append( (char) fillchar);
      }

      if (i < len) {
        c = data[i] & 0x3f;
        ret.append(cvt.charAt(c));
      }
      else {
        ret.append( (char) fillchar);
      }
    }
    return ret.toString();
  }

  /**
   * Base64 解码
   *
   * @param data a base64 encoded String to decode.
   * @return the decoded String.
   */
  public static String decodeBase64(String data) {
    return decodeBase64(data.getBytes());
  }

  /**
   * Decodes a base64 aray of bytes.
   *
   * @param data a base64 encode byte array to decode.
   * @return the decoded String.
   */
  public static String decodeBase64(byte[] data) {
    int c, c1;
    int len = data.length;
    StringBuffer ret = new StringBuffer( (len * 3) / 4);
    for (int i = 0; i < len; ++i) {
      c = cvt.indexOf(data[i]);
      ++i;
      c1 = cvt.indexOf(data[i]);
      c = ( (c << 2) | ( (c1 >> 4) & 0x3));
      ret.append( (char) c);
      if (++i < len) {
        c = data[i];
        if (fillchar == c)
          break;

        c = cvt.indexOf( (char) c);
        c1 = ( (c1 << 4) & 0xf0) | ( (c >> 2) & 0xf);
        ret.append( (char) c1);
      }

      if (++i < len) {
        c1 = data[i];
        if (fillchar == c1)
          break;

        c1 = cvt.indexOf( (char) c1);
        c = ( (c << 6) & 0xc0) | c1;
        ret.append( (char) c);
      }
    }
    return ret.toString();
  }

  private static final int fillchar = '=';
  private static final String cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      + "abcdefghijklmnopqrstuvwxyz"
      + "0123456789+/";

  /**
   *
   * (生成随机字符串)
   * Returns a random String of numbers and letters (lower and upper case)
   * of the specified length. The method uses the Random class that is
   * built-in to Java which is suitable for low to medium grade security uses.
   * This means that the output is only pseudo random, i.e., each number is
   * mathematically generated so is not truly random.<p>
   *
   * The specified length must be at least one. If not, the method will return
   * null.
   *
   * @param length the desired length of the random String to return.
   * @return a random String of numbers and letters of the specified length.
   */
  public static final String randomString(int length) {
    if (length < 1) {
      return null;
    }
    // Create a char buffer to put random letters and numbers in.
    char[] randBuffer = new char[length];
    for (int i = 0; i < randBuffer.length; i++) {
      randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
    }
    return new String(randBuffer);
  }

  /**
   * 产生随即数字
   * @param length int 数字位数
   * @return String
   */
  public static final String randomNumber(int length) {
    if (length < 1) {
      return null;
    }
    // Create a char buffer to put random letters and numbers in.
    char[] randBuffer = new char[length];
    for (int i = 0; i < randBuffer.length; i++) {
      randBuffer[i] = numbers[randGen.nextInt(9)];
    }
    return new String(randBuffer);
  }

  /**
   * Array of numbers and letters of mixed case. Numbers appear in the list
   * twice so that there is a more equal chance that a number will be picked.
   * We can use the array to get a random number or letter by picking a random
   * array index.
   */
  private static char[] numbersAndLetters = (
      "0123456789abcdefghijklmnopqrstuvwxyz" +
      "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();

  private static char[] numbers = (
      "0123456789").toCharArray();

  private static Random randGen = new Random();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -