Utils.java :  » UnTagged » jtorctl2 » net » sanpei » common » Android Open Source

Android Open Source » UnTagged » jtorctl2 
jtorctl2 » net » sanpei » common » Utils.java
//Copyright 2010 333pei@gmail.com

package net.sanpei.common;

import java.lang.reflect.Field;

public class Utils {

  public static Object getField(String className, Object obj, String memberName) throws SecurityException, IllegalArgumentException, NoSuchFieldException,
      IllegalAccessException, ClassNotFoundException {
    Class<?> c = Class.forName(className);
    return getField(c, obj, memberName);
  }

  public static Object getField(Class<?> c, Object obj, String memberName) throws SecurityException, NoSuchFieldException, IllegalArgumentException,
      IllegalAccessException {
    Field f = c.getDeclaredField(memberName);
    boolean tmp = f.isAccessible();
    f.setAccessible(true);
    Object result = f.get(obj);
    f.setAccessible(tmp);
    return result;
  }

  public static String bytesToHexString(byte[] bytes) {
    return bytesToHexString(bytes, 0, bytes.length);
  }

  public static String bytesToHexString(byte[] bytes, int start, int offset) {
    String stmp = "";
    StringBuilder sb = new StringBuilder();
    for (int n = start; n < offset; n++) {
      stmp = (Integer.toHexString(bytes[n] & 0XFF));
      if (stmp.length() == 1) {
        sb.append("0");
      }
      sb.append(stmp);
    }
    return sb.toString().toUpperCase();
  }

  public static <T> void copyArray(T[] src, T[] dst, int dstIndex, boolean bzero) {
    int len = -1;
    if (src.length > dst.length - dstIndex) {
      len = dst.length - dstIndex;
    } else {
      len = src.length;
      if (bzero) {
        for (int i = src.length + dstIndex; i < dst.length; i++) {
          dst[i] = null;
        }
      }
    }
    for (int i = 0; i < len; i++) {
      dst[i + dstIndex] = src[i];
    }
  }

  public static <T> void copyArray(T[] src, T[] dst, boolean bzero) {
    copyArray(src, dst, 0, bzero);
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.