KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > kaha > impl > async > DataFile


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.kaha.impl.async;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.RandomAccessFile JavaDoc;
23
24 import org.apache.activemq.util.LinkedNode;
25 /**
26  * DataFile
27  *
28  * @version $Revision: 1.1.1.1 $
29  */

30 class DataFile extends LinkedNode implements Comparable JavaDoc {
31     
32     private final File JavaDoc file;
33     private final Integer JavaDoc dataFileId;
34     private final int preferedSize;
35
36     int length=0;
37     private int referenceCount;
38         
39     DataFile(File JavaDoc file, int number, int preferedSize){
40         this.file=file;
41         this.preferedSize = preferedSize;
42         this.dataFileId=new Integer JavaDoc(number);
43         length=(int)(file.exists()?file.length():0);
44     }
45
46     public Integer JavaDoc getDataFileId(){
47         return dataFileId;
48     }
49
50     public synchronized int getLength(){
51         return length;
52     }
53     public void setLength(int length) {
54         this.length=length;
55     }
56     public synchronized void incrementLength(int size){
57         length+=size;
58     }
59
60     public synchronized int increment(){
61         return ++referenceCount;
62     }
63
64     public synchronized int decrement(){
65         return --referenceCount;
66     }
67
68     public synchronized boolean isUnused(){
69         return referenceCount<=0;
70     }
71     
72     public synchronized String JavaDoc toString(){
73         String JavaDoc result = file.getName() + " number = " + dataFileId + " , length = " + length + " refCount = " + referenceCount;
74         return result;
75     }
76         
77     public RandomAccessFile JavaDoc openRandomAccessFile(boolean appender) throws IOException JavaDoc {
78         RandomAccessFile JavaDoc rc=new RandomAccessFile JavaDoc(file,"rw");
79         // When we start to write files size them up so that the OS has a chance
80
// to allocate the file contigously.
81
if( appender ){
82             if( length < preferedSize ) {
83                 rc.setLength(preferedSize);
84             }
85         }
86         return rc;
87     }
88
89     public void closeRandomAccessFile(RandomAccessFile JavaDoc file) throws IOException JavaDoc {
90         // On close set the file size to the real size.
91
if( length != file.length() ) {
92             file.setLength(getLength());
93         }
94         file.close();
95     }
96     
97     public synchronized boolean delete() throws IOException JavaDoc{
98         return file.delete();
99     }
100
101     public int compareTo(Object JavaDoc o) {
102         DataFile df = (DataFile) o;
103         return dataFileId - df.dataFileId;
104     }
105
106
107     
108 }
109
Popular Tags