raf = new RandomAccessFile(out_file, "rw");
raf.seek(0);
raf.setLength(0);
//OutputStream out = new FileOutputStream( "WarcWriteTest.warc" );
OutputStream out = new RandomAccessFileOutputStream( raf );
reader = WarcReaderFactory.getReader( in );
if (bBuffer) {
writer = WarcWriterFactory.getWriter(out, 8192, bCompress);
} else {
writer = WarcWriterFactory.getWriter(out, bCompress);
}
//System.out.println(writer);
records = 0;
errors = 0;
warnings = 0;
while ( (record = reader.getNextRecord()) != null ) {
if (bDebugOutput) {
TestBaseUtils.printRecord(record);
TestBaseUtils.printRecordErrors(record);
}
++records;
if (record.diagnostics.hasErrors()) {
errors += record.diagnostics.getErrors().size();
}
if (record.diagnostics.hasWarnings()) {
warnings += record.diagnostics.getWarnings().size();
}
writer.writeRawHeader(record.header.headerBytes, record.header.contentLength);
if ( record.hasPayload() ) {
Payload payload = record.getPayload();
//writer.transfer( payload.getInputStream(), payload.getTotalLength() );
writer.streamPayload( payload.getInputStreamComplete() );
}
writer.closeRecord();
}
if (bDebugOutput) {
System.out.println("--------------");
System.out.println(" Records: " + records);
System.out.println(" Errors: " + errors);
}
reader.close();
in.close();
writer.close();
out.close();
Assert.assertEquals(expected_records, records);
Assert.assertTrue(reader.isCompliant());
Assert.assertEquals(0, errors);
Assert.assertEquals(0, warnings);
Assert.assertEquals(11231015, reader.getConsumed());
/*
* Validate written warc.
*/
raf.seek(0);
in = new RandomAccessFileInputStream( raf );
reader = WarcReaderFactory.getReader( in );
//System.out.println(reader);
records = 0;
errors = 0;
warnings = 0;
while ( (record = reader.getNextRecord()) != null ) {
if (bDebugOutput) {
TestBaseUtils.printRecord(record);
TestBaseUtils.printRecordErrors(record);
}
++records;
if (record.diagnostics.hasErrors()) {
errors += record.diagnostics.getErrors().size();
}
if (record.diagnostics.hasWarnings()) {
warnings += record.diagnostics.getWarnings().size();
}
}
if (bDebugOutput) {
System.out.println("--------------");
System.out.println(" Records: " + records);
System.out.println(" Errors: " + errors);
}
reader.close();
in.close();
writer.close();
out.close();
Assert.assertEquals(expected_records, records);
Assert.assertTrue(reader.isCompliant());
Assert.assertEquals(0, errors);
Assert.assertEquals(0, warnings);