首页 > 代码库 > byte 常用 操作

byte 常用 操作

/** * 低位在前,高位在后 * * @param data * @return */ private byte[] intToBytes(int value) { byte[] src = http://www.mamicode.com/new byte[4];"ObjectToByte异常", e); } finally { try { if (byteOutput != null) { byteOutput.close(); } if (objOutput != null) { objOutput.close(); } } catch (IOException e) { logger.error("ObjectToByte异常", e); } } return bytes; } /** * byte 转对象 * * @param bytes * @return */ public Object byteToObject(byte[] bytes) throws Exception { Object obj = null; ByteArrayInputStream byteInput = null; ObjectInputStream objectInput = null; try { // bytearray to object if (bytes != null) { byteInput = new ByteArrayInputStream(bytes); objectInput = new ObjectInputStream(byteInput); obj = objectInput.readObject(); byteInput.close(); objectInput.close(); } } catch (Exception e) { logger.error("ByteToObject异常", e); throw e; } finally { try { if (byteInput != null) { byteInput.close(); } if (objectInput != null) { objectInput.close(); } } catch (IOException e) { logger.error("byteToObject异常", e); } } return obj; } public byte[] streamToBytes(InputStream inputStream, int len) { /** * inputStream.read(要复制到得字节数组,起始位置下标,要复制的长度) * 该方法读取后input的下标会自动的后移,下次读取的时候还是从上次读取后移动到的下标开始读取 所以每次读取后就不需要在制定起始的下标了 */ byte[] bytes = new byte[len]; try { inputStream.read(bytes, 0, len); } catch (IOException e) { logger.error(ConstantUtils.LOG_EXCEPTION, e); } return bytes; }

byte 常用 操作