1 19 package org.netbeans.mdr.persistence.btreeimpl.btreestorage; 20 21 import java.util.*; 22 import java.io.*; 23 24 import org.netbeans.mdr.persistence.*; 25 26 31 public class CachedPageInputStream extends java.io.InputStream { 32 33 34 private PageDescriptor first; 35 36 37 private PageDescriptor last; 38 39 40 private PageDescriptor currentPage; 41 42 43 private int currentOffset; 44 45 46 private PageDescriptor markedPage; 47 private int markedOffset; 48 49 54 public void addPage(CachedPage pg, int offst, int len) { 55 PageDescriptor newPage = new PageDescriptor(pg, offst, len); 56 if (first == null) { 57 first = last = newPage; 58 currentPage = first; 59 currentOffset = offst; 60 } 61 else { 62 last.next = newPage; 63 last = newPage; 64 } 65 } 66 67 70 public void close() throws IOException { 71 try { 72 unpinAll(); 73 } 74 catch (StorageException ex) { 75 StreamCorruptedException newEx = 78 new StreamCorruptedException(ex.getMessage()); 79 throw newEx; 80 } 81 finally { 82 first = last = currentPage = null; 83 currentOffset = 0; 84 super.close(); 85 } 86 } 87 88 89 92 public int read() { 93 while (true) { 94 if (currentPage == null) { 95 return -1; 96 } 97 else if (currentOffset < currentPage.end) { 98 int val = (int)currentPage.page.contents[currentOffset++]; 99 return (val & 0xFF); 100 } 101 else { 102 currentPage = currentPage.next; 103 if (currentPage != null) 104 currentOffset = currentPage.start; 105 } 106 } 107 } 108 109 113 public int read(byte b[]) { 114 return read(b, 0, b.length); 115 } 116 117 123 public int read(byte b[], int offset, int length) { 124 int left; 125 126 int toCopy = length; 127 int byteOffset = offset; 128 129 while (true) { 130 if (currentPage == null) { 131 return (toCopy == length) ? -1 : (length - toCopy); 132 } 133 else if ((left = currentPage.end - currentOffset) > 0) { 134 int size = Math.min(left, toCopy); 135 System.arraycopy(currentPage.page.contents, currentOffset, 136 b, byteOffset, size); 137 currentOffset += size; 138 byteOffset += size; 139 toCopy -= size; 140 if (toCopy == 0) { 141 return length; 142 } 143 } 144 else { 145 currentPage = currentPage.next; 146 if (currentPage != null) 147 currentOffset = currentPage.start; 148 } 149 } 150 } 151 152 155 public boolean markSupported() { 156 return true; 157 } 158 159 162 public void mark(int readlimit) { 163 markedPage = currentPage; 164 markedOffset = currentOffset; 165 } 166 167 170 public int available() { 171 if (currentPage == null) 172 return 0; 173 174 int total = currentPage.end - currentOffset; 175 176 for (PageDescriptor p = currentPage.next; p != null; p = p.next) { 177 total += p.end - p.start; 178 } 179 180 return total; 181 } 182 183 186 public void reset() { 187 if (markedPage != null) { 188 currentPage = markedPage; 189 currentOffset = markedOffset; 190 } 191 else { 192 currentPage = first; 193 if (first != null) 194 currentOffset = first.start; 195 else 196 currentOffset = 0; 197 } 198 } 199 200 202 protected void finalize() throws StorageException { 203 unpinAll(); 204 } 205 206 207 private void unpinAll() throws StorageException{ 208 for (PageDescriptor p = first; p != null; p = p.next) { 209 p.page.unpin(); 210 } 211 } 212 213 214 216 static class PageDescriptor { 217 218 219 CachedPage page; 220 221 222 int start; 223 224 225 int end; 226 227 228 PageDescriptor next; 229 230 235 public PageDescriptor(CachedPage pg, int offset, int length) { 236 page = pg; 237 start = offset; 238 end = offset + length; 239 } 240 } 241 } 242 243 | Popular Tags |