View | Details | Raw Unified | Return to bug 10976
Collapse All | Expand All

(-)jakarta-poi/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java (-7 / +97 lines)
Lines 54-60 Link Here
54
 */
54
 */
55
55
56
package org.apache.poi.hssf.record;
56
package org.apache.poi.hssf.record;
57
57
import org.apache.poi.util.BinaryTree;
58
import org.apache.poi.util.LittleEndian;
58
import org.apache.poi.util.LittleEndian;
59
import org.apache.poi.util.StringUtil;
59
import org.apache.poi.util.StringUtil;
60
60
Lines 117-122 Link Here
117
        }
117
        }
118
    }
118
    }
119
119
120
    /**
121
     *  lifted from SSTDeserializer
122
     */
123
124
    private void arraycopy( byte[] src, int src_position,
125
                            byte[] dst, int dst_position,
126
                            int length )
127
    {
128
        System.arraycopy( src, src_position, dst, dst_position, length );
129
    }
120
    protected void fillFields(byte [] data, short size, int offset)
130
    protected void fillFields(byte [] data, short size, int offset)
121
    {
131
    {
122
        field_1_position_of_BOF         = LittleEndian.getInt(data,
132
        field_1_position_of_BOF         = LittleEndian.getInt(data,
Lines 125-132 Link Here
125
                4 + offset);
135
                4 + offset);
126
        field_3_sheetname_length        = data[ 6 + offset ];
136
        field_3_sheetname_length        = data[ 6 + offset ];
127
        field_4_compressed_unicode_flag = data[ 7 + offset ];
137
        field_4_compressed_unicode_flag = data[ 7 + offset ];
128
        field_5_sheetname               = new String(data, 8 + offset,
138
129
                LittleEndian.ubyteToInt( field_3_sheetname_length));
139
        int length = LittleEndian.ubyteToInt( field_3_sheetname_length);
140
        if ((field_4_compressed_unicode_flag & 0x01)==1) {
141
            UnicodeString ucs =new UnicodeString(data,(short)length,6+offset,(short)1);
142
            field_5_sheetname = ucs.getString();
143
        }
144
        else {
145
          try {
146
            // should get the codepage from the excel workbook record
147
            // instead of hard coded "ISO-8859-1" here.
148
            field_5_sheetname =   new String(data, 8 + offset,
149
            LittleEndian.ubyteToInt( field_3_sheetname_length),"ISO-8859-1");
150
          }
151
          catch (Exception e){
152
            e.printStackTrace();
153
          }
154
        }
155
        System.out.println("f_5_sn is "+field_5_sheetname);
130
    }
156
    }
131
157
132
    /**
158
    /**
Lines 175-187 Link Here
175
    }
201
    }
176
202
177
    /**
203
    /**
204
     * Check if String use 16-bit encoding character
205
     * Lifted from SSTRecord.addString
206
     */
207
    public boolean is16bitString(String string)
208
    {
209
            // scan for characters greater than 255 ... if any are
210
            // present, we have to use 16-bit encoding. Otherwise, we
211
            // can use 8-bit encoding
212
            boolean useUTF16 = false;
213
            int strlen = string.length();
214
215
            for ( int j = 0; j < strlen; j++ )
216
            {
217
                if ( string.charAt( j ) > 255 )
218
                {
219
                    useUTF16 = true;
220
                    break;
221
                }
222
            }
223
            return useUTF16 ;
224
   }
225
    /**
226
     * Check if String use all 8-bit (outside ASCII 7 bit range) to
227
     * encoding character
228
     * Lifted from SSTRecord.addString
229
     */
230
    public boolean is8bitString(String string)
231
    {
232
            // scan for characters greater than 255
233
            //  ... if any are present then return false
234
            boolean use8bit = true;
235
            int strlen = string.length();
236
237
            for ( int j = 0; j < strlen; j++ )
238
            {
239
                if ( string.charAt( j ) > 255 )
240
                {
241
                    use8bit = false;
242
                    break;
243
                }
244
            }
245
            return use8bit ;
246
   }
247
248
    /**
178
     * Set the sheetname for this sheet.  (this appears in the tabs at the bottom)
249
     * Set the sheetname for this sheet.  (this appears in the tabs at the bottom)
179
     * @param sheetname the name of the sheet
250
     * @param sheetname the name of the sheet
180
     */
251
     */
181
252
182
    public void setSheetname(String sheetname)
