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

(-)src/java/org/apache/poi/hssf/record/SSTRecord.java (-7 / +8 lines)
Lines 72-77 Link Here
72
 * @author Andrew C. Oliver (acoliver at apache dot org)
72
 * @author Andrew C. Oliver (acoliver at apache dot org)
73
 * @author Marc Johnson (mjohnson at apache dot org)
73
 * @author Marc Johnson (mjohnson at apache dot org)
74
 * @author Glen Stampoultzis (glens at apache.org)
74
 * @author Glen Stampoultzis (glens at apache.org)
75
 * @author Amichai Rothman
75
 *
76
 *
76
 * @see org.apache.poi.hssf.record.LabelSSTRecord
77
 * @see org.apache.poi.hssf.record.LabelSSTRecord
77
 * @see org.apache.poi.hssf.record.ContinueRecord
78
 * @see org.apache.poi.hssf.record.ContinueRecord
Lines 206-219 Link Here
206
     * Add a string and assert the encoding (8-bit or 16-bit) to be
207
     * Add a string and assert the encoding (8-bit or 16-bit) to be
207
     * used.
208
     * used.
208
     * <P>
209
     * <P>
209
     * USE THIS METHOD AT YOUR OWN RISK. IF YOU FORCE 8-BIT ENCODING,
210
     * Note: If 8-bit encoding is requested but the given string
210
     * YOU MAY CORRUPT YOUR STRING. IF YOU FORCE 16-BIT ENCODING AND
211
     *		 cannot be represented in 8-bit, 16-bit encoding will be used.
211
     * IT ISN'T NECESSARY, YOU WILL WASTE SPACE WHEN THIS RECORD IS
212
     *		 On the other hand, requesting 16-bit encoding for a string
212
     * WRITTEN OUT.
213
     *		 that can be represented using 8-bit, is wasteful of space.
213
     *
214
     *
214
     * @param string string to be added
215
     * @param string string to be added
215
     * @param useUTF16 if true, forces 16-bit encoding. If false,
216
     * @param useUTF16 if true, forces 16-bit encoding. If false,
216
     *                 forces 8-bit encoding
217
     *                 uses 8-bit encoding unless the string cannot
218
     *				   be represented by 8-bit characters.
217
     *
219
     *
218
     * @return the index of that string in the table
220
     * @return the index of that string in the table
219
     */
221
     */
Lines 228-235 Link Here
228
230
229
        ucs.setString( str );
231
        ucs.setString( str );
230
        ucs.setCharCount( (short) str.length() );
232
        ucs.setCharCount( (short) str.length() );
231
        ucs.setOptionFlags( (byte) ( useUTF16 ? 1
233
        ucs.setOptionFlags( (byte) ( useUTF16 || ucs.isUncompressedUnicode() ? 1 : 0 ) );
232
                : 0 ) );
233
        Integer integer = (Integer) field_3_strings.getKeyForValue( ucs );
234
        Integer integer = (Integer) field_3_strings.getKeyForValue( ucs );
234
235
235
        if ( integer != null )
236
        if ( integer != null )
(-)src/java/org/apache/poi/hssf/record/UnicodeString.java (-38 / +21 lines)
Lines 68-73 Link Here
68
 * @author  Andrew C. Oliver
68
 * @author  Andrew C. Oliver
69
 * @author Marc Johnson (mjohnson at apache dot org)
69
 * @author Marc Johnson (mjohnson at apache dot org)
70
 * @author Glen Stampoultzis (glens at apache.org)
70
 * @author Glen Stampoultzis (glens at apache.org)
71
 * @author Amichai Rothman
71
 * @version 2.0-pre
72
 * @version 2.0-pre
72
 */
73
 */
73
74
Lines 155-167 Link Here
155
        if ((field_2_optionflags & 1) == 0)
156
        if ((field_2_optionflags & 1) == 0)
