Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
 
Training Index

Introduction to the JavaBeans API, Magercise 10
A URL Hex Dump Bean

by John Zukowski, MageLang Institute

[Help | API Docs | Short Course | Magercises]

For this Magercise you use the BeanBox, or another IDE, to design and test a nongraphical URL Bean. The Bean makes a URL connection to a remote HTTP address and then performs a hex dump of the data at the specified URL. The skeleton includes the hex dump-related functionality so that you can focus strictly on setting up properties, making the connection, and related issues.

Magercise 10 Prerequisites

Skeleton Code

Tasks

  1. The first task with virtually any Bean is to handle the implications of the no-argument constructor. In this case, you can't open/access the URL from the constructor; you must wait until the URL is established with an access method, either programmatically or via a property sheet. With this in mind, design and implement:

    • A no-argument constructor
    • A method openUrl() that creates a URL instance, given a string URL that's passed to the URL constructor
    • A string URL property to establish the value used by openUrl()

  2. Next, design and implement the method retrieveUrlContent() that, given an established URL connection, opens an input stream from the URL, processes the content, and returns the content as a string. The skeleton code provides several content-related class members:

    • private int segment[] = new int[SEG_SIZE];
    • private void readSegment(DataInputStream in);
    • private String formatSegment();
    • private void printSegment();

    The instance variable segment is used globally, for convenience, by the xxxSegment() methods. If you allow these methods to work as designed, for example, don't change the segment size, they will read and format the URL content producing a traditional assembly-style dump:

    000 54 ... 20--74 ... 31 |This is -text..01|
    010 32 ... 39--0a ... 0a |23456789-....end.|
    020 00 ... 00--00 ... 00 |........-........|
    

    The dump has the segment offset on the left, hex values in the center, and ASCII interpretation on the right, if any.

  3. Having designed retrieveUrlContent() to return the content as a string, consider adding functionality to support graphical display of the content.

  4. Optionally, provide several (perhaps convenience) properties that facilitate the use of UrlHexDump from an IDE.

  5. Lastly (and optionally), consider expanding UrlHexDump, say, SearchUrlsHexDump, so that it searches multiple sites and sends event notifications to registered targets.

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution Source

Demonstration

After loading the UrlHexDump Bean, it should appear in the palette, from where it can be selected and instantiated on the worksheet:

In this case, the worksheet contains a nongraphical UrlHexDump Bean and a command button labeled "Hex Dump HTTP". The command button's actionPerformed() method is connected to UrlHexDump's retrieveUrlContentGraphical() method.

The property sheet displays the published properties:

You can add a TextArea instance in a top-level window for displaying the results of a URL hex-dump operation:

Your design and implementation will certainly differ, but the basic objective is to process a URL, produce a hex dump of its contents, and make the dump available for retrieval as a string, and, perhaps optionally, in a graphical window. Note that even though it may be desirable to display the result graphically, the Bean itself is not graphical and will function properly outside a graphical context.

Magercise 11
Short Course
About This Short Course

Copyright © 1998-1999 MageLang Institute. All Rights Reserved.


[ This page was updated: 27-Oct-99 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-99 Sun Microsystems, Inc.
All Rights Reserved. Legal Terms. Privacy Policy.