253
    public void setSheetname(String sheetname)
183
    {
254
    {
255
        boolean is16bit = is16bitString(sheetname);
256
        setSheetnameLength((byte) sheetname.length() );
257
        setCompressedUnicodeFlag((byte ) (is16bit?1:0));
184
        field_5_sheetname = sheetname;
258
        field_5_sheetname = sheetname;
259
260
185
    }
261
    }
186
262
187
    /**
263
    /**
Lines 263-282 Link Here
263
    {
339
    {
264
        LittleEndian.putShort(data, 0 + offset, sid);
340
        LittleEndian.putShort(data, 0 + offset, sid);
265
        LittleEndian.putShort(data, 2 + offset,
341
        LittleEndian.putShort(data, 2 + offset,
266
                              ( short ) (0x08 + getSheetnameLength()));
342
                              ( short ) (0x08 + getSheetnameLength()* (getCompressedUnicodeFlag()==0?1:2)));
267
        LittleEndian.putInt(data, 4 + offset, getPositionOfBof());
343
        LittleEndian.putInt(data, 4 + offset, getPositionOfBof());
268
        LittleEndian.putShort(data, 8 + offset, getOptionFlags());
344
        LittleEndian.putShort(data, 8 + offset, getOptionFlags());
269
        data[ 10 + offset ] = getSheetnameLength();
345
        data[ 10 + offset ] = getSheetnameLength();
270
        data[ 11 + offset ] = getCompressedUnicodeFlag();
346
        data[ 11 + offset ] = getCompressedUnicodeFlag();
271
347
272
        // we assume compressed unicode (bein the dern americans we are ;-p)
348
        if (getCompressedUnicodeFlag()==0){
273
        StringUtil.putCompressedUnicode(getSheetname(), data, 12 + offset);
349
          // we assume compressed unicode (bein the dern americans we are ;-p)
350
          StringUtil.putCompressedUnicode(getSheetname(), data, 12 + offset);
351
        }
352
        else {
353
          try {
354
            StringUtil.putUncompressedUnicode(getSheetname(), data, 12 + offset);
355
  //          String unicodeString = new String(getSheetname().getBytes("Unicode"),"Unicode");
356
  //          StringUtil.putUncompressedUnicode(unicodeString, data, 12 + offset);
357
          }
358
          catch (Exception e){
359
            System.out.println("encoding exception in BoundSheetRecord.serialize!");
360
          }
361
362
363
        }
274
        return getRecordSize();
364
        return getRecordSize();
275
    }
365
    }
276
366
277
    public int getRecordSize()
367
    public int getRecordSize()
278
    {
368
    {
279
        return 12 + getSheetnameLength();
369
        return 12 + getSheetnameLength()* (getCompressedUnicodeFlag()==0?1:2);
280
    }
370
    }
281
371
282
    public short getSid()
372
    public short getSid()
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/SSTDeserializer.java (-24 / +21 lines)
Lines 204-227 Link Here
204
    private int processString( final byte[] data, final int dataIndex, final int characters )
204
    private int processString( final byte[] data, final int dataIndex, final int characters )
205
    {
205
    {
206
206
207
        // length is the length we store it as.  not the length that is read.
207
        // use the new UnicodeString constructor
208
        int length = SSTRecord.STRING_MINIMAL_OVERHEAD + calculateByteCount( characters );
208
209
        byte[] unicodeStringBuffer = new byte[length];
209
        int bytesRead =calculateByteCount( characters );
210
210
        UnicodeString string = new UnicodeString( data,
211
        int offset = 0;
211
          (short)(bytesRead +stringHeaderOverhead()),
212
212
           dataIndex, stringHeaderOverhead()-SSTRecord.STRING_MINIMAL_OVERHEAD
213
        // Set the length in characters
213
           ,(short)UnicodeString.CHARCOUNTSIZE_DOUBLEBYTE );
214
        LittleEndian.putUShort( unicodeStringBuffer, offset, characters );
215
        offset += LittleEndianConsts.SHORT_SIZE;
216
        // Set the option flags
217
        unicodeStringBuffer[offset] = data[dataIndex + offset];
218
        // Copy in the string data
219
        int bytesRead = unicodeStringBuffer.length - SSTRecord.STRING_MINIMAL_OVERHEAD;
220
        arraycopy( data, dataIndex + stringHeaderOverhead(), unicodeStringBuffer, SSTRecord.STRING_MINIMAL_OVERHEAD, bytesRead );
221
        // Create the unicode string
222
        UnicodeString string = new UnicodeString( UnicodeString.sid,
223
                (short) unicodeStringBuffer.length,
224
                unicodeStringBuffer );
225
214
226
        if ( isStringFinished() )
215
        if ( isStringFinished() )
227
        {
216
        {
Lines 339-345 Link Here
339
        LittleEndian.putShort( unicodeStringData, 0, (short) getContinuationExpectedChars() );
328
        LittleEndian.putShort( unicodeStringData, 0, (short) getContinuationExpectedChars() );
340
329
341
        // write the options flag
330
        // write the options flag
342
        unicodeStringData[LittleEndianConsts.SHORT_SIZE] = createOptionByte( wideChar, richText, extendedText );
331
//        Can't deal with richtext or Far-East Info.
332
//        unicodeStringData[LittleEndianConsts.SHORT_SIZE] = createOptionByte( wideChar, richText, extendedText );
333
        unicodeStringData[LittleEndianConsts.SHORT_SIZE] = createOptionByte( wideChar, false,false );
343
334
344
        // copy the bytes/words making up the string; skipping
335
        // copy the bytes/words making up the string; skipping
345
        // past all the overhead of the str_data array
336
        // past all the overhead of the str_data array
Lines 348-356 Link Here
348
                unicodeStringData.length - SSTRecord.STRING_MINIMAL_OVERHEAD );
339
                unicodeStringData.length - SSTRecord.STRING_MINIMAL_OVERHEAD );
349
340
350
        // use special constructor to create the final string
341
        // use special constructor to create the final string
351
        UnicodeString string = new UnicodeString( UnicodeString.sid,
342
//        UnicodeString string = new UnicodeString( UnicodeString.sid,
352
                (short) unicodeStringData.length, unicodeStringData,
343
//                (short) unicodeStringData.length, unicodeStringData,
353
                unfinishedString );
344
//                unfinishedString );
345
        UnicodeString string = new UnicodeString( unicodeStringData,
346
                (short) unicodeStringData.length,
347
                unfinishedString , UnicodeString.CHARCOUNTSIZE_DOUBLEBYTE );
348
349
354
        Integer integer = new Integer( strings.size() );
350
        Integer integer = new Integer( strings.size() );
355
351
356
        addToStringTable( strings, integer, string );
352
        addToStringTable( strings, integer, string );
Lines 411-418 Link Here
411
407
412
        LittleEndian.putShort( unicodeStringData, (byte) 0, (short) calculateCharCount( dataLengthInBytes ) );
408
        LittleEndian.putShort( unicodeStringData, (byte) 0, (short) calculateCharCount( dataLengthInBytes ) );
413
        arraycopy( record, 0, unicodeStringData, LittleEndianConsts.SHORT_SIZE, record.length );
409
        arraycopy( record, 0, unicodeStringData, LittleEndianConsts.SHORT_SIZE, record.length );
414
        UnicodeString ucs = new UnicodeString( UnicodeString.sid, (short) unicodeStringData.length, unicodeStringData );
410
        //UnicodeString ucs = new UnicodeString( UnicodeString.sid, (short) unicodeStringData.length, unicodeStringData );
415
411
        UnicodeString ucs = new UnicodeString( unicodeStringData,
412
            (short) unicodeStringData.length,UnicodeString.CHARCOUNTSIZE_DOUBLEBYTE );
416
        unfinishedString = unfinishedString + ucs.getString();
413
        unfinishedString = unfinishedString + ucs.getString();
417
        setContinuationExpectedChars( getContinuationExpectedChars() - calculateCharCount( dataLengthInBytes ) );
414
        setContinuationExpectedChars( getContinuationExpectedChars() - calculateCharCount( dataLengthInBytes ) );
418
    }
415
    }
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/SeriesTextRecord.java (-1 / +1 lines)
Lines 169-175 Link Here
169
        LittleEndian.putShort(data, 4 + offset, field_1_id);
169
        LittleEndian.putShort(data, 4 + offset, field_1_id);
170
        data[ 6 + offset ] = field_2_textLength;
170
        data[ 6 + offset ] = field_2_textLength;
171
        data[ 7 + offset ] = field_3_undocumented;
171
        data[ 7 + offset ] = field_3_undocumented;
172
        StringUtil.putUncompressedUnicodeHigh(field_4_text, data, 8 + offset);
172
        StringUtil.putUncompressedUnicode(field_4_text, data, 8 + offset);
173
173
174
        return getRecordSize();
174
        return getRecordSize();
175
    }
175
    }
(-)jakarta-poi/src/java/org/apache/poi/hssf/record/UnicodeString.java (-32 / +216 lines)
Lines 56-61 Link Here
56
package org.apache.poi.hssf.record;
56
package org.apache.poi.hssf.record;
57
57
58
import org.apache.poi.util.LittleEndian;
58
import org.apache.poi.util.LittleEndian;
59
import org.apache.poi.util.LittleEndianConsts;
59
import org.apache.poi.util.StringUtil;
60
import org.apache.poi.util.StringUtil;
60
61
61
/**
62
/**
Lines 74-83 Link Here
74
    extends Record
75
    extends Record
75
    implements Comparable
76
    implements Comparable
76
{
77
{
78
    /**  character count field size in byte */
