1 8 package org.ozoneDB.core.storage.gammaStore; 9 10 import java.io.DataInputStream ; 11 import java.io.DataOutputStream ; 12 import java.io.File ; 13 import java.io.FileInputStream ; 14 import java.io.FileOutputStream ; 15 import java.io.IOException ; 16 import java.math.BigInteger ; 17 18 22 public class FileStreamStorage extends AbstractStorage { 23 24 private DataInputStream in = null; 25 private DataOutputStream out = null; 26 private File file; 27 28 FileStreamStorage(File file) throws IOException { 29 this.file = file; 30 } 31 32 private void checkForWrite() throws IOException { 33 if (out == null) { 34 out = new DataOutputStream (new FileOutputStream (file)); 35 } 36 } 37 38 private void checkForRead() throws IOException { 39 if (in == null) { 40 in = new DataInputStream (new FileInputStream (file)); 41 } 42 } 43 44 public void setLength(long length) throws IOException { 45 throw new UnsupportedOperationException ("not supported in FileStreamStorage"); 46 } 47 48 public void seek(long pos) throws IOException { 49 throw new UnsupportedOperationException ("not supported in FileStreamStorage"); 50 } 51 52 public void readFully(byte[] b, int offset, int length) throws IOException { 53 checkForRead(); 54 in.readFully(b, offset, length); 55 } 56 57 public void write(byte[] b, int offset, int length) throws IOException { 58 checkForWrite(); 59 out.write(b, offset, length); 60 out.flush(); 61 } 62 63 public long readLong() throws IOException { 64 checkForRead(); 65 return in.readLong(); 66 } 67 68 public void writeLong(long value) throws IOException { 69 checkForWrite(); 70 out.writeLong(value); 71 out.flush(); 72 } 73 74 public void close() throws IOException { 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 { 87 return file.length(); 88 } 89 90 public int readInt() throws IOException { 91 checkForRead(); 92 return in.readInt(); 93 } 94 95 public void writeInt(int value) throws IOException { 96 checkForWrite(); 97 out.writeInt(value); 98 out.flush(); 99 } 100 101 } 102 | Popular Tags |