从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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值