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é)或者另外一個線程也讀了導(dǎo)致返回部分字節(jié),也就是說如果使用available()方法去獲取InputStream的長度來作為字節(jié)數(shù)組的長度,那可能會出現(xiàn)字節(jié)接受不完整的錯誤,所以不推薦使用該方法的返回值去分配一個緩沖的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的MyBatis框架中的緩存與緩存的使用改進(jìn)
很多人在使用MyBatis的緩存后經(jīng)常會遇到MySQL分頁查詢的顯示問題,針對于此,這里我們就來詳解Java的MyBatis框架中的緩存與緩存的使用改進(jìn),首先來回顧一下MyBatis的緩存機(jī)制與執(zhí)行:2016-06-06Elasticsearch 基礎(chǔ)介紹及索引原理分析
這篇文章主要介紹了Elasticsearch 基礎(chǔ)介紹及索引原理分析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07Java實(shí)現(xiàn)數(shù)據(jù)脫敏的方法詳細(xì)講解
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)數(shù)據(jù)脫敏的相關(guān)資料,數(shù)據(jù)脫敏是指對某些敏感信息通過脫敏規(guī)則進(jìn)行數(shù)據(jù)的變形,實(shí)現(xiàn)敏感隱私數(shù)據(jù)的可靠保護(hù),需要的朋友可以參考下2023-06-06javaweb啟動時啟動socket服務(wù)端代碼實(shí)現(xiàn)
這篇文章主要介紹了javaweb啟動時啟動socket服務(wù)端代碼實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11關(guān)于IDEA 2020使用 mybatis-log-plugin插件的問題
這篇文章主要介紹了關(guān)于IDEA 2020使用 mybatis-log-plugin插件的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11