1    
2    /* ====================================================================
3     * The Apache Software License, Version 1.1
4     *
5     * Copyright (c) 2002 The Apache Software Foundation.  All rights
6     * reserved.
7     *
8     * Redistribution and use in source and binary forms, with or without
9     * modification, are permitted provided that the following conditions
10    * are met:
11    *
12    * 1. Redistributions of source code must retain the above copyright
13    *    notice, this list of conditions and the following disclaimer.
14    *
15    * 2. Redistributions in binary form must reproduce the above copyright
16    *    notice, this list of conditions and the following disclaimer in
17    *    the documentation and/or other materials provided with the
18    *    distribution.
19    *
20    * 3. The end-user documentation included with the redistribution,
21    *    if any, must include the following acknowledgment:
22    *       "This product includes software developed by the
23    *        Apache Software Foundation (http://www.apache.org/)."
24    *    Alternately, this acknowledgment may appear in the software itself,
25    *    if and wherever such third-party acknowledgments normally appear.
26    *
27    * 4. The names "Apache" and "Apache Software Foundation" and
28    *    "Apache POI" must not be used to endorse or promote products
29    *    derived from this software without prior written permission. For
30    *    written permission, please contact apache@apache.org.
31    *
32    * 5. Products derived from this software may not be called "Apache",
33    *    "Apache POI", nor may "Apache" appear in their name, without
34    *    prior written permission of the Apache Software Foundation.
35    *
36    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47    * SUCH DAMAGE.
48    * ====================================================================
49    *
50    * This software consists of voluntary contributions made by many
51    * individuals on behalf of the Apache Software Foundation.  For more
52    * information on the Apache Software Foundation, please see
53    * <http://www.apache.org/>.
54    */
55   
56   package org.apache.poi.hssf.record;
57   
58   import java.util.ArrayList;
59   
60   import org.apache.poi.util.LittleEndian;
61   
62   /**
63    * Title:        Continue Record - Helper class used primarily for SST Records <P>
64    * Description:  handles overflow for prior record in the input
65    *               stream; content is tailored to that prior record<P>
66    * @author Marc Johnson (mjohnson at apache dot org)
67    * @author Andrew C. Oliver (acoliver at apache dot org)
68    * @version 2.0-pre
69    */
70   
71   public class ContinueRecord
72       extends Record
73   {
74       public final static short sid = 0x003C;
75       private byte[]            field_1_data;
76   
77       /**
78        * default constructor
79        */
80   
81       public ContinueRecord()
82       {
83       }
84   
85       /**
86        * Main constructor -- kinda dummy because we don't validate or fill fields
87        *
88        * @param id record id
89        * @param size record size
90        * @param data raw data
91        */
92   
93       public ContinueRecord(short id, short size, byte [] data)
94       {
95           super(id, size, data);
96       }
97   
98       /**
99        * Main constructor -- kinda dummy because we don't validate or fill fields
100       *
101       * @param id record id
102       * @param size record size
103       * @param data raw data
104       * @param offset of the record's data
105       */
106  
107      public ContinueRecord(short id, short size, byte [] data, int offset)
108      {
109          super(id, size, data, offset);
110      }
111  
112      /**
113       * USE ONLY within "processContinue"
114       */
115  
116      public byte [] serialize()
117      {
118          byte[] retval = new byte[ field_1_data.length + 4 ];
119  
120          LittleEndian.putShort(retval, 0, sid);
121          LittleEndian.putShort(retval, 2, ( short ) field_1_data.length);
122          System.arraycopy(field_1_data, 0, retval, 4, field_1_data.length);
123          return retval;
124      }
125  
126      public int serialize(int offset, byte [] data)
127      {
128          throw new RecordFormatException(
129              "You're not supposed to serialize Continue records like this directly");
130      }
131  
132      /**
133       * set the data for continuation
134       * @param data - a byte array containing all of the continued data
135       */
136  
137      public void setData(byte [] data)
138      {
139          field_1_data = data;
140      }
141  
142      /**
143       * get the data for continuation
144       * @return byte array containing all of the continued data
145       */
146  
147      public byte [] getData()
148      {
149          return field_1_data;
150      }
151  
152      /**
153       * Use to serialize records that are too big for their britches (>8228..why 8228 and
154       * not 8192 aka 8k?  Those folks in washington don't ususally make sense...
155       * or at least to anyone outside fo marketing...
156       * @deprecated handle this within the record...this didn't actualyl work out
157       */
158  
159      public static byte [] processContinue(byte [] data)
160      {   // could do this recursively but that seems hard to debug
161  
162          // how many continue records do we need
163          // System.out.println("In ProcessContinue");
164          int       records   = (data.length / 8214);   // we've a 1 offset but we're also off by one due to rounding...so it balances out
165          int       offset    = 8214;
166  
167          // System.out.println("we have "+records+" continue records to process");
168          ArrayList crs       = new ArrayList(records);
169          int       totalsize = 8214;
170          byte[]    retval    = null;
171  
172          for (int cr = 0; cr < records; cr++)
173          {
174              ContinueRecord contrec   = new ContinueRecord();
175              int            arraysize = Math.min((8214 - 4), (data.length - offset));
176              byte[]         crdata    = new byte[ arraysize ];
177  
178              System.arraycopy(data, offset, crdata, 0, arraysize);
179  
180              // System.out.println("arraycopy(data,"+offset+",crdata,"+0+","+arraysize+");");
181              offset += crdata.length;
182              contrec.setData(crdata);
183              crs.add(contrec.serialize());
184          }
185          for (int cr = 0; cr < records; cr++)
186          {
187              totalsize += (( byte [] ) crs.get(cr)).length;
188          }
189  
190          // System.out.println("totalsize="+totalsize);
191          retval = new byte[ totalsize ];
192          offset = 8214;
193          System.arraycopy(data, 0, retval, 0, 8214);
194          for (int cr = 0; cr < records; cr++)
195          {
196              byte[] src = ( byte [] ) crs.get(cr);
197  
198              System.arraycopy(src, 0, retval, offset, src.length);
199  
200              // System.out.println("arraycopy(src,"+0+",retval,"+offset+","+src.length+");");
201              offset += src.length;
202          }
203          return retval;
204      }
205  
206      /**
207       * Fill the fields. Only thing is, this record has no fields --
208       *
209       * @param ignored_parm1 Ignored
210       * @param ignored_parm2 Ignored
211       */
212  
213      protected void fillFields(byte [] ignored_parm1, short ignored_parm2)
214      {
215  
216          // throw new RecordFormatException("Are you crazy?  Don't fill a continue record");
217          // do nothing
218      }
219  
220      /**
221       * Make sure we have a good id
222       *
223       * @param id the alleged id
224       */
225  
226      protected void validateSid(short id)
227      {
228          if (id != ContinueRecord.sid)
229          {
230              throw new RecordFormatException("Not a Continue Record");
231          }
232      }
233  
234      /**
235       * Debugging toString
236       *
237       * @return string representation
238       */
239  
240      public String toString()
241      {
242          StringBuffer buffer = new StringBuffer();
243  
244          buffer.append("[CONTINUE RECORD]\n");
245          buffer.append("    .id        = ").append(Integer.toHexString(sid))
246              .append("\n");
247          buffer.append("[/CONTINUE RECORD]\n");
248          return buffer.toString();
249      }
250  
251      public short getSid()
252      {
253          return this.sid;
254      }
255  
256      /**
257       * Fill the fields. Only thing is, this record has no fields --
258       *
259       * @param ignored_parm1 Ignored
260       * @param ignored_parm2 Ignored
261       * @param ignored_parm3 Ignored
262       */
263  
264      protected void fillFields(byte [] ignored_parm1, short ignored_parm2, int ignored_parm3)
265      {
266      }
267  }
268