KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > kaha > impl > data > 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.data;
19
20 import java.io.File JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.RandomAccessFile JavaDoc;
24 /**
25  * DataFile
26  *
27  * @version $Revision: 1.1.1.1 $
28  */

29 class DataFile{
30     
31     private File JavaDoc file;
32     private Integer JavaDoc number;
33     private int referenceCount;
34     private RandomAccessFile JavaDoc randomAcessFile;
35     private Object JavaDoc writerData;
36     long length=0;
37     private boolean dirty;
38
39     DataFile(File JavaDoc file,int number){
40         this.file=file;
41         this.number=new Integer JavaDoc(number);
42         length=file.exists()?file.length():0;
43     }
44
45     Integer JavaDoc getNumber(){
46         return number;
47     }
48
49     synchronized RandomAccessFile JavaDoc getRandomAccessFile() throws FileNotFoundException JavaDoc{
50         if(randomAcessFile==null){
51             randomAcessFile=new RandomAccessFile JavaDoc(file,"rw");
52         }
53         return randomAcessFile;
54     }
55
56     synchronized long getLength(){
57         return length;
58     }
59
60     synchronized void incrementLength(int size){
61         length+=size;
62     }
63
64     synchronized void purge() throws IOException JavaDoc{
65         if(randomAcessFile!=null){
66             randomAcessFile.close();
67             randomAcessFile=null;
68         }
69     }
70
71     synchronized boolean delete() throws IOException JavaDoc{
72         purge();
73         return file.delete();
74     }
75
76     synchronized void close() throws IOException JavaDoc{
77         if(randomAcessFile!=null){
78             randomAcessFile.close();
79         }
80     }
81
82     synchronized int increment(){
83         return ++referenceCount;
84     }
85
86     synchronized int decrement(){
87         return --referenceCount;
88     }
89
90     synchronized boolean isUnused(){
91         return referenceCount<=0;
92     }
93     
94     public String JavaDoc toString(){
95         String JavaDoc result = file.getName() + " number = " + number + " , length = " + length + " refCount = " + referenceCount;
96         return result;
97     }
98
99     /**
100      * @return Opaque data that a DataFileWriter may want to associate with the DataFile.
101      */

102     public synchronized Object JavaDoc getWriterData() {
103         return writerData;
104     }
105
106     /**
107      * @param writerData - Opaque data that a DataFileWriter may want to associate with the DataFile.
108      */

109     public synchronized void setWriterData(Object JavaDoc writerData) {
110         this.writerData = writerData;
111         dirty=true;
112     }
113     
114     public synchronized boolean isDirty() {
115         return dirty;
116     }
117     
118     public synchronized void setDirty(boolean value) {
119         this.dirty = value;
120     }
121
122 }
123
Popular Tags