KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > RecordProcessor


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

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 /**
25  * Process a single record. That is, an SST record or a continue record.
26  * Refactored from code originally in SSTRecord.
27  *
28  * @author Glen Stampoultzis (glens at apache.org)
29  */

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             // write reminant -- it'll all fit neatly
67
System.arraycopy( stringreminant, 0, data, recordOffset + offset + totalWritten, stringreminant.length );
68             adjustPointers( stringreminant.length );
69         }
70         else
71         {
72             // write as much of the remnant as possible
73
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 ); // Consume all available remaining space
80
}
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