HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

EPUB reader for Android

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
androidepubforreader

Problem

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

}

Solution

-
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 be private? Why has it default access?

-
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.

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

Book book = readEpub(epubInputStream);

Code Snippets

Book book = readEpub(epubInputStream);

Context

StackExchange Code Review Q#2966, answer score: 3

Revisions (0)

No revisions yet.