Search in sources :

Example 1 with LeftoverDataException

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;
}
Also used : LeftoverDataException(org.apache.poi.hssf.record.RecordInputStream.LeftoverDataException) ArrayList(java.util.ArrayList) WindowProtectRecord(org.apache.poi.hssf.record.WindowProtectRecord) DrawingSelectionRecord(org.apache.poi.hssf.record.DrawingSelectionRecord) GutsRecord(org.apache.poi.hssf.record.GutsRecord) MergeCellsRecord(org.apache.poi.hssf.record.MergeCellsRecord) PasswordRev4Record(org.apache.poi.hssf.record.PasswordRev4Record) ArrayRecord(org.apache.poi.hssf.record.ArrayRecord) DefaultColWidthRecord(org.apache.poi.hssf.record.DefaultColWidthRecord) DateWindow1904Record(org.apache.poi.hssf.record.DateWindow1904Record) FormatRecord(org.apache.poi.hssf.record.FormatRecord) FooterRecord(org.apache.poi.hssf.record.FooterRecord) DatRecord(org.apache.poi.hssf.record.chart.DatRecord) ContinueRecord(org.apache.poi.hssf.record.ContinueRecord) SCLRecord(org.apache.poi.hssf.record.SCLRecord) ChartRecord(org.apache.poi.hssf.record.chart.ChartRecord) PageItemRecord(org.apache.poi.hssf.record.pivottable.PageItemRecord) TableStylesRecord(org.apache.poi.hssf.record.TableStylesRecord) BookBoolRecord(org.apache.poi.hssf.record.BookBoolRecord) LegendRecord(org.apache.poi.hssf.record.chart.LegendRecord) StyleRecord(org.apache.poi.hssf.record.StyleRecord) AreaRecord(org.apache.poi.hssf.record.chart.AreaRecord) RecalcIdRecord(org.apache.poi.hssf.record.RecalcIdRecord) SeriesTextRecord(org.apache.poi.hssf.record.chart.SeriesTextRecord) HideObjRecord(org.apache.poi.hssf.record.HideObjRecord) DefaultDataLabelTextPropertiesRecord(org.apache.poi.hssf.record.chart.DefaultDataLabelTextPropertiesRecord) PrintSetupRecord(org.apache.poi.hssf.record.PrintSetupRecord) Record(org.apache.poi.hssf.record.Record) HyperlinkRecord(org.apache.poi.hssf.record.HyperlinkRecord) PrintHeadersRecord(org.apache.poi.hssf.record.PrintHeadersRecord) MMSRecord(org.apache.poi.hssf.record.MMSRecord) PrecisionRecord(org.apache.poi.hssf.record.PrecisionRecord) DBCellRecord(org.apache.poi.hssf.record.DBCellRecord) BeginRecord(org.apache.poi.hssf.record.chart.BeginRecord) TopMarginRecord(org.apache.poi.hssf.record.TopMarginRecord) RowRecord(org.apache.poi.hssf.record.RowRecord) FnGroupCountRecord(org.apache.poi.hssf.record.FnGroupCountRecord) HorizontalPageBreakRecord(org.apache.poi.hssf.record.HorizontalPageBreakRecord) EndRecord(org.apache.poi.hssf.record.chart.EndRecord) CalcCountRecord(org.apache.poi.hssf.record.CalcCountRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) PlotGrowthRecord(org.apache.poi.hssf.record.chart.PlotGrowthRecord) UnitsRecord(org.apache.poi.hssf.record.chart.UnitsRecord) ObjectLinkRecord(org.apache.poi.hssf.record.chart.ObjectLinkRecord) HeaderRecord(org.apache.poi.hssf.record.HeaderRecord) CodepageRecord(org.apache.poi.hssf.record.CodepageRecord) IterationRecord(org.apache.poi.hssf.record.IterationRecord) SeriesRecord(org.apache.poi.hssf.record.chart.SeriesRecord) ExtendedPivotTableViewFieldsRecord(org.apache.poi.hssf.record.pivottable.ExtendedPivotTableViewFieldsRecord) FeatHdrRecord(org.apache.poi.hssf.record.FeatHdrRecord) LabelSSTRecord(org.apache.poi.hssf.record.LabelSSTRecord) RKRecord(org.apache.poi.hssf.record.RKRecord) CatLabRecord(org.apache.poi.hssf.record.chart.CatLabRecord) MulRKRecord(org.apache.poi.hssf.record.MulRKRecord) ChartEndBlockRecord(org.apache.poi.hssf.record.chart.ChartEndBlockRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) FilePassRecord(org.apache.poi.hssf.record.FilePassRecord) FontRecord(org.apache.poi.hssf.record.FontRecord) ViewSourceRecord(org.apache.poi.hssf.record.pivottable.ViewSourceRecord) DataFormatRecord(org.apache.poi.hssf.record.chart.DataFormatRecord) PaneRecord(org.apache.poi.hssf.record.PaneRecord) InterfaceEndRecord(org.apache.poi.hssf.record.InterfaceEndRecord) NumberRecord(org.apache.poi.hssf.record.NumberRecord) FontBasisRecord(org.apache.poi.hssf.record.chart.FontBasisRecord) ChartStartBlockRecord(org.apache.poi.hssf.record.chart.ChartStartBlockRecord) PasswordRecord(org.apache.poi.hssf.record.PasswordRecord) SeriesIndexRecord(org.apache.poi.hssf.record.chart.SeriesIndexRecord) DConRefRecord(org.apache.poi.hssf.record.DConRefRecord) PaletteRecord(org.apache.poi.hssf.record.PaletteRecord) ExtSSTRecord(org.apache.poi.hssf.record.ExtSSTRecord) SheetPropertiesRecord(org.apache.poi.hssf.record.chart.SheetPropertiesRecord) WSBoolRecord(org.apache.poi.hssf.record.WSBoolRecord) BottomMarginRecord(org.apache.poi.hssf.record.BottomMarginRecord) RightMarginRecord(org.apache.poi.hssf.record.RightMarginRecord) TableRecord(org.apache.poi.hssf.record.TableRecord) StreamIDRecord(org.apache.poi.hssf.record.pivottable.StreamIDRecord) CFHeader12Record(org.apache.poi.hssf.record.CFHeader12Record) BlankRecord(org.apache.poi.hssf.record.BlankRecord) AutoFilterInfoRecord(org.apache.poi.hssf.record.AutoFilterInfoRecord) DVRecord(org.apache.poi.hssf.record.DVRecord) RefreshAllRecord(org.apache.poi.hssf.record.RefreshAllRecord) LeftMarginRecord(org.apache.poi.hssf.record.LeftMarginRecord) StringRecord(org.apache.poi.hssf.record.StringRecord) CFRuleRecord(org.apache.poi.hssf.record.CFRuleRecord) UnknownRecord(org.apache.poi.hssf.record.UnknownRecord) VCenterRecord(org.apache.poi.hssf.record.VCenterRecord) AxisUsedRecord(org.apache.poi.hssf.record.chart.AxisUsedRecord) DefaultRowHeightRecord(org.apache.poi.hssf.record.DefaultRowHeightRecord) GridsetRecord(org.apache.poi.hssf.record.GridsetRecord) LinkedDataRecord(org.apache.poi.hssf.record.chart.LinkedDataRecord) VerticalPageBreakRecord(org.apache.poi.hssf.record.VerticalPageBreakRecord) ChartFRTInfoRecord(org.apache.poi.hssf.record.chart.ChartFRTInfoRecord) ValueRangeRecord(org.apache.poi.hssf.record.chart.ValueRangeRecord) ChartStartObjectRecord(org.apache.poi.hssf.record.chart.ChartStartObjectRecord) NoteRecord(org.apache.poi.hssf.record.NoteRecord) ViewFieldsRecord(org.apache.poi.hssf.record.pivottable.ViewFieldsRecord) CFHeaderRecord(org.apache.poi.hssf.record.CFHeaderRecord) AxisParentRecord(org.apache.poi.hssf.record.chart.AxisParentRecord) BoolErrRecord(org.apache.poi.hssf.record.BoolErrRecord) SeriesToChartGroupRecord(org.apache.poi.hssf.record.chart.SeriesToChartGroupRecord) ObjRecord(org.apache.poi.hssf.record.ObjRecord) ProtectionRev4Record(org.apache.poi.hssf.record.ProtectionRev4Record) EOFRecord(org.apache.poi.hssf.record.EOFRecord) CFRule12Record(org.apache.poi.hssf.record.CFRule12Record) AreaFormatRecord(org.apache.poi.hssf.record.chart.AreaFormatRecord) RefModeRecord(org.apache.poi.hssf.record.RefModeRecord) BoundSheetRecord(org.apache.poi.hssf.record.BoundSheetRecord) CountryRecord(org.apache.poi.hssf.record.CountryRecord) HCenterRecord(org.apache.poi.hssf.record.HCenterRecord) NameCommentRecord(org.apache.poi.hssf.record.NameCommentRecord) DrawingGroupRecord(org.apache.poi.hssf.record.DrawingGroupRecord) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) BackupRecord(org.apache.poi.hssf.record.BackupRecord) ColumnInfoRecord(org.apache.poi.hssf.record.ColumnInfoRecord) PrintGridlinesRecord(org.apache.poi.hssf.record.PrintGridlinesRecord) ExternSheetRecord(org.apache.poi.hssf.record.ExternSheetRecord) WriteAccessRecord(org.apache.poi.hssf.record.WriteAccessRecord) DataItemRecord(org.apache.poi.hssf.record.pivottable.DataItemRecord) CalcModeRecord(org.apache.poi.hssf.record.CalcModeRecord) ExternalNameRecord(org.apache.poi.hssf.record.ExternalNameRecord) SelectionRecord(org.apache.poi.hssf.record.SelectionRecord) BarRecord(org.apache.poi.hssf.record.chart.BarRecord) ChartFormatRecord(org.apache.poi.hssf.record.chart.ChartFormatRecord) TextObjectRecord(org.apache.poi.hssf.record.TextObjectRecord) NameRecord(org.apache.poi.hssf.record.NameRecord) AxisLineFormatRecord(org.apache.poi.hssf.record.chart.AxisLineFormatRecord) SeriesListRecord(org.apache.poi.hssf.record.chart.SeriesListRecord) IndexRecord(org.apache.poi.hssf.record.IndexRecord) CategorySeriesAxisRecord(org.apache.poi.hssf.record.chart.CategorySeriesAxisRecord) FrameRecord(org.apache.poi.hssf.record.chart.FrameRecord) PlotAreaRecord(org.apache.poi.hssf.record.chart.PlotAreaRecord) LabelRecord(org.apache.poi.hssf.record.LabelRecord) DVALRecord(org.apache.poi.hssf.record.DVALRecord) UseSelFSRecord(org.apache.poi.hssf.record.UseSelFSRecord) WindowOneRecord(org.apache.poi.hssf.record.WindowOneRecord) DeltaRecord(org.apache.poi.hssf.record.DeltaRecord) WriteProtectRecord(org.apache.poi.hssf.record.WriteProtectRecord) ProtectRecord(org.apache.poi.hssf.record.ProtectRecord) TextRecord(org.apache.poi.hssf.record.chart.TextRecord) InterfaceHdrRecord(org.apache.poi.hssf.record.InterfaceHdrRecord) DSFRecord(org.apache.poi.hssf.record.DSFRecord) SupBookRecord(org.apache.poi.hssf.record.SupBookRecord) SSTRecord(org.apache.poi.hssf.record.SSTRecord) ChartEndObjectRecord(org.apache.poi.hssf.record.chart.ChartEndObjectRecord) TabIdRecord(org.apache.poi.hssf.record.TabIdRecord) AxisOptionsRecord(org.apache.poi.hssf.record.chart.AxisOptionsRecord) FeatRecord(org.apache.poi.hssf.record.FeatRecord) ViewDefinitionRecord(org.apache.poi.hssf.record.pivottable.ViewDefinitionRecord) TickRecord(org.apache.poi.hssf.record.chart.TickRecord) SaveRecalcRecord(org.apache.poi.hssf.record.SaveRecalcRecord) ExtendedFormatRecord(org.apache.poi.hssf.record.ExtendedFormatRecord) AxisRecord(org.apache.poi.hssf.record.chart.AxisRecord) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) FileSharingRecord(org.apache.poi.hssf.record.FileSharingRecord) UncalcedRecord(org.apache.poi.hssf.record.UncalcedRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) FontIndexRecord(org.apache.poi.hssf.record.chart.FontIndexRecord) LineFormatRecord(org.apache.poi.hssf.record.chart.LineFormatRecord) RecordInputStream(org.apache.poi.hssf.record.RecordInputStream)

