EOFException is thrown when reading contents of an ePub file

555 Views Asked by At

I'm trying to read the contents of an ePub file using the library epublib and this example demonstrates that.

For me, an exception is thrown when loading the book from the input stream

// Load Book from inputStream
Book book = (new EpubReader()).readEpub(epubInputStream);

Not sure why the code isn't working and the exception is thrown for me? It has worked for other users of the StackOverflow.

Full Stack trace is shared below:

W/System.err: java.io.EOFException
W/System.err:     at libcore.io.Streams.readFully(Streams.java:83)
W/System.err:     at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:235)
W/System.err:     at nl.siegmann.epublib.epub.EpubReader.readResources(EpubReader.java:184)
W/System.err:     at nl.siegmann.epublib.epub.EpubReader.readEpub(EpubReader.java:94)
W/System.err:     at nl.siegmann.epublib.epub.EpubReader.readEpub(EpubReader.java:53)
W/System.err:     at nl.siegmann.epublib.epub.EpubReader.readEpub(EpubReader.java:37)
W/System.err:     at com.blogspot.gsrikar.ePubViewerActivity.readEPubContents(ePubViewerActivity.java:102)
W/System.err:     at com.blogspot.gsrikar.ePubViewerActivity.onCreate(ePubViewerActivity.java:88)
W/System.err:     at android.app.Activity.performCreate(Activity.java:6904)
W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
W/System.err:     at android.app.ActivityThread.-wrap17(ActivityThread.java)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:148)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7325)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
2

There are 2 best solutions below

0
Chetan Chaudhari On

EOFException is thrown:

  • if there is no data in a stream but you are trying to read.. eg read methods of chain streams like DataInputStream, ObjectInputStream throw EOFException if they are trying to read from FileInputStream but the FileInputStream is empty or

  • if the formats are not matching...eg if int is present and you are using readFloat() of DataInputStream

0
Harshit Thacker On

You should try calling below method before passing it to readEpub. Java Doc link : http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available()

//assuming the above variables are already declared.
if(epubInputStream.available() > 0)
    Book book = (new EpubReader()).readEpub(epubInputStream);
else
    System.out.println("no data to read");