API Example - Showing Header Information for a UNC Image

This example shows you how to create a simple programme to write UNC image header information to standard output. To try out the programme, save the following text to a file called UNCHeader.java. Make sure that the jar file xinapse6.jar is in your classpath, then compile it with the javac command:

javac UNCHeader.java

This will create a file called UNCHeader.class. To run the programme, type (for example):

java UNCHeader image

where image is the name of an the UNC from which to find the header information. The programme will also read the image from standard input, so you could type:

cat image | java UNCHeader

It will also read the image from a URL, so you could type:

java UNCHeader URL

import java.net.*;

import com.xinapse.multisliceimage.UNC.*;
import com.xinapse.multisliceimage.MultiSliceImageException;
import com.xinapse.platform.ExitStatus;

public class UNCHeader {

  /**
     Java application to write the image header information for one or more UNCImages
     to standard output. The UNCImage for which header information will be printed may
     come from a standard input, from a URL, or from a file.
   */
  public static void main(String argv[]) {

    UNCImage im = null;
    int nfiles = argv.length;

    try {
      if (nfiles == 0) {
	// Get input from standard input, skipping over pixel data
	im = new UNCImage(java.lang.System.in, true);
	if (im != null) {
	  System.out.print(im.toString());
	}
      }
      else {
	
	for (int i = 0; i < nfiles; i++) {
	  
	  // See if the argument is a url
	  try {
	    URL url = new URL(argv[i]);
	    // Load but skip over the pixel data
	    im = new UNCImage(url, true);
	  }
	  catch (MalformedURLException e) {
	    // Not a URL - try a regular file
	    try { 
	      im = new UNCImage(argv[i], "r");
	    }
	    catch (java.io.FileNotFoundException fnfE) {
	      System.err.println("Error processing file " + argv[i] + ": file not found");
	    }
	    catch (java.io.IOException ioE) {
	      System.err.println("Error processing file " + argv[i] + ": " + ioE.getMessage());
	    }
	  }
	  catch (java.io.IOException e) {
	    System.err.println("Error processing URL " + argv[i]);
	    System.err.println(e.getMessage());
	  }

	  if (im != null) {
	    System.out.print(im.toString());
	  }
	}

	System.exit(ExitStatus.NORMAL);
      }
    }
    catch (MultiSliceImageException e) {
      System.err.println("Error reading UNC Image object: " + e.getMessage());
      System.exit(ExitStatus.NON_SPECIFIC_ERROR);
    }    
  }
}


    

Jim Home