1 21 package com.db4o; 22 23 import java.io.*; 24 25 import com.db4o.config.*; 26 import com.db4o.ext.*; 27 import com.db4o.inside.*; 28 29 32 public class YapMemoryFile extends YapFile { 33 34 private boolean i_closed = false; 35 final MemoryFile i_memoryFile; 36 private int i_length = 0; 37 38 protected YapMemoryFile(Configuration config,YapStream a_parent, MemoryFile memoryFile) { 39 super(config,a_parent); 40 this.i_memoryFile = memoryFile; 41 try { 42 open(); 43 } catch (Exception e) { 44 Exceptions4.throwRuntimeException(22, e); 45 } 46 initialize3(); 47 } 48 49 YapMemoryFile(Configuration config,MemoryFile memoryFile) { 50 this(config, null, memoryFile); 51 } 52 53 public void backup(String path)throws IOException{ 54 Exceptions4.throwRuntimeException(60); 55 } 56 57 public void blockSize(int size){ 58 } 60 61 void checkDemoHop() { 62 } 64 65 protected boolean close2() { 66 if (Deploy.debug) { 67 write(true); 68 } else { 69 try { 70 write(true); 71 } catch (Throwable t) { 72 fatalException(t); 73 } 74 } 75 super.close2(); 76 if (i_closed == false) { 77 byte[] temp = new byte[i_length]; 78 System.arraycopy(i_memoryFile.getBytes(), 0, temp, 0, i_length); 79 i_memoryFile.setBytes(temp); 80 } 81 i_closed = true; 82 return true; 83 } 84 85 public void copy(int oldAddress, int oldAddressOffset, int newAddress, int newAddressOffset, int length) { 86 byte[] bytes = memoryFileBytes(newAddress + newAddressOffset + length); 87 System.arraycopy(bytes, oldAddress + oldAddressOffset, bytes, newAddress + newAddressOffset, length); 88 } 89 90 void emergencyClose() { 91 super.emergencyClose(); 92 i_closed = true; 93 } 94 95 public long fileLength() { 96 return i_length; 97 } 98 99 String fileName() { 100 return "Memory File"; 101 } 102 103 protected boolean hasShutDownHook() { 104 return false; 105 } 106 107 public final boolean needsLockFileThread() { 108 return false; 109 } 110 111 private void open() throws IOException { 112 byte[] bytes = i_memoryFile.getBytes(); 113 if (bytes == null || bytes.length == 0) { 114 i_memoryFile.setBytes(new byte[i_memoryFile.getInitialSize()]); 115 configureNewFile(); 116 write(false); 117 writeHeader(false); 118 } else { 119 i_length = bytes.length; 120 readThis(); 121 } 122 } 123 124 public void readBytes(byte[] a_bytes, int a_address, int a_length) { 125 try { 126 System.arraycopy(i_memoryFile.getBytes(), a_address, a_bytes, 0, a_length); 127 } catch (Exception e) { 128 Exceptions4.throwRuntimeException(13, e); 129 } 130 } 131 132 public void readBytes(byte[] bytes, int address, int addressOffset, int length){ 133 readBytes(bytes, address + addressOffset, length); 134 } 135 136 public void syncFiles() { 137 } 138 139 public boolean writeAccessTime(int address, int offset, long time) { 140 return true; 141 } 142 143 public void writeBytes(YapReader a_bytes, int address, int addressOffset) { 144 int fullAddress = address + addressOffset; 145 int length = a_bytes.getLength(); 146 System.arraycopy(a_bytes._buffer, 0, memoryFileBytes(fullAddress + length), fullAddress , length); 147 } 148 149 private byte[] memoryFileBytes(int a_lastByte) { 150 byte[] bytes = i_memoryFile.getBytes(); 151 if (a_lastByte > i_length) { 152 if (a_lastByte > bytes.length) { 153 int increase = a_lastByte - bytes.length; 154 if (increase < i_memoryFile.getIncrementSizeBy()) { 155 increase = i_memoryFile.getIncrementSizeBy(); 156 } 157 byte[] temp = new byte[bytes.length + increase]; 158 System.arraycopy(bytes, 0, temp, 0, bytes.length); 159 i_memoryFile.setBytes(temp); 160 bytes = temp; 161 } 162 i_length = a_lastByte; 163 } 164 return bytes; 165 } 166 167 public void debugWriteXBytes(int a_address, int a_length) { 168 } 170 171 } 172
| Popular Tags
|