KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > core > storage > gammaStore > FileStreamStorage


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Core License version 1 published by ozone-db.org.
3
//
4
// Copyright (C) 2003-@year@, Leo Mekenkamp. All rights reserved.
5
//
6
// $Id: FileStreamStorage.java,v 1.1 2004/01/02 09:24:38 leomekenkamp Exp $
7

8 package org.ozoneDB.core.storage.gammaStore;
9
10 import java.io.DataInputStream JavaDoc;
11 import java.io.DataOutputStream JavaDoc;
12 import java.io.File JavaDoc;
13 import java.io.FileInputStream JavaDoc;
14 import java.io.FileOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.math.BigInteger JavaDoc;
17
18 /**
19  * @author <a HREF="mailto:leoATmekenkampD0Tcom">Leo Mekenkamp (mind the anti sp@m)</a>
20  * @version $Id $
21  */

22 public class FileStreamStorage extends AbstractStorage {
23     
24     private DataInputStream JavaDoc in = null;
25     private DataOutputStream JavaDoc out = null;
26     private File JavaDoc file;
27     
28     FileStreamStorage(File JavaDoc file) throws IOException JavaDoc {
29         this.file = file;
30     }
31     
32     private void checkForWrite() throws IOException JavaDoc {
33         if (out == null) {
34             out = new DataOutputStream JavaDoc(new FileOutputStream JavaDoc(file));
35         }
36     }
37
38     private void checkForRead() throws IOException JavaDoc {
39         if (in == null) {
40             in = new DataInputStream JavaDoc(new FileInputStream JavaDoc(file));
41         }
42     }
43
44     public void setLength(long length) throws IOException JavaDoc {
45         throw new UnsupportedOperationException JavaDoc("not supported in FileStreamStorage");
46     }
47     
48     public void seek(long pos) throws IOException JavaDoc {
49         throw new UnsupportedOperationException JavaDoc("not supported in FileStreamStorage");
50     }
51     
52     public void readFully(byte[] b, int offset, int length) throws IOException JavaDoc {
53         checkForRead();
54         in.readFully(b, offset, length);
55     }
56     
57     public void write(byte[] b, int offset, int length) throws IOException JavaDoc {
58         checkForWrite();
59         out.write(b, offset, length);
60         out.flush();
61     }
62     
63     public long readLong() throws IOException JavaDoc {
64         checkForRead();
65         return in.readLong();
66     }
67     
68     public void writeLong(long value) throws IOException JavaDoc {
69         checkForWrite();
70         out.writeLong(value);
71         out.flush();
72     }
73     
74     public void close() throws IOException JavaDoc {
75         try {
76             if (in != null) {
77                 in.close();
78             }
79         } finally {
80             if (out != null) {
81                 out.close();
82             }
83         }
84     }
85     
86     public long length() throws IOException JavaDoc {
87         return file.length();
88     }
89     
90     public int readInt() throws IOException JavaDoc {
91         checkForRead();
92         return in.readInt();
93     }
94     
95     public void writeInt(int value) throws IOException JavaDoc {
96         checkForWrite();
97         out.writeInt(value);
98         out.flush();
99     }
100     
101 }
102
Popular Tags