use of java.io.PushbackInputStream in project beam by apache.
the class AvroSourceTest method testAdvancePastNextSyncMarkerAt.
/**
* Asserts that advancePastNextSyncMarker advances an input stream past a sync marker and
* correctly returns the number of bytes consumed from the stream.
* Creates a haystack of size bytes and places a 16-byte sync marker at the position specified.
*/
private void testAdvancePastNextSyncMarkerAt(int position, int size) throws IOException {
byte sentinel = (byte) 0xFF;
byte[] marker = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
byte[] haystack = createHaystack(marker, position, size);
PushbackInputStream stream = new PushbackInputStream(new ByteArrayInputStream(haystack), marker.length);
if (position + marker.length < size) {
haystack[position + marker.length] = sentinel;
assertEquals(position + marker.length, AvroReader.advancePastNextSyncMarker(stream, marker));
assertEquals(sentinel, (byte) stream.read());
} else {
assertEquals(size, AvroReader.advancePastNextSyncMarker(stream, marker));
assertEquals(-1, stream.read());
}
}
use of java.io.PushbackInputStream in project poi by apache.
the class OPCFileHandler method test.
// a test-case to test this locally without executing the full TestAllFiles
@Test
public void test() throws Exception {
File file = new File("test-data/diagram/test.vsdx");
InputStream stream = new PushbackInputStream(new FileInputStream(file), 100000);
try {
handleFile(stream, file.getPath());
} finally {
stream.close();
}
handleExtracting(file);
}
use of java.io.PushbackInputStream in project poi by apache.
the class XWPFFileHandler method test.
// a test-case to test this locally without executing the full TestAllFiles
@Test
public void test() throws Exception {
File file = new File("test-data/document/51921-Word-Crash067.docx");
InputStream stream = new PushbackInputStream(new FileInputStream(file), 100000);
try {
handleFile(stream, file.getPath());
} finally {
stream.close();
}
handleExtracting(file);
}
use of java.io.PushbackInputStream in project poi by apache.
the class ReadOnlySharedStringsTable method readFrom.
/**
* Read this shared strings table from an XML file.
*
* @param is The input stream containing the XML document.
* @throws IOException if an error occurs while reading.
* @throws SAXException if parsing the XML data fails.
*/
public void readFrom(InputStream is) throws IOException, SAXException {
// test if the file is empty, otherwise parse it
PushbackInputStream pis = new PushbackInputStream(is, 1);
int emptyTest = pis.read();
if (emptyTest > -1) {
pis.unread(emptyTest);
InputSource sheetSource = new InputSource(pis);
try {
XMLReader sheetParser = SAXHelper.newXMLReader();
sheetParser.setContentHandler(this);
sheetParser.parse(sheetSource);
} catch (ParserConfigurationException e) {
throw new RuntimeException("SAX parser appears to be broken - " + e.getMessage());
}
}
}
use of java.io.PushbackInputStream in project poi by apache.
the class DocumentFactoryHelper method hasOOXMLHeader.
/**
* Checks that the supplied InputStream (which MUST
* support mark and reset, or be a PushbackInputStream)
* has a OOXML (zip) header at the start of it.
* If your InputStream does not support mark / reset,
* then wrap it in a PushBackInputStream, then be
* sure to always use that, and not the original!
* @param inp An InputStream which supports either mark/reset, or is a PushbackInputStream
*/
public static boolean hasOOXMLHeader(InputStream inp) throws IOException {
// We want to peek at the first 4 bytes
inp.mark(4);
byte[] header = new byte[4];
int bytesRead = IOUtils.readFully(inp, header);
// Wind back those 4 bytes
if (inp instanceof PushbackInputStream) {
PushbackInputStream pin = (PushbackInputStream) inp;
pin.unread(header, 0, bytesRead);
} else {
inp.reset();
}
// Did it match the ooxml zip signature?
return (bytesRead == 4 && header[0] == POIFSConstants.OOXML_FILE_HEADER[0] && header[1] == POIFSConstants.OOXML_FILE_HEADER[1] && header[2] == POIFSConstants.OOXML_FILE_HEADER[2] && header[3] == POIFSConstants.OOXML_FILE_HEADER[3]);
}
Aggregations