Example usage for org.apache.poi.poifs.filesystem DocumentInputStream DocumentInputStream

List of usage examples for org.apache.poi.poifs.filesystem DocumentInputStream DocumentInputStream

Introduction

In this page you can find the example usage for org.apache.poi.poifs.filesystem DocumentInputStream DocumentInputStream.

Prototype

public DocumentInputStream(POIFSDocument document) 

Source Link

Document

Create an InputStream from the specified Document

Usage

From source file:ReadOLE2Entry.java

License:Open Source License

public static double[] ReadDouble(DocumentEntry document) throws IOException {
    DocumentInputStream stream = new DocumentInputStream(document);
    int len = document.getSize();
    byte[] buf = new byte[len];
    double[] bufDbl = new double[len / 8];
    try {/*from ww w  .ja  va 2 s.  c om*/
        stream.readFully(buf, 0, len);
    } finally {
        stream.close();
    }
    ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN).asDoubleBuffer().get(bufDbl);
    return bufDbl;
}

From source file:ReadOLE2Entry.java

License:Open Source License

public static int[] ReadInt(DocumentEntry document) throws IOException {
    DocumentInputStream stream = new DocumentInputStream(document);
    int len = document.getSize();
    byte[] buf = new byte[len];
    int[] bufInt = new int[len / 4];
    try {//ww w  .j  av a  2 s  . c om
        stream.readFully(buf, 0, len);
    } finally {
        stream.close();
    }
    ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get(bufInt);
    return bufInt;
}

From source file:ReadOLE2Entry.java

License:Open Source License

public static short[] ReadShort(DocumentEntry document) throws IOException {
    DocumentInputStream stream = new DocumentInputStream(document);
    int len = document.getSize();
    byte[] buf = new byte[len];
    short[] bufShort = new short[len / 2];
    try {//from  ww w  . j  ava 2  s .co  m
        stream.readFully(buf, 0, len);
    } finally {
        stream.close();
    }
    ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(bufShort);
    return bufShort;
}

From source file:ReadOLE2Entry.java

License:Open Source License

public static byte[] ReadBytes(DocumentEntry document) throws IOException {
    DocumentInputStream stream = new DocumentInputStream(document);
    int len = document.getSize();
    byte[] buf = new byte[len];
    try {/*from   w w w  . j av a 2  s . c  om*/
        stream.readFully(buf, 0, len);
    } finally {
        stream.close();
    }
    return buf;
}

From source file:ReadOLE2Entry.java

License:Open Source License

public static String ReadString(DocumentEntry document) throws IOException {
    DocumentInputStream stream = new DocumentInputStream(document);
    int len = document.getSize();
    byte[] buf = new byte[len];
    try {//  ww w.  j av  a 2  s  . c o  m
        stream.readFully(buf, 0, len);
    } finally {
        stream.close();
    }
    String str = new String(buf);
    return str;
}

From source file:com.argo.hwp.v5.HwpTextExtractorV5.java

License:Open Source License

/**
 * HWP? FileHeader //  w  w  w .j a  va 2 s  .  co  m
 * 
 * @param fs
 * @return
 * @throws IOException
 */
private static FileHeader getHeader(NPOIFSFileSystem fs) throws IOException {
    DirectoryNode root = fs.getRoot();

    // ??? p.18

    // FileHeader  
    Entry headerEntry = root.getEntry("FileHeader");
    if (!headerEntry.isDocumentEntry())
        return null;

    //  ?
    byte[] header = new byte[256]; // FileHeader ? 256
    DocumentInputStream headerStream = new DocumentInputStream((DocumentEntry) headerEntry);
    try {
        int read = headerStream.read(header);
        if (read != 256
                || !Arrays.equals(HWP_V5_SIGNATURE, Arrays.copyOfRange(header, 0, HWP_V5_SIGNATURE.length)))
            return null;
    } finally {
        headerStream.close();
    }

    FileHeader fileHeader = new FileHeader();

    // . debug
    fileHeader.version = HwpVersion.parseVersion(LittleEndian.getUInt(header, 32));
    long flags = LittleEndian.getUInt(header, 36);
    log.debug("Flags={}", Long.toBinaryString(flags).replace(' ', '0'));

    fileHeader.compressed = (flags & 0x01) == 0x01;
    fileHeader.encrypted = (flags & 0x02) == 0x02;
    fileHeader.viewtext = (flags & 0x04) == 0x04;

    return fileHeader;
}

From source file:com.auxilii.msgparser.MsgParser.java

License:Open Source License

