How to send EOF for Java InputStream element?

so I have the following code that opens an input stream and collects information successfully:

             httpInput = httpConnection.openInputStream();               

             sb= new StringBuffer();             

             while (ch != -1) 
             {
                 ch = httpInput.read();
                 sb.append((char)ch);
             }

however, when I try to use the same line (sb.toString ()) in another method, I get an error "Waiting for end of file". so how can i bind the EOF character to my string? NOTE. The answer is basically an XML document coming from a remote server.

so when the code reaches the string "parse", it gives me the error above:

bis = new ByteArrayInputStream (sb.toString (). getBytes ()); doc = docBuilder.parse (bis);

I code this for a blackberry application.

ace

+3
source share
4 answers

ch = httpInput.read(); -1, StringBuffer sb. read() , , , .

:

ByteArrayOutputStream out = new ByteArrayOutputStream();
while (true) 
{
    ch = httpInput.read();
    if( ch == -1 ) {
        break;
    }
    out.write( ch );
}
String result = out.toString( "utf-8" ); // or whatever charset is used

// or if you only need another InputStream:
ByteArrayInputStream in = new ByteArrayInputStream( out.getBytes() );
+2

, , , , . , , , , ASCII. .

, , EOF - "EOF" - -1, read(), .

What is this other method? which character does he expect - can you find out? then just add this? It is not clear what you are actually trying to do there.

+2
source

Perhaps the "Waiting for end of file" error in the XML parser has nothing to do with the EOF character. This may indicate some XML syntax problem (maybe the XML parser encountered a lot of characters after the end of a well-formed XML document)

+1
source

Source: https://habr.com/ru/post/1731270/


All Articles