ASF Bugzilla – Attachment 17643 Details for
Bug 38607
POI corrupts sheets containing EndSubRecord due to bug in RecordInputStream
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Updated fix.
recordInputStreamFix2.patch (text/plain), 5.80 KB, created by
Matt Hillsdon
on 2006-02-10 18:32:36 UTC
(
hide
)
Description:
Updated fix.
Filename:
MIME Type:
Creator:
Matt Hillsdon
Created:
2006-02-10 18:32:36 UTC
Size:
5.80 KB
patch
obsolete
>Index: src/java/org/apache/poi/hssf/record/RecordInputStream.java >=================================================================== >--- src/java/org/apache/poi/hssf/record/RecordInputStream.java (revision 376246) >+++ src/java/org/apache/poi/hssf/record/RecordInputStream.java (working copy) >@@ -18,11 +18,11 @@ > > package org.apache.poi.hssf.record; > >-import org.apache.poi.util.LittleEndian; >- >+import java.io.ByteArrayOutputStream; > import java.io.IOException; > import java.io.InputStream; >-import java.io.ByteArrayOutputStream; >+ >+import org.apache.poi.util.LittleEndian; > > /** > * Title: Record Input Stream<P> >@@ -37,9 +37,9 @@ > public final static short MAX_RECORD_DATA_SIZE = 8224; > > private InputStream in; >- protected short currentSid; >+ protected short currentSid = -1; > protected short currentLength = -1; >- protected short nextSid = -1; >+ protected short nextSid; > > protected byte[] data = new byte[MAX_RECORD_DATA_SIZE]; > protected short recordOffset; >@@ -47,10 +47,23 @@ > > private boolean autoContinue = true; > >+ /** currentSid with this value indicates EOF. */ >+ private final short eof; >+ > public RecordInputStream(InputStream in) throws RecordFormatException { >+ this(in, (short) 0); >+ } >+ >+ /** >+ * Constructs. >+ * @param in The input stream. >+ * @param eof The sid that indicates end of stream. >+ */ >+ public RecordInputStream(InputStream in, short eof) throws RecordFormatException { > this.in = in; >+ this.eof = eof; > try { >- nextSid = LittleEndian.readShort(in); >+ nextSid = readLEShortOrEOF(); > //Dont increment the pos just yet (technically we are at the start of > //the record stream until nextRecord is called). > } catch (IOException ex) { >@@ -85,7 +98,7 @@ > } > > public boolean hasNextRecord() { >- return (nextSid != 0); >+ return nextSid != eof; > } > > /** Moves to the next record in the stream. >@@ -109,12 +122,21 @@ > in.read(data, 0, currentLength); > > //Read the Sid of the next record >- nextSid = LittleEndian.readShort(in); >+ nextSid = readLEShortOrEOF(); > } catch (IOException ex) { > throw new RecordFormatException("Error reading bytes"); > } > } > >+ private short readLEShortOrEOF() throws IOException { >+ int firstByte = in.read(); >+ if (-1 == firstByte) { >+ return eof; >+ } >+ int secondByte = in.read(); >+ return (short) ((secondByte << 8) | firstByte); >+ } >+ > public void setAutoContinue(boolean enable) { > this.autoContinue = enable; > } >Index: src/java/org/apache/poi/hssf/record/ObjRecord.java >=================================================================== >--- src/java/org/apache/poi/hssf/record/ObjRecord.java (revision 376246) >+++ src/java/org/apache/poi/hssf/record/ObjRecord.java (working copy) >@@ -81,7 +81,8 @@ > //Check if this can be continued, if so then the > //following wont work properly > byte[] subRecordData = in.readRemainder(); >- RecordInputStream subRecStream = new RecordInputStream(new ByteArrayInputStream(subRecordData)); >+ // We stop when the stream runs out of data. >+ RecordInputStream subRecStream = new RecordInputStream(new ByteArrayInputStream(subRecordData), (short) -1); > while(subRecStream.hasNextRecord()) { > subRecStream.nextRecord(); > Record subRecord = SubRecord.createSubRecord(subRecStream); >Index: src/testcases/org/apache/poi/hssf/record/TestRecordInputStream.java >=================================================================== >--- src/testcases/org/apache/poi/hssf/record/TestRecordInputStream.java (revision 0) >+++ src/testcases/org/apache/poi/hssf/record/TestRecordInputStream.java (revision 0) >@@ -0,0 +1,60 @@ >+ >+/* ==================================================================== >+ Copyright 2002-2004 Apache Software Foundation >+ >+ Licensed under the Apache License, Version 2.0 (the "License"); >+ you may not use this file except in compliance with the License. >+ You may obtain a copy of the License at >+ >+ http://www.apache.org/licenses/LICENSE-2.0 >+ >+ Unless required by applicable law or agreed to in writing, software >+ distributed under the License is distributed on an "AS IS" BASIS, >+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. >+ See the License for the specific language governing permissions and >+ limitations under the License. >+==================================================================== */ >+ >+package org.apache.poi.hssf.record; >+ >+import java.io.ByteArrayInputStream; >+ >+import junit.framework.TestCase; >+ >+/** >+ * Tests for {@link org.apache.poi.hssf.record.RecordInputStream}. >+ * >+ * @author Andrzej Cichocki (arc at decisionsoft.com) >+ * @author Matt Hillsdon (mth at decisionsoft.com) >+ */ >+public class TestRecordInputStream extends TestCase { >+ >+ public TestRecordInputStream(String name) { >+ super(name); >+ } >+ >+ public void testEmptyStreamHasNoRecords() { >+ assertFalse(new RecordInputStream(new ByteArrayInputStream(new byte[0])).hasNextRecord()); >+ assertFalse(new RecordInputStream(new ByteArrayInputStream(new byte[0]), (short) -1).hasNextRecord()); >+ } >+ >+ public void testRecordWithZeroSidIsNotIgnored() { >+ byte[] data = new byte[] { >+ 0x15, 0, 18, 0, // CommonObjectDataSubRecord >+ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18, // 18 bytes record data >+ 0,0,0,0 // EndSubRecord has a zero sid and size. >+ }; >+ RecordInputStream ris = new RecordInputStream(new ByteArrayInputStream(data), (short) -1); >+ assertTrue(ris.hasNextRecord()); >+ ris.nextRecord(); >+ assertEquals(0x15, ris.getSid()); >+ assertEquals(18, ris.readRemainder().length); >+ assertTrue(ris.hasNextRecord()); >+ ris.nextRecord(); >+ assertEquals(0, ris.getSid()); >+ assertEquals(0, ris.readRemainder().length); >+ assertFalse(ris.hasNextRecord()); >+ } >+ >+} >+
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 38607
:
17639
|
17640
| 17643