1 2 17 18 19 package org.apache.poi.hssf.record; 20 21 import org.apache.poi.util.LittleEndianConsts; 22 import org.apache.poi.util.LittleEndian; 23 24 30 class RecordProcessor 31 { 32 private byte[] data; 33 private int recordOffset; 34 private int available; 35 private SSTRecordHeader sstRecordHeader; 36 37 public RecordProcessor( byte[] data, int available, int numStrings, int numUniqueStrings ) 38 { 39 this.data = data; 40 this.available = available; 41 this.sstRecordHeader = new SSTRecordHeader(numStrings, numUniqueStrings); 42 } 43 44 public int getAvailable() 45 { 46 return available; 47 } 48 49 public void writeRecordHeader( int offset, int totalWritten, int recordLength, boolean first_record ) 50 { 51 if ( first_record ) 52 { 53 available -= 8; 54 recordOffset = sstRecordHeader.writeSSTHeader( data, recordOffset + offset + totalWritten, recordLength ); 55 } 56 else 57 { 58 recordOffset = writeContinueHeader( data, recordOffset + offset + totalWritten, recordLength ); 59 } 60 } 61 62 public byte[] writeStringRemainder( boolean lastStringCompleted, byte[] stringreminant, int offset, int totalWritten ) 63 { 64 if ( !lastStringCompleted ) 65 { 66 System.arraycopy( stringreminant, 0, data, recordOffset + offset + totalWritten, stringreminant.length ); 68 adjustPointers( stringreminant.length ); 69 } 70 else 71 { 72 System.arraycopy( stringreminant, 0, data, recordOffset + offset + totalWritten, available ); 74 byte[] leftover = new byte[( stringreminant.length - available ) + LittleEndianConsts.BYTE_SIZE]; 75 76 System.arraycopy( stringreminant, available, leftover, LittleEndianConsts.BYTE_SIZE, stringreminant.length - available ); 77 leftover[0] = stringreminant[0]; 78 stringreminant = leftover; 79 adjustPointers( available ); } 81 return stringreminant; 82 } 83 84 public void writeWholeString( UnicodeString unistr, int offset, int totalWritten ) 85 { 86 unistr.serialize( recordOffset + offset + totalWritten, data ); 87 int rsize = unistr.getRecordSize(); 88 adjustPointers( rsize ); 89 } 90 91 public byte[] writePartString( UnicodeString unistr, int offset, int totalWritten ) 92 { 93 byte[] stringReminant; 94 byte[] ucs = unistr.serialize(); 95 96 System.arraycopy( ucs, 0, data, recordOffset + offset + totalWritten, available ); 97 stringReminant = new byte[( ucs.length - available ) + LittleEndianConsts.BYTE_SIZE]; 98 System.arraycopy( ucs, available, stringReminant, LittleEndianConsts.BYTE_SIZE, ucs.length - available ); 99 stringReminant[0] = ucs[LittleEndianConsts.SHORT_SIZE]; 100 available = 0; 101 return stringReminant; 102 } 103 104 105 private int writeContinueHeader( final byte[] data, final int pos, 106 final int recsize ) 107 { 108 int offset = pos; 109 110 LittleEndian.putShort( data, offset, ContinueRecord.sid ); 111 offset += LittleEndianConsts.SHORT_SIZE; 112 LittleEndian.putShort( data, offset, (short) ( recsize ) ); 113 offset += LittleEndianConsts.SHORT_SIZE; 114 return offset - pos; 115 } 116 117 118 private void adjustPointers( int amount ) 119 { 120 recordOffset += amount; 121 available -= amount; 122 } 123 124 public int getRecordOffset() 125 { 126 return recordOffset; 127 } 128 } 129 130 | Popular Tags |