KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > btreeimpl > btreestorage > FileHeader


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /** Files stored in the FileCache must start with this header; it contains
28 * information used by the logging system. The client of the cache
29 * should assume that the first 64 bytes of the file are reserved (64
30 * to allow for growth). All files stored in the cache are committed
31 * together, and should always have identical file headers.
32 */

33 public class FileHeader {
34
35     /** A random number generated when this set of files is created */
36     long fileId;
37
38     private static final int TIMESTAMP_OFFSET = 8;
39
40     /** A timestamp associated with the last comitted transaction
41     * for this set of files */

42     long timeStamp;
43
44     /** The size of the header */
45     public static final int HEADER_SIZE = 64;
46
47     /** create a new file header
48     */

49     FileHeader() {
50         fileId = new Random(new Object JavaDoc().hashCode()).nextLong();
51         updateTime();
52     }
53
54     /** Write the file header to a file.
55     * @param file the file to which the header is written
56     * @exception StorageException I/O error writing to the file
57     */

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     /** Write the file header to a byte array
71     * @param buffer the byte array to write the header to.
72     */

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     /** Read the header from an existing file
83     * @param file the file from which the header is read
84     * @exception StorageException I/O error reading the file
85     */

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     /** update the time stamp in the header
98     */

99     void updateTime() {
100         timeStamp = System.currentTimeMillis();
101     }
102
103     /** update the timestamp in the cache
104     * @param page the page to update
105     */

106     static void updateTime(CachedPage page, long newTimeStamp) {
107         Converter.writeLong(page.contents, TIMESTAMP_OFFSET, newTimeStamp);
108     }
109         
110     /** Create new files with a given header
111     * @param names the names of the files
112     * @param size if greater than 0, the size to make the files
113     * @param replace if true, replace existing files. if false, throw
114     * an exception if any of the files exists
115     * @exception StorageException I/O error creating the files
116     * or "replace" was false, and a file already exists
117     */

118     public void addFiles(
119                     String JavaDoc 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 JavaDoc[] {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     /** Create a set of files with a common header
150     * @param names the names of the files
151     * @param size if greater than 0, the size to make the files
152     * @param replace if true, replace existing files. if false, throw
153     * an exception if any of the files exists
154     * @return the header used to create the files
155     * @exception StorageException I/O error creating the files
156     * or "replace" was false, and a file already exists
157     */

158     public static FileHeader createFiles(
159                     String JavaDoc 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     /**
168     * test for equality with another FileHeader
169     * @param equals compare for equality with this
170     */

171     public boolean equals(Object JavaDoc 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