Re: XML document利用tcp socket來傳遞
【 在 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]
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
java 近期熱門文章
PTT數位生活區 即時熱門文章