1 19 20 package jxl.biff; 21 22 import common.Assert; 23 import common.Logger; 24 25 import jxl.read.biff.Record; 26 27 31 public abstract class WritableRecordData extends RecordData 32 implements ByteData 33 { 34 37 private static Logger logger = Logger.getLogger(WritableRecordData.class); 38 41 protected static final int maxRecordLength = 8228; 42 43 48 protected WritableRecordData(Type t) 49 { 50 super(t); 51 } 52 53 58 protected WritableRecordData(Record t) 59 { 60 super(t); 61 } 62 63 70 public final byte[] getBytes() 71 { 72 byte[] data = getData(); 73 74 int dataLength = data.length; 75 76 if (data.length > maxRecordLength - 4) 81 { 82 dataLength = maxRecordLength - 4; 83 data = handleContinueRecords(data); 84 } 85 86 byte[] bytes = new byte[data.length + 4]; 87 88 System.arraycopy(data, 0, bytes, 4, data.length); 89 90 IntegerHelper.getTwoBytes(getCode(), bytes, 0); 91 IntegerHelper.getTwoBytes(dataLength, bytes, 2); 92 93 return bytes; 94 } 95 96 102 private byte[] handleContinueRecords(byte[] data) 103 { 104 int continuedData = data.length - (maxRecordLength - 4); 106 int numContinueRecords = continuedData / (maxRecordLength - 4) + 1; 107 108 byte[] newdata = new byte[data.length + numContinueRecords * 4]; 111 112 System.arraycopy(data, 0, newdata, 0, maxRecordLength - 4); 115 int oldarraypos = maxRecordLength - 4; 116 int newarraypos = maxRecordLength - 4; 117 118 for (int i = 0; i < numContinueRecords; i++) 120 { 121 int length = Math.min(data.length - oldarraypos, maxRecordLength - 4); 123 124 IntegerHelper.getTwoBytes(Type.CONTINUE.value, newdata, newarraypos); 126 IntegerHelper.getTwoBytes(length, newdata, newarraypos + 2); 127 128 System.arraycopy(data, oldarraypos, newdata, newarraypos + 4, length); 130 131 oldarraypos += length; 133 newarraypos += length + 4; 134 } 135 136 return newdata; 137 } 138 139 146 protected abstract byte[] getData(); 147 } 148 | Popular Tags |