简体   繁体   English

从InputStream获取URL

[英]Get URL from InputStream

I'm wondering how InputStream in java is implemented from low level perspective. 我想知道如何从低角度实现Java中的InputStream

Suppose I write a below java code for making connection with a website. 假设我编写了下面的Java代码来与网站建立连接。

url = new URL("[some url info]");
URLConnection urlcon = url.openConnection();
InputStream in = urlcon.getInputStream();
while((readcount = in.read(buffer)) != -1){
        fos.write(buffer,0,readcount);

Could I know URL from InputStream("in" in the above code block) directly by casting it's type and call an appropriate method like below? 通过转换类型并调用如下所示的适当方法,我可以直接从InputStream(在上面的代码块中的“ in”)中了解URL吗? Are there any other ways to get URL from InputStream? 还有其他方法可以从InputStream获取URL吗?

(newtype) new = (newtype) in;
String Url = new.appropriatemethod();

I searched all subclasses of InputStream, but I couldn't find any classes which have a interface to give it's URL. 我搜索了InputStream的所有子类,但是找不到任何具有提供其URL的接口的类。 ( https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html ) https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html

However, I think InputStream somehow have information of URL to receive data from a website which has this URL. 但是,我认为InputStream某种程度上具有URL信息以从具有此URL的网站接收数据。

I might have big misunderstanding about "Stream". 我可能对“流”有很大的误解。

Thank you for reading my quetion. 感谢您阅读我的问题。 :) :)

InputStream is just a stream not a URLConnection . InputStream只是一个流,而不是URLConnection When you type InputStream in = urlcon.getInputStream(); 当您InputStream in = urlcon.getInputStream();键入InputStream in = urlcon.getInputStream(); . You will get input stream not a url connection. 您将获得输入流而不是URL连接。 When you type in.read then you are reading the Stream and not URLConnection . 当您输入in.read您正在读取Stream而不是URLConnection

An InputStream is a reference to source of data (be it a file, network connection etc), that we want to process as follows: InputStream是对数据源(例如文件,网络连接等)的引用,我们要按以下方式对其进行处理:

  • we generally want to read the data as "raw bytes" and then write our own code to do something interesting with the bytes; 我们通常希望将数据读取为“原始字节”,然后编写自己的代码以对字节进行一些有趣的操作;

  • we generally want to read the data in sequential order: that is, to get to the nth byte of data, we have to read all the preceding bytes first, and we're not guaranteed to be able to "jump back" again once we've read them. 我们通常希望按顺序读取数据:也就是说,要获取数据的第n个字节,我们必须先读取所有前面的字节,并且不保证一旦我们再次能够“跳回”已经读过了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM