Search in sources :

Example 26 with PushbackInputStream

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());
    }
}
Also used : PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 27 with PushbackInputStream

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);
}
Also used : PushbackInputStream(java.io.PushbackInputStream) PushbackInputStream(java.io.PushbackInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 28 with PushbackInputStream

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);
}
Also used : PushbackInputStream(java.io.PushbackInputStream) PushbackInputStream(java.io.PushbackInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 29 with PushbackInputStream

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());
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) PushbackInputStream(java.io.PushbackInputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader)

Example 30 with PushbackInputStream

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]);
}
Also used : PushbackInputStream(java.io.PushbackInputStream)

Aggregations

PushbackInputStream (java.io.PushbackInputStream)72 IOException (java.io.IOException)40 ByteArrayInputStream (java.io.ByteArrayInputStream)12 InputStream (java.io.InputStream)12 FileInputStream (java.io.FileInputStream)6 CertificateException (java.security.cert.CertificateException)5 File (java.io.File)4 CRLException (java.security.cert.CRLException)4 CertificateParsingException (java.security.cert.CertificateParsingException)4 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)4 InputStreamReader (java.io.InputStreamReader)3 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)3 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Charset (java.nio.charset.Charset)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Attributes (java.util.jar.Attributes)2 JarEntry (java.util.jar.JarEntry)2