I have a problem with Socket connection closing too fast. I was told I need to temporarily load data from Socket and the parse it. Here is my code:
ServerSocket listen = new ServerSocket(this.port);
Socket server;
while(i < this.maxConnections)
{
server = listen.accept();
processRequest(server);
i++;
}
processRequest
protected void processRequest(Socket server) throws IOException
{
ProcessXML response = new ProcessXML(server.getInputStream());
new PrintWriter(server.getOutputStream(), true).println("response text");
}
processXML
public ProcessXML(InputStream is)
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(false);
Document doc = factory.newDocumentBuilder().parse(new InputSource(is));
....
}
error
[Fatal Error] :2:1: Premature end of file.
org.xml.sax.SAXParseException: Premature end of file.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at ProcessXML.<init>(ProcessXML.java:22)
at Bank.processRequest(Bank.java:41)
at Bank.listen(Bank.java:25)
at BankStart.main(BankStart.java:6)
Now I could store content of server.GetInputStream() into file and then supply that to DocumentBuilder, but I think its a bad solution. How could I store content to some temporary storage and also be able to supply that to .parse() without closing the socket.