Fixed a bug where a mutable section's format ID was written in the wrong byte order. Thanks to Bernd Freigang for pointing this out!

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353581 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Rainer Klute 2004-08-13 22:38:52 +00:00
parent e6d500243c
commit b46136d63b
3 changed files with 35 additions and 7 deletions

View File

@ -95,6 +95,20 @@ public class ClassID
/**
* <p>Sets the bytes making out the class ID.</p>
*
* @param bytes The bytes making out the class ID in big-endian format. They
* are copied without their order being changed.
*/
public void setBytes(final byte[] bytes)
{
for (int i = 0; i < this.bytes.length; i++)
this.bytes[i] = bytes[i];
}
/** /**
* <p>Reads the class ID's value from a byte array by turning * <p>Reads the class ID's value from a byte array by turning
* little-endian into big-endian.</p> * little-endian into big-endian.</p>
@ -134,7 +148,7 @@ public class ClassID
/** /**
* <p>Writes the class ID to a byte array in the * <p>Writes the class ID to a byte array in the
* little-endian.</p> * little-endian format.</p>
* *
* @param dst The byte array to write to. * @param dst The byte array to write to.
* *

View File

@ -127,7 +127,13 @@ public class MutableSection extends Section
*/ */
public void setFormatID(final byte[] formatID) public void setFormatID(final byte[] formatID)
{ {
setFormatID(new ClassID(formatID, 0)); ClassID fid = getFormatID();
if (fid == null)
{
fid = new ClassID();
setFormatID(fid);
}
fid.setBytes(formatID);
} }

View File

@ -1,4 +1,3 @@
/* ==================================================================== /* ====================================================================
Copyright 2002-2004 Apache Software Foundation Copyright 2002-2004 Apache Software Foundation
@ -38,6 +37,7 @@ import java.util.Map;
import junit.framework.Assert; import junit.framework.Assert;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.hpsf.ClassID;
import org.apache.poi.hpsf.Constants; import org.apache.poi.hpsf.Constants;
import org.apache.poi.hpsf.HPSFRuntimeException; import org.apache.poi.hpsf.HPSFRuntimeException;
import org.apache.poi.hpsf.IllegalPropertySetDataException; import org.apache.poi.hpsf.IllegalPropertySetDataException;
@ -259,6 +259,8 @@ public class TestWrite extends TestCase
SummaryInformation.DEFAULT_STREAM_NAME); SummaryInformation.DEFAULT_STREAM_NAME);
r.read(new FileInputStream(filename)); r.read(new FileInputStream(filename));
Assert.assertNotNull(psa[0]); Assert.assertNotNull(psa[0]);
Assert.assertTrue(psa[0].isSummaryInformation());
final Section s = (Section) (psa[0].getSections().get(0)); final Section s = (Section) (psa[0].getSections().get(0));
Object p1 = s.getProperty(PropertyIDMap.PID_AUTHOR); Object p1 = s.getProperty(PropertyIDMap.PID_AUTHOR);
Object p2 = s.getProperty(PropertyIDMap.PID_TITLE); Object p2 = s.getProperty(PropertyIDMap.PID_TITLE);
@ -293,9 +295,9 @@ public class TestWrite extends TestCase
final MutablePropertySet ps = new MutablePropertySet(); final MutablePropertySet ps = new MutablePropertySet();
ps.clearSections(); ps.clearSections();
final byte[] formatID = final ClassID formatID = new ClassID();
new byte[]{0, 1, 2, 3, 4, 5, 6, 7, formatID.setBytes(new byte[]{0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15}; 8, 9, 10, 11, 12, 13, 14, 15});
final MutableSection s1 = new MutableSection(); final MutableSection s1 = new MutableSection();
s1.setFormatID(formatID); s1.setFormatID(formatID);
s1.setProperty(2, SECTION1); s1.setProperty(2, SECTION1);
@ -336,6 +338,7 @@ public class TestWrite extends TestCase
r.read(new FileInputStream(filename)); r.read(new FileInputStream(filename));
Assert.assertNotNull(psa[0]); Assert.assertNotNull(psa[0]);
Section s = (Section) (psa[0].getSections().get(0)); Section s = (Section) (psa[0].getSections().get(0));
assertEquals(s.getFormatID(), formatID);
Object p = s.getProperty(2); Object p = s.getProperty(2);
Assert.assertEquals(SECTION1, p); Assert.assertEquals(SECTION1, p);
s = (Section) (psa[0].getSections().get(1)); s = (Section) (psa[0].getSections().get(1));
@ -529,6 +532,7 @@ public class TestWrite extends TestCase
byte[] bytes = out.toByteArray(); byte[] bytes = out.toByteArray();
PropertySet psr = new PropertySet(bytes); PropertySet psr = new PropertySet(bytes);
assertTrue(psr.isSummaryInformation());
Section sr = (Section) psr.getSections().get(0); Section sr = (Section) psr.getSections().get(0);
String title = (String) sr.getProperty(PropertyIDMap.PID_TITLE); String title = (String) sr.getProperty(PropertyIDMap.PID_TITLE);
assertEquals(TITLE, title); assertEquals(TITLE, title);
@ -709,7 +713,7 @@ public class TestWrite extends TestCase
/* Compare the property set stream with the corresponding one /* Compare the property set stream with the corresponding one
* from the origin file and check whether they are equal. */ * from the origin file and check whether they are equal. */
assertEquals("Equality for file "+f.getName(),ps1, ps2); assertEquals("Equality for file " + f.getName(), ps1, ps2);
} }
} }
catch (Exception ex) catch (Exception ex)
@ -755,6 +759,10 @@ public class TestWrite extends TestCase
final InputStream in = new ByteArrayInputStream(bytes); final InputStream in = new ByteArrayInputStream(bytes);
final PropertySet ps2 = PropertySetFactory.create(in); final PropertySet ps2 = PropertySetFactory.create(in);
/* Check if the result is a DocumentSummaryInformation stream, as
* specified. */
assertTrue(ps2.isDocumentSummaryInformation());
/* Compare the property set stream with the corresponding one /* Compare the property set stream with the corresponding one
* from the origin file and check whether they are equal. */ * from the origin file and check whether they are equal. */
assertEquals(ps1, ps2); assertEquals(ps1, ps2);