use of org.apache.poi.hssf.record.RecordInputStream 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 in project poi by apache.
the class RecordLister method run.
public void run() throws IOException {
NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(file), true);
try {
InputStream din = BiffViewer.getPOIFSInputStream(fs);
try {
RecordInputStream rinp = new RecordInputStream(din);
while (rinp.hasNextRecord()) {
int sid = rinp.getNextSid();
rinp.nextRecord();
int size = rinp.available();
Class<? extends Record> clz = RecordFactory.getRecordClass(sid);
System.out.print(formatSID(sid) + " - " + formatSize(size) + " bytes");
if (clz != null) {
System.out.print(" \t");
System.out.print(clz.getName().replace("org.apache.poi.hssf.record.", ""));
}
System.out.println();
byte[] data = rinp.readRemainder();
if (data.length > 0) {
System.out.print(" ");
System.out.println(formatData(data));
}
}
} finally {
din.close();
}
} finally {
fs.close();
}
}
use of org.apache.poi.hssf.record.RecordInputStream in project poi by apache.
the class TestViewFieldsRecord method testUnicodeFlag_bug46693.
public void testUnicodeFlag_bug46693() {
byte[] data = HexRead.readFromString("01 00 01 00 01 00 04 00 05 00 00 6D 61 72 63 6F");
RecordInputStream in = TestcaseRecordInputStream.create(ViewFieldsRecord.sid, data);
ViewFieldsRecord rec = new ViewFieldsRecord(in);
if (in.remaining() == 1) {
throw new AssertionFailedError("Identified bug 46693b");
}
assertEquals(0, in.remaining());
assertEquals(4 + data.length, rec.getRecordSize());
}
use of org.apache.poi.hssf.record.RecordInputStream in project poi by apache.
the class OldExcelExtractor method open.
private void open(InputStream biffStream) throws IOException {
BufferedInputStream bis = (biffStream instanceof BufferedInputStream) ? (BufferedInputStream) biffStream : new BufferedInputStream(biffStream, 8);
if (NPOIFSFileSystem.hasPOIFSHeader(bis)) {
NPOIFSFileSystem poifs = new NPOIFSFileSystem(bis);
try {
open(poifs);
} finally {
poifs.close();
}
} else {
ris = new RecordInputStream(bis);
toClose = bis;
prepare();
}
}
use of org.apache.poi.hssf.record.RecordInputStream in project poi by apache.
the class TestSeriesTextRecord method testReserializeLongTitle.
public void testReserializeLongTitle() {
// Hex dump from bug 45784 attachment 22560 streamOffset=0x0CD1
byte[] data = HexRead.readFromString("00 00, " + "82 " + "01 " + "50 00 6C 00 61 00 73 00 6D 00 61 00 20 00 4C 00 " + "65 00 76 00 65 00 6C 00 73 00 20 00 6F 00 66 00 " + "20 00 4C 00 2D 00 30 00 30 00 30 00 31 00 31 00 " + "31 00 32 00 32 00 32 00 2D 00 33 00 33 00 33 00 " + "58 00 34 00 34 00 34 00 20 00 69 00 6E 00 20 00 " + "53 00 44 00 20 00 72 00 61 00 74 00 0A 00 50 00 " + "4F 00 20 00 33 00 2E 00 30 00 20 00 6D 00 67 00 " + "2F 00 6B 00 67 00 20 00 28 00 35 00 2E 00 30 00 " + "20 00 6D 00 4C 00 2F 00 6B 00 67 00 29 00 20 00 " + "69 00 6E 00 20 00 4D 00 65 00 74 00 68 00 6F 00 " + "63 00 65 00 6C 00 0A 00 49 00 56 00 20 00 30 00 " + "2E 00 35 00 20 00 6D 00 67 00 2F 00 6B 00 67 00 " + "20 00 28 00 31 00 2E 00 30 00 20 00 6D 00 4C 00 " + "2F 00 6B 00 67 00 29 00 20 00 69 00 6E 00 20 00 " + "36 00 30 00 25 00 20 00 50 00 45 00 47 00 20 00 " + "32 00 30 00 30 00 0A 00 46 00 20 00 3D 00 61 00 " + "62 00 63 00");
RecordInputStream in = TestcaseRecordInputStream.create(SeriesTextRecord.sid, data);
SeriesTextRecord str;
try {
str = new SeriesTextRecord(in);
} catch (RecordFormatException e) {
if (e.getCause() instanceof IllegalArgumentException) {
// "Bad requested string length (-126)"
throw new AssertionFailedError("Identified bug 45784a");
}
throw e;
}
if (str.getRecordSize() < 0) {
throw new AssertionFailedError("Identified bug 45784b");
}
byte[] ser;
try {
ser = str.serialize();
} catch (Exception e) {
throw new RuntimeException(e);
}
TestcaseRecordInputStream.confirmRecordEncoding(SeriesTextRecord.sid, data, ser);
}
Aggregations