1 29 package net.metanotion.io.block; 30 31 import java.io.IOException ; 32 33 import net.metanotion.io.RandomAccessInterface; 34 35 public class FreeListBlock { 36 public int page; 37 public int nextPage; 38 public int len; 39 public int[] branches = null; 40 public RandomAccessInterface file; 41 42 public FreeListBlock(RandomAccessInterface file, int startPage) throws IOException { 43 this.file = file; 44 this.page = startPage; 45 BlockFile.pageSeek(file, startPage); 46 nextPage = file.readInt(); 47 len = file.readInt(); 48 if(len > 0) { 49 branches = new int[len]; 50 for(int i=0;i<len;i++) { 51 branches[i] = file.readInt(); 52 } 53 } 54 } 55 56 public void writeBlock() throws IOException { 57 BlockFile.pageSeek(file, page); 58 file.writeInt(nextPage); 59 if(len > 0) { 60 file.writeInt(len); 61 for(int i=0;i<len;i++) { file.writeInt(branches[i]); } 62 } else { 63 file.writeInt(0); 64 } 65 } 66 67 public boolean isFull() { 68 int cells = (int) ((BlockFile.PAGESIZE - 8) / 4); 69 if(cells - len > 0) { return false; } 70 return true; 71 } 72 73 public void addPage(int page) { 74 int[] t = new int[len + 1]; 75 if(len > 0) { 76 for(int i=0;i<len;i++) { t[i] = branches[i]; } 77 } 78 t[len] = page; 79 len++; 80 branches = t; 81 } 82 83 public static void initPage(RandomAccessInterface file, int page) throws IOException { 84 BlockFile.pageSeek(file, page); 85 file.writeInt(0); 86 file.writeInt(0); 87 } 88 } 89 90 | Popular Tags |