URLConnection类中的getInputStream方法是如何返回一个InputStream 对象的?

ewwerpm 2010-06-09 11:42:51
jad反编译后的代码:

public abstract class URLConnection
{
public InputStream getInputStream()
throws IOException
{
throw new UnknownServiceException("protocol doesn't support input");
}
}

根本就没用return语句,是怎么回事呢?
还有,比如

public abstract class InputStream
implements Closeable
{

public InputStream()
{
}

public abstract int read()
throws IOException;
public int read(byte abyte0[])
throws IOException
{
return read(abyte0, 0, abyte0.length);
}
}

API 说是read()“从输入流读取下一个数据字节。”这抽象方法是如何读取下一个数据字节的呢?read(byte abyte0[])又是返回一个什么样的int整数呢?
...全文
693 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
sun.net.www.protocol.http.HttpURLConnection#getInputStream() 方法非常复杂,有 300 多行源代码,这里也贴不下。

  • 打赏
  • 举报
回复
如果 URL 的 openConnection 打开的是 HTTP 协议,那么就是使用 sun.net.www.protocol.http.HttpURLConnection 实现的,这个类是 java.net.HttpURLConnection 的子类,两个类的类名相同,呵呵。当然了,他们同时都是 java.net.URLConnection 的子类。
answer76 2010-06-10
  • 打赏
  • 举报
回复
抽象方法没有具体实现,更谈不上return.
answer76 2010-06-10
  • 打赏
  • 举报
回复
抽象方法没有个体的实现,要子类继承他,然后去重写他的方法,就有了实现的细节。抽象方法咱就不说了。

至于你说的普通的read方法看API
Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.
他是通过从字节流中给定偏移量逐个读取各个字节进字节数组中。那个返回的int看这里
the number of bytes read, or -1 if the end of the stream has been reached.
如果是读到节尾了,返回-1否则返回已读字节数。

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