看板java
标 题Re: XML document利用tcp socket来传递
发信站资讯传奇 (Thu May 18 17:12:32 2006)
转信站ptt!ctu-reader!ctu-gate!news.ntu!mars.scc.ntnu!ctu-peer!news.nctu!news
【 在 Julian (不喜欢台中) 的大作中提到: 】
: 我想要将利用jdom所产生的xml document直接利用socket来传递
: 可是我试了许多方法都不可行
: 因为XML parser好像读到eof就会关闭
: jdom也只提供将document转为outputstream的方法
: 不知道大家是否有遇过这样的问题
: 麻烦大家了
http://www.jdom.org/docs/faq.html#a0340
Why does passing a document through a socket sometimes hang the parser?
The problem is that several XML parsers close the input stream when they read E
OF (-1). This is true of Xerces, which is JDOM's default parser. It is also tru
e of Crimson. Unfortunately, closing a SocketInputStream closes the underlying
SocketImpl, setting the file descriptor to null. The socket's output stream is
useless after this, so your application will be unable to send a response. To w
orkaround, protect your socket's input stream with an InputStream wrapper that
doesn't close the underlying stream (override the close() method), or read ever
ything into a buffer before handing off to the JDOM builder:
byte[] buf = new byte[length];
new DataInputStream(inputStream).readFully(buf);
InputStream in = new ByteArrayInputStream(buf);
(Contributed by Joseph Bowbeer)
--
※ 来源:‧资讯传奇 inf.csie.thu.edu.tw‧[FROM: mail.csie.thu.edu.tw]