Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: java.io.IOException: Premature EOF

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java.io.IOException: Premature EOF

    Hi guys!

    I am getting a java.io.IOException: Premature EOF error about some urls (usually, the ones with a huge content), after executing the following peace of code. Any ideas?

    public static void urlReader(String url, String path, String fileName) {
            String inputLine = null;
     
            URL x = null;
            BufferedReader in = null;
     
            createFile(path, fileName);
     
            try {
                // Create file
                FileWriter fwstream = new FileWriter(path + fileName);
                BufferedWriter out = new BufferedWriter(fwstream);
     
                x = new URL(url);
                in = new BufferedReader(new InputStreamReader(x.openStream()));
     
                while ((inputLine = in.readLine()) != null) {
     
                    if (inputLine.length() > 0) {
                        //System.out.println(inputLine + "\n");
                        out.write(inputLine + "\n");
                    }
                }
     
                // Close the input stream
                in.close();
     
                // Close the output stream
                out.close();
            } catch (IOException ex) {
                System.out.println(ex);
                System.out.println("Application aborted while function \"urlReader();\" was executing!");
                System.out.println("url = \"" + url + "\"\n");
                System.out.println("inputLine = \"" + inputLine + "\"\n");
                //System.exit(-1);
            }
        }


    NOTE: I tried to modify the above code using the following one (and different values of BUFFER_SIZE variable) but unfortunately, the same error still occurs. Please, take a look and tell me if there is any wrong with it!


    static int BUFFER_SIZE=1024;
    byte[] buffer = new byte[BUFFER_SIZE]; // or some other size, 
    int charsRead = 0;
    while ( (charsRead  = in.read(buffer,0,BUFFER_SIZE) != -1) {
      out.write(buffer,0, charsRead);
    }


    Kind regards,
    CrazyVag.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: java.io.IOException: Premature EOF

    With large amounts of data being downloaded, it's very likely that your application is processing data faster than it can come in. Before each read, check to make sure that the underlying data stream is ready to be read from.

    while (!in.ready())
    {
         Thread.sleap(100); // wait for stream to be ready.
    }

    Also, a second problem you could encounter (though I don't think there will be any exception thrown for this) is that if you fill up the buffered writer, it won't automatically flush sometimes, leaving bits of data out of your file. Simply flush the stream every now and then to ensure that the writer buffer doesn't fill up completely.

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.io.IOException: Premature EOF

    Hi! and thanks for your response. Do you mean something like the following? Unfortunately, this doesn't work. Actually, application stops working after reading the first file or something like that... Is there any misunderstood? Sorry about my quwestions but I am newbie in Java.


    public static void urlReader(String url, String path, String fileName) {
    String inputLine = null;

    URL x = null;
    BufferedReader in = null;

    createFile(path, fileName);

    try {
    // Create file
    FileWriter fwstream = new FileWriter(path + fileName);
    BufferedWriter out = new BufferedWriter(fwstream);

    x = new URL(url);
    in = new BufferedReader(new InputStreamReader(x.openStream()));

    while ((inputLine = in.readLine()) != null) {

    if (inputLine.length() > 0) {
    //System.out.println(inputLine + "\n");
    out.write(inputLine + "\n");
    }

    while (!in.ready())
    {
    try {
    Thread.sleep(100); // wait for stream to be ready.
    } catch (InterruptedException ex) {
    Logger.getLogger(application.class.getName()).log( Level.SEVERE, null, ex);
    }
    }
    }

    // Close the input stream
    in.close();

    // Close the output stream
    out.close();
    } catch (IOException ex) {
    System.out.println(ex);
    System.out.println("Application aborted while function \"urlReader();\" was executing!");
    System.out.println("url = \"" + url + "\"\n");
    System.out.println("inputLine = \"" + inputLine + "\"\n");
    //System.exit(-1);
    }

  4. #4
    Junior Member
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.io.IOException: Premature EOF

    Any suggestion?

  5. #5
    Junior Member
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.io.IOException: Premature EOF

    Okay. Here we are!

    I added the following command (i.e. ex.printStackTrace() inside the catch block in order to show you the stack trace.
    I really concern about the behavior of my application because the "stuck" comes from different urls in each execution...
    Also, it is worth to notice that, after the installation of netBeans and the app-execution in a new machine, the app completed successfully!
    But, this doesn't happen again! What the !@#$ is going on? How can I solve the bug?
    Some guys told me about the following improvement but neither this worked out!

    while (!in.ready())
    {
    Thread.sleap(100); // wait for stream to be ready.
    }


    run:
    Application aborted while function "urlReader();" was executing!
    java.io.IOException: Premature EOF
    java.io.IOException: Premature EOF
            at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:538)
            at sun.net.www.http.ChunkedInputStream.readAhead(ChunkedInputStream.java:582)
            at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:669)
            at java.io.FilterInputStream.read(FilterInputStream.java:116)
            at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2512)
            at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
            at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
            at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
            at java.io.InputStreamReader.read(InputStreamReader.java:167)
            at java.io.BufferedReader.fill(BufferedReader.java:136)
            at java.io.BufferedReader.readLine(BufferedReader.java:299)
            at java.io.BufferedReader.readLine(BufferedReader.java:362)
            at x.application.urlReader(application.java:429)
            at x.application.downloadPages(application.java:410)
            at x.application.main(application.java:46)
    Java Result: -1
    BUILD SUCCESSFUL (total time: 2 minutes 10 seconds)

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: java.io.IOException: Premature EOF

    Hmm.. Since it's just the reading part that's having troubles, maybe this can help: How to grab html source website url. Maybe you can adapt it to fit your needs.

  7. #7
    Junior Member
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.io.IOException: Premature EOF

    Thanks helloworld922, but...
    Ohhhh my GOD!
    What can I do...
    It starts getting on my nerves....

  8. #8
    Junior Member
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.io.IOException: Premature EOF

    As a newbie, I am not familiar with the stack trace.
    Is there any way of using this kind information?

  9. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.io.IOException: Premature EOF

    Hi!

    I see it's a really old post, but now I have the same problem, really annoying. It works on an other machine, but on my mashine I get the same Exception.. I'M using Jax-ws rt and only occurs the problem when using mtom..

    Exception in thread "main" javax.xml.ws.WebServiceException: java.io.IOException: Premature EOF
    at com.sun.xml.ws.transport.http.client.HttpTransport Pipe.process(HttpTransportPipe.java:225)
    at com.sun.xml.ws.transport.http.client.HttpTransport Pipe.processRequest(HttpTransportPipe.java:136)
    at com.sun.xml.ws.transport.DeferredTransportPipe.pro cessRequest(DeferredTransportPipe.java:110)
    at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:1 063)
    at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:97 9)
    at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:950 )
    at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:8 25)
    at com.sun.xml.ws.client.Stub.process(Stub.java:443)
    at com.sun.xml.ws.client.sei.SEIStub.doProcess(SEIStu b.java:174)
    at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke (SyncMethodHandler.java:119)
    at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke (SyncMethodHandler.java:102)
    at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.j ava:154)
    at $Proxy31.call(Unknown Source)
    at com.etixpert.test.ws.client.WSClient.testInvocatio nService(WSClient.java:95)
    at com.etixpert.test.ws.client.WSClient.main(WSClient .java:37)
    Caused by: java.io.IOException: Premature EOF
    at sun.net.http://www.http.ChunkedInputStream.r...ream.java:538)
    at sun.net.http://www.http.ChunkedInputStream.r...ream.java:582)
    at sun.net.http://www.http.ChunkedInputStream.r...ream.java:669)
    at java.io.FilterInputStream.read(FilterInputStream.j ava:116)
    at sun.net.www.protocol.http.HttpURLConnection$HttpIn putStream.read(HttpURLConnection.java:2672)
    at sun.net.www.protocol.http.HttpURLConnection$HttpIn putStream.read(HttpURLConnection.java:2667)
    at com.sun.xml.ws.transport.http.client.HttpClientTra nsport$1.close(HttpClientTransport.java:197)
    at com.sun.xml.ws.transport.http.client.HttpTransport Pipe.createResponsePacket(HttpTransportPipe.java:2 55)
    at com.sun.xml.ws.transport.http.client.HttpTransport Pipe.process(HttpTransportPipe.java:221)

    I think we have the same problem