InputStream 中的 read() api 是如何工作的?
How does read() api in InputStream works?
我正在尝试编写扩展 InputStream 的输入流并了解以下内容
输入流是一个抽象class,它包含抽象方法和它所具有的某些方法的实现。注意到 InputStream 包含一个抽象方法
public abstract int read() throws IOException;
我不太确定这个方法的作用。因此,引用了扩展 InputStream 的其他输入流并得到扩展 InputStream
的 FilterInputStream
并且其中的读取方法实现就像
public int read() throws IOException {
return in.read();
}
in
这里指的是底层输入流。考虑 FilterInputStream
的构造函数,它类似于
protected FilterInputStream(InputStream in) {
this.in = in;
}
在 read() 方法实现中,这只是调用其父 class 中的 read 方法,即 InputStream
这只是一个抽象方法。
- InputStream 中的 read() 方法有什么作用?
- 当我调用 FSDataInputStream 的 read 方法时会发生什么?
- 我们在这里添加read()方法只是为了避免编译错误吗?或者我们还有其他用途吗?
我对这些感到困惑。请帮我了解一下。
What read() method in InputStream does?
读取一个字节的数据。它必须由 InputStream
.
的任何(非抽象)subclass 实现
在FilterInputStream
的情况下,是从另一个流中读取实现的;即过滤器正在包装的流。该流将是 InputStream
的某个子 class 的实例,它实现 read()
以实际从某处读取数据。
What happens when i invoke the read method of FSDataInputStream?
您最终在 FSDataInputStream
包装的流上调用 read()
;即在 FSDataInputStream
构造函数中传递的那个。
Do we add read() method here just to avoid compilation error? Or do we have any other use with this?
InputStream
API 中的 read()
方法的目的是作为实际流中实际方法的占位符 class.
FilterInputStream
中的 read()
方法的目的是成为实际方法。它通过将 read()
调用委托给链中的下一个流来实现。 (请注意,一般来说,FilterInputStream
必须被 subclassed 才有用,并且您会期望 subclass 至少会覆盖某些 read
方法.)
在这种情况下,FSDataInputStream
(即 FilterInputStream
)充当适配器,允许将常规 FSInputStream
用作 DataInputStream
。对于read()
方法,简单的委托就足够了。
因为,@JB Nizet 建议ByteArrayInputStream
以清晰的方式实现读取,代码片段如下,
public synchronized int read() {
return (pos < count) ? (buf[pos++] & 0xff) : -1;
}
所以,这 return 是缓冲区中的下一个字节,声明为
protected byte buf[];
其中 buf[] 是由流的创建者提供的字节数组。缓冲区中的元素是唯一可以从流中读取的字节;元素 buf[pos] 是要读取的下一个字节。
为什么 return 结果像 (buf[pos++] & 0xff)?
这里有一个 nice explanation 让我们清楚为什么 value & with 0xff
完成了。
即;到 return 有符号值的结果(0 到 255 之间)。这将 &
和 0xff
.
的无符号值(字节)转换为有符号值
我正在尝试编写扩展 InputStream 的输入流并了解以下内容
输入流是一个抽象class,它包含抽象方法和它所具有的某些方法的实现。注意到 InputStream 包含一个抽象方法
public abstract int read() throws IOException;
我不太确定这个方法的作用。因此,引用了扩展 InputStream 的其他输入流并得到扩展 InputStream
的 FilterInputStream
并且其中的读取方法实现就像
public int read() throws IOException {
return in.read();
}
in
这里指的是底层输入流。考虑 FilterInputStream
的构造函数,它类似于
protected FilterInputStream(InputStream in) {
this.in = in;
}
在 read() 方法实现中,这只是调用其父 class 中的 read 方法,即 InputStream
这只是一个抽象方法。
- InputStream 中的 read() 方法有什么作用?
- 当我调用 FSDataInputStream 的 read 方法时会发生什么?
- 我们在这里添加read()方法只是为了避免编译错误吗?或者我们还有其他用途吗?
我对这些感到困惑。请帮我了解一下。
What read() method in InputStream does?
读取一个字节的数据。它必须由 InputStream
.
在FilterInputStream
的情况下,是从另一个流中读取实现的;即过滤器正在包装的流。该流将是 InputStream
的某个子 class 的实例,它实现 read()
以实际从某处读取数据。
What happens when i invoke the read method of FSDataInputStream?
您最终在 FSDataInputStream
包装的流上调用 read()
;即在 FSDataInputStream
构造函数中传递的那个。
Do we add read() method here just to avoid compilation error? Or do we have any other use with this?
InputStream
API 中的 read()
方法的目的是作为实际流中实际方法的占位符 class.
FilterInputStream
中的 read()
方法的目的是成为实际方法。它通过将 read()
调用委托给链中的下一个流来实现。 (请注意,一般来说,FilterInputStream
必须被 subclassed 才有用,并且您会期望 subclass 至少会覆盖某些 read
方法.)
在这种情况下,FSDataInputStream
(即 FilterInputStream
)充当适配器,允许将常规 FSInputStream
用作 DataInputStream
。对于read()
方法,简单的委托就足够了。
因为,@JB Nizet 建议ByteArrayInputStream
以清晰的方式实现读取,代码片段如下,
public synchronized int read() {
return (pos < count) ? (buf[pos++] & 0xff) : -1;
}
所以,这 return 是缓冲区中的下一个字节,声明为
protected byte buf[];
其中 buf[] 是由流的创建者提供的字节数组。缓冲区中的元素是唯一可以从流中读取的字节;元素 buf[pos] 是要读取的下一个字节。
为什么 return 结果像 (buf[pos++] & 0xff)?
这里有一个 nice explanation 让我们清楚为什么 value & with 0xff
完成了。
即;到 return 有符号值的结果(0 到 255 之间)。这将 &
和 0xff
.