与字节数组相关的IO操作

简介:

总结一下与字节数组相关的IO操作。

关于 把十六进制的位串转化为byte数组,请参阅 http://hw1287789687.iteye.com/blog/1882644

 

(1)从InputStream 读取字节数组

方式一:

Java代码   收藏代码
  1. /*** 
  2.      * Has been tested 
  3.      *  
  4.      * @param in 
  5.      * @return 
  6.      * @throws IOException 
  7.      */  
  8.     public static byte[] readBytes3(InputStream in) throws IOException {  
  9.         BufferedInputStream bufin = new BufferedInputStream(in);  
  10.         int buffSize = BUFFSIZE_1024;  
  11.         ByteArrayOutputStream out = new ByteArrayOutputStream(buffSize);  
  12.   
  13.         // System.out.println("Available bytes:" + in.available());  
  14.   
  15.         byte[] temp = new byte[4096];  
  16.         int size = 0;  
  17.         while ((size = bufin.read(temp)) != -1) {  
  18.             out.write(temp, 0, size);  
  19.         }  
  20.         bufin.close();  
  21.         in.close();  
  22.         byte[] content = out.toByteArray();  
  23.         return content;  
  24.     }  

 说明:先把inputstream的字节读到ByteArrayOutputStream中,读完之后再调用toByteArray() 转化为字节数组。

 

方式二:

Java代码   收藏代码
  1. /*** 
  2.      * Has been tested 
  3.      *  
  4.      * @param in 
  5.      * @return 
  6.      * @throws IOException 
  7.      */  
  8.     public static byte[] readBytes(InputStream in) throws IOException {  
  9.         byte[] temp = new byte[in.available()];  
  10.         byte[] result = new byte[0];  
  11.         int size = 0;  
  12.         while ((size = in.read(temp)) != -1) {  
  13.             byte[] readBytes = new byte[size];  
  14.             System.arraycopy(temp, 0, readBytes, 0, size);  
  15.             result = mergeArray(result, readBytes);  
  16.         }  
  17.         return result;  
  18.     }  
  19. /*** 
  20.      * 合并字节数组 
  21.      *  
  22.      * @param a 
  23.      * @return 
  24.      */  
  25.     public static byte[] mergeArray(byte[]... a) {  
  26.         // 合并完之后数组的总长度  
  27.         int index = 0;  
  28.         int sum = 0;  
  29.         for (int i = 0; i < a.length; i++) {  
  30.             sum = sum + a[i].length;  
  31.         }  
  32.         byte[] result = new byte[sum];  
  33.         for (int i = 0; i < a.length; i++) {  
  34.             int lengthOne = a[i].length;  
  35.             if (lengthOne == 0) {  
  36.                 continue;  
  37.             }  
  38.             // 拷贝数组  
  39.             System.arraycopy(a[i], 0, result, index, lengthOne);  
  40.             index = index + lengthOne;  
  41.         }  
  42.         return result;  
  43.     }  

 

 

(2)把字节数组写入文件

Java代码   收藏代码
  1. /*** 
  2.      * write byte[] to file 
  3.      *  
  4.      * @param bytes 
  5.      * @param destFile 
  6.      * @throws IOException 
  7.      */  
  8.     public static void writeBytesToFile(byte[] bytes, File destFile)  
  9.             throws IOException {  
  10.         FileOutputStream out = new FileOutputStream(destFile);  
  11.         write2File(bytes, out);  
  12.     }  
  13. /*** 
  14.      *  
  15.      * @param bytes 
  16.      * @param out 
  17.      * @throws IOException 
  18.      */  
  19.     public static void write2File(byte[] bytes, FileOutputStream out)  
  20.             throws IOException {  
  21.         out.write(bytes);  
  22.         out.close();  
  23.     }  

 

 

(3)在已有字节数组基础上追加一个字节

Java代码   收藏代码
  1. /*** 
  2.      * append a byte. 
  3.      *  
  4.      * @param a 
  5.      * @param b 
  6.      * @return 
  7.      */  
  8.     public static byte[] appandByte(byte[] a, byte b) {  
  9.         int length = a.length;  
  10.         byte[] resultBytes = new byte[length + 1];  
  11.         System.arraycopy(a, 0, resultBytes, 0, length);  
  12.         resultBytes[length] = b;  
  13.         return resultBytes;  
  14.     }  

 

(4)比较两个字节数组是否相同

Java代码   收藏代码
  1. /*** 
  2.      * Compare two byte arrays whether are the same. 
  3.      *  
  4.      * @param a 
  5.      * @param b 
  6.      * @return 
  7.      */  
  8.     public static boolean arrayIsEqual(byte[] a, byte[] b) {  
  9.         if(a==null&&b==null){  
  10.             return true;  
  11.         }  
  12.           
  13.         if (a != null && b != null) {  
  14.             if (a.length != b.length) {  
  15.                 return false;  
  16.             } else {  
  17.                 for (int i = 0; i < a.length; i++) {  
  18.                     if (a[i] != b[i]) {  
  19.                         return false;  
  20.                     }  
  21.                 }  
  22.             }  
  23.         }else {//one is null, the other is not null  
  24.             return false;  
  25.         }  
  26.         return true;  
  27.     }  

 

