在java.io.InputStream類中定義了skip這個方法。在API中的描述如下:
<dl></dl>
<dd>Skips over and discards <code>n</code> bytes of data from this input stream. The <code>skip</code> method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly <code>0</code>. This may result from any of a number of conditions; reaching end of file before<code>n</code> bytes have been skipped is only one possibility. The actual number of bytes skipped is returned. If <code>n</code> is negative, no bytes are skipped.</dd>
The <code>skip</code> method of this class creates a byte array and then repeatedly reads into it until <code>n</code> 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.
<dd></dd>
<dd><dl></dl></dd>
<dt>Parameters:</dt>
<code>n</code> - the number of bytes to be skipped.
<dt>Returns:</dt>
<dd>the actual number of bytes skipped.</dd>
<dt>Throws:</dt>
翻譯如下:
跳過和丢棄此輸入流中資料的 <code>n</code> 個位元組。出于各種原因,<code>skip</code> 方法結束時跳過的位元組數可能小于該數,也可能為 <code>0</code>。導緻這種情況的原因很多,跳過 <code>n</code> 個位元組之前已到達檔案末尾隻是其中一種可能。傳回跳過的實際位元組數。如果 <code>n</code> 為負,則不跳過任何位元組。
此類的 <code>skip</code> 方法建立一個 byte 數組,然後重複将位元組讀入其中,直到讀夠 <code>n</code> 個位元組或已到達流末尾為止。建議子類提供此方法更為有效的實作。例如,可依賴搜尋能力的實作。
n
要跳過的位元組數。
return
跳過的實際位元組數。
Throws
如果流不支援搜尋,或者發生其他 I/O 錯誤。
同時,其子類FileInputStream中也繼承了skip這個方法。如API中所描述的,skip方法會存在跳過的位元組數小于預期的情況。如果不對傳回值進行處理的話,很容易忽視這個問題,導緻結果錯誤。最近在看baksmali的源碼,其中有一個簡單而巧妙的方法來避過skip方法的這個弊端。
在分片上傳檔案的時候,進行skip,該處理手段非常重要。


本文轉自demoblog部落格園部落格,原文連結http://www.cnblogs.com/0616--ataozhijia/p/4973042.html如需轉載請自行聯系原作者
demoblog