1    /* ====================================================================
2     * The Apache Software License, Version 1.1
3     *
4     * Copyright (c) 2002 The Apache Software Foundation.  All rights
5     * reserved.
6     *
7     * Redistribution and use in source and binary forms, with or without
8     * modification, are permitted provided that the following conditions
9     * are met:
10    *
11    * 1. Redistributions of source code must retain the above copyright
12    *    notice, this list of conditions and the following disclaimer.
13    *
14    * 2. Redistributions in binary form must reproduce the above copyright
15    *    notice, this list of conditions and the following disclaimer in
16    *    the documentation and/or other materials provided with the
17    *    distribution.
18    *
19    * 3. The end-user documentation included with the redistribution,
20    *    if any, must include the following acknowledgment:
21    *       "This product includes software developed by the
22    *        Apache Software Foundation (http://www.apache.org/)."
23    *    Alternately, this acknowledgment may appear in the software itself,
24    *    if and wherever such third-party acknowledgments normally appear.
25    *
26    * 4. The names "Apache" and "Apache Software Foundation" and
27    *    "Apache POI" must not be used to endorse or promote products
28    *    derived from this software without prior written permission. For
29    *    written permission, please contact apache@apache.org.
30    *
31    * 5. Products derived from this software may not be called "Apache",
32    *    "Apache POI", nor may "Apache" appear in their name, without
33    *    prior written permission of the Apache Software Foundation.
34    *
35    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46    * SUCH DAMAGE.
47    * ====================================================================
48    *
49    * This software consists of voluntary contributions made by many
50    * individuals on behalf of the Apache Software Foundation.  For more
51    * information on the Apache Software Foundation, please see
52    * <http://www.apache.org/>.
53    */
54   
55   package org.apache.poi.hssf.record;
56   
57   import org.apache.poi.util.LittleEndianConsts;
58   import org.apache.poi.util.LittleEndian;
59   
60   /**
61    * Process a single record.  That is, an SST record or a continue record.
62    * Refactored from code originally in SSTRecord.
63    *
64    * @author Glen Stampoultzis (glens at apache.org)
65    */
66   class RecordProcessor
67   {
68       private byte[] data;
69       private int recordOffset;
70       private int available;
71       private SSTRecordHeader sstRecordHeader;
72   
73       public RecordProcessor( byte[] data, int available, int numStrings, int numUniqueStrings )
74       {
75           this.data = data;
76           this.available = available;
77           this.sstRecordHeader = new SSTRecordHeader(numStrings, numUniqueStrings);
78       }
79   
80       public int getAvailable()
81       {
82           return available;
83       }
84   
85       public void writeRecordHeader( int offset, int totalWritten, int recordLength, boolean first_record )
86       {
87           if ( first_record )
88           {
89               available -= 8;
90               recordOffset = sstRecordHeader.writeSSTHeader( data, recordOffset + offset + totalWritten, recordLength );
91           }
92           else
93           {
94               recordOffset = writeContinueHeader( data, recordOffset + offset + totalWritten, recordLength );
95           }
96       }
97   
98       public byte[] writeStringRemainder( boolean lastStringCompleted, byte[] stringreminant, int offset, int totalWritten )
99       {
100          if ( !lastStringCompleted )
101          {
102              // write reminant -- it'll all fit neatly
103              System.arraycopy( stringreminant, 0, data, recordOffset + offset + totalWritten, stringreminant.length );
104              adjustPointers( stringreminant.length );
105          }
106          else
107          {
108              // write as much of the remnant as possible
109              System.arraycopy( stringreminant, 0, data, recordOffset + offset + totalWritten, available );
110              byte[] leftover = new byte[( stringreminant.length - available ) + LittleEndianConsts.BYTE_SIZE];
111  
112              System.arraycopy( stringreminant, available, leftover, LittleEndianConsts.BYTE_SIZE, stringreminant.length - available );
113              leftover[0] = stringreminant[0];
114              stringreminant = leftover;
115              adjustPointers( available );    // Consume all available remaining space
116          }
117          return stringreminant;
118      }
119  
120      public void writeWholeString( UnicodeString unistr, int offset, int totalWritten )
121      {
122          unistr.serialize( recordOffset + offset + totalWritten, data );
123          int rsize = unistr.getRecordSize();
124          adjustPointers( rsize );
125      }
126  
127      public byte[] writePartString( UnicodeString unistr, int offset, int totalWritten )
128      {
129          byte[] stringReminant;
130          byte[] ucs = unistr.serialize();
131  
132          System.arraycopy( ucs, 0, data, recordOffset + offset + totalWritten, available );
133          stringReminant = new byte[( ucs.length - available ) + LittleEndianConsts.BYTE_SIZE];
134          System.arraycopy( ucs, available, stringReminant, LittleEndianConsts.BYTE_SIZE, ucs.length - available );
135          stringReminant[0] = ucs[LittleEndianConsts.SHORT_SIZE];
136          available = 0;
137          return stringReminant;
138      }
139  
140  
141      private int writeContinueHeader( final byte[] data, final int pos,
142                                       final int recsize )
143      {
144          int offset = pos;
145  
146          LittleEndian.putShort( data, offset, ContinueRecord.sid );
147          offset += LittleEndianConsts.SHORT_SIZE;
148          LittleEndian.putShort( data, offset, (short) ( recsize ) );
149          offset += LittleEndianConsts.SHORT_SIZE;
150          return offset - pos;
151      }
152  
153  
154      private void adjustPointers( int amount )
155      {
156          recordOffset += amount;
157          available -= amount;
158      }
159  }
160  
161