FileDocCategorySizeDatePackage
BookImageServletData.javaAPI DocExample4164Tue Jul 23 16:54:38 BST 2002ora.jwsnut.chapter3.bookimageservice

BookImageServletData.java

package ora.jwsnut.chapter3.bookimageservice;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;

class BookImageServletData {
    
    /**
     * Map from book name to image data
     */
    private static HashMap bookMap;
    
    /**
     * List of book titles.
     */
    private static String[] bookTitles;
    
    /**
     * Buffer used when reading image data
     */
    private static byte[] buffer;
    
    /**
     * Stream used when reading image data
     */
    private static ByteArrayOutputStream os;
    
    /**
     * Gets an image for a book with a given title.
     * @param title the book title
     * @param gif <code>true</code> to return GIF data,
     * <code>false</code> for JPEG data.
     */
    static byte[] getBookImage(String title, boolean gif) {
        byte[] data = null;
        if (bookMap == null) {
            // Load the data on first call
            getBookImageData();
        }
        
        if (bookMap != null) {
            byte[][] imageData = (byte[][])bookMap.get(title);
            if (imageData != null) {
                data = imageData[gif ? 1 : 0];
            }
        }
        return data;        
    }
    
    /**
     * Gets a list of all of the book names.
     */
    public static String[] getBookTitles() {
        if (bookTitles == null) {
            if (bookMap == null) {
                // Load the data on first call
                getBookImageData();
            }

            if (bookMap != null) {
                bookTitles = new String[bookMap.size()];
                Iterator iter = bookMap.keySet().iterator();
                int index = 0;
                while (iter.hasNext()) {
                    bookTitles[index++] = (String)iter.next();
                }
            } else {
                bookTitles = new String[0];
            }            
        }
        return bookTitles;
    }
    /**
     * Gets the book image data in the form of a HashMap
     * where the key is the book title and the value is
     * a two dimensional byte array in which the first
     * array provides a JPEG image and the second a GIF.
     */
    private static void getBookImageData() {
        try {
            bookMap = new HashMap();
            InputStream is = 
                    BookImageServletData.class.getResourceAsStream("booklist.txt");
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            buffer = new byte[1024];
            os = new ByteArrayOutputStream();
            String line;
            while ((line = reader.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(line, "!");
                if (st.countTokens() == 3) {
                    String title = st.nextToken();
                    String jpegImagePath = st.nextToken();
                    String gifImagePath = st.nextToken();
                    byte[][] imageData = new byte[2][];
                    imageData[0] = getImage(jpegImagePath);
                    imageData[1] = getImage(gifImagePath);                        
                    bookMap.put(title, imageData);
                }
            }  
            buffer = null;
            os.close();
            os = null;
        } catch (Exception ex) {
            // Just return an empty or partial map                
            ex.printStackTrace();
        }
    }  
    
    /**
     * Reads an image file and returns the content.
     */
    private static byte[] getImage(String path) throws IOException {
        InputStream is = BookImageServletData.class.getResourceAsStream(path);
        int count;
        while ((count = is.read(buffer, 0, buffer.length)) > 0) {
            os.write(buffer, 0, count);
        }
        byte[] content = os.toByteArray();
        os.reset();
        return content;       
    }
}