import java.io.*; import java.lang.*; class Bil { // Copyleft Harvey Greenberg, UW hgreen@u.washington.edu 1/27/97 // Version 1.1 handles integers, and writes all types. // Error handling is neither complete nor elegant // The constructor argument is the root of a file name. // The constructor looks for a file named .hdr // It then opens a data file named either .bil (for binary integers) // or (for binary floats). // if nb (number of bits) == 8, it returns a valid byte array barray // == 16, it returns a short array sarray // == 32, it returns a float array farray. // == 33, it returns a float array farray. // (yes, it's a strange flag.) // The other arrays are null pointers. // The write method takes a full file name as an argument, // and writes out the array. // This class was written with ARC/INFO in mind, but file formats are general. // The header file simply contains keyword-value pairs, // and the data file is a binary array. // Notes: When we talk about BIL (band interleaved by line) files, we are // alwasy assuming single-band files; this is all ARC/INFO writes. // ARC/INFO uses the bil extension for integer files, but floating // point files have no extension. There is no bit count in the // header for a float file. int nr,nc,nb; String datfilename; byte barray[][]; short sarray[][]; int iarray[][]; float farray[][]; Bil (String root) throws IOException { StreamTokenizer tokob; int toktyp,val,byteread; String keyw; String headerfilename = root + ".hdr"; System.out.println("Looking for file " + headerfilename); // deprecated DataInputStream s = new DataInputStream(new FileInputStream(headerfilename)); Reader s = new BufferedReader(new InputStreamReader(new FileInputStream(headerfilename))); tokob = new StreamTokenizer(s); while (true){ toktyp = tokob.nextToken(); if (toktyp == tokob.TT_WORD) { // System.out.println("Token on line " + tokob.lineno() + " is " + tokob.sval); if (tokob.sval.equalsIgnoreCase("NROWS")){ toktyp = tokob.nextToken(); if(toktyp != tokob.TT_NUMBER) System.out.println("Read error"); nr = (int) tokob.nval; } else if (tokob.sval.equalsIgnoreCase("NCOLS")){ toktyp = tokob.nextToken(); if(toktyp != tokob.TT_NUMBER) System.out.println("Read error"); // System.out.println("Value is " + tokob.nval); nc = (int) tokob.nval; } else if (tokob.sval.equalsIgnoreCase("NBITS")){ toktyp = tokob.nextToken(); if(toktyp != tokob.TT_NUMBER) System.out.println("Read error"); nb = (int) tokob.nval; } } else if (toktyp == tokob.TT_EOF) { break; } } s.close(); StringBuffer sb = new StringBuffer (root); if(nb > 0) // bil file sb.append(".bil"); String datfilename = sb.toString(); System.out.println("Looking for file " + datfilename + " " + nr + " × " + nc + " cells, " + nb + " bits long"); File f2 = new File (datfilename); DataInputStream s2 = new DataInputStream(new FileInputStream(f2)); if(nb == 8){ barray = new byte[nr][nc]; for(int i = 0;i < nr;i++){ byteread = s2.read(barray[i],0,nc); //System.out.println("i=" + i + ",byteread=" + byteread); } } else if(nb == 16){ sarray = new short[nr][nc]; for(int i = 0;i < nr;i++){ for(int j = 0;j < nc;j++){ this.sarray[i][j] = s2.readShort(); } } } else if (nb == 32){ iarray = new int[nr][nc]; for(int i = 0;i < nr;i++){ for(int j = 0;j < nc;j++){ this.iarray[i][j] = s2.readInt(); } } } else if (nb == 0){ //floating point nb = 33; farray = new float[nr][nc]; for(int i = 0;i < nr;i++){ for(int j = 0;j < nc;j++){ this.farray[i][j] = s2.readFloat(); } } } else{ System.out.println("Only 8,16, and 32 bit and float files now supported"); return; } s2.close(); } void write(String filename) throws IOException { System.out.println("Opening " + filename + " to write"); DataOutputStream s3 = new DataOutputStream(new FileOutputStream(filename)); if(nb == 8){ for(int i = 0;i < nr;i++) s3.write(barray[i],0,nc); } else if(nb == 16){ for(int i = 0;i < nr;i++){ for(int j = 0;j < nc;j++){ s3.writeShort(sarray[i][j]); } } } else if (nb == 32){ for(int i = 0;i < nr;i++){ for(int j = 0;j < nc;j++){ s3.writeInt(iarray[i][j]); } } } else if (nb == 33){ //floating point for(int i = 0;i < nr;i++){ for(int j = 0;j < nc;j++){ s3.writeFloat(farray[i][j]); } } } s3.close(); } //write }