日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

[Android Pro] InputStream.skip方法的思考

發布時間:2025/7/14 Android 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Android Pro] InputStream.skip方法的思考 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考 :?http://blog.csdn.net/gsyzhu/article/details/8102286

在java.io.InputStream類中定義了skip這個方法。在API中的描述如下:

skip

public long skip(long?n)throws IOException
Skips over and discards?n?bytes of data from this input stream. The?skip?method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly?0. This may result from any of a number of conditions; reaching end of file beforen?bytes have been skipped is only one possibility. The actual number of bytes skipped is returned. If?n?is negative, no bytes are skipped.

The?skip?method of this class creates a byte array and then repeatedly reads into it until?n?bytes have been read or the end of the stream has been reached. Subclasses are encouraged to provide a more efficient implementation of this method. For instance, the implementation may depend on the ability to seek.

?

Parameters:
n?- the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.
Throws:
IOException?- if the stream does not support seek, or if some other I/O error occurs.

翻譯如下:

?skip(long n) throws?IOException


跳過和丟棄此輸入流中數據的?n?個字節。出于各種原因,skip?方法結束時跳過的字節數可能小于該數,也可能為?0。導致這種情況的原因很多,跳過?n?個字節之前已到達文件末尾只是其中一種可能。返回跳過的實際字節數。如果?n?為負,則不跳過任何字節。

此類的?skip?方法創建一個 byte 數組,然后重復將字節讀入其中,直到讀夠?n?個字節或已到達流末尾為止。建議子類提供此方法更為有效的實現。例如,可依賴搜索能力的實現。

n
要跳過的字節數。
return
跳過的實際字節數。
ThrowsIOException:?
如果流不支持搜索,或者發生其他 I/O 錯誤。

?

同時,其子類FileInputStream中也繼承了skip這個方法。如API中所描述的,skip方法會存在跳過的字節數小于預期的情況。如果不對返回值進行處理的話,很容易忽視這個問題,導致結果錯誤。最近在看baksmali的源碼,其中有一個簡單而巧妙的方法來避過skip方法的這個弊端。

在分片上傳文件的時候,進行skip,該處理手段非常重要。

FileInputStream in = new FileInputStream(file); int at = offset; while(at > 0) {long realSkip = in.skip(at);if (realSkip == -1) {throw new RuntimeException(file + ": unexpected EOF");}at -= realSkip; }

總結

以上是生活随笔為你收集整理的[Android Pro] InputStream.skip方法的思考的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。