79
    static final short CHARCOUNTSIZE_SINGLEBYTE = LittleEndianConsts.BYTE_SIZE;
80
    static final short CHARCOUNTSIZE_DOUBLEBYTE = LittleEndianConsts.SHORT_SIZE;
81
82
77
    public final static short sid = 0xFFF;
83
    public final static short sid = 0xFFF;
78
    private short             field_1_charCount;     // = 0;
84
    private short             field_1_charCount;     // = 0;
79
    private byte              field_2_optionflags;   // = 0;
85
    private byte              field_2_optionflags;   // = 0;
80
    private String            field_3_string;        // = null;
86
    private String            field_3_string;        // = null;
87
    private byte              field_4_ccAdjust =1;   // charCount size adjustment
88
                                                     // default to 1 i.e. charCount as short
81
    private final int RICH_TEXT_BIT = 8;
89
    private final int RICH_TEXT_BIT = 8;
82
    private final int EXT_BIT = 4;
90
    private final int EXT_BIT = 4;
83
91
Lines 85-91 Link Here
85
    {
93
    {
86
    }
94
    }
87
95
88
89
    public int hashCode()
96
    public int hashCode()
90
    {
97
    {
91
        int stringHash = 0;
98
        int stringHash = 0;
Lines 114-119 Link Here
114
                && field_3_string.equals(other.field_3_string));
121
                && field_3_string.equals(other.field_3_string));