(5)查找指定字节findTarget在指定字节数组source中的位置

Java代码   收藏代码
  1. /*** 
  2.      *  
  3.      * @param source 
  4.      * @param findTarget 
  5.      *            :key word 
  6.      * @param pos 
  7.      *            :where start from 
  8.      * @return index 
  9.      */  
  10.     public static int findBytes(byte[] source, byte[] findTarget, int pos) {  
  11.         int i, j, k = 0;  
  12.         i = pos;  
  13.         j = 0;  
  14.         while (i < source.length && j < findTarget.length) {  
  15.             if (source[i] == findTarget[j]) {  
  16.                 ++i;  
  17.                 ++j;  
  18.                 if (j == findTarget.length) {  
  19.                     k = k + 1;// k++  
  20.                     break;  
  21.                     // j = 0;  
  22.                 }  
  23.             } else {  
  24.                 i = i - j + 1;  
  25.                 j = 0;  
  26.             }  
  27.         }  
  28.         return k == 0 ? -1 : i - j;  
  29.     }  

 

 

测试代码:

Java代码   收藏代码
  1. @Test  
  2.    public void test_arrayIsEqual2(){  
  3.     System.out.println("test_filterFrontBytes");  
  4.     byte[] a = new byte[] { 1234 };  
  5.     byte[] b = new byte[] { 123 };  
  6.     System.out.println( <span style="font-size: 1em; line-height: 1.5;">arrayIsEqual</span><span style="font-size: 1em; line-height: 1.5;">(a, b));</span>  
Java代码   收藏代码
  1. }  
  2. @Test  
  3.     public void test_appandByte(){  
  4.         byte[]bytes=new byte[]{1,2,3};  
  5.         byte[]resultBytes=appandByte(bytes, (byte)32);  
  6.         <span style="font-size: 1em; line-height: 1.5;">arrayIsEqual</span><span style="font-size: 1em; line-height: 1.5;">(resultBytes, new byte[]{12332});</span>  
Java代码   收藏代码
  1. }  
相关文章
|
5月前
|
存储 Linux API
Linux应用开发基础知识——文件IO操作(三)
Linux应用开发基础知识——文件IO操作(三)
77 2
Linux应用开发基础知识——文件IO操作(三)
|
12月前
|
Java 测试技术 Apache
Java IO 与 NIO:高效的输入输出操作探究
输入输出(IO)是任何编程语言中的核心概念,而在Java中,IO操作更是应用程序成功运行的基石。随着计算机系统变得越来越复杂,对IO的要求也日益增加。在本文中,我们将探讨Java IO和非阻塞IO(NIO)的重要性以及如何在Java中实现高效的输入输出操作。
|
12月前
|
存储 数据处理 索引
【100天精通python】Day27:文件与IO操作_CSV文件处理
【100天精通python】Day27:文件与IO操作_CSV文件处理
68 0
|
5月前
|
数据采集 异构计算
LabVIEW编程LabVIEW开发高级数据采集技术 操作数字IO 例程与相关资料
LabVIEW编程LabVIEW开发高级数据采集技术 操作数字IO 例程与相关资料
81 22
|
4月前
|
缓存 NoSQL Redis
redis管道操作(节省网络IO开销)
pipeline中发送的每个command都会被server立即执行,如果执行失败,将会在此后的响应中得到信息;也就是pipeline并不是表达“所有command都一起成功”的语义,管道中前面命令失败,后面命令不会有影响,继续执行。
39 1
|
5月前
|
Java
|
5月前
|
消息中间件 关系型数据库 Kafka
实时计算 Flink版操作报错之在执行任务时遇到了一个IO错误,具体表现为无法从本地主机(localhost)下载文件,该怎么解决
在使用实时计算Flink版过程中,可能会遇到各种错误,了解这些错误的原因及解决方法对于高效排错至关重要。针对具体问题,查看Flink的日志是关键,它们通常会提供更详细的错误信息和堆栈跟踪,有助于定位问题。此外,Flink社区文档和官方论坛也是寻求帮助的好去处。以下是一些常见的操作报错及其可能的原因与解决策略。
|
5月前
|
监控 Java
Java一分钟之-NIO:非阻塞IO操作
【5月更文挑战第14天】Java的NIO(New IO)解决了传统BIO在高并发下的低效问题,通过非阻塞方式提高性能。NIO涉及复杂的选择器和缓冲区管理,易出现线程、内存和中断处理的误区。要避免这些问题,可以使用如Netty的NIO库,谨慎设计并发策略,并建立标准异常处理。示例展示了简单NIO服务器,接收连接并发送欢迎消息。理解NIO工作原理和最佳实践,有助于构建高效网络应用。
47 2
|
5月前
|
存储 Go 数据处理
Golang简单实现IO操作
Golang简单实现IO操作
44 1
|
5月前
|
存储 Java 编译器
Java文件IO操作基础
Java文件IO操作基础
45 0