KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > prevayler > foundation > SimpleInputStream


1 //Prevayler(TM) - The Free-Software Prevalence Layer.
2
//Copyright (C) 2001-2003 Klaus Wuestefeld
3
//This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
4

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 JavaDoc readObject() throws IOException, ClassNotFoundException JavaDoc {
24         if (_EOF) throw new EOFException();
25
26         try {
27             return _delegate.readObject();
28         } catch (EOFException eofx) {
29             // Do nothing.
30
} catch (ObjectStreamException scx) {
31             message(scx);
32         } catch (UTFDataFormatException utfx) {
33             message(utfx);
34         } catch (RuntimeException JavaDoc rx) { //Some stream corruptions cause runtime exceptions in JDK1.3.1!
35
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 JavaDoc 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