1 19 package org.netbeans.mdr.persistence.btreeimpl.btreestorage; 20 21 import java.io.*; 22 import java.text.*; 23 import java.util.*; 24 25 import org.netbeans.mdr.persistence.*; 26 27 33 public class FileHeader { 34 35 36 long fileId; 37 38 private static final int TIMESTAMP_OFFSET = 8; 39 40 42 long timeStamp; 43 44 45 public static final int HEADER_SIZE = 64; 46 47 49 FileHeader() { 50 fileId = new Random(new Object ().hashCode()).nextLong(); 51 updateTime(); 52 } 53 54 58 void write(RandomAccessFile file) throws StorageException { 59 try { 60 byte buffer[] = new byte[HEADER_SIZE]; 61 write(buffer); 62 file.seek(0); 63 file.write(buffer); 64 } 65 catch (IOException ex) { 66 throw new StorageIOException(ex); 67 } 68 } 69 70 73 void write(byte buffer[]) { 74 int offset = 0; 75 offset = Converter.writeLong(buffer, offset, fileId); 76 offset = Converter.writeLong(buffer, offset, timeStamp); 77 for (int i = offset; i < HEADER_SIZE; i++) { 78 buffer[i] = 0; 79 } 80 } 81 82 86 FileHeader(RandomAccessFile file) throws StorageException { 87 try { 88 file.seek(0); 89 fileId = file.readLong(); 90 timeStamp = file.readLong(); 91 } 92 catch (IOException ex) { 93 throw new StorageIOException(ex); 94 } 95 } 96 97 99 void updateTime() { 100 timeStamp = System.currentTimeMillis(); 101 } 102 103 106 static void updateTime(CachedPage page, long newTimeStamp) { 107 Converter.writeLong(page.contents, TIMESTAMP_OFFSET, newTimeStamp); 108 } 109 110 118 public void addFiles( 119 String names[], int size, boolean replace) 120 throws StorageException { 121 122 if (!replace) { 123 for (int i = 0; i < names.length; i++) { 124 File file = new File(names[i]); 125 if (file.exists()) { 126 throw new StorageBadRequestException( 127 MessageFormat.format("File {0} already exists", 128 new Object [] {names[i]} )); 129 } 130 } 131 } 132 133 try { 134 for (int i = 0; i < names.length; i++) { 135 RandomAccessFile file = new RandomAccessFile(names[i], "rw"); 136 write(file); 137 if (size > 0) 138 file.setLength(size); 139 else 140 file.setLength(file.getFilePointer()); 141 file.close(); 142 } 143 } 144 catch (IOException ex) { 145 throw new StorageIOException(ex); 146 } 147 } 148 149 158 public static FileHeader createFiles( 159 String names[], int size, boolean replace) 160 throws StorageException { 161 162 FileHeader header = new FileHeader(); 163 header.addFiles(names, size, replace); 164 return header; 165 } 166 167 171 public boolean equals(Object o) { 172 if (!(o instanceof FileHeader)) 173 return false; 174 175 FileHeader header = (FileHeader)o; 176 return (header.fileId == this.fileId) && 177 (header.timeStamp == this.timeStamp); 178 } 179 180 } 181 182 | Popular Tags |