Example 2 with LeftoverDataException

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);
}
Also used : LeftoverDataException(org.apache.poi.hssf.record.RecordInputStream.LeftoverDataException) AssertionFailedError(junit.framework.AssertionFailedError)

Aggregations

ArrayList (java.util.ArrayList)1 AssertionFailedError (junit.framework.AssertionFailedError)1 ArrayRecord (org.apache.poi.hssf.record.ArrayRecord)1 AutoFilterInfoRecord (org.apache.poi.hssf.record.AutoFilterInfoRecord)1 BOFRecord (org.apache.poi.hssf.record.BOFRecord)1 BackupRecord (org.apache.poi.hssf.record.BackupRecord)1 BlankRecord (org.apache.poi.hssf.record.BlankRecord)1 BookBoolRecord (org.apache.poi.hssf.record.BookBoolRecord)1 BoolErrRecord (org.apache.poi.hssf.record.BoolErrRecord)1 BottomMarginRecord (org.apache.poi.hssf.record.BottomMarginRecord)1 BoundSheetRecord (org.apache.poi.hssf.record.BoundSheetRecord)1 CFHeader12Record (org.apache.poi.hssf.record.CFHeader12Record)1 CFHeaderRecord (org.apache.poi.hssf.record.CFHeaderRecord)1 CFRule12Record (org.apache.poi.hssf.record.CFRule12Record)1 CFRuleRecord (org.apache.poi.hssf.record.CFRuleRecord)1 CalcCountRecord (org.apache.poi.hssf.record.CalcCountRecord)1 CalcModeRecord (org.apache.poi.hssf.record.CalcModeRecord)1 CodepageRecord (org.apache.poi.hssf.record.CodepageRecord)1 ColumnInfoRecord (org.apache.poi.hssf.record.ColumnInfoRecord)1 ContinueRecord (org.apache.poi.hssf.record.ContinueRecord)1