java 流与 byte[] 的互转操作

作者:微观尽头 时间:2023-06-26 11:25:46 

1. InputStream -> byte[]

引入 apache.commons.is 包

import org.apache.commons.io.IOUtils;

byte[] bytes = IOUtils.toByteArray(inputStream);

2. byte[] -> InputStream

InputStream inputStream = new ByteArrayInputStream(bytes);

补充知识:byte[]与各种数据类型互相转换示例

在socket开发过程中,通常需要将一些具体的值(这些值可能是各种JAVA类型)转化为byte[]类型,为此我总结了如下这个示例,贴出来,以便经常翻看


public class TestCase {

/**
  * short到字节数组的转换.
  */
 public static byte[] shortToByte(short number) {
   int temp = number;
   byte[] b = new byte[2];
   for (int i = 0; i < b.length; i++) {
     b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
     temp = temp >> 8;// 向右移8位
   }
   return b;
 }

/**
  * 字节数组到short的转换.
  */
 public static short byteToShort(byte[] b) {
   short s = 0;
   short s0 = (short) (b[0] & 0xff);// 最低位
   short s1 = (short) (b[1] & 0xff);
   s1 <<= 8;
   s = (short) (s0 | s1);
   return s;
 }    

/**
  * int到字节数组的转换.
  */
 public static byte[] intToByte(int number) {
   int temp = number;
   byte[] b = new byte[4];
   for (int i = 0; i < b.length; i++) {
     b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
     temp = temp >> 8;// 向右移8位
   }
   return b;
 }

/**
  * 字节数组到int的转换.
  */
 public static int byteToInt(byte[] b) {
   int s = 0;
   int s0 = b[0] & 0xff;// 最低位
   int s1 = b[1] & 0xff;
   int s2 = b[2] & 0xff;
   int s3 = b[3] & 0xff;
   s3 <<= 24;
   s2 <<= 16;
   s1 <<= 8;
   s = s0 | s1 | s2 | s3;
   return s;
 }    

/**
  * long类型转成byte数组
  */
 public static byte[] longToByte(long number) {
   long temp = number;
   byte[] b = new byte[8];
   for (int i = 0; i < b.length; i++) {
     b[i] = new Long(temp & 0xff).byteValue();// 将最低位保存在最低位 temp = temp
                           // >> 8;// 向右移8位
   }
   return b;
 }

/**
  * 字节数组到long的转换.
  */
 public static long byteToLong(byte[] b) {
   long s = 0;
   long s0 = b[0] & 0xff;// 最低位
   long s1 = b[1] & 0xff;
   long s2 = b[2] & 0xff;
   long s3 = b[3] & 0xff;
   long s4 = b[4] & 0xff;// 最低位
   long s5 = b[5] & 0xff;
   long s6 = b[6] & 0xff;
   long s7 = b[7] & 0xff;

// s0不变
   s1 <<= 8;
   s2 <<= 16;
   s3 <<= 24;
   s4 <<= 8 * 4;
   s5 <<= 8 * 5;
   s6 <<= 8 * 6;
   s7 <<= 8 * 7;
   s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;
   return s;
 }

/**
  * double到字节数组的转换.
  */
 public static byte[] doubleToByte(double num) {  
   byte[] b = new byte[8];  
   long l = Double.doubleToLongBits(num);  
   for (int i = 0; i < 8; i++) {  
     b[i] = new Long(l).byteValue();  
     l = l >> 8;  
   }
   return b;
 }

/**
  * 字节数组到double的转换.
  */
 public static double getDouble(byte[] b) {  
   long m;  
   m = b[0];  
   m &= 0xff;  
   m |= ((long) b[1] << 8);  
   m &= 0xffff;  
   m |= ((long) b[2] << 16);  
   m &= 0xffffff;  
   m |= ((long) b[3] << 24);  
   m &= 0xffffffffl;  
   m |= ((long) b[4] << 32);  
   m &= 0xffffffffffl;  
   m |= ((long) b[5] << 40);  
   m &= 0xffffffffffffl;  
   m |= ((long) b[6] << 48);  
   m &= 0xffffffffffffffl;  
   m |= ((long) b[7] << 56);  
   return Double.longBitsToDouble(m);  
 }    

/**
  * float到字节数组的转换.
  */
 public static void floatToByte(float x) {
   //先用 Float.floatToIntBits(f)转换成int
 }

/**
  * 字节数组到float的转换.
  */
 public static float getFloat(byte[] b) {  
   // 4 bytes  
   int accum = 0;  
   for ( int shiftBy = 0; shiftBy < 4; shiftBy++ ) {  
       accum |= (b[shiftBy] & 0xff) << shiftBy * 8;  
   }  
   return Float.intBitsToFloat(accum);  
 }  

/**
  * char到字节数组的转换.
  */
  public static byte[] charToByte(char c){
   byte[] b = new byte[2];
   b[0] = (byte) ((c & 0xFF00) >> 8);
   b[1] = (byte) (c & 0xFF);
   return b;
  }

/**
  * 字节数组到char的转换.
  */
  public static char byteToChar(byte[] b){
   char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
   return c;
  }

/**
  * string到字节数组的转换.
  */
 public static byte[] stringToByte(String str) throws UnsupportedEncodingException{
   return str.getBytes("GBK");
 }

/**
  * 字节数组到String的转换.
  */
 public static String bytesToString(byte[] str) {
   String keyword = null;
   try {
     keyword = new String(str,"GBK");
   } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
   }
   return keyword;
 }    

