use of org.apache.poi.hssf.record.RecordInputStream.LeftoverDataException in project poi by apache.
the class BiffViewer method createRecords.
/**
* Create an array of records from an input stream
*
* @param is the InputStream from which the records will be obtained
* @param ps the PrintWriter to output the record data
* @param recListener the record listener to notify about read records
* @param dumpInterpretedRecords if {@code true}, the read records will be written to the PrintWriter
*
* @return an array of Records created from the InputStream
* @exception org.apache.poi.util.RecordFormatException on error processing the InputStream
*/
public static Record[] createRecords(InputStream is, PrintWriter ps, BiffRecordListener recListener, boolean dumpInterpretedRecords) throws org.apache.poi.util.RecordFormatException {
List<Record> temp = new ArrayList<Record>();
RecordInputStream recStream = new RecordInputStream(is);
while (true) {
boolean hasNext;
try {
hasNext = recStream.hasNextRecord();
} catch (LeftoverDataException e) {
logger.log(POILogger.ERROR, "Discarding " + recStream.remaining() + " bytes and continuing", e);
recStream.readRemainder();
hasNext = recStream.hasNextRecord();
}
if (!hasNext) {
break;
}
recStream.nextRecord();
if (recStream.getSid() == 0) {
continue;
}
Record record;
if (dumpInterpretedRecords) {
record = createRecord(recStream);
if (record.getSid() == ContinueRecord.sid) {
continue;
}
temp.add(record);
for (String header : recListener.getRecentHeaders()) {
ps.println(header);
}
ps.print(record);
} else {
recStream.readRemainder();
}
ps.println();
}
Record[] result = new Record[temp.size()];
temp.toArray(result);
return result;
}
use of org.apache.poi.hssf.record.RecordInputStream.LeftoverDataException in project poi by apache.
the class TestBoolErrRecord method testOooBadFormat_bug47479.
/**
* Bugzilla 47479 was due to an apparent error in OOO which (as of version 3.0.1)
* writes the <i>value</i> field of BOOLERR records as 2 bytes instead of 1.<br/>
* Coincidentally, the extra byte written is zero, which is exactly the value
* required by the <i>isError</i> field. This probably why Excel seems to have
* no problem. OOO does not have the same bug for error values (which wouldn't
* work by the same coincidence).
*/
public void testOooBadFormat_bug47479() {
byte[] data = HexRead.readFromString(// sid, size
"05 02 09 00 " + // row, col, xfIndex
"00 00 00 00 0F 00 " + // extra 00 byte here
"01 00 00 ");
RecordInputStream in = TestcaseRecordInputStream.create(data);
BoolErrRecord ber = new BoolErrRecord(in);
boolean hasMore;
try {
hasMore = in.hasNextRecord();
} catch (LeftoverDataException e) {
if ("Initialisation of record 0x205 left 1 bytes remaining still to be read.".equals(e.getMessage())) {
throw new AssertionFailedError("Identified bug 47479");
}
throw e;
}
assertFalse(hasMore);
assertTrue(ber.isBoolean());
assertEquals(true, ber.getBooleanValue());
// Check that the record re-serializes correctly
byte[] outData = ber.serialize();
byte[] expData = HexRead.readFromString("05 02 08 00 " + "00 00 00 00 0F 00 " + // normal number of data bytes
"01 00 ");
assertArrayEquals(expData, outData);
}
Aggregations