Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Premature end of file using JAXB and Unmarshaller. The xml fromt he response looks valid to me

I don't know what to do anymore. Everything seems correct; input/output.

I generate xml file and send to some service to validate.

The response is:

11:10:34,922 INFO  [STDOUT] printing out the input stream
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Response>
    <Method name="XML/Release/New" time="2013-04-23T15:10:35.1446238Z">
        <ResponseStatus>100</ResponseStatus>
    </Method>
</Response>
finished printing out the input stream
11:10:34,922 INFO  [STDOUT] got the unmarshaller
11:10:34,925 ERROR [PRNDataAccessUtil] Caught an error: javax.xml.bind.UnmarshalException
 - with linked exception: [org.xml.sax.SAXParseException: Premature end of file.] : null

The code:

try {
            out = connection.getOutputStream();
            ByteArrayOutputStream bos = PRNPostNewsReleaseUtil.createNewsReleaseXml(newsRelease);
            bos.writeTo(out);

            JAXBContext context = JAXBContext.newInstance(Response.class.getPackage().getName());
            in = connection.getInputStream();

            BufferedReader inp = new BufferedReader(new InputStreamReader(in));
            System.out.println("printing out the input stream");
            String line;
            while((line = inp.readLine()) != null) {
                System.out.println(line);
            }
            System.out.println("finished printing out the input stream");

            Unmarshaller unmarshaller = context.createUnmarshaller();
            response = (Response) unmarshaller.unmarshal(in);

        } catch (Exception ex) {
            log.error("Caught an error: " + ex + " : " + ex.getMessage());
            return null;
        } finally {
            if (null != in) connection.disconnect();
        }
like image 603
iCodeLikeImDrunk Avatar asked Apr 23 '13 15:04

iCodeLikeImDrunk


People also ask

What does Premature end of file mean?

Premature end of file indicates your XML file was not complete, since your are using URL connection here, I suspect network issues. Best way to solve this issue is to capture this XML file using wireshark or TCP monitor kind of tools and then check if it is complete.

Is JAXB Unmarshaller thread safe?

All other objects, including Marshaller and Unmarshaller, are not thread-safe and must not be shared. The static helper methods in the JAXB class can be used from several threads, of course. In practice, this means that if you need a JAXBContext instance, you should probably store in a static member.


2 Answers

You are getting the error because the InputStream has been advanced to the end during the output. Assuming the buffer in your BufferedReader is large enough to contain the whole XML document you can reset it after outputting and then unmarshal that.

like image 50
bdoughan Avatar answered Oct 24 '22 10:10

bdoughan


One time happened to me that I was using the wrong class name to build the JAXBContext object, so when I tried to marshall an object, an empty XML file was created, thus making the unmarshaller fail.

So make sure the JAXBContext object is instantiated with the class you're trying to marshall.

like image 3
Pedro Madrid Avatar answered Oct 24 '22 09:10

Pedro Madrid