Storing Objects and Classes
Page 4 of 5

Storing Media Objects and Playing Them within Cloudview

Part of the reason for the popularity of the Java programming language is how easy it makes it to integrate media objects like pictures, video, and sound into your applications.

You can use Cloudscape to help organize your application's media in a number of different ways.

If a class is serializable, you can store it directly in the database. If it is not, you can store the object as a binary (LONG VARBINARY or VARBINARY) data type. However, you cannot execute any database-side methods on objects stored as non-object binaries. (To execute application-side methods on objects stored in that way, reserialize the object after retrieval.)

If you want to execute database-side methods on objects stored in the database, one solution is to make a serializable thin wrapper class around a byte array field. The examples in this section use that solution.

Media objects such as these aren't much use unless you have a way to play them. Application developers provide such a way in end-user applications. However, you may want to "play" such objects stored in the database while still in the development phase. If your class has a method that creates a window or dialog box and then displays the object in the window or dialog box, Cloudview lets you "play" these objects.

For example, toursDB has a table called Maps. This table stores objects of type JBMSTours.serializabletypes.Picture. The Picture class has a field called content which stores an image as a byte array. Its display() method creates a new Image from that byte array and displays it in a window, so it can be displayed by Cloudview.

Play the Maps in toursDB

  1. Start Cloudview as described in Start Cloudview in the Home Directory. Then open a connection to toursDB as described in Open a Connection to toursDB. Select the toursDB icon in the left-hand hierarchy window. Click the "+" to the left of the icon to display the Table icon.
  2. Click the plus sign to the left of the Table icon to display the tables in toursDB.
  3. Select the Maps table in the left-hand hierarchy window.
  4. Select the Data tab.
  5. The third column in the table, map, is the one that stores the picture. Select the map column in the last row. Click the magnifying class icon.

    The Object Inspector window appears.

  6. Select MAP=JBMSTours.serializabletypes.Picture.

    A list of available methods appears in the right-hand window.

  7. Select display and click Execute.

    The map appears in a new frame.

  8. Select any other picture to display it.

Examine Picture.java to See How the Image Is Stored

  1. Open the source file for Picture, JBMSTours.serializabletypes.Picture.java in /demo/programs/tours/JBMSTours/serializabletypes.
  2. Examine the class definition.

    One of the fields in the class is an array of bytes. When displaying the image, the class creates a new instance of a java.awt.Image, reading in the data from the byte array field.

    Quiz: Why can't you just store a java.awt.Image?

    Answer: Because java.awt.Image is not serializable. All Java data types stored in a Cloudscape database must be serializable.

Insert Some URLPictures

JBMSTours.serializabletypes.Picture loads a picture available to the JVM on the class path. Sometimes you want to work with media in various places, such as on the Internet. You may want to store the actual media, or you may want to store only references to the media.

JBMSTours.serializabletypes.URLPicture is a subclass of Picture that gets its content from a URL. It contains a static field called MAXSIZE. If the content is larger than that field, the class stores only the URL as a reference. If the content is smaller than that field, the class gets the entire content from the URL and stores that. The display() method is implemented to allow Cloudview to display the picture in either case.

  1. Go to the SQL window.
  2. Insert a "small" URLPicture (in which the content is actually stored) from the Internet:

    INSERT INTO MAPS VALUES ('AU', false, new JBMSTours.serializabletypes.URLPicture(
        'http://www.lib.utexas.edu/Libs/PCL/Map_collection/australia/Australia_sm97.gif',
        'Small map of Australia'));

    You can store the URLPicture in the Maps table because URLPicture is a subclass of Picture.

  3. Insert a "large" image (in which only a reference is stored) from the Internet:

    INSERT INTO MAPS VALUES ('RU', true, new JBMSTours.serializabletypes.URLPicture('http://www.lib.utexas.edu/Libs/PCL/Map_collection/commonwealth/Russia.94.jpg', 'Big map of Russia'));

  4. Play these new images. Select the Maps table in the left-hand hierarchy window.
  5. Select the Data tab.
  6. The third column in the table, map, is the one that stores the picture. Select the map column in the row that contains one of the new pictures. Click the Magnifying Glass icon.

    The Object Inspector window appears.

  7. Select MAP=JBMSTours.serializabletypes.URLPicture.

    A list of available methods appears in the right-hand window.

  8. Select display().
  9. Click Execute.

    The map appears in a new frame.

  10. Exit Cloudview.