java处理字节的常用工具类

处理字节的常用工具类方法,供大家参考,具体内容如下

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;

/**
 * 处理字节的常用工具类方法
 * @author dongyangyang
 * @Date 2017/3/13 12:31
 * @Version 1.0
 *
 */
public class ByteUtils {

 /**
  * 构造新字节时需要与的值表
  */
 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转换到字节数组
  *
  * @param number
  *   需要转换的数据。
  * @return 转换后的字节数组。
  */
 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;
 }

 /**
  * 字节到short转换
  *
  * @param b
  *   short的字节数组
  * @return short数值。
  */
 public static short byteToShort(byte[] b) {
  return (short) ((((b[0] & 0xff) << 8) | b[1] & 0xff));
 }

 /**
  * 整型转换到字节数组
  *
  * @param number
  *   整形数据。
  * @return 整形数据的字节数组。
  */
 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;
 }

 /**
  * 字节数组到整型转换
  *
  * @param b
  *   整形数据的字节数组。
  * @return 字节数组转换成的整形数据。
  */
 public static int byteToInt(byte[] b) {
  return ((((b[0] & 0xff) << 24) | ((b[1] & 0xff) << 16) | ((b[2] & 0xff) << 8) | (b[3] & 0xff)));
 }

 /**
  * long转换到字节数组
  *
  * @param number
  *   长整形数据。
  * @return 长整形转换成的字节数组。
  */
 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;
 }

 /**
  * 字节数组到整型的转换
  *
  * @param b
  *   长整形字节数组。
  * @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转换到字节数组
  *
  * @param d
  *   双精度浮点。
  * @return 双精度浮点的字节数组。
  */
 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;
 }

 /**
  * 字节数组到double转换
  *
  * @param b
  *   双精度浮点字节数组。
  * @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转换到字节数组
  *
  * @param d
  *   浮点型数据。
  * @return 浮点型数据转换后的字节数组。
  */
 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;
 }

 /**
  * 字节数组到float的转换
  *
  * @param b
  *   浮点型数据字节数组。
  * @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);
 }

 /**
  * 字符串到字节数组转换
  *
  * @param s
  *   字符串。
  * @param charset
  *   字符编码
  * @return 字符串按相应字符编码编码后的字节数组。
  */
 public static byte[] stringToByte(String s, Charset charset) {
  return s.getBytes(charset);
 }

 /**
  * 字节数组带字符串的转换
  *
  * @param b
  *   字符串按指定编码转换的字节数组。
  * @param charset
  *   字符编码。
  * @return 字符串。
  */
 public static String byteToString(byte[] b, Charset charset) {
  return new String(b, charset);
 }

 /**
  * 对象转换成字节数组。
  *
  * @param obj
  *   字节数组。
  * @return 对象实例相应的序列化后的字节数组。
  * @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();
  }
 }

 /**
  * 序死化字节数组转换成实际对象。
  *
  * @param b
  *   字节数组。
  * @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();
  }
 }

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

 /**
  * 比较两个数组中的每一个字节,两个字节必须二进制字节码每一位都相同才表示两个 byte相同.
  *
  * @param a
  *   比较的字节数组.
  * @param b
  *   被比较的字节数.
  * @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;
 }

 /**
  * 返回某个字节的bit组成的字符串.
  *
  * @param b
  *   字节.
  * @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
  *   字节.
  * @return 指定字节的每一位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;
 }

 /**
  * 返回指定字节中指定bit位,true为1,false为0. 指定的位从0-7,超出将抛出数据越界异常.
  *
  * @param b
  *   需要判断的字节.
  * @param index
  *   字节中指定位.
  * @return 指定位的值.
  */
 public static boolean byteBitValue(byte b, int index) {
  return byteToBitArray(b)[index];
 }

 /**
  * 根据布尔数组表示的二进制构造一个新的字节.
  *
  * @param values
  *   布尔数组,其中true表示为1,false表示为0.
  * @return 构造的新字节.
  */
 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;
 }

 /**
  * 将指定字节中的某个bit位替换成指定的值,true代表1,false代表0.
  *
  * @param b
  *   需要被替换的字节.
  * @param index
  *   位的序号,从0开始.超过7将抛出越界异常.
  * @param newValue
  *   新的值.
  * @return 替换好某个位值的新字节.
  */
 public static byte changeByteBitValue(byte b, int index, boolean newValue) {
  boolean[] bitValues = byteToBitArray(b);
  bitValues[index] = newValue;
  return buildNewByte(bitValues);
 }

 /**
  * 将指定的IP地址转换成字节表示方式. IP数组的每一个数字都不能大于255,否则将抛出IllegalArgumentException异常.
  *
  * @param ipNums
  *   IP地址数组.
  * @return IP地址字节表示方式.
  */
 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;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Java socket字节流传输示例解析

    本文为大家分享了Java socket字节流传输示例,供大家参考,具体内容如下 服务端server端: package com.yuan.socket; import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * Created by YUAN on 2016-09-17. */ public class TalkServer4Byte { private ServerSocket server; p

  • 详解Java中ByteArray字节数组的输入输出流的用法

    ByteArrayInputStream 介绍 ByteArrayInputStream 是字节数组输入流.它继承于InputStream. 它包含一个内部缓冲区,该缓冲区包含从流中读取的字节:通俗点说,它的内部缓冲区就是一个字节数组,而ByteArrayInputStream本质就是通过字节数组来实现的. 我们都知道,InputStream通过read()向外提供接口,供它们来读取字节数据:而ByteArrayInputStream 的内部额外的定义了一个计数器,它被用来跟踪 read() 方

  • java按字节截取带有汉字的字符串的解法(推荐)

    由于接口使用的oracle字段长度为固定字节数,然后传进来的字符串估计比数据库字段的总字节数要大,那么截取小于数据库字节数的字符串. 自己参考网上的例子,整了个递归调用就可以了,因为截取的字符字节长度必须小与数据库的字节长度,即如果最后一个字符为汉字,那么只能去掉往前截取. /** * 判断传进来的字符串,是否 * 大于指定的字节,如果大于递归调用 * 直到小于指定字节数 ,一定要指定字符编码,因为各个系统字符编码都不一样,字节数也不一样 * @param s * 原始字符串 * @param

  • Java 将字符串动态生成字节码的实现方法

    可以生成可执行的class文件 直接上能执行代码: 复制代码 代码如下: public class Test { /**  * @param args  */@SuppressWarnings("static-access")public static void main(String[] args) {  try {   new Test().calculate("234 - ( 1 + 45 * 4 ) / 5");  } catch (Exception e)

  • java 解决异常 2 字节的 UTF-8 序列的字节2 无效的问题

    java 解决异常 2 字节的 UTF-8 序列的字节 2 无效的问题 最近做项目,遇到异常 2 字节的 UTF-8 序列的字节 2 无效的问题,上网找了下资料,这里记录下解决方法,有遇到同样问题的大家,可以看下 详细异常: 十二月 08, 2015 7:16:55 下午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() for servlet [jsp] in context with path

  • Java字节码指令集的使用详细

    Java虚拟机指令由一个字节长度的.代表某种特定含义的操作码(Opcode)以及其后的零个至多个代表此操作参数的操作数构成.虚拟机中许多指令并不包含操作数,只有一个操作码.若忽略异常,JVM解释器使用一下为代码即可有效工作. 复制代码 代码如下: do{    自动计算PC寄存器以及从PC寄存器的位置取出操作码    if(存在操作数) 取出操作数;    执行操作码所定义的操作;}while(处理下一次循环) 操作数的数量以及长度,取决于操作码,若一个操作数长度超过了一个字节,将会以Big-E

  • java从输入流中获取数据并返回字节数组示例

    复制代码 代码如下: import java.io.ByteArrayOutputStream;import java.io.InputStream;//从输入流中获取数据并以字节数组返回public class StreamTool {    /**     * 从输入流获取数据     * @param inputStream     * @return     * @throws Exception     */    public static byte[] readInputStrea

  • Java 按照字节来截取字符串的代码(不会出现半个汉字)

    复制代码 代码如下: /* *    Copyright 2012-2013 The Haohui Network Corporation */package com.haohui.common.utils; /** * <pre> * 字符串辅助工具 * </pre> *  * @project baidamei * @author cevencheng <cevencheng@gmail.com> * @create 2012-11-30 下午2:42:56 */p

  • 计算一个Java对象占用字节数的方法

    本文实例讲述了如何计算(或者说,估算)一个Java对象占用的内存数量的方法.分享给大家供大家参考.具体分析如下: 通常,我们谈论的堆内存使用的前提是以"一般情况"为背景的.不包括下面两种情形:   某些情况下,JVM根本就没有把Object放入堆中.例如:原则上讲,一个小的thread-local对象存在于栈中,而不是在堆中. 被Object占用内存的大小依赖于Object的当前状态.例如:Object的同步锁是否生效,或者,Object是否正在被回收. 我们先来看看在堆中单个的Obj

  • 详解Java中字符流与字节流的区别

    本文为大家分析了Java中字符流与字节流的区别,供大家参考,具体内容如下 1. 什么是流 Java中的流是对字节序列的抽象,我们可以想象有一个水管,只不过现在流动在水管中的不再是水,而是字节序列.和水流一样,Java中的流也具有一个"流动的方向",通常可以从中读入一个字节序列的对象被称为输入流:能够向其写入一个字节序列的对象被称为输出流. 2. 字节流 Java中的字节流处理的最基本单位为单个字节,它通常用来处理二进制数据.Java中最基本的两个字节流类是InputStream和Out

随机推荐