Methods Summary |
---|
static byte[] | getBookImage(java.lang.String title, boolean gif)Gets an image for a book with a given title.
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 void | getBookImageData()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;
|