从inputStream中读取指定长度的字节

简介:

Java 中如何从InputStream中读取指定长度的字节呢?

代码如下:

Java代码   收藏代码
  1. /*** 
  2.      * 从输入流获取字节数组,当文件很大时,报java.lang.OutOfMemoryError: Java heap space 
  3.      *  
  4.      * @since 2014-02-19 
  5.      * @param br_right 
  6.      * @param length2 
  7.      * @return 
  8.      * @throws IOException 
  9.      */  
  10.     public static byte[] readBytesFromInputStream(InputStream br_right,  
  11.             int length2) throws IOException {  
  12.         int readSize;  
  13.         byte[] bytes = null;  
  14.         bytes = new byte[length2];  
  15.   
  16.         long length_tmp = length2;  
  17.         long index = 0;// start from zero  
  18.         while ((readSize = br_right.read(bytes, (int) index, (int) length_tmp)) != -1) {  
  19.             length_tmp -= readSize;  
  20.             if (length_tmp == 0) {  
  21.                 break;  
  22.             }  
  23.             index = index + readSize;  
  24.         }  
  25.         return bytes;  
  26.     }  
  27. /*** 
  28.      * 读取指定长度的字节 
  29.      * @since 2014-02-27 
  30.      * @param ins 
  31.      * @param sumLeng : 要读取的字节数 
  32.      * @return 
  33.      * @throws IOException 
  34.      */  
  35.     public static byte[]readBytesFromGzipInputStream(GZIPInputStream ins,long sumLeng) throws IOException{  
  36.         byte[] fileNameBytes = new byte[(int) sumLeng];  
  37.         int fileNameReadLength=0;  
  38.         int hasReadLength=0;//已经读取的字节数  
  39.         while((fileNameReadLength=ins.read(fileNameBytes,hasReadLength,(int)sumLeng-hasReadLength))>0){  
  40.             hasReadLength=hasReadLength+fileNameReadLength;  
  41.         }  
  42.         return fileNameBytes;  
  43.     }  
  44. /*** 
  45.      * read char array from inputstream according to specified length. 
  46.      *  
  47.      * @param file 
  48.      * @param ins 
  49.      * @param length2 
  50.      *            :要读取的字符总数 
  51.      * @throws IOException 
  52.      */  
  53.     public static char[] getCharsFromInputStream(BufferedReader br_right,  
  54.             int length2) throws IOException {  
  55.         int readSize;  
  56.         char[] chars = null;  
  57.         chars = new char[length2];  
  58.   
  59.         long length_tmp = length2;  
  60.         long index = 0;// start from zero  
  61.         while ((readSize = br_right.read(chars, (int) index, (int) length_tmp)) != -1) {  
  62.             length_tmp -= readSize;  
  63.             if (length_tmp == 0) {  
  64.                 break;  
  65.             }  
  66.             index = index + readSize;// 写入字符数组的offset(偏移量)  
  67.         }  
  68.         return chars;  
  69.     }  
  70.   
  71. /*** 
  72.      * 从文件中读取指定长度的字符(注意:不是字节) 
  73.      *  
  74.      * @param file 
  75.      * @param length2 
  76.      * @return 
  77.      * @throws IOException 
  78.      */  
  79.     public static char[] getCharsFromFile(File file, int length2)  
  80.             throws IOException {  
  81.         FileInputStream fin = new FileInputStream(file);  
  82.         InputStreamReader inr = new InputStreamReader(fin);  
  83.         BufferedReader br = new BufferedReader(inr);  
  84.         return getCharsFromInputStream(br, length2);  
  85.     }  
  86. private static byte[] readDataFromLength(HttpURLConnection huc,  
  87.             int contentLength) throws Exception {  
  88.   
  89.         InputStream in = huc.getInputStream();  
  90.         BufferedInputStream bis = new BufferedInputStream(in);  
  91.   
  92.         // 数据字节数组  
  93.         byte[] receData = new byte[contentLength];  
  94.   
  95.         // 已读取的长度  
  96.         int readAlreadyLength = 0;  
  97.   
  98.         // while ((readAlreadyLength+= bis.read(receData, readAlreadyLength,  
  99.         // contentLength-readAlreadyLength))< contentLength) {  
  100.         // System.out.println("right");  
  101.         // }  
  102.         while ((readAlreadyLength = readAlreadyLength  
  103.                 + bis.read(receData, readAlreadyLength, contentLength  
  104.                         - readAlreadyLength)) < contentLength) {  
  105.         }  
  106.         // System.out.println("readLength=" + readLength);  
  107.         return receData;  
  108.     }  

 

 

参考我的另外一篇博客:http://hw1287789687.iteye.com/blog/2019425

相关文章
|
8月前
字节输入流
字节输入流
43 0
|
8月前
字节输出流
字节输出流
56 0
|
8月前
|
存储 Java
字节缓冲流
字节缓冲流
40 0
|
4月前
|
C#
C# (File方法)对文件的操作,字节写入和读取
C# (File方法)对文件的操作,字节写入和读取
|
6月前
|
Java 数据处理 数据安全/隐私保护
【JavaSE专栏74】字节输入流InputStream,用于从输入源读取字节数据的流
【JavaSE专栏74】字节输入流InputStream,用于从输入源读取字节数据的流
|
6月前
|
存储 Java 数据库
【JavaSE专栏75】字节输出流OutputStream,用于将字节数据写入到输出目标的流
【JavaSE专栏75】字节输出流OutputStream,用于将字节数据写入到输出目标的流
|
6月前
|
自然语言处理
转换流,字节字符的转换
转换流,字节字符的转换
|
8月前
|
Java
Java IO流之访问文件的字节输入流FileInputStream和字节输入流FileOutputStream的详解
Java IO流之访问文件的字节输入流FileInputStream和字节输入流FileOutputStream的详解
71 0
|
8月前
缓冲流与打印流(字节与字符)
缓冲流与打印流(字节与字符)
39 0
java152-字节缓冲输出流
java152-字节缓冲输出流
84 0
java152-字节缓冲输出流