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 = 165 (data.length 166 / 8214); // we've a 1 offset but we're also off by one due to rounding...so it balances out 167 int offset = 8214; 168 169 // System.out.println("we have "+records+" continue records to process"); 170 ArrayList crs = new ArrayList(records); 171 int totalsize = 8214; 172 byte[] retval = null; 173 174 for (int cr = 0; cr < records; cr++) 175 { 176 ContinueRecord contrec = new ContinueRecord(); 177 int arraysize = Math.min((8214 - 4), 178 (data.length - offset)); 179 byte[] crdata = new byte[ arraysize ]; 180 181 System.arraycopy(data, offset, crdata, 0, arraysize); 182 183 // System.out.println("arraycopy(data,"+offset+",crdata,"+0+","+arraysize+");"); 184 offset += crdata.length; 185 contrec.setData(crdata); 186 crs.add(contrec.serialize()); 187 } 188 for (int cr = 0; cr < records; cr++) 189 { 190 totalsize += (( byte [] ) crs.get(cr)).length; 191 } 192 193 // System.out.println("totalsize="+totalsize); 194 retval = new byte[ totalsize ]; 195 offset = 8214; 196 System.arraycopy(data, 0, retval, 0, 8214); 197 for (int cr = 0; cr < records; cr++) 198 { 199 byte[] src = ( byte [] ) crs.get(cr); 200 201 System.arraycopy(src, 0, retval, offset, src.length); 202 203 // System.out.println("arraycopy(src,"+0+",retval,"+offset+","+src.length+");"); 204 offset += src.length; 205 } 206 return retval; 207 } 208 209 /** 210 * Fill the fields. Only thing is, this record has no fields -- 211 * 212 * @param ignored_parm1 Ignored 213 * @param ignored_parm2 Ignored 214 */ 215 216 protected void fillFields(byte [] ignored_parm1, short ignored_parm2) 217 { 218 219 // throw new RecordFormatException("Are you crazy? Don't fill a continue record"); 220 // do nothing 221 } 222 223 /** 224 * Make sure we have a good id 225 * 226 * @param id the alleged id 227 */ 228 229 protected void validateSid(short id) 230 { 231 if (id != ContinueRecord.sid) 232 { 233 throw new RecordFormatException("Not a Continue Record"); 234 } 235 } 236 237 /** 238 * Debugging toString 239 * 240 * @return string representation 241 */ 242 243 public String toString() 244 { 245 StringBuffer buffer = new StringBuffer(); 246 247 buffer.append("[CONTINUE RECORD]\n"); 248 buffer.append(" .id = ").append(Integer.toHexString(sid)) 249 .append("\n"); 250 buffer.append("[/CONTINUE RECORD]\n"); 251 return buffer.toString(); 252 } 253 254 public short getSid() 255 { 256 return this.sid; 257 } 258 259 /** 260 * Fill the fields. Only thing is, this record has no fields -- 261 * 262 * @param ignored_parm1 Ignored 263 * @param ignored_parm2 Ignored 264 * @param ignored_parm3 Ignored 265 */ 266 267 protected void fillFields(byte [] ignored_parm1, short ignored_parm2, int ignored_parm3) 268 { 269 } 270 } 271