KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
22
23 import org.apache.poi.util.LittleEndian;
24
25 /**
26  * Title: Continue Record - Helper class used primarily for SST Records <P>
27  * Description: handles overflow for prior record in the input
28  * stream; content is tailored to that prior record<P>
29  * @author Marc Johnson (mjohnson at apache dot org)
30  * @author Andrew C. Oliver (acoliver at apache dot org)
31  * @author Csaba Nagy (ncsaba at yahoo dot com)
32  * @version 2.0-pre
33  */

34
35 public class ContinueRecord
36     extends Record
37 {
38     public final static short sid = 0x003C;
39     private byte[] field_1_data;
40
41     /**
42      * default constructor
43      */

44
45     public ContinueRecord()
46     {
47     }
48
49     /**
50      * Main constructor -- kinda dummy because we don't validate or fill fields
51      *
52      * @param id record id
53      * @param size record size
54      * @param data raw data
55      */

56
57     public ContinueRecord(short id, short size, byte [] data)
58     {
59         super(id, size, data);
60     }
61
62     /**
63      * Main constructor -- kinda dummy because we don't validate or fill fields
64      *
65      * @param id record id
66      * @param size record size
67      * @param data raw data
68      * @param offset of the record's data
69      */

70
71     public ContinueRecord(short id, short size, byte [] data, int offset)
72     {
73         super(id, size, data, offset);
74     }
75
76     /**
77      * USE ONLY within "processContinue"
78      */

79
80     public byte [] serialize()
81     {
82         byte[] retval = new byte[ field_1_data.length + 4 ];
83         serialize(0, retval);
84         return retval;
85     }
86
87     public int serialize(int offset, byte [] data)
88     {
89
90         LittleEndian.putShort(data, offset, sid);
91         LittleEndian.putShort(data, offset + 2, ( short ) field_1_data.length);
92         System.arraycopy(field_1_data, 0, data, offset + 4, field_1_data.length);
93         return field_1_data.length + 4;
94         // throw new RecordFormatException(
95
// "You're not supposed to serialize Continue records like this directly");
96
}
97
98     /**
99      * set the data for continuation
100      * @param data - a byte array containing all of the continued data
101      */

102
103     public void setData(byte [] data)
104     {
105         field_1_data = data;
106     }
107
108     /**
109      * get the data for continuation
110      * @return byte array containing all of the continued data
111      */

112
113     public byte [] getData()
114     {
115         return field_1_data;
116     }
117
118     /**
119      * Use to serialize records that are too big for their britches (>8228..why 8228 and
120      * not 8192 aka 8k? Those folks in washington don't ususally make sense...
121      * or at least to anyone outside fo marketing...
122      * @deprecated handle this within the record...this didn't actualyl work out
123      */

124
125     public static byte [] processContinue(byte [] data)
126     { // could do this recursively but that seems hard to debug
127

128         // how many continue records do we need
129
// System.out.println("In ProcessContinue");
130
int records = (data.length / 8214); // we've a 1 offset but we're also off by one due to rounding...so it balances out
131
int offset = 8214;
132
133         // System.out.println("we have "+records+" continue records to process");
134
ArrayList JavaDoc crs = new ArrayList JavaDoc(records);
135         int totalsize = 8214;
136         byte[] retval = null;
137
138         for (int cr = 0; cr < records; cr++)
139         {
140             ContinueRecord contrec = new ContinueRecord();
141             int arraysize = Math.min((8214 - 4), (data.length - offset));
142             byte[] crdata = new byte[ arraysize ];
143
144             System.arraycopy(data, offset, crdata, 0, arraysize);
145
146             // System.out.println("arraycopy(data,"+offset+",crdata,"+0+","+arraysize+");");
147
offset += crdata.length;
148             contrec.setData(crdata);
149             crs.add(contrec.serialize());
150         }
151         for (int cr = 0; cr < records; cr++)
152         {
153             totalsize += (( byte [] ) crs.get(cr)).length;
154         }
155
156         // System.out.println("totalsize="+totalsize);
157
retval = new byte[ totalsize ];
158         offset = 8214;
159         System.arraycopy(data, 0, retval, 0, 8214);
160         for (int cr = 0; cr < records; cr++)
161         {
162             byte[] src = ( byte [] ) crs.get(cr);
163
164             System.arraycopy(src, 0, retval, offset, src.length);
165
166             // System.out.println("arraycopy(src,"+0+",retval,"+offset+","+src.length+");");
167
offset += src.length;
168         }
169         return retval;
170     }
171
172     /**
173      * Fill the fields. Only thing is, this record has no fields --
174      *
175      * @param ignored_parm1 Ignored
176      * @param ignored_parm2 Ignored
177      */

178
179     protected void fillFields(byte [] ignored_parm1, short ignored_parm2)
180     {
181         this.field_1_data = ignored_parm1;
182         // throw new RecordFormatException("Are you crazy? Don't fill a continue record");
183
// do nothing
184
}
185
186     /**
187      * Make sure we have a good id
188      *
189      * @param id the alleged id
190      */

191
192     protected void validateSid(short id)
193     {
194         if (id != ContinueRecord.sid)
195         {
196             throw new RecordFormatException("Not a Continue Record");
197         }
198     }
199
200     /**
201      * Debugging toString
202      *
203      * @return string representation
204      */

205
206     public String JavaDoc toString()
207     {
208         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
209
210         buffer.append("[CONTINUE RECORD]\n");
211         buffer.append(" .id = ").append(Integer.toHexString(sid))
212             .append("\n");
213         buffer.append("[/CONTINUE RECORD]\n");
214         return buffer.toString();
215     }
216
217     public short getSid()
218     {
219         return this.sid;
220     }
221
222     /**
223      * Fill the fields. Only thing is, this record has no fields --
224      *
225      * @param ignored_parm1 Ignored
226      * @param ignored_parm2 Ignored
227      * @param ignored_parm3 Ignored
228      */

229
230     protected void fillFields(byte [] ignored_parm1, short ignored_parm2, int ignored_parm3)
231     {
232     }
233
234     /**
235      * Clone this record.
236      */

237     public Object JavaDoc clone() {
238       ContinueRecord clone = new ContinueRecord();
239       clone.setData(field_1_data);
240       return clone;
241     }
242
243 }
244
Popular Tags