115
    }
122
    }
116
123
124
117
    /**
125
    /**
118
     * construct a unicode string record and fill its fields, ID is ignored
126
     * construct a unicode string record and fill its fields, ID is ignored
119
     * @param id - ignored
127
     * @param id - ignored
Lines 127-132 Link Here
127
    }
135
    }
128
136
129
    /**
137
    /**
138
     * construct a unicode string record and fill its fields, ID is ignored
139
     * @param data - the bytes of the string/fields
140
     * @param size - size of the data
141
     * @param ccSize - the size (in byte) of the charCount field
142
     */
143
144
    public UnicodeString(byte [] data, short size,  short ccSize )
145
    {
146
        super();
147
        field_4_ccAdjust =  (byte)((ccSize == (short) 2)?1:0) ;
148
        fillFields(data, size);
149
    }
150
151
    /**
152
     * construct a unicode string record and fill its fields, ID is ignored
153
     * @param data - the bytes of the string/fields
154
     * @param size - size of the data
155
     * @param offset - offset of the record
156
     * @param ccSize - the size (in byte) of the charCount field
157
     */
158
159
    public UnicodeString(byte [] data, short size, int offset , short ccSize )
160
    {
161
        super();
162
        //UnicodeString us = new UnicodeString();
163
        field_4_ccAdjust =  (byte)((ccSize == (short) 2)?1:0) ;
164
        fillFields(data, size,offset);
165
166
    }
167
    /**
168
     * construct a unicode string from a string fragment + data with an
169
     * additional offset for extended(Far East and Rich Text) info and
170
     * the size (in byte) of the charCount field given as last parameter
171
     */
172
    public UnicodeString(byte [] data, short size, int offset ,int extOffset,
173
                         short ccSize )
174
    {
175
        super();
176
        //UnicodeString us = new UnicodeString();
177
        field_4_ccAdjust =  (byte)((ccSize == (short) 2)?1:0) ;
178
        fillFields(data, size,offset,extOffset);
179
    }
180
181
    /**
130
     * construct a unicode string from a string fragment + data
182
     * construct a unicode string from a string fragment + data
131
     */
