4
\$\begingroup\$

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;
  }


}
\$\endgroup\$
  • 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 Jun 15 '11 at 19:59
3
\$\begingroup\$
  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.

  2. Couldn't the readEpub method be private? Why has it default access?

  3. The readEpub does not use the epubInputStream parameter. Why is it read sample.epub? It looks like a test code. Furthermore, this class seems really incomplete. onCreate creates a new Eread instance then calls its readEpub method. The readEpub creates (again!) a new Eread instance (epubReader) then calls its readEpub method. It's an endless loop.

  4. onCreate is an instance method, so you could call readEpub without creating a new Eread instance:

    Book book = readEpub(epubInputStream);
    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.