1 21 package com.db4o.header; 22 23 import java.io.*; 24 25 import com.db4o.*; 26 import com.db4o.inside.*; 27 28 29 32 public abstract class FileHeader { 33 34 private static final FileHeader[] AVAILABLE_FILE_HEADERS = new FileHeader[]{ 35 new FileHeader0(), 36 new FileHeader1() 37 }; 38 39 private static int readerLength(){ 40 int length = AVAILABLE_FILE_HEADERS[0].length(); 41 for (int i = 1; i < AVAILABLE_FILE_HEADERS.length; i++) { 42 length = YInt.max(length, AVAILABLE_FILE_HEADERS[i].length()); 43 } 44 return length; 45 } 46 47 public static FileHeader readFixedPart(YapFile file) throws IOException{ 48 YapReader reader = prepareFileHeaderReader(file); 49 FileHeader header = detectFileHeader(file, reader); 50 if(header == null){ 51 Exceptions4.throwRuntimeException(17); 52 } else { 53 header.readFixedPart(file, reader); 54 } 55 return header; 56 } 57 58 private static YapReader prepareFileHeaderReader(YapFile file) { 59 YapReader reader = new YapReader(readerLength()); 60 reader.read(file, 0, 0); 61 return reader; 62 } 63 64 private static FileHeader detectFileHeader(YapFile file, YapReader reader) { 65 for (int i = 0; i < AVAILABLE_FILE_HEADERS.length; i++) { 66 reader.seek(0); 67 FileHeader result = AVAILABLE_FILE_HEADERS[i].newOnSignatureMatch(file, reader); 68 if(result != null) { 69 return result; 70 } 71 } 72 return null; 73 } 74 75 public abstract void close() throws IOException; 76 77 public abstract void initNew(YapFile file) throws IOException; 78 79 public abstract Transaction interruptedTransaction(); 80 81 public abstract int length(); 82 83 protected abstract FileHeader newOnSignatureMatch(YapFile file, YapReader reader); 84 85 protected long timeToWrite(long time, boolean shuttingDown) { 86 return shuttingDown ? 0 : time; 87 } 88 89 protected abstract void readFixedPart(YapFile file, YapReader reader) throws IOException; 90 91 public abstract void readVariablePart(YapFile file); 92 93 protected boolean signatureMatches(YapReader reader, byte[] signature, byte version){ 94 for (int i = 0; i < signature.length; i++) { 95 if(reader.readByte() != signature[i]){ 96 return false; 97 } 98 } 99 return reader.readByte() == version; 100 } 101 102 public abstract void writeFixedPart( 104 YapFile file, boolean shuttingDown, YapWriter writer, int blockSize, int freespaceID); 105 106 public abstract void writeTransactionPointer(Transaction systemTransaction, int transactionAddress); 107 108 protected void writeTransactionPointer(Transaction systemTransaction, int transactionAddress, final int address, final int offset) { 109 YapWriter bytes = new YapWriter(systemTransaction, address, YapConst.INT_LENGTH * 2); 110 bytes.moveForward(offset); 111 bytes.writeInt(transactionAddress); 112 bytes.writeInt(transactionAddress); 113 if (Debug.xbytes && Deploy.overwrite) { 114 bytes.setID(YapConst.IGNORE_ID); 115 } 116 bytes.write(); 117 } 118 119 public abstract void writeVariablePart(YapFile file, int part); 120 121 protected void readClassCollectionAndFreeSpace(YapFile file, YapReader reader) { 122 SystemData systemData = file.systemData(); 123 systemData.classCollectionID(reader.readInt()); 124 systemData.freespaceID(reader.readInt()); 125 } 126 127 } 128 | Popular Tags |