private void parseMsg(DirectoryEntry dir, Message msg) throws IOException {
    DocumentEntry propertyEntry = (DocumentEntry) dir.getEntry("__properties_version1.0");
    try (DocumentInputStream propertyStream = new DocumentInputStream(propertyEntry)) {
        propertyStream.skip(8);/*  ww  w.j  a  v  a 2s . c  o  m*/
        int nextRecipientId = propertyStream.readInt();
        int nextAttachmentId = propertyStream.readInt();
        int recipientCount = propertyStream.readInt();
        int attachmentCount = propertyStream.readInt();
        boolean topLevel = dir.getParent() == null;
        if (topLevel) {
            propertyStream.skip(8);
        }

        for (int index = 0; index < recipientCount; index++) {
            DirectoryEntry entry = (DirectoryEntry) dir
                    .getEntry(String.format("__recip_version1.0_#%08X", index));
            parseRecipient(entry, msg);
        }
        for (int index = 0; index < attachmentCount; index++) {
            DirectoryEntry entry = (DirectoryEntry) dir
                    .getEntry(String.format("__attach_version1.0_#%08X", index));
            parseAttachment(entry, msg);
        }
        while (propertyStream.available() > 0) {
            msg.setProperty(new Property(propertyStream, dir));
        }
    }
}

From source file:com.auxilii.msgparser.MsgParser.java

License:Open Source License

/**
 * Parses a recipient directory entry which holds informations about one of possibly multiple recipients.
 * The parsed information is put into the {@link Message} object.
 *
 * @param dir The current node in the .msg file.
 * @param msg The resulting {@link Message} object.
 * @throws IOException Thrown if the .msg file could not
 *  be parsed.//from  w w  w .ja v  a2 s.  c  o m
 */
protected void parseRecipient(DirectoryEntry dir, Message msg) throws IOException {
    RecipientEntry recipient = new RecipientEntry();
    DocumentEntry propertyEntry = (DocumentEntry) dir.getEntry("__properties_version1.0");
    try (DocumentInputStream propertyStream = new DocumentInputStream(propertyEntry)) {
        propertyStream.skip(8);
        while (propertyStream.available() > 0) {
            recipient.setProperty(new Property(propertyStream, dir));
        }
    }

    msg.addRecipient(recipient);
}

From source file:com.auxilii.msgparser.MsgParser.java

License:Open Source License

private void ParseFileAttachment(DirectoryEntry dir, Message msg) throws IOException {
    FileAttachment fileAttachment = new FileAttachment();
    DocumentEntry propertyEntry = (DocumentEntry) dir.getEntry("__properties_version1.0");
    try (DocumentInputStream propertyStream = new DocumentInputStream(propertyEntry)) {
        propertyStream.skip(8);/* www  .j a  v a2 s  .c om*/
        while (propertyStream.available() > 0) {
            Property property = new Property(propertyStream, dir);
            fileAttachment.setProperty(property.getPid(), property.getValue());
        }
    }

    msg.addAttachment(fileAttachment);
}

From source file:com.ezdi.rtf.testRTFParser.RTFObjDataParser.java

License:Apache License

private byte[] handleEmbeddedPOIFS(InputStream is, Metadata metadata, AtomicInteger unknownFilenameCount)
        throws IOException {

    byte[] ret = null;
    try (NPOIFSFileSystem fs = new NPOIFSFileSystem(is)) {

        DirectoryNode root = fs.getRoot();

        if (root == null) {
            return ret;
        }//from  ww  w. jav  a2s .c  o m

        if (root.hasEntry("Package")) {
            Entry ooxml = root.getEntry("Package");
            TikaInputStream stream = TikaInputStream.get(new DocumentInputStream((DocumentEntry) ooxml));

            ByteArrayOutputStream out = new ByteArrayOutputStream();

            IOUtils.copy(stream, out);
            ret = out.toByteArray();
        } else {
            // try poifs
            POIFSDocumentType type = POIFSDocumentType.detectType(root);
            if (type == POIFSDocumentType.OLE10_NATIVE) {
                try {
                    // Try to un-wrap the OLE10Native record:
                    Ole10Native ole = Ole10Native.createFromEmbeddedOleObject(root);
                    ret = ole.getDataBuffer();
                } catch (Ole10NativeException ex) {
                    // Not a valid OLE10Native record, skip it
                }
            } else if (type == POIFSDocumentType.COMP_OBJ) {

                DocumentEntry contentsEntry;
                try {
                    contentsEntry = (DocumentEntry) root.getEntry("CONTENTS");
                } catch (FileNotFoundException ioe) {
                    contentsEntry = (DocumentEntry) root.getEntry("Contents");
                }

                try (DocumentInputStream inp = new DocumentInputStream(contentsEntry)) {
                    ret = new byte[contentsEntry.getSize()];
                    inp.readFully(ret);
                }
            } else {

                ByteArrayOutputStream out = new ByteArrayOutputStream();
                is.reset();
                IOUtils.copy(is, out);
                ret = out.toByteArray();
                metadata.set(Metadata.RESOURCE_NAME_KEY,
                        "file_" + unknownFilenameCount.getAndIncrement() + "." + type.getExtension());
                metadata.set(Metadata.CONTENT_TYPE, type.getType().toString());
            }
        }
    }
    return ret;
}