1 19 20 package jxl.write.biff; 21 22 import java.io.OutputStream ; 23 import java.io.IOException ; 24 25 import common.Logger; 26 import jxl.WorkbookSettings; 27 import jxl.biff.ByteData; 28 29 35 public final class File 36 { 37 40 private static Logger logger = Logger.getLogger(File.class); 41 42 45 private byte[] data; 46 49 private int pos; 50 53 private OutputStream outputStream; 54 57 private int initialFileSize; 58 61 private int arrayGrowSize; 62 65 private WorkbookSettings workbookSettings; 66 70 jxl.read.biff.CompoundFile readCompoundFile; 71 72 79 File(OutputStream os, WorkbookSettings ws, jxl.read.biff.CompoundFile rcf) 80 { 81 initialFileSize = ws.getInitialFileSize(); 82 arrayGrowSize = ws.getArrayGrowSize(); 83 data = new byte[initialFileSize]; 84 pos = 0; 85 outputStream = os; 86 workbookSettings = ws; 87 readCompoundFile = rcf; 88 } 89 90 99 void close(boolean cs) throws IOException , JxlWriteException 100 { 101 CompoundFile cf = new CompoundFile(data, pos, outputStream, 102 readCompoundFile); 103 cf.write(); 104 105 outputStream.flush(); 106 107 if (cs) 108 { 109 outputStream.close(); 110 } 111 112 data = null; 114 115 if (!workbookSettings.getGCDisabled()) 116 { 117 System.gc(); 118 } 119 } 120 121 127 public void write(ByteData record) throws IOException 128 { 129 try 130 { 131 byte[] bytes = record.getBytes(); 132 133 while (pos + bytes.length > data.length) 134 { 135 byte[] newdata = new byte[data.length + arrayGrowSize]; 137 System.arraycopy(data, 0, newdata, 0, pos); 138 data = newdata; 139 } 140 141 System.arraycopy(bytes, 0, data, pos, bytes.length); 142 pos += bytes.length; 143 } 144 catch (Throwable t) 145 { 146 logger.debug("record is " + record.getClass().getName()); 147 t.printStackTrace(); 148 throw new RuntimeException (t); 149 } 150 151 } 152 153 158 int getPos() 159 { 160 return pos; 161 } 162 163 170 void setData(byte[] newdata, int pos) 171 { 172 System.arraycopy(newdata, 0, data, pos, newdata.length); 173 } 174 175 182 public void setOutputFile(OutputStream os) 183 { 184 if (data != null) 185 { 186 logger.warn("Rewriting a workbook with non-empty data"); 187 } 188 189 outputStream = os; 190 data = new byte[initialFileSize]; 191 pos = 0; 192 } 193 } 194 | Popular Tags |