183
     */
132
184
Lines 138-173 Link Here
138
    }
190
    }
139
191
140
    /**
192
    /**
141
     * NO OP
193
     * construct a unicode string from a string fragment + data with the size
194
     * (in byte) of the charCount field given as last parameter
142
     */
195
     */
143
196
144
    protected void validateSid(short id)
197
    public UnicodeString( byte [] data, short size, String prefix,
198
                         short ccSize)
145
    {
199
    {
200
        super();
201
        field_4_ccAdjust =  (byte)((ccSize == (short) 2)?1:0) ;
202
        fillFields(data,  size, 0);
203
        field_3_string = prefix + field_3_string;
204
        setCharCount();
205
    }
206
207
    /**
208
     * construct a unicode string from a string fragment + data with the size
209
     * (in byte) of the charCount field given as last parameter
210
     */
211
212
    public UnicodeString( byte [] data, short size, int offset, String prefix,
213
                         short ccSize)
214
    {
215
        super();
216
        field_4_ccAdjust =  (byte)((ccSize == (short) 2)?1:0) ;
217
        fillFields(data,  size, offset);
218
        field_3_string = prefix + field_3_string;
219
        setCharCount();
146
220
147
        // included only for interface compliance
148
    }
221
    }
149
222
150
    protected void fillFields(byte [] data, short size)
223
    /**
224
     * construct a unicode string from a string fragment + data with an
225
     * additional offset for extended(Far East and Rich Text) info and the size
226
     * (in byte) of the charCount field given as last parameter
227
     */
228
229
    public UnicodeString( byte [] data, short size, int offset, int extOffset,
230
                           String prefix, short ccSize)
231
    {
232
/*
233
        super();
234
        field_4_ccAdjust =  (byte)((ccSize == (short) 2)?1:0) ;
235
        fillFields(data,  size, offset);
236
        field_3_string = prefix + field_3_string;
237
        setCharCount();
238
*/
239
240
    }
241
242
243
    /**
244
     * NO OP
245
     */
246
247
    protected void validateSid(short id)
151
    {
248
    {
152
        field_1_charCount   = LittleEndian.getShort(data, 0);
153
        field_2_optionflags = data[ 2 ];
154
        if ((field_2_optionflags & 1) == 0)
155
        {
156
            field_3_string = new String(data, 3, getCharCount());
157
        }
158
        else
159
        {
160
            char[] array = new char[ getCharCount() ];
161
249
162
            for (int j = 0; j < array.length; j++)
250
        // included only for interface compliance
163
            {
164
                array[ j ] = ( char ) LittleEndian.getShort(data,
165
                                                            3 + (j * 2));
166
            }
167
            field_3_string = new String(array);
168
        }
169
    }
251
    }
