import java.util.*;
import java.awt.image.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.Color;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
/**
*
* @author Viera K. Proulx
* @since 4 April 2010, 23 March 2011
*
* This class reads the Mars image files
* generated by the Viking Explorer and provided by NASA.
* The program that wishes to use that data can read the labels
* from the String labels
field and get the image byte data
* from the BufferedInputStream bytestream
*/
public class MarsFileReader{
/** the input reader for reading the text labels */
protected BufferedReader buffer;
/** the byte input reader for reading the pixel shade data */
protected BufferedInputStream bytestream;
/** the file reader - used first to read lines, then bytes */
FileInputStream filestream;
/** the file that holds the original images */
File marsfile;
/** one line of input at a time */
protected String line;
/** the labels read from the Mars file */
protected String labels;
/** a flag to see if the input file is available */
boolean closed = true;
public MarsFileReader() {
/**
* Build a file chooser and have the user choose a file
* Recommend only files with no extension
* Quit this operation if the user canceled the choice
*/
JFileChooser chooser = new JFileChooser(".");
/** set the file extension to recommend files with no extension */
chooser.setFileFilter(new ImageFileFilter());
this.closed = false;
/** see if file was selected - quit if user canceled */
if (chooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION){
this.closed = true;
return;
}
/**
* Now we can set up the buffer to read labels
* BufferedReader delivers the data as characters */
try{
this.marsfile = chooser.getSelectedFile();
this.buffer = new BufferedReader(new FileReader(this.marsfile));
}
catch(FileNotFoundException e){
System.out.println("File not found exception: " + e);
this.closed = true;
}
/** reading 72 lines long header of the file */
int count = 0;
while(!this.closed && count < 73){
/** read one line of input from the selected file */
getnext();
count ++;
}
System.out.println(this.labels);
/**
* Now we set up the FileInputStream for the user to process
* BufferedInputStream delivers the data as bytes that convert
* easily to non-negative integer values */
try{
this.filestream = new FileInputStream(this.marsfile);
this.bytestream = new BufferedInputStream(this.filestream);
//... now we jump ahead the number of bytes seen
// so we can read the colors:
// 306 * 12 (8 records of labels, 4 records of histo data)
// each record is 306 bytes long
// total of 3672 bytes to skip
this.bytestream.skip(3672);
}
catch(FileNotFoundException e){
System.out.println("File not found exception: " + e);
this.closed = true;
}
catch(IOException e){
System.out.println("Error when reading bytes: " + e);
this.closed = true;
}
}
/**
* Process next item, if available
*/
private void getnext() {
if (!this.closed)
try{
if (this.buffer.ready()){
this.line = new String(this.buffer.readLine());
if ((this.line == null)){
this.buffer.close();
this.closed = true;
}
else{
this.labels = this.labels + this.line + "\n";
//System.out.println(this.line);
}
}
else{
this.buffer.close();
this.closed = true;
}
}
catch(Exception e) {
System.out.println("Error in reading line exception: " + e);
this.closed = true;
}
}
public static void main(String[] argv){
System.out.println("ready to read");
MarsFileReader mfr = new MarsFileReader();
System.out.println("done reading");
}
}
/**
* Class used to assure that only file names with no suffix
* are seen in the FileChooser dialog.
*
* @author Viera K. Proulx
* @since 4 April 2010
*
*/
class ImageFileFilter extends FileFilter{
public boolean accept(File f){
// accept if file name has no suffix
return !f.getName().contains(".") ;
}
public String getDescription(){
return "Files with no suffix only";
}
}