欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java處理字節(jié)的常用工具類

 更新時間:2018年03月25日 10:00:09   作者:Smile_Pride  
這篇文章主要為大家詳細介紹了java處理字節(jié)的常用工具類,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

處理字節(jié)的常用工具類方法,供大家參考,具體內容如下

package com.demo.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.charset.Charset;
import java.util.Arrays;

/**
 * 處理字節(jié)的常用工具類方法
 * @author dongyangyang
 * @Date 2017/3/13 12:31
 * @Version 1.0
 *
 */
public class ByteUtils {

 /**
  * 構造新字節(jié)時需要與的值表
  */
 private static final byte[] BUILD_BYTE_TABLE = new byte[] { (byte) 128, (byte) 64, (byte) 32, (byte) 16, (byte) 8, (byte) 4, (byte) 2, (byte) 1 };

 private ByteUtils() {}

 /**
  * short轉換到字節(jié)數組
  * 
  * @param number
  *   需要轉換的數據。
  * @return 轉換后的字節(jié)數組。
  */
 public static byte[] shortToByte(short number) {
  byte[] b = new byte[2];
  for (int i = 1; i >= 0; i--) {
   b[i] = (byte) (number % 256);
   number >>= 8;
  }
  return b;
 }

 /**
  * 字節(jié)到short轉換
  * 
  * @param b
  *   short的字節(jié)數組
  * @return short數值。
  */
 public static short byteToShort(byte[] b) {
  return (short) ((((b[0] & 0xff) << 8) | b[1] & 0xff));
 }

 /**
  * 整型轉換到字節(jié)數組
  * 
  * @param number
  *   整形數據。
  * @return 整形數據的字節(jié)數組。
  */
 public static byte[] intToByte(int number) {
  byte[] b = new byte[4];
  for (int i = 3; i >= 0; i--) {
   b[i] = (byte) (number % 256);
   number >>= 8;
  }
  return b;
 }

 /**
  * 字節(jié)數組到整型轉換
  * 
  * @param b
  *   整形數據的字節(jié)數組。
  * @return 字節(jié)數組轉換成的整形數據。
  */
 public static int byteToInt(byte[] b) {
  return ((((b[0] & 0xff) << 24) | ((b[1] & 0xff) << 16) | ((b[2] & 0xff) << 8) | (b[3] & 0xff)));
 }

 /**
  * long轉換到字節(jié)數組
  * 
  * @param number
  *   長整形數據。
  * @return 長整形轉換成的字節(jié)數組。
  */
 public static byte[] longToByte(long number) {
  byte[] b = new byte[8];
  for (int i = 7; i >= 0; i--) {
   b[i] = (byte) (number % 256);
   number >>= 8;
  }
  return b;
 }

 /**
  * 字節(jié)數組到整型的轉換
  * 
  * @param b
  *   長整形字節(jié)數組。
  * @return 長整形數據。
  */
 public static long byteToLong(byte[] b) {
  return ((((long) b[0] & 0xff) << 56) | (((long) b[1] & 0xff) << 48) | (((long) b[2] & 0xff) << 40) | (((long) b[3] & 0xff) << 32) | (((long) b[4] & 0xff) << 24)
    | (((long) b[5] & 0xff) << 16) | (((long) b[6] & 0xff) << 8) | ((long) b[7] & 0xff));
 }

 /**
  * double轉換到字節(jié)數組
  * 
  * @param d
  *   雙精度浮點。
  * @return 雙精度浮點的字節(jié)數組。
  */
 public static byte[] doubleToByte(double d) {
  byte[] bytes = new byte[8];
  long l = Double.doubleToLongBits(d);
  for (int i = 0; i < bytes.length; i++) {
   bytes[i] = Long.valueOf(l).byteValue();
   l = l >> 8;
  }
  return bytes;
 }

 /**
  * 字節(jié)數組到double轉換
  * 
  * @param b
  *   雙精度浮點字節(jié)數組。
  * @return 雙精度浮點數據。
  */
 public static double byteToDouble(byte[] b) {
  long l;
  l = b[0];
  l &= 0xff;
  l |= ((long) b[1] << 8);
  l &= 0xffff;
  l |= ((long) b[2] << 16);
  l &= 0xffffff;
  l |= ((long) b[3] << 24);
  l &= 0xffffffffl;
  l |= ((long) b[4] << 32);
  l &= 0xffffffffffl;

  l |= ((long) b[5] << 40);
  l &= 0xffffffffffffl;
  l |= ((long) b[6] << 48);
  l &= 0xffffffffffffffl;

  l |= ((long) b[7] << 56);

  return Double.longBitsToDouble(l);
 }

 /**
  * float轉換到字節(jié)數組
  * 
  * @param d
  *   浮點型數據。
  * @return 浮點型數據轉換后的字節(jié)數組。
  */
 public static byte[] floatToByte(float d) {
  byte[] bytes = new byte[4];
  int l = Float.floatToIntBits(d);
  for (int i = 0; i < bytes.length; i++) {
   bytes[i] = Integer.valueOf(l).byteValue();
   l = l >> 8;
  }
  return bytes;
 }

 /**
  * 字節(jié)數組到float的轉換
  * 
  * @param b
  *   浮點型數據字節(jié)數組。
  * @return 浮點型數據。
  */
 public static float byteToFloat(byte[] b) {
  int l;
  l = b[0];
  l &= 0xff;
  l |= ((long) b[1] << 8);
  l &= 0xffff;
  l |= ((long) b[2] << 16);
  l &= 0xffffff;
  l |= ((long) b[3] << 24);
  l &= 0xffffffffl;

  return Float.intBitsToFloat(l);
 }

 /**
  * 字符串到字節(jié)數組轉換
  * 
  * @param s
  *   字符串。
  * @param charset
  *   字符編碼
  * @return 字符串按相應字符編碼編碼后的字節(jié)數組。
  */
 public static byte[] stringToByte(String s, Charset charset) {
  return s.getBytes(charset);
 }

 /**
  * 字節(jié)數組帶字符串的轉換
  * 
  * @param b
  *   字符串按指定編碼轉換的字節(jié)數組。
  * @param charset
  *   字符編碼。
  * @return 字符串。
  */
 public static String byteToString(byte[] b, Charset charset) {
  return new String(b, charset);
 }

 /**
  * 對象轉換成字節(jié)數組。
  * 
  * @param obj
  *   字節(jié)數組。
  * @return 對象實例相應的序列化后的字節(jié)數組。
  * @throws IOException
  */
 public static byte[] objectToByte(Object obj) throws IOException {
  ByteArrayOutputStream buff = new ByteArrayOutputStream();
  ObjectOutputStream out = new ObjectOutputStream(buff);
  out.writeObject(obj);
  try {
   return buff.toByteArray();
  } finally {
   out.close();
  }
 }

 /**
  * 序死化字節(jié)數組轉換成實際對象。
  * 
  * @param b
  *   字節(jié)數組。
  * @return 對象。
  * @throws IOException
  * @throws ClassNotFoundException
  */
 public static Object byteToObject(byte[] b) throws IOException, ClassNotFoundException {
  ByteArrayInputStream buff = new ByteArrayInputStream(b);
  ObjectInputStream in = new ObjectInputStream(buff);
  Object obj = in.readObject();
  try {
   return obj;
  } finally {
   in.close();
  }
 }

 /**
  * 比較兩個字節(jié)的每一個bit位是否相等.
  * 
  * @param a
  *   比較的字節(jié).
  * @param b
  *   比較的字節(jié)
  * @return ture 兩個字節(jié)每一位都相等,false有至少一位不相等.
  */
 public static boolean equalsBit(byte a, byte b) {
  return Arrays.equals(byteToBitArray(a), byteToBitArray(b));
 }

