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

BookImageServletData

public class BookImageServletData extends Object

Fields Summary
private static HashMap
bookMap
Map from book name to image data
private static String[]
bookTitles
List of book titles.
private static byte[]
buffer
Buffer used when reading image data
private static ByteArrayOutputStream
os
Stream used when reading image data
Constructors Summary
Methods Summary
static byte[]getBookImage(java.lang.String title, boolean gif)
Gets an image for a book with a given title.

param
title the book title
param
gif true to return GIF data, false for JPEG data.

        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;        
    
private static voidgetBookImageData()
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.

        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();
        }
    
public static java.lang.String[]getBookTitles()
Gets a list of all of the book names.

        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;
    
private static byte[]getImage(java.lang.String path)
Reads an image file and returns the content.

        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;