ContentConnection

Most connections are stream connections. StreamConnection is a convenience interface that implements both InputConnection and OutputConnection:

public interface StreamConnection extends InputConnection, OutputConnection

StreamConnection declares no additional methods of its own, just the five it inherits from its superinterfaces: openInputStream( ), openOutputStream( ), openDataInputStream( ), openDataOutputStream( ), and close( ).

Most stream connections are also instances of its subinterface, ContentConnection:

public interface ContentConnection extends StreamConnection

ContentConnection does add three methods for getting the length, content encoding, and MIME media type of the content in the stream:

public String getType( ) public String getEncoding( ) public long getLength( )

getType( ) usually returns a MIME media type such as text/html or application/xml. However, it may return null if the content type can be determined.

The getEncoding( ) method returns a string indicating which (if any) additional encodings have been applied to the content. For instance, if the content has been gzipped, it returns the string "gzip". This method is mostly meant for HTTP, where it returns the value of the Content-encoding header. In most other protocols, it will likely return null.

Finally, getLength( ) returns the length of the content in bytes. For HTTP, this is the value in the Content-length header. It returns -1 if the content length is not known.

These three methods are mostly designed for HTTP, but they can sometimes work for file streams or other kinds of streams as well. Example 24-2 is a simple MIDlet that displays this information for a user-supplied URL.

Example 24-2. A MIDlet for showing the type and length of data

import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.io.*; import java.io.IOException; public class ContentInfo extends MIDlet implements CommandListener { private Display display; private TextBox textBox; public void startApp( ) { display = Display.getDisplay(this); if (textBox == null) { textBox = new TextBox("URL", "http://", 255, TextField.URL); } display.setCurrent(textBox); Command getInfo = new Command("Get Info", Command.OK, 10); textBox.addCommand(getInfo); textBox.setCommandListener(this); } public void commandAction(Command command, Displayable displayable) { // Network operations should not run in this same thread Thread t = new Thread( new Runnable( ) { public void run( ) { display.setCurrent(getInfo( )); } } ); t.start( ); } private Form getInfo( ) { Form form = new Form("Content Info"); ContentConnection conn = null; try { conn = (ContentConnection) Connector.open(textBox.getString( )); String type = conn.getType( ); String encoding = conn.getEncoding( ); long length = conn.getLength( ); form.append("Media type: " + type + " "); if (encoding != null) form.append("Encoding: " + encoding + " "); form.append("Length: " + String.valueOf(length)); } catch (IOException ex) { form.append(ex.getMessage( )); } finally { try { if (conn != null) conn.close( ); } catch (IOException ex) { /* Oh well. we tried.*/ } } return form; } public void pauseApp( ) {} public void destroyApp(boolean unconditional) {} }

When the MIDlet starts up, it displays a text box in which the user can enter a URL. Once the user enters the URL and activates the Get Info command, the MIDlet changes display to a form showing the content info. It does this in a separate thread to avoid a potential deadlock condition. The getInfo( ) method reads the URL from the text box and connects to it (after checking with the user to make sure the connection is allowed). It then displays the media type, content length, and content encoding (if any). Figure 24-2 shows this program in a J2ME emulator.

Figure 24-2. The content info MIDlet

Категории

© amp.flylib.com,