java中InputStream獲取字節(jié)大小相關(guān)方法詳解
1 通過StreamUtils工具類的copyToByteArray()方法獲?。ㄍ扑])
正常大部分項(xiàng)目都是使用的Spring,而Spring已經(jīng)幫我們開發(fā)好了相應(yīng)的工具類,我們直接調(diào)用即可。
InputStream is = this.getClass().getResourceAsStream(filePath); byte[] bytes = StreamUtils.copyToByteArray(is); is.read(bytes);
2 通過available()方法獲取(不推薦)
InputStream is = this.getClass().getResourceAsStream(filePath); byte[] bytes = new byte[is.available()]; is.read(bytes);
2.1 不推薦理由
可以看一下方法注釋:
/**
* Returns an estimate of the number of bytes that can be read (or
* skipped over) from this input stream without blocking by the next
* invocation of a method for this input stream. The next invocation
* might be the same thread or another thread. A single read or skip of this
* many bytes will not block, but may read or skip fewer bytes.
*
* <p> Note that while some implementations of {@code InputStream} will return
* the total number of bytes in the stream, many will not. It is
* never correct to use the return value of this method to allocate
* a buffer intended to hold all data in this stream.
*
* <p> A subclass' implementation of this method may choose to throw an
* {@link java.io.IOException} if this input stream has been closed by
* invoking the {@link #close()} method.
*
* <p> The {@code available} method for class {@code InputStream} always
* returns {@code 0}.
*
* <p> This method should be overridden by subclasses.
*
* @return an estimate of the number of bytes that can be read (or skipped
* over) from this input stream without blocking or {@code 0} when
* it reaches the end of the input stream.
* @exception java.io.IOException if an I/O error occurs.
*/大致意思是返回的字節(jié)數(shù)可能由于網(wǎng)絡(luò)原因阻塞一次只能返回部分字節(jié)或者另外一個(gè)線程也讀了導(dǎo)致返回部分字節(jié),也就是說如果使用available()方法去獲取InputStream的長度來作為字節(jié)數(shù)組的長度,那可能會(huì)出現(xiàn)字節(jié)接受不完整的錯(cuò)誤,所以不推薦使用該方法的返回值去分配一個(gè)緩沖的byte數(shù)組。
3 通過file.length()來獲取
File file = new File(path); InputStream stream = new FileInputStream(file); byte[] bytes = new byte[file.length()]
總結(jié)
到此這篇關(guān)于java中InputStream獲取字節(jié)大小相關(guān)方法的文章就介紹到這了,更多相關(guān)java InputStream獲取字節(jié)大小內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)Dijkstra最短路徑算法
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)Dijkstra最短路徑算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Java?synchornized與ReentrantLock處理并發(fā)出現(xiàn)的錯(cuò)誤
synchronized機(jī)制提供了對每個(gè)對象相關(guān)的隱式監(jiān)視器鎖,并強(qiáng)制所有鎖的獲取和釋放都必須在同一個(gè)塊結(jié)構(gòu)中。當(dāng)獲取了多個(gè)鎖時(shí),必須以相反的順序釋放。即synchronized對于鎖的釋放是隱式的2023-01-01
Java MultipartFile實(shí)現(xiàn)上傳文件/上傳圖片
這篇文章主要介紹了Java MultipartFile實(shí)現(xiàn)上傳文件/上傳圖片,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-12-12
Apache Maven創(chuàng)建工程的實(shí)現(xiàn)示例
本文詳細(xì)介紹了如何使用Maven創(chuàng)建一個(gè)新的Java工程,包括使用maven-archetype-plugin插件、項(xiàng)目的基本結(jié)構(gòu)和文件、構(gòu)建和運(yùn)行項(xiàng)目的方法以及常見問題的解決,Maven通過簡化項(xiàng)目構(gòu)建和依賴管理,成為Java開發(fā)中不可或缺的工具2024-11-11
詳解SpringCloud eureka服務(wù)狀態(tài)監(jiān)聽
這篇文章主要介紹了詳解SpringCloud eureka服務(wù)狀態(tài)監(jiān)聽,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
Stream distinct根據(jù)list某個(gè)字段去重的解決方案
這篇文章主要介紹了Stream distinct根據(jù)list某個(gè)字段去重,stream的distinct去重方法,是根據(jù) Object.equals,和 Object.hashCode這兩個(gè)方法來判斷是否重復(fù)的,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05

