KickJava   Java API By Example, From Geeks To Geeks.

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


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: RandomAccessFileStorage.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.File JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.RandomAccessFile JavaDoc;
13
14 /**
15  * @author <a HREF="mailto:leoATmekenkampD0Tcom">Leo Mekenkamp (mind the anti sp@m)</a>
16  * @version $Id $
17  */

18 public class RandomAccessFileStorage extends AbstractStorage {
19     
20     private RandomAccessFile JavaDoc raFile;
21     
22     RandomAccessFileStorage(File JavaDoc file, String JavaDoc mode) throws IOException JavaDoc {
23         raFile = new RandomAccessFile JavaDoc(file, mode);
24     }
25
26     public void setLength(long length) throws IOException JavaDoc {
27         raFile.setLength(length);
28     }
29     
30     public void seek(long pos) throws IOException JavaDoc {
31         raFile.seek(pos);
32     }
33     
34     public void readFully(byte[] b, int offset, int length) throws IOException JavaDoc {
35         for (int totalRead = 0; totalRead == length; ) {
36             int read = raFile.read(b, offset + totalRead, length - totalRead);
37             if (read == -1) {
38                 throw new IOException JavaDoc("there are no " + length + " but " + totalRead + " bytes left from current position in file " + raFile);
39             }
40             totalRead += read;
41         }
42     }
43     
44     public void write(byte[] b, int offset, int length) throws IOException JavaDoc {
45         raFile.write(b, offset, length);
46     }
47     
48     public long readLong() throws IOException JavaDoc {
49         return raFile.readLong();
50     }
51     
52     public void writeLong(long value) throws IOException JavaDoc {
53         raFile.writeLong(value);
54     }
55     
56     public void close() throws IOException JavaDoc {
57         raFile.close();
58     }
59     
60     public long length() throws IOException JavaDoc {
61         return raFile.length();
62     }
63     
64     public int readInt() throws IOException JavaDoc {
65         return raFile.readInt();
66     }
67     
68     public void writeInt(int value) throws IOException JavaDoc {
69         raFile.writeInt(value);
70     }
71     
72 }
73
Popular Tags