Connecting Shell Commands


The final iteration of this application shows how to redirect primefind's standard output. It requires no changes to primefind's code. Instead, we define another shell extension, compress, that compresses its standard input and writes that compressed data to a specified file. The two commands can be combined as follows:

 JXTA>primefind 1 100 | compress /export/home/myfiles/primesfile  

The parameter to compress takes the name of the file to which the compressed output is written. Listing 17.5 shows compress' startApp() method.

Listing 17.5 A Message Compression Shell Command
 package net.jxta.impl.shell.bin.compress; ... public class compress extends ShellApp {     /**             This method uses Java's ZIP compression utility to compress its standard  graphics/ccc.gifoutput.             /     public int startApp(String[] argv) {         if (argv.length == 1) {             try {                 Message mes = inputPipe.waitForMessage();                 FileOutputStream os = new FileOutputStream(argv[0] + ".zip");                 ZipOutputStream zipStream = new ZipOutputStream(os);                 MessageElementEnumeration en = mes.getElements();                 while (en.hasMoreElements()) {                     MessageElement element = (MessageElement) en.nextElement();                     ZipEntry entry = new ZipEntry(element.getName());                     zipStream.putNextEntry(entry);                     byte[] content = element.getBytesOffset();                     zipStream.write(content, 0, content.length);                     zipStream.closeEntry();                 }                 zipStream.close();                 os.close();             } catch (InterruptedException e) {             } catch (IOException e) {                 e.printStackTrace();                 return ShellApp.appMiscError;             }         } else {             return ShellApp.appParamError;         }         return ShellApp.appNoError;     }      ... } 

This shell command uses the java.util.zip package's zip compression classes. Having obtained the command's argument from the command line the filename to write the compressed output to the program waits for a message on its standard input. As with the standard output, the standard input is referenced by a variable inherited from ShellApp inputPipe.

The | sign on the command line tells the shell to connect the standard output of primefind into the standard input of compress. Thus, instead of sending the output message to the console, primefind's output message flows into compress's input stream. waitForMessage() then retrieves that message, and each element of that message is subsequently written to a compressed file.

Compressing primefind's output might be useful when searching for very long lists for prime numbers: Instead of overwhelming the console screen, the piped output simply writes a compressed list to the file system. You can then use your favorite unzipping utility to decompress that file; the message element result will contain the comma-separated list of primes.



JavaT P2P Unleashed
JavaT P2P Unleashed
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 209

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net