170
252
253
171
    /**
254
    /**
172
     * get the number of characters in the string
255
     * get the number of characters in the string
173
     *
256
     *
Lines 286-291 Link Here
286
            .append(Integer.toHexString(getOptionFlags())).append("\n");
369
            .append(Integer.toHexString(getOptionFlags())).append("\n");
287
        buffer.append("    .string          = ").append(getString())
370
        buffer.append("    .string          = ").append(getString())
288
            .append("\n");
371
            .append("\n");
372
        buffer.append("    .extOffset       = ").append(field_4_ccAdjust).append("\n");
289
        buffer.append("[/UNICODESTRING]\n");
373
        buffer.append("[/UNICODESTRING]\n");
290
        return buffer.toString();
374
        return buffer.toString();
291
    }
375
    }
Lines 300-307 Link Here
300
        }
384
        }
301
385
302
        // byte[] retval = new byte[ 3 + (getString().length() * charsize)];
386
        // byte[] retval = new byte[ 3 + (getString().length() * charsize)];
303
        LittleEndian.putShort(data, 0 + offset, getCharCount());
387
        if ( field_4_ccAdjust == 1) {
304
        data[ 2 + offset ] = getOptionFlags();
388
          LittleEndian.putShort(data, 0 + offset, getCharCount());
389
        }
390
        else {
391
          data[ offset]= (byte)getCharCount();
392
        }
393
394
        data[ 1+ field_4_ccAdjust+ offset ] = getOptionFlags();
305
395
306
//        System.out.println("Unicode: We've got "+retval[2]+" for our option flag");
396
//        System.out.println("Unicode: We've got "+retval[2]+" for our option flag");
307
        try {
397
        try {
Lines 309-333 Link Here
309
String(getString().getBytes("Unicode"),"Unicode");
399
String(getString().getBytes("Unicode"),"Unicode");
310
            if (getOptionFlags() == 0)
400
            if (getOptionFlags() == 0)
311
            {
401
            {
312
                StringUtil.putCompressedUnicode(unicodeString, data, 0x3 +
402
                StringUtil.putCompressedUnicode(unicodeString, data,
313
offset);
403
                  0x2 + field_4_ccAdjust + offset);
314
            }
404
            }
315
            else
405
            else
316
            {
406
            {
317
                StringUtil.putUncompressedUnicode(unicodeString, data,
407
                StringUtil.putUncompressedUnicode(unicodeString, data,
318
                                                    0x3 + offset);
408
                  0x2 + field_4_ccAdjust + offset);
319
            }
409
            }
320
        }
410
        }
321
        catch (Exception e) {
411
        catch (Exception e) {
322
            if (getOptionFlags() == 0)
412
            if (getOptionFlags() == 0)
323
            {
413
            {
324
                StringUtil.putCompressedUnicode(getString(), data, 0x3 +
414
                StringUtil.putCompressedUnicode(getString(), data,
325
                                                offset);
415
                  0x2 + field_4_ccAdjust + offset);
326
            }
416
            }
327
            else
417
            else
328
            {
418
            {
329
                StringUtil.putUncompressedUnicode(getString(), data,
419
                StringUtil.putUncompressedUnicode(getString(), data,
330
                                                  0x3 + offset);
420
                  0x2 +field_4_ccAdjust + offset);
331
            }
421
            }
332
        }
422
        }
333
        return getRecordSize();
423
        return getRecordSize();
Lines 341-347 Link Here
341
    public int getRecordSize()
431
    public int getRecordSize()
342
    {
432
    {
343
        int charsize = isUncompressedUnicode() ? 2 : 1;
433
        int charsize = isUncompressedUnicode() ? 2 : 1;
344
        return 3 + (getString().length() * charsize);
434
        return  2 + field_4_ccAdjust +
435
        (getString().length() * charsize);
345
    }
436
    }
346
437
347
    public short getSid()
438
    public short getSid()
Lines 349-354 Link Here
349
        return this.sid;
440
        return this.sid;
350
    }
441
    }
351
442
443
    protected void fillFields(byte [] data, short size)
444
    {
445
        fillFields(data,  size, 0);
446
    }
447
352
    /**
448
    /**
353
     * called by the constructor, should set class level fields.  Should throw
449
     * called by the constructor, should set class level fields.  Should throw
354
     * runtime exception for bad/icomplete data.
450
     * runtime exception for bad/icomplete data.
Lines 360-365 Link Here
360
456
361
    protected void fillFields(byte [] data, short size, int offset)
457
    protected void fillFields(byte [] data, short size, int offset)
362
    {
458
    {
459
      fillFields(data, size,  offset,0);
460
/*
461
        if ( field_4_ccAdjust == 1) {// CharCount field is 2 bytes
462
          field_1_charCount   = LittleEndian.getShort(data, offset);
463
        }
464
        else {
465
          field_1_charCount   = (short)LittleEndian.getUnsignedByte(data, offset);
466
        }
467
468
        field_2_optionflags = data[ offset+ 1+field_4_ccAdjust ];
469
        if ((field_2_optionflags & 1) == 0)
470
        {
471
            field_3_string = new String(data,offset + 2+field_4_ccAdjust, getCharCount());
472
        }
473
        else
474
        {
475
            char[] array = new char[ getCharCount() ];
476
477
            for (int j = 0; j < array.length; j++)
478
            {
479
                array[ j ] = ( char ) LittleEndian.getShort(data,offset+
480
                                      2+field_4_ccAdjust + (j * 2));
481
            }
482
            field_3_string = new String(array);
483
        }
484
*/
485
    }
486
487
    /**
488
     * called by the constructor, should set class level fields.  Should throw
489
     * runtime exception for bad/icomplete data.
490
     *
491
     * @param data raw data
492
     * @param size size of data
493
     * @param offset of the records data (provided a big array of the file)
494
     * @param extOffset skip additional offset after header and before string start
495
     */
496
497
    protected void fillFields(byte [] data, short size, int offset,int extOffset)