156
        {
157
        {
157
            try {
158
            try {
158
                field_3_string = new String(data, 3, getCharCount(), 
159
                field_3_string = new String(data, 3, getCharCount(),
159
                                        StringUtil.getPreferredEncoding());
160
                                        StringUtil.getPreferredEncoding());
160
            } catch (UnsupportedEncodingException e) {
161
            } catch (UnsupportedEncodingException e) {
161
                // Extract the message out of our encoding
162
                // Extract the message out of our encoding
162
            	// error and then bubble a runtime exception.
163
            	// error and then bubble a runtime exception.
163
            	String errorMessage = e.getMessage();
164
            	String errorMessage = e.getMessage();
164
            	
165
165
            	// Make sure the message isn't null
166
            	// Make sure the message isn't null
166
            	if (errorMessage == null) {
167
            	if (errorMessage == null) {
167
            		errorMessage = e.toString();
168
            		errorMessage = e.toString();
Lines 264-269 Link Here
264
    public void setString(String string)
265
    public void setString(String string)
265
    {
266
    {
266
        field_3_string = string;
267
        field_3_string = string;
268
   		// check if using compressed unicode will ruin data
269
		try {
270
			if (!isUncompressedUnicode() &&
271
				!getString().equals(new String(getString().getBytes("ISO-8859-1"),"ISO-8859-1"))) {
272
					setOptionFlags((byte)(getOptionFlags() | 0x01)); // if so, make it uncompressed
273
			}
274
		} catch (UnsupportedEncodingException uee) {
275
			// should never happen, since "ISO-8859-1" support is required by the Java specification
276
		}
277
		// adjust length
267
        if (getCharCount() < field_3_string.length())
278
        if (getCharCount() < field_3_string.length())
268
        {
279
        {
269
            setCharCount();
280
            setCharCount();
Lines 306-353 Link Here
306
317
307
    public int serialize(int offset, byte [] data)
318
    public int serialize(int offset, byte [] data)
308
    {
319
    {
309
        int charsize = 1;
320
		// header fields
310
311
        if (getOptionFlags() == 1)
312
        {
313
            charsize = 2;
314
        }
315
316
        // byte[] retval = new byte[ 3 + (getString().length() * charsize)];
317
        LittleEndian.putShort(data, 0 + offset, getCharCount());
321
        LittleEndian.putShort(data, 0 + offset, getCharCount());
318
        data[ 2 + offset ] = getOptionFlags();
322
        data[ 2 + offset ] = getOptionFlags();
319
323
320
//        System.out.println("Unicode: We've got "+retval[2]+" for our option flag");
324
		// compressed/uncompressed string
321
        try {
325
		if (isUncompressedUnicode())
322
            String unicodeString = new
326
			StringUtil.putUnicodeLE(getString(), data, 0x3 + offset);
323
String(getString().getBytes("Unicode"),"Unicode");
327
		else
324
            if (getOptionFlags() == 0)
328
			StringUtil.putCompressedUnicode(getString(), data, 0x3 + offset);
325
            {
329
326
                StringUtil.putCompressedUnicode(unicodeString, data, 0x3 +
327
offset);
328
            }
329
            else
330
            {
331
                StringUtil.putUnicodeLE(unicodeString, data,
332
                                                    0x3 + offset);
333
            }
334
        }
335
        catch (Exception e) {
336
            if (getOptionFlags() == 0)
337
            {
338
                StringUtil.putCompressedUnicode(getString(), data, 0x3 +
339
                                                offset);
340
            }
341
            else
342
            {
343
                StringUtil.putUnicodeLE(getString(), data,
344
                                                  0x3 + offset);
345
            }
346
        }
347
        return getRecordSize();
330
        return getRecordSize();
348
    }
331
    }
349
332
350
    private boolean isUncompressedUnicode()
333
    protected boolean isUncompressedUnicode()
351
    {
334
    {
352
        return (getOptionFlags() & 0x01) == 1;
335
        return (getOptionFlags() & 0x01) == 1;
353
    }
336
    }

Return to bug 22957