1 19 20 package jxl.read.biff; 21 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 import java.io.InterruptedIOException ; 25 import java.io.FileOutputStream ; 26 27 import common.Logger; 28 29 import jxl.WorkbookSettings; 30 import jxl.biff.IntegerHelper; 31 import jxl.biff.Type; 32 import jxl.biff.BaseCompoundFile; 33 34 37 public class File 38 { 39 42 private static Logger logger = Logger.getLogger(File.class); 43 44 47 private byte[] data; 48 51 private int filePos; 52 55 private int oldPos; 56 59 private int initialFileSize; 60 63 private int arrayGrowSize; 64 68 private CompoundFile compoundFile; 69 72 private WorkbookSettings workbookSettings; 73 74 82 public File(InputStream is, WorkbookSettings ws) 83 throws IOException , BiffException 84 { 85 workbookSettings = ws; 87 initialFileSize = workbookSettings.getInitialFileSize(); 88 arrayGrowSize = workbookSettings.getArrayGrowSize(); 89 90 byte[] d = new byte[initialFileSize]; 91 int bytesRead = is.read(d); 92 int pos = bytesRead; 93 94 if (Thread.currentThread().isInterrupted()) 97 { 98 throw new InterruptedIOException (); 99 } 100 101 while (bytesRead != -1) 102 { 103 if (pos >= d.length) 104 { 105 byte[] newArray = new byte[d.length + arrayGrowSize]; 107 System.arraycopy(d, 0, newArray, 0, d.length); 108 d = newArray; 109 } 110 bytesRead = is.read(d, pos, d.length - pos); 111 pos += bytesRead; 112 113 if (Thread.currentThread().isInterrupted()) 114 { 115 throw new InterruptedIOException (); 116 } 117 } 118 119 bytesRead = pos + 1; 120 121 if (bytesRead == 0) 123 { 124 throw new BiffException(BiffException.excelFileNotFound); 125 } 126 127 CompoundFile cf = new CompoundFile(d, ws); 128 try 129 { 130 data = cf.getStream("workbook"); 131 } 132 catch (BiffException e) 133 { 134 data = cf.getStream("book"); 136 } 137 138 if (!workbookSettings.getPropertySetsDisabled() && 139 (cf.getPropertySetNames().length > 140 BaseCompoundFile.STANDARD_PROPERTY_SETS.length)) 141 { 142 compoundFile = cf; 143 } 144 145 cf = null; 146 147 if (!workbookSettings.getGCDisabled()) 148 { 149 System.gc(); 150 } 151 152 155 159 } 160 161 169 public File(byte[] d) 170 { 171 data = d; 172 } 173 174 179 Record next() 180 { 181 Record r = new Record(data, filePos, this); 182 return r; 183 } 184 185 190 Record peek() 191 { 192 int tempPos = filePos; 193 Record r = new Record(data, filePos, this); 194 filePos = tempPos; 195 return r; 196 } 197 198 203 public void skip(int bytes) 204 { 205 filePos += bytes; 206 } 207 208 215 public byte[] read(int pos, int length) 216 { 217 byte[] ret = new byte[length]; 218 try 219 { 220 System.arraycopy(data, pos, ret, 0, length); 221 } 222 catch (ArrayIndexOutOfBoundsException e) 223 { 224 logger.error("Array index out of bounds at position " + pos + 225 " record length " + length); 226 throw e; 227 } 228 return ret; 229 } 230 231 236 public int getPos() 237 { 238 return filePos; 239 } 240 241 252 public void setPos(int p) 253 { 254 oldPos = filePos; 255 filePos = p; 256 } 257 258 264 public void restorePos() 265 { 266 filePos = oldPos; 267 } 268 269 272 private void moveToFirstBof() 273 { 274 boolean bofFound = false; 275 while (!bofFound) 276 { 277 int code = IntegerHelper.getInt(data[filePos], data[filePos + 1]); 278 if (code == Type.BOF.value) 279 { 280 bofFound = true; 281 } 282 else 283 { 284 skip(128); 285 } 286 } 287 } 288 289 294 public void close() 295 { 296 } 297 298 301 public void clear() 302 { 303 data = null; 304 } 305 306 311 public boolean hasNext() 312 { 313 return filePos < data.length - 4; 315 } 316 317 324 CompoundFile getCompoundFile() 325 { 326 return compoundFile; 327 } 328 } 329 | Popular Tags |