1 5 package org.prevayler.foundation; 6 7 import java.io.*; 8 9 public class SimpleInputStream { 10 11 private final File _file; 12 private final ObjectInputStream _delegate; 13 private boolean _EOF = false; 14 15 16 public SimpleInputStream(File file) throws IOException { 17 System.out.println("Reading " + file + "..."); 18 _file = file; 19 _delegate = new ObjectInputStream(new FileInputStream(file)); 20 } 21 22 23 public Object readObject() throws IOException, ClassNotFoundException { 24 if (_EOF) throw new EOFException(); 25 26 try { 27 return _delegate.readObject(); 28 } catch (EOFException eofx) { 29 } catch (ObjectStreamException scx) { 31 message(scx); 32 } catch (UTFDataFormatException utfx) { 33 message(utfx); 34 } catch (RuntimeException rx) { message(rx); 36 } 37 38 _delegate.close(); 39 _EOF = true; 40 throw new EOFException(); 41 } 42 43 44 public void close() throws IOException { 45 _delegate.close(); 46 _EOF = true; 47 } 48 49 private void message(Exception exception) { 50 System.out.println( 51 "\n" + exception + " (File: " + _file + ")" + 52 "\n The above is a stream corruption that can be caused by:" + 53 "\n - A system crash while writing to the file (that is OK)." + 54 "\n - A corruption in the file system (that is NOT OK)." + 55 "\n - Tampering with the file (that is NOT OK)." 56 ); 57 } 58 } 59 | Popular Tags |