从InputStream中读取多种格式
我正在尝试编写一个读取 HTTP 请求和响应并解析它们的类。 由于标题是普通文本,因此使用 BufferedReader 和 readLine 方法读取它们似乎是最简单的。这显然不适用于数据体,因为它可能是二进制的,所以我想在读取标头后切换到读取原始字节。
现在,我正在做这样的事情:
InputStream input=socket.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(input));
BufferedInputStream binstream=new BufferedInputStream(input);
问题是 BufferedReader 正在提前读取并吞噬流中的所有二进制数据,然后我才有机会使用二进制流获取它。
有没有办法防止每次调用 readLine 时读取超出换行符的内容? 或者是否有更好的方法来读取原始二进制数据后的单行 ASCII 文本?
I'm trying to write a class that reads HTTP requests and responses and parses them.
Since the headers are ordinary text it seemed easiest to read them using a BufferedReader and the readLine method. This obviously won't do for the data body as it may be binary, so I want to switch over to read raw bytes after the headers have been read.
Right now, I'm doing something like this:
InputStream input=socket.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(input));
BufferedInputStream binstream=new BufferedInputStream(input);
The problem is that the BufferedReader is reading ahead and gobbling up all the binary data from the stream before I have a chance to get at it with the binstream.
Is there a way to prevent it from reading beyond the newline for each call to readLine?
Or is there a better way to read single lines of ASCII text followed raw binary data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不想像 Konstantin 提议的那样使用现成的 HTTP 客户端/服务器实现,DataInputStream 有一个
readLine
方法。它已被弃用,因为它没有进行正确的转换(主要是直接字节 -> 字符转换),但我认为对于纯 ASCII 标题行,你应该很好。(您应该在 DataInputStream 下放置一个 BufferedInputStream,因为 readLine 单独读取每个字节。)
If you don't want to use a ready HTTP client/server implementation like Konstantin proposed, DataInputStream has a
readLine
method. It is deprecated since it isn't doing a proper conversion (mostly a direct byte -> char casting conversion), but I think for pure ASCII header lines you should be good.(You should put a BufferedInputStream under you DataInputStream, since readLine reads each byte individually.)
Java 中已经有一个类用于处理 HTTP 请求和响应。您应该使用它,而不是尝试自己解析响应。解析 HTTP 响应比您想象的要困难,因为您必须处理不同的编码方法。它实际上并不是响应负载中的原始二进制数据。 HttpURLConnection 类将为您解析标头,并为您提供有效负载的 InputStream。
http://download.oracle.com/ javase/1.4.2/docs/api/java/net/HttpURLConnection.html
There is already a class in Java for handling HTTP requests and responses. You should use that instead of trying to parse the response on your own. Parsing HTTP response is more difficult than you think as there are different encoding methods that you have to deal with. It isn't really raw binary data in the response payload. The HttpURLConnection class will parse headers for you and give you InputStream for the payload.
http://download.oracle.com/javase/1.4.2/docs/api/java/net/HttpURLConnection.html
commons-httpclient 可能会为您节省大量工作。
commons-httpclient might save you a heap of work here.