最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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é)的常用工具類(lèi)方法
 * @author dongyangyang
 * @Date 2017/3/13 12:31
 * @Version 1.0
 *
 */
public class ByteUtils {

 /**
  * 構(gòu)造新字節(jié)時(shí)需要與的值表
  */
 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轉(zhuǎn)換到字節(jié)數(shù)組
  * 
  * @param number
  *   需要轉(zhuǎn)換的數(shù)據(jù)。
  * @return 轉(zhuǎn)換后的字節(jié)數(shù)組。
  */
 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轉(zhuǎn)換
  * 
  * @param b
  *   short的字節(jié)數(shù)組
  * @return short數(shù)值。
  */
 public static short byteToShort(byte[] b) {
  return (short) ((((b[0] & 0xff) << 8) | b[1] & 0xff));
 }

 /**
  * 整型轉(zhuǎn)換到字節(jié)數(shù)組
  * 
  * @param number
  *   整形數(shù)據(jù)。
  * @return 整形數(shù)據(jù)的字節(jié)數(shù)組。
  */
 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é)數(shù)組到整型轉(zhuǎn)換
  * 
  * @param b
  *   整形數(shù)據(jù)的字節(jié)數(shù)組。
  * @return 字節(jié)數(shù)組轉(zhuǎn)換成的整形數(shù)據(jù)。
  */
 public static int byteToInt(byte[] b) {
  return ((((b[0] & 0xff) << 24) | ((b[1] & 0xff) << 16) | ((b[2] & 0xff) << 8) | (b[3] & 0xff)));
 }

 /**
  * long轉(zhuǎn)換到字節(jié)數(shù)組
  * 
  * @param number
  *   長(zhǎng)整形數(shù)據(jù)。
  * @return 長(zhǎng)整形轉(zhuǎn)換成的字節(jié)數(shù)組。
  */
 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é)數(shù)組到整型的轉(zhuǎn)換
  * 
  * @param b
  *   長(zhǎng)整形字節(jié)數(shù)組。
  * @return 長(zhǎng)整形數(shù)據(jù)。
  */
 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轉(zhuǎn)換到字節(jié)數(shù)組
  * 
  * @param d
  *   雙精度浮點(diǎn)。
  * @return 雙精度浮點(diǎn)的字節(jié)數(shù)組。
  */
 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é)數(shù)組到double轉(zhuǎn)換
  * 
  * @param b
  *   雙精度浮點(diǎn)字節(jié)數(shù)組。
  * @return 雙精度浮點(diǎn)數(shù)據(jù)。
  */
 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轉(zhuǎn)換到字節(jié)數(shù)組
  * 
  * @param d
  *   浮點(diǎn)型數(shù)據(jù)。
  * @return 浮點(diǎn)型數(shù)據(jù)轉(zhuǎn)換后的字節(jié)數(shù)組。
  */
 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é)數(shù)組到float的轉(zhuǎn)換
  * 
  * @param b
  *   浮點(diǎn)型數(shù)據(jù)字節(jié)數(shù)組。
  * @return 浮點(diǎn)型數(shù)據(jù)。
  */
 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é)數(shù)組轉(zhuǎn)換
  * 
  * @param s
  *   字符串。
  * @param charset
  *   字符編碼
  * @return 字符串按相應(yīng)字符編碼編碼后的字節(jié)數(shù)組。
  */
 public static byte[] stringToByte(String s, Charset charset) {
  return s.getBytes(charset);
 }

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

 /**
  * 對(duì)象轉(zhuǎn)換成字節(jié)數(shù)組。
  * 
  * @param obj
  *   字節(jié)數(shù)組。
  * @return 對(duì)象實(shí)例相應(yīng)的序列化后的字節(jié)數(shù)組。
  * @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é)數(shù)組轉(zhuǎn)換成實(shí)際對(duì)象。
  * 
  * @param b
  *   字節(jié)數(shù)組。
  * @return 對(duì)象。
  * @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();
  }
 }

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

 /**
  * 比較兩個(gè)數(shù)組中的每一個(gè)字節(jié),兩個(gè)字節(jié)必須二進(jìn)制字節(jié)碼每一位都相同才表示兩個(gè) byte相同.
  * 
  * @param a
  *   比較的字節(jié)數(shù)組.
  * @param b
  *   被比較的字節(jié)數(shù).
  * @return ture每一個(gè)元素的每一位兩個(gè)數(shù)組都是相等的,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;
 }

 /**
  * 返回某個(gè)字節(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();
 }

 /**
  * 計(jì)算出給定byte中的每一位,并以一個(gè)布爾數(shù)組返回. true表示為1,false表示為0.
  * 
  * @param b
  *   字節(jié).
  * @return 指定字節(jié)的每一位bit組成的數(shù)組.
  */
 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,超出將拋出數(shù)據(jù)越界異常.
  *
  * @param b
  *   需要判斷的字節(jié).
  * @param index
  *   字節(jié)中指定位.
  * @return 指定位的值.
  */
 public static boolean byteBitValue(byte b, int index) {
  return byteToBitArray(b)[index];
 }

 /**
  * 根據(jù)布爾數(shù)組表示的二進(jìn)制構(gòu)造一個(gè)新的字節(jié).
  * 
  * @param values
  *   布爾數(shù)組,其中true表示為1,false表示為0.
  * @return 構(gòu)造的新字節(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é)中的某個(gè)bit位替換成指定的值,true代表1,false代表0.
  * 
  * @param b
  *   需要被替換的字節(jié).
  * @param index
  *   位的序號(hào),從0開(kāi)始.超過(guò)7將拋出越界異常.
  * @param newValue
  *   新的值.
  * @return 替換好某個(gè)位值的新字節(jié).
  */
 public static byte changeByteBitValue(byte b, int index, boolean newValue) {
  boolean[] bitValues = byteToBitArray(b);
  bitValues[index] = newValue;
  return buildNewByte(bitValues);
 }

 /**
  * 將指定的IP地址轉(zhuǎn)換成字節(jié)表示方式. IP數(shù)組的每一個(gè)數(shù)字都不能大于255,否則將拋出IllegalArgumentException異常.
  *
  * @param ipNums
  *   IP地址數(shù)組.
  * @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位數(shù)
  final char ipSpace = '.';// IP數(shù)字的分隔符
  int[] ipNums = new int[ipSize];
  StringBuilder number = new StringBuilder();// 當(dāng)前操作的數(shù)字
  StringBuilder buff = new StringBuilder(address);
  int point = 0;// 當(dāng)前操作的數(shù)字下標(biāo),最大到3.
  char currentChar;
  for (int i = 0; i < buff.length(); i++) {
   currentChar = buff.charAt(i);
   if (ipSpace == currentChar) {
    // 當(dāng)前位置等于最大于序號(hào)后,還有字符沒(méi)有處理表示這是一個(gè)錯(cuò)誤的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;
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用java.util.Timer實(shí)現(xiàn)任務(wù)調(diào)度

    使用java.util.Timer實(shí)現(xiàn)任務(wù)調(diào)度

    這篇文章主要為大家詳細(xì)介紹了使用java.util.Timer實(shí)現(xiàn)任務(wù)調(diào)度,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Mybatis查詢(xún)語(yǔ)句結(jié)果集的總結(jié)大全

    Mybatis查詢(xún)語(yǔ)句結(jié)果集的總結(jié)大全

    這篇文章主要給大家總結(jié)介紹了關(guān)于Mybatis查詢(xún)語(yǔ)句結(jié)果集的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • IDEA 2021配置JavaWeb項(xiàng)目超詳細(xì)教程

    IDEA 2021配置JavaWeb項(xiàng)目超詳細(xì)教程

    本文通過(guò)圖文并茂的形式給大家介紹IDEA 2021配置JavaWeb項(xiàng)目的過(guò)程,內(nèi)容簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-08-08
  • 淺拷貝和深拷貝原理分析

    淺拷貝和深拷貝原理分析

    Java 對(duì)象拷貝是為對(duì)象賦值的一種方式,簡(jiǎn)單來(lái)說(shuō)就是創(chuàng)建一個(gè)和原對(duì)象相同的對(duì)象,新創(chuàng)建的對(duì)象是原對(duì)象的一個(gè)副本。面試官賊拉喜歡在面試的時(shí)候問(wèn)一問(wèn)你淺拷貝和深拷貝的原理
    2021-08-08
  • 一文盤(pán)點(diǎn)Java創(chuàng)建實(shí)例對(duì)象的方式

    一文盤(pán)點(diǎn)Java創(chuàng)建實(shí)例對(duì)象的方式

    Java對(duì)象是通過(guò)加載、鏈接、初始化三大步驟來(lái)完成對(duì)象的創(chuàng)建及初始化,那么接下來(lái)就說(shuō)一下Java創(chuàng)建實(shí)例對(duì)象的方式有哪幾種,文中并通過(guò)代碼示例講解的非常詳細(xì),需要的朋友可以參考下
    2025-02-02
  • 運(yùn)行java的class文件方法詳解

    運(yùn)行java的class文件方法詳解

    這篇文章主要詳細(xì)介紹了運(yùn)行java的class文件方法的相關(guān)資料,需要的朋友可以參考下
    2015-02-02
  • 解決idea2020.2遇到pom.xml文件報(bào)錯(cuò)maven插件tomcat7的問(wèn)題

    解決idea2020.2遇到pom.xml文件報(bào)錯(cuò)maven插件tomcat7的問(wèn)題

    這篇文章主要介紹了idea2020.2遇到pom.xml文件報(bào)錯(cuò)maven插件tomcat7的問(wèn)題,本文給大家分享解決方法,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • SpringBoot?緩存預(yù)熱的實(shí)現(xiàn)

    SpringBoot?緩存預(yù)熱的實(shí)現(xiàn)

    本文主要介紹了SpringBoot?緩存預(yù)熱的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2007-11-11
  • springboot使用Validator校驗(yàn)方式

    springboot使用Validator校驗(yàn)方式

    這篇文章主要介紹了springboot使用Validator校驗(yàn)方式,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • IDEA無(wú)法識(shí)別相關(guān)module模塊問(wèn)題的解決過(guò)程

    IDEA無(wú)法識(shí)別相關(guān)module模塊問(wèn)題的解決過(guò)程

    這篇文章主要給大家介紹了關(guān)于IDEA無(wú)法識(shí)別相關(guān)module模塊問(wèn)題的解決過(guò)程,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用IDEA具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07

最新評(píng)論

孟津县| 东安县| 徐汇区| 威信县| 浦江县| 汤原县| 惠东县| 遂溪县| 湖南省| 天峨县| 汉寿县| 宁陵县| 元朗区| 申扎县| 林甸县| 平定县| 乐至县| 当雄县| 正蓝旗| 武功县| 镇原县| 桃源县| 佛冈县| 定兴县| 北安市| 托里县| 叙永县| 汾阳市| 南宁市| 广平县| 盱眙县| 江安县| 亳州市| 佛冈县| 泸定县| 信宜市| 潜江市| 满洲里市| 静海县| 县级市| 治县。|