1 18 package org.apache.activemq.kaha.impl.data; 19 20 import java.io.File ; 21 import java.io.FileNotFoundException ; 22 import java.io.IOException ; 23 import java.io.RandomAccessFile ; 24 29 class DataFile{ 30 31 private File file; 32 private Integer number; 33 private int referenceCount; 34 private RandomAccessFile randomAcessFile; 35 private Object writerData; 36 long length=0; 37 private boolean dirty; 38 39 DataFile(File file,int number){ 40 this.file=file; 41 this.number=new Integer (number); 42 length=file.exists()?file.length():0; 43 } 44 45 Integer getNumber(){ 46 return number; 47 } 48 49 synchronized RandomAccessFile getRandomAccessFile() throws FileNotFoundException { 50 if(randomAcessFile==null){ 51 randomAcessFile=new RandomAccessFile (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 { 65 if(randomAcessFile!=null){ 66 randomAcessFile.close(); 67 randomAcessFile=null; 68 } 69 } 70 71 synchronized boolean delete() throws IOException { 72 purge(); 73 return file.delete(); 74 } 75 76 synchronized void close() throws IOException { 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 toString(){ 95 String result = file.getName() + " number = " + number + " , length = " + length + " refCount = " + referenceCount; 96 return result; 97 } 98 99 102 public synchronized Object getWriterData() { 103 return writerData; 104 } 105 106 109 public synchronized void setWriterData(Object 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 |