Most visited

Recently visited

Added in API level 3

AssetFileDescriptor.AutoCloseInputStream

public static class AssetFileDescriptor.AutoCloseInputStream
extends ParcelFileDescriptor.AutoCloseInputStream

java.lang.Object
   ↳ java.io.InputStream
     ↳ java.io.FileInputStream
       ↳ android.os.ParcelFileDescriptor.AutoCloseInputStream
         ↳ android.content.res.AssetFileDescriptor.AutoCloseInputStream


您可以在ParcelFileDescriptor上创建一个InputStream,该流将关闭时为您调用 ParcelFileDescritor.close()

Summary

Public constructors

AssetFileDescriptor.AutoCloseInputStream(AssetFileDescriptor fd)

Public methods

int available()

返回可从此输入流中读取(或跳过)的剩余字节数的估计值,而不会因为此输入流的下一次方法调用而被阻塞。

void mark(int readlimit)

标记此输入流中的当前位置。

boolean markSupported()

测试此输入流是否支持 markreset方法。

int read()

从此输入流中读取一个字节的数据。

int read(byte[] buffer, int offset, int count)

从该输入流中最多读取 len个字节的数据到一个字节数组中。

int read(byte[] buffer)

从这个输入流中读取多达 b.length个字节的数据到一个字节数组中。

void reset()

将此流重新定位到上次在此输入流上调用 mark方法时的位置。

long skip(long count)

跳过并丢弃来自输入流的 n字节的数据。

Inherited methods

From class android.os.ParcelFileDescriptor.AutoCloseInputStream
From class java.io.FileInputStream
From class java.io.InputStream
From class java.lang.Object
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Public constructors

AssetFileDescriptor.AutoCloseInputStream

Added in API level 3
AssetFileDescriptor.AutoCloseInputStream (AssetFileDescriptor fd)

Parameters
fd AssetFileDescriptor
Throws
IOException

Public methods

available

Added in API level 3
int available ()

返回可从此输入流中读取(或跳过)的剩余字节数的估计值,而不会因为此输入流的下一次方法调用而被阻塞。 下一次调用可能是同一个线程或另一个线程。 单个读取或跳过这么多字节不会被阻塞,但可以读取或跳过更少的字节。

在某些情况下,非阻塞式读取(或跳过)在缓慢时可能会被阻止,例如,通过慢速网络读取大型文件时。

Returns
int an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking.
Throws
IOException

mark

Added in API level 3
void mark (int readlimit)

标记此输入流中的当前位置。 随后调用reset方法将此流重新定位到最后标记的位置,以便后续读取重新读取相同的字节。

参数 readlimit告诉这个输入流允许在标记位置失效之前读取很多字节。

mark的总体合同是,如果方法markSupported返回true ,则流以某种方式记住了在调用mark之后读取的所有字节,并准备在调用方法reset时再次提供那些相同的字节。 但是,如果在reset之前从流中读取了超过readlimit个字节,则根本不需要记录任何数据。

标记封闭的流不应该对流有任何影响。

mark的方法 InputStream什么都不做。

Parameters
readlimit int: the maximum limit of bytes that can be read before the mark position becomes invalid.

markSupported

Added in API level 3
boolean markSupported ()

测试此输入流是否支持markreset方法。 是否支持markreset是特定输入流实例的不变特性。 markSupported方法InputStream回报false

Returns
boolean true if this stream instance supports the mark and reset methods; false otherwise.

read

Added in API level 3
int read ()

从此输入流中读取一个字节的数据。 如果没有输入可用,此方法会阻止。

Returns
int the next byte of data, or -1 if the end of the file is reached.
Throws
IOException

read

Added in API level 3
int read (byte[] buffer, 
                int offset, 
                int count)

从这个输入流中读取多达len个字节的数据到一个字节数组中。 如果len不为零,则该方法阻塞,直到某些输入可用; 否则,不读取字节并返回0

Parameters
buffer byte: the buffer into which the data is read.
offset int: the start offset in the destination array b
count int: the maximum number of bytes read.
Returns
int the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.
Throws
IOException

read

Added in API level 3
int read (byte[] buffer)

从这个输入流中最多读取b.length个字节的数据到一个字节数组中。 此方法阻塞,直到有些输入可用。

Parameters
buffer byte: the buffer into which the data is read.
Returns
int the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.
Throws
IOException

reset

Added in API level 3
void reset ()

将此流重新定位到上次在此输入流上调用方法 mark时的位置。

reset的总合同是:

  • If the method markSupported returns true, then:
    • If the method mark has not been called since the stream was created, or the number of bytes read from the stream since mark was last called is larger than the argument to mark at that last call, then an IOException might be thrown.
    • If such an IOException is not thrown, then the stream is reset to a state such that all the bytes read since the most recent call to mark (or since the start of the file, if mark has not been called) will be resupplied to subsequent callers of the read method, followed by any bytes that otherwise would have been the next input data as of the time of the call to reset.
  • If the method markSupported returns false, then:
    • The call to reset may throw an IOException.
    • If an IOException is not thrown, then the stream is reset to a fixed state that depends on the particular type of the input stream and how it was created. The bytes that will be supplied to subsequent callers of the read method depend on the particular type of the input stream.

InputStream的方法 reset除了抛出 IOException之外什么也不 IOException

Throws
IOException

skip

Added in API level 3
long skip (long count)

跳过并丢弃来自输入流的 n字节的数据。

由于各种原因, skip方法可能会跳过一些较小数量的字节,可能是0 如果n为负数,则抛出IOException ,即使InputStream超类的skip方法在这种情况下不起作用。 返回跳过的实际字节数。

此方法可能会跳过比备份文件中剩余的更多的字节。 这不会产生异常,并且跳过的字节数可能包含超出备份文件EOF的一些字节数。 在跳过结尾后试图从流中读取将导致-1指示文件的结尾。

Parameters
count long: the number of bytes to be skipped.
Returns
long the actual number of bytes skipped.
Throws
IOException

Hooray!