/* * TestRandomAccessFile.java * * Created on 06 December 2001, 16:45 */ package uk.ac.leeds.ccg.andyt.tests.java; import java.io.*; /** * * @author andyt * @version */ public class TestRandomAccessFile { /** Creates new TestRandomAccessFile */ public TestRandomAccessFile() { } /** * @param args the command line arguments */ public static void main (String args[]) { long startTime=System.currentTimeMillis(); System.out.println("Start time in milliseconds"+startTime); String dataDirectoryName1,tempFileName1; File dataDirectory1,tempFile1; RandomAccessFile tempFile1raf; // Set up dataDirectory1 System.out.println("Set up dataDirectory1"); dataDirectoryName1=new String("/data/feathers_2/andyt/data"+System.currentTimeMillis()); dataDirectory1=new File(dataDirectoryName1); if (dataDirectory1.exists()) { boolean setUpDataDirectory1=false; int escape=0; while (!setUpDataDirectory1 || escape==10 ) { dataDirectoryName1="/data/feathers_2/andyt/data"+System.currentTimeMillis(); dataDirectory1=new File(dataDirectoryName1); dataDirectory1.mkdir(); escape++; if (dataDirectory1.exists()) { setUpDataDirectory1=true; } } } else { dataDirectory1.mkdir(); } if (!dataDirectory1.exists()) { System.out.println("Error setting up dataDirectory1"); System.exit(0); } // Set to delete //dataDirectory1.deleteOnExit(); // Set up tempFile1 System.out.println("Set up tempFile1"); tempFileName1=new String("file"+System.currentTimeMillis()); System.out.println("Creating abstract file "+dataDirectory1.getAbsolutePath()+tempFileName1); tempFile1=new File(dataDirectory1,tempFileName1); if (tempFile1.exists()) { boolean setUpTempFile1=false; int escape=0; while (!setUpTempFile1 || escape==10 ) { tempFileName1="file"+System.currentTimeMillis(); tempFile1=new File(dataDirectory1,tempFileName1); try { tempFile1.createNewFile(); } catch (java.io.IOException ioe1) { System.out.println("Exception creating file: "+ioe1); } escape++; if (tempFile1.exists()) { setUpTempFile1=true; } } } else { try { tempFile1.createNewFile(); } catch (java.io.IOException ioe1) { System.out.println("Exception creating file: "+ioe1); } } if (!tempFile1.exists()) { System.out.println("Error setting up tempFile1"); System.exit(0); } // Set to delete //tempFile1.deleteOnExit(); /* * The following was a test to find out what is returned from various tyes of read from a * RandomAccessFile. In summary: * a) seek(pos); writeByte(); seek(pos+1) where pos+1>length; readByte(); would bomb out * with an IOException * b) seek(pos); writeByte(); seek(pos-1) where pos-1>0; readByte(); returns zero or if a * byte was already written there it would return the value of that byte * c) seek(pos) where pos<0 would bomb out with an IOException * So by catching the IOExceptions and returning zero a RandomAccessFile can be used like * a hashtable for a sparse matrix where the noDataValue is zero and fixed. */ // Set up randomAccessFile1 System.out.println("Set up randomAccessFile1"); try { tempFile1raf=new RandomAccessFile(tempFile1,"rw"); // write a few doubles try { tempFile1raf.seek(100*8); for (int i=0;i<10;i++) { tempFile1raf.writeDouble(1.0d); } } catch (java.io.IOException ioe1) { System.out.println(""+ioe1+" writing"); System.exit(0); } // Try to read from in bounds uninitialised try { System.out.println("read in bounds uninitialised"); tempFile1raf.seek(0); System.out.println(""+tempFile1raf.readDouble()); } catch (java.io.IOException ioe1) { System.out.println(""+ioe1+" read in bounds uninitialised"); System.out.println(""+-1.0d); } // Try to read from in bounds initialised try { System.out.println("read in bounds initialised"); tempFile1raf.seek(88); System.out.println(""+tempFile1raf.readDouble()); } catch (java.io.IOException ioe1) { System.out.println(""+ioe1+" read in bounds initialised"); System.out.println(""+-1.0d); } // Try to read before bounds try { System.out.println("read before bounds"); tempFile1raf.seek(-8); System.out.println(""+tempFile1raf.readDouble()); } catch (java.io.IOException ioe1) { /*if (ioe1.toString().equalsIgnoreCase("java.io.IOException: Negative seek offset")) { System.out.println("Bish bash"); }*/ if (ioe1 instanceof java.io.IOException) { System.out.println("Bish bash"); } System.out.println(""+ioe1+" read before bounds"); System.out.println(""+-1.0d); } // Try to read after bounds try { System.out.println("read after bounds"); tempFile1raf.seek(10008); System.out.println(""+tempFile1raf.readDouble()); } catch (java.io.IOException ioe1) { System.out.println(""+ioe1+" read after bounds"); System.out.println(""+-1.0d); } } catch (java.io.FileNotFoundException fnfe1) { System.out.println(""+fnfe1+" setting up "+tempFile1); System.exit(0); } } }