Avatar billede fredand Forsker
15. september 2006 - 21:34 Der er 4 kommentarer og
1 løsning

Why does it only reads half of the file from the end?

Hello!

I have experience something really strange that I hope some one could explain.


I read a file from inside a jar with a EJB like


java.io.InputStream is = getClass().getClassLoader().getResourceAsStream(xsdFilePath);
byte[] xsdByte = new byte[100000];
//Also tried
//byte[] xsdByte = new byte[is.available()];
is.read(xsdByte);

How ever when I try to print out this file, with the code below for debug reson, I only get like half of the file from the end???!!!!!
The file is a xsd-file about 4179 chars.


java.io.InputStreamReader inr = new java.io.InputStreamReader(is);
java.io.BufferedReader buf = new java.io.BufferedReader(inr);
String line = null;
StringBuffer stringBuffer = new StringBuffer();
while( (line = buf.readLine()) != null )
{
    System.out.println(line);
    stringBuffer.append(line);
}
//is.close();
//inr.close();
//buf.close();

I have both try to close the resources and not but that according to me can not have any impact since it take place after the output.

Hopefully I will get the whole file but I do not know whats wrong!

Does any one see the error?

Best regards
Fredrik
Avatar billede arne_v Ekspert
15. september 2006 - 21:46 #1
.available() is a rather "unreliable" method

well the method works fine, but it does not do what most people think it does

it does not return how many bytes there are left

it returns how many bytes are available in the IO buffer (= how many byteas
can be read without waiting for IO to complete)

read until EOF instead
Avatar billede arne_v Ekspert
16. september 2006 - 02:20 #2
BTW, you do probably not need the ByteArrayInputStream - just use the
getClass().getClassLoader().getResourceAsStream(xsdFilePath)
Avatar billede fredand Forsker
18. september 2006 - 08:30 #3
Hello!

It looks like there might be an bug in the :
parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", inputSource);

I ended up in a lot of different errors. For eg
1) I just got half of the file from the end?!!!
2) Premature end of file (I propably the other half)
3) Strange charachters

It looks like I needed to read the xsd in some other way.
With some test back and fore I ended up with:

    public boolean validateXML(String xml, String xsdFilePath, String nameSpace, String rootElementName)
    {

        try {
            //Build temporary document
            Document documentTemp = buildDocumentFromString(xml);

            //Set namespace and schemaLocation for validation this will not concern the paths in xml sent to IC
            setXmlElementAttribute(rootElementName, "xmlns", nameSpace, documentTemp);

            //Transform document to xml String
            String tempXml = getXmlStringFromDocument(documentTemp);

            //Validate the xml String
            saxParseException = null;
            SAXParser parser = new SAXParser();
            parser.setFeature("http://xml.org/sax/features/validation", true);
            parser.setFeature("http://apache.org/xml/features/validation/schema", true);
            parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);

            //Use this when file is outside jar
            //parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", xsdFilePath);

            java.io.InputStream is = getClass().getClassLoader().getResourceAsStream(xsdFilePath);
            //Seems to be a bug with
            //parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", inputSource);

            //There for we must read the xsd like
            java.io.InputStreamReader inr = new java.io.InputStreamReader(is);
            java.io.BufferedReader buf = new java.io.BufferedReader(inr);
            String line = null;
            StringBuffer stringBuffer = new StringBuffer();
            while( (line = buf.readLine()) != null )
            {
                stringBuffer.append(line);
            }
            is.close();
            inr.close();
            buf.close();
            org.xml.sax.InputSource inputSource = new  org.xml.sax.InputSource(new java.io.StringReader(stringBuffer.toString()));
            parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", inputSource);

            //Parse xml against xsd
            parser.setErrorHandler(this);
            parser.parse(new InputSource(new StringReader(tempXml)));

            if (saxParseException == null) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            logger.error(e, e);

            return false;
        }
    }

Please comment of you see anything stupid.

But it looks like we must re-read the xsd.

BTW Please give a "svar" so I can reward you for your time and valuable help!

/Fredrik
Avatar billede arne_v Ekspert
19. september 2006 - 05:21 #4
svar
Avatar billede fredand Forsker
19. september 2006 - 10:31 #5
Thanks alot!!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester