1 2 17 18 19 package org.apache.poi.hssf.record; 20 21 import org.apache.poi.util.LittleEndianConsts; 22 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 35 class SSTRecordSizeCalculator 36 { 37 private UnicodeString unistr = null; 38 private int stringReminant = 0; 39 private int unipos = 0; 40 41 private boolean isRemainingString = false; 42 private int totalBytesWritten = 0; 43 private boolean finished = false; 44 private boolean firstRecord = true; 45 private int totalWritten = 0; 46 private int recordSize = 0; 47 private List recordLengths = new ArrayList (); 48 private int pos = 0; 49 private Map strings; 50 51 public SSTRecordSizeCalculator(Map strings) 52 { 53 this.strings = strings; 54 } 55 56 private boolean canFitStringInRecord(int recordLength) { 57 return (recordLength+SSTRecord.STRING_MINIMAL_OVERHEAD) < SSTRecord.MAX_RECORD_SIZE; 58 } 59 60 public int getRecordSize() { 61 int continueSize = SSTRecord.SST_RECORD_OVERHEAD; 64 int recordSize = 0; 65 for (int i=0; i < strings.size(); i++ ) 66 { 67 Integer intunipos = new Integer (i); 68 UnicodeString unistr = ( (UnicodeString) strings.get(intunipos)); 69 final int stringLength = unistr.getRecordSize(); 70 if ((continueSize + stringLength) <= SSTRecord.MAX_RECORD_SIZE) { 71 continueSize += stringLength; 73 74 if ((i < (strings.size()-1)) && !canFitStringInRecord(continueSize)) { 75 recordLengths.add(new Integer (continueSize)); 77 recordSize += continueSize; 78 continueSize = 4; 80 } 81 } else { 82 int stringRemainder = stringLength; 83 while (stringRemainder != 0) { 84 if ( (continueSize + stringRemainder) > SSTRecord.MAX_RECORD_SIZE) { 85 int bytesWritten = Math.min((SSTRecord.MAX_RECORD_SIZE - continueSize), stringRemainder); 88 89 bytesWritten = unistr.maxBrokenLength(bytesWritten); 94 continueSize += bytesWritten; 95 stringRemainder -= bytesWritten; 96 recordLengths.add(new Integer (continueSize)); 97 recordSize += continueSize; 98 continueSize = 4; 100 stringRemainder++; 103 } else { 104 continueSize += stringRemainder; 107 stringRemainder = 0; 108 if ((i < (strings.size()-1)) && !canFitStringInRecord(continueSize)) { 109 recordLengths.add(new Integer (continueSize)); 111 recordSize += continueSize; 112 continueSize = 4; 114 } 115 } 116 } 117 } 118 } 119 recordLengths.add(new Integer (continueSize)); 120 recordSize += continueSize; 121 return recordSize; 122 } 123 124 public List getRecordLengths() 125 { 126 return recordLengths; 127 } 128 } 129 | Popular Tags |