1 18 package org.apache.activemq.kaha.impl.async; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.RandomAccessFile ; 23 24 import org.apache.activemq.util.LinkedNode; 25 30 class DataFile extends LinkedNode implements Comparable { 31 32 private final File file; 33 private final Integer dataFileId; 34 private final int preferedSize; 35 36 int length=0; 37 private int referenceCount; 38 39 DataFile(File file, int number, int preferedSize){ 40 this.file=file; 41 this.preferedSize = preferedSize; 42 this.dataFileId=new Integer (number); 43 length=(int)(file.exists()?file.length():0); 44 } 45 46 public Integer 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 toString(){ 73 String result = file.getName() + " number = " + dataFileId + " , length = " + length + " refCount = " + referenceCount; 74 return result; 75 } 76 77 public RandomAccessFile openRandomAccessFile(boolean appender) throws IOException { 78 RandomAccessFile rc=new RandomAccessFile (file,"rw"); 79 if( appender ){ 82 if( length < preferedSize ) { 83 rc.setLength(preferedSize); 84 } 85 } 86 return rc; 87 } 88 89 public void closeRandomAccessFile(RandomAccessFile file) throws IOException { 90 if( length != file.length() ) { 92 file.setLength(getLength()); 93 } 94 file.close(); 95 } 96 97 public synchronized boolean delete() throws IOException { 98 return file.delete(); 99 } 100 101 public int compareTo(Object o) { 102 DataFile df = (DataFile) o; 103 return dataFileId - df.dataFileId; 104 } 105 106 107 108 } 109 | Popular Tags |