498
    {
499
500
        field_2_optionflags = data[ offset+ 1+field_4_ccAdjust ];
501
        short charByteSize = (short)(size - stringHeaderOverhead());
502
503
        if ( field_4_ccAdjust == 1) {// CharCount field is 2 bytes
504
          field_1_charCount   = LittleEndian.getShort(data, offset);
505
        }
506
        else {
507
          field_1_charCount   = (short)LittleEndian.getUnsignedByte(data, offset);
508
        }
509
510
        if ((field_2_optionflags & 1) == 0) // 8bit char
511
        {
512
          if ( field_1_charCount  > charByteSize) { // string in this record is broken over continuation
513
            field_1_charCount  = charByteSize;
514
          }
515
          try {
516
            field_3_string = new String(data,offset + 2 + field_4_ccAdjust +
517
                              extOffset , getCharCount(),"ISO-8859-1");
518
          }
519
          catch (Exception e){
520
            e.printStackTrace();
521
          }
522
523
        }
524
        else // 16bit char
525
        {
526
            if ( field_1_charCount  >( charByteSize / 2)) { // string in this record is broken over continuation
527
              field_1_charCount  = (short) (charByteSize / 2) ;
528
            }
529
            char[] array = new char[ getCharCount() ];
530
531
            for (int j = 0; j < array.length; j++)
532
            {
533
                array[ j ] = ( char ) LittleEndian.getShort(data,offset+
534
                                      2+field_4_ccAdjust + extOffset  + (j * 2));
535
            }
536
            field_3_string = new String(array);
537
        }
363
    }
538
    }
364
539
365
    public int compareTo(Object obj)
540
    public int compareTo(Object obj)
Lines 380-392 Link Here
380
555
381
        if (isUncompressedUnicode())
556
        if (isUncompressedUnicode())
382
        {
557
        {
383
            int proposedStringLength = proposedBrokenLength - 3;
558
            int proposedStringLength = proposedBrokenLength - (2 + field_4_ccAdjust);
384
559
385
            if ((proposedStringLength % 2) == 1)
560
            if ((proposedStringLength % 2) == 1)
386
            {
561
            {
387
                proposedStringLength--;
562
                proposedStringLength--;
388
            }
563
            }
389
            rval = proposedStringLength + 3;
564
            rval = proposedStringLength + 2 + field_4_ccAdjust;
390
        }
565
        }
391
        return rval;
566
        return rval;
392
    }
567
    }
Lines 394-399 Link Here
394
    public boolean isExtendedText()
569
    public boolean isExtendedText()
395
    {
570
    {
396
        return (getOptionFlags() & EXT_BIT) != 0;
571
        return (getOptionFlags() & EXT_BIT) != 0;
572
    }
573
574
    private short stringHeaderOverhead()
575
    {
576
        return  (short)(2 + field_4_ccAdjust +
577
                 + ( ((field_2_optionflags & RICH_TEXT_BIT) != 0)?
578
                     LittleEndianConsts.SHORT_SIZE : 0 )
579
                 + (  ((field_2_optionflags & EXT_BIT) != 0)?
580
                     LittleEndianConsts.INT_SIZE : 0 )  );
397
    }
581
    }
398
582
399
}
583
}
(-)jakarta-poi/src/java/org/apache/poi/util/StringUtil.java (-7 / +7 lines)
Lines 74-80 Link Here
74
     */
74
     */
75
    private StringUtil() { }
75
    private StringUtil() { }
76
76
77
    
