package com.yuyh.library.utils.data; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; /** * @author yuyh. * @date 16/4/9. */ public class ByteUtils { /** * byte[] 转为 对象 * * @param bytes * @return */ public static Object byteToObject(byte[] bytes) throws Exception { ObjectInputStream ois = null; try { ois = new ObjectInputStream(new ByteArrayInputStream(bytes)); return ois.readObject(); } finally { if (ois != null) ois.close(); } } /** * 对象 转为 byte[] * * @param obj * @return */ public static byte[] objectToByte(Object obj) throws Exception { ObjectOutputStream oos = null; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bos); oos.writeObject(obj); return bos.toByteArray(); } finally { if (oos != null) oos.close(); } } public static void byteToBit(byte[] bytes, StringBuilder sb) { for (int i = 0; i < Byte.SIZE * bytes.length; i++) sb.append((bytes[i / Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' : '1'); } public static String byteToBit(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < Byte.SIZE * bytes.length; i++) sb.append((bytes[i / Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' : '1'); return sb.toString(); } }