How do I improve this code for reading an epub file? The code is as follow:
package org.example.mymenu;
import java.awt.print.Book;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
//import java.util.List;
//import org.example.mymenu.Book;
//import nl.siegmann.epublib.domain.TocReference;
//import nl.siegmann.epublib.epub.epubReader;
import android.app.Activity;
import android.content.res.AssetManager;
//import android.graphics.Bitmap;
//import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
/**
* Log the info of 'assets/books/testbook.epub'.
*
* @author paul.siegmann
*
*/
public class Eread extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AssetManager assetManager = getAssets();
try {
// find InputStream for book
InputStream epubInputStream = assetManager
.open("/assets/sample.epub");
// Load Book from inputStream
Book book = (new Eread()).readEpub(epubInputStream);
} catch (IOException e) {
Log.e("epublib", e.getMessage());
}
}
Book readEpub(InputStream epubInputStream) {
// TODO Auto-generated method stub
Eread epubReader = new Eread();
try {
Book book = epubReader.readEpub(new FileInputStream("sample.epub"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
-
2\$\begingroup\$ To be honest, I don't know what to review here. There is virtually no notable code there other than the initialization of a Book object. The only thing think I can think of is: handle the exceptions better. \$\endgroup\$RoToRa– RoToRa2011年06月15日 19:59:02 +00:00Commented Jun 15, 2011 at 19:59
1 Answer 1
According to the Code Conventions for the Java Programming Language, 9. Naming Conventions, I'd call the class
EpubReader
. The name should be a noun, and try to avoid abbreviations which makes the code harder to read.Couldn't the
readEpub
method beprivate
? Why has it default access?The
readEpub
does not use theepubInputStream
parameter. Why is it readsample.epub
? It looks like a test code. Furthermore, this class seems really incomplete.onCreate
creates a newEread
instance then calls itsreadEpub
method. ThereadEpub
creates (again!) a newEread
instance (epubReader
) then calls itsreadEpub
method. It's an endless loop.onCreate
is an instance method, so you could callreadEpub
without creating a newEread
instance:Book book = readEpub(epubInputStream);