 /**
  * 比較兩個數組中的每一個字節(jié),兩個字節(jié)必須二進制字節(jié)碼每一位都相同才表示兩個 byte相同.
  * 
  * @param a
  *   比較的字節(jié)數組.
  * @param b
  *   被比較的字節(jié)數.
  * @return ture每一個元素的每一位兩個數組都是相等的,false至少有一位不相等.
  */
 public static boolean equalsBit(byte[] a, byte[] b) {
  if (a == b) {
   return true;
  }
  if (a == null || b == null) {
   return false;
  }

  int length = a.length;
  if (b.length != length) {
   return false;
  }

  for (int count = 0; count < a.length; count++) {
   if (!equalsBit(a[count], b[count])) {
    return false;
   }
  }
  return true;
 }

 /**
  * 返回某個字節(jié)的bit組成的字符串.
  * 
  * @param b
  *   字節(jié).
  * @return Bit位組成的字符串.
  */
 public static String bitString(byte b) {
  StringBuilder buff = new StringBuilder();
  boolean[] array = byteToBitArray(b);
  for (int i = 0; i < array.length; i++) {
   buff.append(array[i] ? 1 : 0);
  }
  return buff.toString();
 }

 /**
  * 計算出給定byte中的每一位,并以一個布爾數組返回. true表示為1,false表示為0.
  * 
  * @param b
  *   字節(jié).
  * @return 指定字節(jié)的每一位bit組成的數組.
  */
 public static boolean[] byteToBitArray(byte b) {
  boolean[] buff = new boolean[8];
  int index = 0;
  for (int i = 7; i >= 0; i--) {
   buff[index++] = ((b >>> i) & 1) == 1;
  }
  return buff;
 }

 /**
  * 返回指定字節(jié)中指定bit位,true為1,false為0. 指定的位從0-7,超出將拋出數據越界異常.
  *
  * @param b
  *   需要判斷的字節(jié).
  * @param index
  *   字節(jié)中指定位.
  * @return 指定位的值.
  */
 public static boolean byteBitValue(byte b, int index) {
  return byteToBitArray(b)[index];
 }

 /**
  * 根據布爾數組表示的二進制構造一個新的字節(jié).
  * 
  * @param values
  *   布爾數組,其中true表示為1,false表示為0.
  * @return 構造的新字節(jié).
  */
 public static byte buildNewByte(boolean[] values) {
  byte b = 0;
  for (int i = 0; i < 8; i++) {
   if (values[i]) {
    b |= BUILD_BYTE_TABLE[i];
   }
  }
  return b;
 }

 /**
  * 將指定字節(jié)中的某個bit位替換成指定的值,true代表1,false代表0.
  * 
  * @param b
  *   需要被替換的字節(jié).
  * @param index
  *   位的序號,從0開始.超過7將拋出越界異常.
  * @param newValue
  *   新的值.
  * @return 替換好某個位值的新字節(jié).
  */
 public static byte changeByteBitValue(byte b, int index, boolean newValue) {
  boolean[] bitValues = byteToBitArray(b);
  bitValues[index] = newValue;
  return buildNewByte(bitValues);
 }