/**
  * object到字节数组的转换
  */
 @Test
 public void testObject2ByteArray() throws IOException,
     ClassNotFoundException {
   // Object obj = "";
   Integer[] obj = { 1, 3, 4 };

// // object to bytearray
   ByteArrayOutputStream bo = new ByteArrayOutputStream();
   ObjectOutputStream oo = new ObjectOutputStream(bo);
   oo.writeObject(obj);
   byte[] bytes = bo.toByteArray();
   bo.close();
   oo.close();
   System.out.println(Arrays.toString(bytes));

Integer[] intArr = (Integer[]) testByteArray2Object(bytes);
   System.out.println(Arrays.asList(intArr));

byte[] b2 = intToByte(123);
   System.out.println(Arrays.toString(b2));

int a = byteToInt(b2);
   System.out.println(a);

}

/**
  * 字节数组到object的转换.
  */
 private Object testByteArray2Object(byte[] bytes) throws IOException,
     ClassNotFoundException {
   // byte[] bytes = null;
   Object obj;
   // bytearray to object
   ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
   ObjectInputStream oi = new ObjectInputStream(bi);
   obj = oi.readObject();
   bi.close();
   oi.close();
   System.out.println(obj);
   return obj;
 }  
}

来源:https://blog.csdn.net/huangdingsheng/article/details/93400547

标签:java,流,byte[]
0
投稿

猜你喜欢

  • android中SQLite使用及特点

    2023-07-24 23:33:28
  • java JUC信号量Semaphore原理及使用介绍

    2023-01-02 02:08:57
  • 解决IDEA springboot"spring-boot-maven-plugin"报红问题

    2023-08-15 21:29:19
  • C#程序(含多个Dll)合并成一个Exe的简单方法

    2023-04-09 15:55:08
  • Android开发改变字体颜色方法

    2022-11-10 05:51:01
  • 利用Jetpack Compose实现绘制五角星效果

    2023-04-10 06:20:48
  • Android Studio轻松构建自定义模板的步骤记录

    2023-07-19 01:11:51
  • SpringCloud实现灰度发布的方法步骤

    2023-03-17 05:18:37
  • Android RecyclerView 基础知识详解

    2022-10-04 13:40:20
  • java中struts2实现简单的文件上传与下载

    2022-12-23 22:53:21
  • 5步学会使用VideoView播放视频

    2023-09-12 05:51:07
  • Android通知栏微技巧一些需要注意的小细节

    2021-10-07 12:40:59
  • Springmvc Controller接口代码示例

    2023-11-28 10:13:25
  • MyBatis控制台显示SQL语句的方法实现

    2021-10-18 04:10:00
  • 详解Java中restTemplate的使用

    2023-06-19 23:20:29
  • Spring Cloud Hystrix 线程池队列配置(踩坑)

    2023-12-18 16:13:22
  • Java实现删除排序数组中重复元素的方法小结【三种方法比较】

    2023-09-28 15:21:48
  • 解析Java继承中方法的覆盖和重载

    2021-09-02 02:02:32
  • Android实现自动填写获取验证码功能

    2023-04-17 13:47:12
  • 在Java的Struts中判断是否调用AJAX及用拦截 器对其优化

    2023-11-25 00:40:40
  • asp之家 软件编程 m.aspxhome.com