77
78
    /**
78
    /**
79
     *  given a byte array of 16-bit unicode characters, compress to 8-bit and
79
     *  given a byte array of 16-bit unicode characters, compress to 8-bit and
80
     *  return a string
80
     *  return a string
Lines 113-120 Link Here
113
        }
113
        }
114
        return new String(bstring);
114
        return new String(bstring);
115
    }
115
    }
116
    
116
117
    
117
118
118
119
    /**
119
    /**
120
     *  given a byte array of 16-bit unicode characters, compress to 8-bit and
120
     *  given a byte array of 16-bit unicode characters, compress to 8-bit and
Lines 227-238 Link Here
227
            char c = input.charAt(k);
227
            char c = input.charAt(k);
228
228
229
            output[offset + (2 * k)] = (byte) (c >> 8);
229
            output[offset + (2 * k)] = (byte) (c >> 8);
230
            output[offset + (2 * k)] = (byte) c;
230
            output[offset + (2 * k)+1] = (byte) c;
231
        }
231
        }
232
    }
232
    }
233
    
233
234
    
234
235
    
235
236
236
237
    /**
237
    /**
238
     *  Description of the Method
238
     *  Description of the Method
(-)jakarta-poi/src/testcases/org/apache/poi/hssf/record/TestSSTRecordSizeCalculator.java (-1 / +4 lines)
Lines 186-192 Link Here
186
        int offset = LittleEndianConsts.SHORT_SIZE;
186
        int offset = LittleEndianConsts.SHORT_SIZE;
187
        unicodeStringBuffer[offset++] = 0;
187
        unicodeStringBuffer[offset++] = 0;
188
        System.arraycopy( s.getBytes(), 0, unicodeStringBuffer, offset, s.length() );
188
        System.arraycopy( s.getBytes(), 0, unicodeStringBuffer, offset, s.length() );
189
        return new UnicodeString( UnicodeString.sid, (short) unicodeStringBuffer.length, unicodeStringBuffer );
189
//        return new UnicodeString( UnicodeString.sid, (short) unicodeStringBuffer.length, unicodeStringBuffer );
190
        return new UnicodeString(  unicodeStringBuffer,(short) unicodeStringBuffer.length,
191
                    UnicodeString.CHARCOUNTSIZE_DOUBLEBYTE     );
192
190
    }
193
    }
191
194
192
}
195
}
(-)jakarta-poi/src/testcases/org/apache/poi/hssf/record/TestSeriesTextRecord.java (-1 / +11 lines)
Lines 83-89 Link Here
83
            throws Exception
83
            throws Exception
84
    {
84
    {
85
        SeriesTextRecord record = new SeriesTextRecord((short)0x100d, (short)data.length, data);
85
        SeriesTextRecord record = new SeriesTextRecord((short)0x100d, (short)data.length, data);
86
        
86
87
87
88
        assertEquals( (short)0, record.getId());
88
        assertEquals( (short)0, record.getId());
89
89
Lines 118-122 Link Here
118
        assertEquals(recordBytes.length - 4, data.length);
118
        assertEquals(recordBytes.length - 4, data.length);
119
        for (int i = 0; i < data.length; i++)
119
        for (int i = 0; i < data.length; i++)
120
            assertEquals("At offset " + i, data[i], recordBytes[i+4]);
120
            assertEquals("At offset " + i, data[i], recordBytes[i+4]);
121
    }
122
    public static void main(String[] args)
123
        throws Exception
124
    {
125
      TestSeriesTextRecord t = new TestSeriesTextRecord ("test instance");
126
      //t.setUp();
127
      //t._test_file_path =
128
      //"J:\\poi-cvs\\jakarta-poi\\src\\testcases\\org\\apache\\poi\\HSSF\\data";
129
      t.testLoad();
130
      t.testStore();
121
    }
131
    }
122
}
132
}
(-)jakarta-poi/src/testcases/org/apache/poi/util/TestStringUtil.java (-3 / +8 lines)
Lines 103-109 Link Here
103
     * Test more complex form of getFromUnicode
103
     * Test more complex form of getFromUnicode
104
     */
104
     */
105
105
106
    public void testComplexGetFromUnicode()
106
  public void testComplexGetFromUnicode()
107
    {
107
    {
108
        byte[] test_data = new byte[ 32 ];
108
        byte[] test_data = new byte[ 32 ];
109
        int    index     = 0;
109
        int    index     = 0;
Lines 172-179 Link Here
172
            ( byte ) 'o', ( byte ) ' ', ( byte ) 'W', ( byte ) 'o',
172
            ( byte ) 'o', ( byte ) ' ', ( byte ) 'W', ( byte ) 'o',
173
            ( byte ) 'r', ( byte ) 'l', ( byte ) 'd', ( byte ) 0xAE
173
            ( byte ) 'r', ( byte ) 'l', ( byte ) 'd', ( byte ) 0xAE
174
        };
174
        };
175
        String input           = new String(expected_output);
175
        String input =new String("");
176
176
        try {
177
          input           = new String(expected_output,"ISO-8859-1");
178
        }
179
        catch (Exception e){
180
            e.printStackTrace();
181
        }
177
        StringUtil.putCompressedUnicode(input, output, 0);
182
        StringUtil.putCompressedUnicode(input, output, 0);
178
        for (int j = 0; j < expected_output.length; j++)
183
        for (int j = 0; j < expected_output.length; j++)
179
        {
184
        {

Return to bug 10976