 /**
  * 將指定的IP地址轉換成字節(jié)表示方式. IP數組的每一個數字都不能大于255,否則將拋出IllegalArgumentException異常.
  *
  * @param ipNums
  *   IP地址數組.
  * @return IP地址字節(jié)表示方式.
  */
 public static byte[] ipAddressBytes(String address) {
  if (address == null || address.length() < 0 || address.length() > 15) {
   throw new IllegalArgumentException("Invalid IP address.");
  }

  final int ipSize = 4;// 最大IP位數
  final char ipSpace = '.';// IP數字的分隔符
  int[] ipNums = new int[ipSize];
  StringBuilder number = new StringBuilder();// 當前操作的數字
  StringBuilder buff = new StringBuilder(address);
  int point = 0;// 當前操作的數字下標,最大到3.
  char currentChar;
  for (int i = 0; i < buff.length(); i++) {
   currentChar = buff.charAt(i);
   if (ipSpace == currentChar) {
    // 當前位置等于最大于序號后,還有字符沒有處理表示這是一個錯誤的IP.
    if (point == ipSize - 1 && buff.length() - (i + 1) > 0) {
     throw new IllegalArgumentException("Invalid IP address.");
    }
    ipNums[point++] = Integer.parseInt(number.toString());
    number.delete(0, number.length());
   } else {
    number.append(currentChar);
   }
  }
  ipNums[point] = Integer.parseInt(number.toString());

  byte[] ipBuff = new byte[ipSize];
  int pointNum = 0;
  for (int i = 0; i < 4; i++) {
   pointNum = Math.abs(ipNums[i]);
   if (pointNum > 255) {
    throw new IllegalArgumentException("Invalid IP address.");
   }
   ipBuff[i] = (byte) (pointNum & 0xff);
  }

  return ipBuff;
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 詳解使用Spring Cloud Consul實現服務的注冊和發(fā)現

    詳解使用Spring Cloud Consul實現服務的注冊和發(fā)現

    這篇文章主要介紹了詳解使用Spring Cloud Consul實現服務的注冊和發(fā)現,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • 深入理解Spring Boot屬性配置文件

    深入理解Spring Boot屬性配置文件

    這篇文章主要給大家深入的介紹了關于Spring Boot屬性配置文件的相關資料,文中介紹的很詳細,相信對大家具有一定的參考借鑒價值,需要的朋友們下面來一起看看吧。
    2017-02-02
  • SpringBoot  jdbctemplate使用方法解析

    SpringBoot jdbctemplate使用方法解析

    這篇文章主要介紹了SpringBoot jdbctemplate使用方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • Spring boot將配置屬性注入到bean類中

    Spring boot將配置屬性注入到bean類中

    本篇文章主要介紹了Spring boot將配置屬性注入到bean類中,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • java中Consumer接口的使用教程詳解

    java中Consumer接口的使用教程詳解

    Java?8?引入了?java.util.function?包,其中包含了一些常用的函數式接口,Consumer?接口是其中一個函數式接口,用于表示接受一個輸入參數并執(zhí)行某種操作的操作者,下面我們就來學習一下他的具體使用吧
    2023-12-12
  • 如何基于Idea遠程調試tomcat war包及jar包

    如何基于Idea遠程調試tomcat war包及jar包

    這篇文章主要介紹了如何基于Idea遠程調試tomcat war包及jar包,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • JAVA實現FTP斷點上傳的方法

    JAVA實現FTP斷點上傳的方法

    這篇文章主要介紹了JAVA實現FTP斷點上傳的方法,涉及java使用FTP實現文件傳輸的相關技巧,需要的朋友可以參考下
    2015-06-06
  • 一文帶你了解RabbitMQ消息轉換器

    一文帶你了解RabbitMQ消息轉換器

    這篇文章主要為大家詳細介紹了RabbitMQ中消息轉換器的相關知識,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以了解一下
    2023-04-04
  • Spring Boot Admin郵件警報整合過程解析

    Spring Boot Admin郵件警報整合過程解析

    這篇文章主要介紹了Spring Boot Admin郵件警報整合過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • 解決idea?中?SpringBoot?點擊運行沒反應按鈕成灰色的問題

    解決idea?中?SpringBoot?點擊運行沒反應按鈕成灰色的問題

    在使用 Spring Boot 開發(fā)項目時,可能會遇到一個問題:點擊運行按鈕后,控制臺沒有任何輸出,項目界面也沒有顯示,這種情況可能是由多種原因導致的,本文將介紹一些常見的解決方法,需要的朋友可以參考下
    2023-08-08

最新評論