1 30 31 32 package org.hsqldb.persist; 33 34 import java.io.IOException ; 35 36 import org.hsqldb.Database; 37 38 51 public class ScaledRAFileHybrid implements ScaledRAInterface { 52 53 final Database database; 54 final String fileName; 55 final boolean isReadOnly; 56 final boolean wasNio; 57 long maxLength; 58 ScaledRAInterface store; 59 60 public ScaledRAFileHybrid(Database database, String name, 61 boolean readOnly) throws IOException { 62 63 this.database = database; 64 this.fileName = name; 65 this.isReadOnly = readOnly; 66 67 newStore(0); 68 69 this.wasNio = store.wasNio(); 70 } 71 72 public long length() throws IOException { 73 return store.length(); 74 } 75 76 public void seek(long position) throws IOException { 77 checkSeek(position); 78 store.seek(position); 79 } 80 81 public long getFilePointer() throws IOException { 82 return store.getFilePointer(); 83 } 84 85 public int read() throws IOException { 86 87 checkLength(1); 88 89 return store.read(); 90 } 91 92 public void read(byte[] b, int offset, int length) throws IOException { 93 checkLength(length); 94 store.read(b, offset, length); 95 } 96 97 public void write(byte[] b, int offset, int length) throws IOException { 98 checkLength(length); 99 store.write(b, offset, length); 100 } 101 102 public int readInt() throws IOException { 103 104 checkLength(4); 105 106 return store.readInt(); 107 } 108 109 public void writeInt(int i) throws IOException { 110 checkLength(4); 111 store.writeInt(i); 112 } 113 114 public long readLong() throws IOException { 115 116 checkLength(8); 117 118 return store.readLong(); 119 } 120 121 public void writeLong(long i) throws IOException { 122 checkLength(8); 123 store.writeLong(i); 124 } 125 126 public void close() throws IOException { 127 store.close(); 128 } 129 130 public boolean isReadOnly() { 131 return store.isReadOnly(); 132 } 133 134 public boolean wasNio() { 135 return wasNio; 136 } 137 138 public boolean canAccess(int length) { 139 return true; 140 } 141 142 public boolean canSeek(long position) { 143 return true; 144 } 145 146 public Database getDatabase() { 147 return null; 148 } 149 150 private void checkLength(int length) throws IOException { 151 152 if (store.canAccess(length)) { 153 return; 154 } 155 156 newStore(store.getFilePointer() + length); 157 } 158 159 private void checkSeek(long position) throws IOException { 160 161 if (store.canSeek(position)) { 162 return; 163 } 164 165 newStore(position); 166 } 167 168 void newStore(long requiredPosition) throws IOException { 169 170 long currentPosition = 0; 171 172 if (store != null) { 173 currentPosition = store.getFilePointer(); 174 175 store.close(); 176 } 177 178 if (requiredPosition <= ScaledRAFile.MAX_NIO_LENGTH) { 179 try { 180 store = new ScaledRAFileNIO(database, fileName, isReadOnly, 181 (int) requiredPosition); 182 183 store.seek(currentPosition); 184 185 return; 186 } catch (Throwable e) {} 187 } 188 189 store = new ScaledRAFile(database, fileName, isReadOnly); 190 191 store.seek(currentPosition); 192 } 193 } 194 | Popular Tags |