1 8 package org.ozoneDB.core.storage.gammaStore; 9 10 import java.io.File ; 11 import java.io.IOException ; 12 import java.io.RandomAccessFile ; 13 14 18 public class RandomAccessFileStorage extends AbstractStorage { 19 20 private RandomAccessFile raFile; 21 22 RandomAccessFileStorage(File file, String mode) throws IOException { 23 raFile = new RandomAccessFile (file, mode); 24 } 25 26 public void setLength(long length) throws IOException { 27 raFile.setLength(length); 28 } 29 30 public void seek(long pos) throws IOException { 31 raFile.seek(pos); 32 } 33 34 public void readFully(byte[] b, int offset, int length) throws IOException { 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 ("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 { 45 raFile.write(b, offset, length); 46 } 47 48 public long readLong() throws IOException { 49 return raFile.readLong(); 50 } 51 52 public void writeLong(long value) throws IOException { 53 raFile.writeLong(value); 54 } 55 56 public void close() throws IOException { 57 raFile.close(); 58 } 59 60 public long length() throws IOException { 61 return raFile.length(); 62 } 63 64 public int readInt() throws IOException { 65 return raFile.readInt(); 66 } 67 68 public void writeInt(int value) throws IOException { 69 raFile.writeInt(value); 70 } 71 72 } 73 | Popular Tags |