org.apache.poi.poifs.common.POIFSConstants Java Examples

The following examples show how to use org.apache.poi.poifs.common.POIFSConstants. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MiniStore.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@Override
public int getBlockOffset(int miniBlock) {
  // we want to read mini block with index = miniBlock

  // first we need to figure out which DIFAT block the mini block is in
  int byteOffset = miniBlock * POIFSConstants.SMALL_BLOCK_SIZE; // offset from beginning of SBAT
  int difatBlockNumber = byteOffset / difats.getBlockSize();
  int offsetInDifatBlock = byteOffset % difats.getBlockSize();

  // Now locate the data block for it, starting from the 1st block of the mini-FAT
  int nextBlock = root.getStartBlock(); // first sector in mini-FAT chain
  int idx = 0;
  while (nextBlock != POIFSConstants.END_OF_CHAIN && idx < difatBlockNumber) {
    nextBlock = difats.getNextBlock(nextBlock); // get next block in the chain
    idx++;
  }

  Preconditions.checkState(idx == difatBlockNumber, "DIFAT block " + difatBlockNumber + " outside stream chain");

  int difatBlockOffset = difats.getBlockOffset(nextBlock);

  return difatBlockOffset + offsetInDifatBlock;
}
 
Example #2
Source File: BlockAllocationTableWriter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Allocate space for a block of indices
 *
 * @param blockCount the number of blocks to allocate space for
 *
 * @return the starting index of the blocks
 */
public int allocateSpace(final int blockCount)
{
    int startBlock = _entries.size();

    if (blockCount > 0)
    {
        int limit = blockCount - 1;
        int index = startBlock + 1;

        for (int k = 0; k < limit; k++)
        {
            _entries.add(index++);
        }
        _entries.add(POIFSConstants.END_OF_CHAIN);
    }
    return startBlock;
}
 
Example #3
Source File: BlockAllocationTableReader.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert an array of blocks into a set of integer indices
 *
 * @param blocks the array of blocks containing the indices
 * @param raw_blocks the list of blocks being managed. Unused
 *                   blocks will be eliminated from the list
 */
private void setEntries(ListManagedBlock[] blocks, BlockList raw_blocks) throws IOException {
    int limit = bigBlockSize.getBATEntriesPerBlock(); 

    for (int block_index = 0; block_index < blocks.length; block_index++)
    {
        byte[] data   = blocks[ block_index ].getData();
        int    offset = 0;

        for (int k = 0; k < limit; k++)
        {
            int entry = LittleEndian.getInt(data, offset);

            if (entry == POIFSConstants.UNUSED_BLOCK)
            {
                raw_blocks.zap(_entries.size());
            }
            _entries.add(entry);
            offset += LittleEndianConsts.INT_SIZE;
        }

        // discard block
        blocks[ block_index ] = null;
    }
    raw_blocks.setBAT(this);
}
 
Example #4
Source File: OPOIFSDocument.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor from large blocks
 *
 * @param name the name of the POIFSDocument
 * @param blocks the big blocks making up the POIFSDocument
 * @param length the actual length of the POIFSDocument
 */
public OPOIFSDocument(String name, RawDataBlock[] blocks, int length) throws IOException {
	_size = length;
	if(blocks.length == 0) {
	   _bigBigBlockSize = POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS;
	} else {
	   _bigBigBlockSize = (blocks[0].getBigBlockSize() == POIFSConstants.SMALLER_BIG_BLOCK_SIZE ?
	         POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS : 
	         POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS
	   );
	}
	
	_big_store = new BigBlockStore(_bigBigBlockSize, convertRawBlocksToBigBlocks(blocks));
	_property = new DocumentProperty(name, _size);
	_small_store = new SmallBlockStore(_bigBigBlockSize, EMPTY_SMALL_BLOCK_ARRAY);
	_property.setDocument(this);
}
 
Example #5
Source File: HeaderBlock.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Write the block's data to an OutputStream
 *
 * @param stream the OutputStream to which the stored data should
 *               be written
 *
 * @exception IOException on problems writing to the specified
 *            stream
 */
void writeData(final OutputStream stream) throws IOException {
   // Update the counts and start positions 
   new IntegerField(_bat_count_offset,      _bat_count, _data);
   new IntegerField(_property_start_offset, _property_start, _data);
   new IntegerField(_sbat_start_offset,     _sbat_start, _data);
   new IntegerField(_sbat_block_count_offset, _sbat_count, _data);
   new IntegerField(_xbat_start_offset,      _xbat_start, _data);
   new IntegerField(_xbat_count_offset,      _xbat_count, _data);
   
   // Write the main data out
   stream.write(_data, 0, 512);
   
   // Now do the padding if needed
   for(int i=POIFSConstants.SMALLER_BIG_BLOCK_SIZE; i<bigBlockSize.getBigBlockSize(); i++) {
      stream.write(0);
   }
}
 
Example #6
Source File: NPOIFSMiniStore.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Load the block at the given offset.
 */
protected ByteBuffer getBlockAt(final int offset) throws IOException {
   // Which big block is this?
   int byteOffset = offset * POIFSConstants.SMALL_BLOCK_SIZE;
   int bigBlockNumber = byteOffset / _filesystem.getBigBlockSize();
   int bigBlockOffset = byteOffset % _filesystem.getBigBlockSize();
   
   // Now locate the data block for it
   Iterator<ByteBuffer> it = _mini_stream.getBlockIterator();
   for(int i=0; i<bigBlockNumber; i++) {
      it.next();
   }
   ByteBuffer dataBlock = it.next();
   if(dataBlock == null) {
      throw new IndexOutOfBoundsException("Big block " + bigBlockNumber + " outside stream");
   }

   // Position ourselves, and take a slice 
   dataBlock.position(
         dataBlock.position() + bigBlockOffset
   );
   ByteBuffer miniBuffer = dataBlock.slice();
   miniBuffer.limit(POIFSConstants.SMALL_BLOCK_SIZE);
   return miniBuffer;
}
 
Example #7
Source File: BlockStoreInputStream.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
BlockStoreInputStream(final XlsInputStream inputStream, final BlockStore blockStore, final int startBlock) {
  this.inputStream = Preconditions.checkNotNull(inputStream, "input stream should not be null");
  this.blockStore = Preconditions.checkNotNull(blockStore, "block store should not be null");
  nextBlock = startBlock;
  blockSize = blockStore.getBlockSize();

  Preconditions.checkState(startBlock != POIFSConstants.END_OF_CHAIN, "startBlock cannot be END_OF_CHAIN");

  // count number of blocks that are part of the stream chain, including current block!
  remainingBlocks = 0;
  int block = nextBlock;
  while (block != POIFSConstants.END_OF_CHAIN) {
    remainingBlocks++;
    block = blockStore.getNextBlock(block);
  }

  // move to the beginning of the first block in the chain
  inputStream.seek(blockStore.getBlockOffset(nextBlock));
}
 
Example #8
Source File: NPOIFSFileSystem.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor, intended for writing
 */
public NPOIFSFileSystem()
{
   this(true);
   
    // Reserve block 0 for the start of the Properties Table
    // Create a single empty BAT, at pop that at offset 1
    _header.setBATCount(1);
    _header.setBATArray(new int[] { 1 });
    BATBlock bb = BATBlock.createEmptyBATBlock(bigBlockSize, false);
    bb.setOurBlockIndex(1);
    _bat_blocks.add(bb);

    setNextBlock(0, POIFSConstants.END_OF_CHAIN);
    setNextBlock(1, POIFSConstants.FAT_SECTOR_BLOCK);

    _property_table.setStartBlock(0);
}
 
Example #9
Source File: PropertyTable.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public PropertyTable(final XlsInputStream is, HeaderBlock header, BlockStore blockStore) {
  this.is = is;
  this.sectorSize = header.getBigBlockSize().getBigBlockSize();


  // Directory sectors are stored as a chain starting from sector # header.propertyStart
  // and FAT table contains link to next sector in the chain

  int nextBlock = header.getPropertyStart(); // first sector in directory chain
  while (nextBlock != POIFSConstants.END_OF_CHAIN) {
    int blockOffset = blockStore.getBlockOffset(nextBlock);
    processSector(blockOffset);
    nextBlock = blockStore.getNextBlock(nextBlock); // get next block in the chain
  }

  populatePropertyTree((DirectoryProperty) properties.get(0));
}
 
Example #10
Source File: NPOIFSDocument.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public NPOIFSDocument(String name, int size, NPOIFSFileSystem filesystem, POIFSWriterListener writer) 
   throws IOException 
{
    this._filesystem = filesystem;

    if (size < POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE) {
        _stream = new NPOIFSStream(filesystem.getMiniStore());
        _block_size = _filesystem.getMiniStore().getBlockStoreBlockSize();
    } else {
        _stream = new NPOIFSStream(filesystem);
        _block_size = _filesystem.getBlockStoreBlockSize();
    }
    
    OutputStream innerOs = _stream.getOutputStream();
    DocumentOutputStream os = new DocumentOutputStream(innerOs, size);
    POIFSDocumentPath path = new POIFSDocumentPath(name.split("\\\\"));
    String docName = path.getComponent(path.length()-1);
    POIFSWriterEvent event = new POIFSWriterEvent(os, path, docName, size);
    writer.processPOIFSWriterEvent(event);
    innerOs.close();

    // And build the property for it
    this._property = new DocumentProperty(name, size);
    _property.setStartBlock(_stream.getStartBlock());     
}
 
Example #11
Source File: POIFSHeaderDumper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public static void displayBATReader(String type, BlockAllocationTableReader batReader) throws Exception {
    System.out.println("Sectors, as referenced from the "+type+" FAT:");
    IntList entries = batReader.getEntries();

    for(int i=0; i<entries.size(); i++) {
        int bn = entries.get(i);
        String bnS = Integer.toString(bn);
        if(bn == POIFSConstants.END_OF_CHAIN) {
            bnS = "End Of Chain";
        } else if(bn == POIFSConstants.DIFAT_SECTOR_BLOCK) {
            bnS = "DI Fat Block";
        } else if(bn == POIFSConstants.FAT_SECTOR_BLOCK) {
            bnS = "Normal Fat Block";
        } else if(bn == POIFSConstants.UNUSED_BLOCK) {
            bnS = "Block Not Used (Free)";
        }

        System.out.println("  Block  # " + i + " -> " + bnS);
    }

    System.out.println("");
}
 
Example #12
Source File: RootProperty.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
RootProperty()
{
    super(NAME);

    // overrides
    setNodeColor(_NODE_BLACK);
    setPropertyType(PropertyConstants.ROOT_TYPE);
    setStartBlock(POIFSConstants.END_OF_CHAIN);
}
 
Example #13
Source File: HeaderBlock.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a single instance initialized with default values
 */
public HeaderBlock(POIFSBigBlockSize bigBlockSize)
{
   this.bigBlockSize = bigBlockSize;

   // Our data is always 512 big no matter what
   _data = new byte[ POIFSConstants.SMALLER_BIG_BLOCK_SIZE ];
   Arrays.fill(_data, _default_value);
   
   // Set all the default values
   new LongField(_signature_offset, _signature, _data);
   new IntegerField(0x08, 0, _data);
   new IntegerField(0x0c, 0, _data);
   new IntegerField(0x10, 0, _data);
   new IntegerField(0x14, 0, _data);
   new ShortField(0x18, ( short ) 0x3b, _data);
   new ShortField(0x1a, ( short ) 0x3, _data);
   new ShortField(0x1c, ( short ) -2, _data);
    
   new ShortField(0x1e, bigBlockSize.getHeaderValue(), _data);
   new IntegerField(0x20, 0x6, _data);
   new IntegerField(0x24, 0, _data);
   new IntegerField(0x28, 0, _data);
   new IntegerField(0x34, 0, _data);
   new IntegerField(0x38, 0x1000, _data);
   
   // Initialize the variables
   _bat_count = 0;
   _sbat_count = 0;
   _xbat_count = 0;
   _property_start = POIFSConstants.END_OF_CHAIN;
   _sbat_start = POIFSConstants.END_OF_CHAIN;
   _xbat_start = POIFSConstants.END_OF_CHAIN;
}
 
Example #14
Source File: HeaderBlock.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets the offsets of the first (up to) 109
 *  BAT sectors.
 */
public void setBATArray(int[] bat_array) {
   int count = Math.min(bat_array.length, _max_bats_in_header);
   int blank = _max_bats_in_header - count;
   
     int offset = _bat_array_offset;
   for(int i=0; i<count; i++) {
      LittleEndian.putInt(_data, offset, bat_array[i]);
        offset += LittleEndianConsts.INT_SIZE;
   }
   for(int i=0; i<blank; i++) {
        LittleEndian.putInt(_data, offset, POIFSConstants.UNUSED_BLOCK);
        offset += LittleEndianConsts.INT_SIZE;
   }
}
 
Example #15
Source File: PropertyFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
static void convertToProperties(byte[] data, List<Property> properties)
    throws IOException
{
   int property_count = data.length / POIFSConstants.PROPERTY_SIZE;
   int offset         = 0;

   for (int k = 0; k < property_count; k++) {
      switch (data[ offset + PropertyConstants.PROPERTY_TYPE_OFFSET ]) {
      case PropertyConstants.DIRECTORY_TYPE :
         properties.add(
               new DirectoryProperty(properties.size(), data, offset)
         );
         break;

      case PropertyConstants.DOCUMENT_TYPE :
         properties.add(
               new DocumentProperty(properties.size(), data, offset)
         );
         break;

      case PropertyConstants.ROOT_TYPE :
         properties.add(
               new RootProperty(properties.size(), data, offset)
         );
         break;

      default :
         properties.add(null);
         break;
      }
      
      offset += POIFSConstants.PROPERTY_SIZE;
   }
}
 
Example #16
Source File: BlockAllocationTableReader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * walk the entries from a specified point and return the
 * associated blocks. The associated blocks are removed from the
 * block list
 *
 * @param startBlock the first block in the chain
 * @param blockList the raw data block list
 *
 * @return array of ListManagedBlocks, in their correct order
 *
 * @exception IOException if there is a problem acquiring the blocks
 */
ListManagedBlock[] fetchBlocks(int startBlock, int headerPropertiesStartBlock,
        BlockList blockList) throws IOException {
    List<ListManagedBlock> blocks = new ArrayList<ListManagedBlock>();
    int  currentBlock = startBlock;
    boolean firstPass = true;
    ListManagedBlock dataBlock = null;

    // Process the chain from the start to the end
    // Normally we have header, data, end
    // Sometimes we have data, header, end
    // For those cases, stop at the header, not the end
    while (currentBlock != POIFSConstants.END_OF_CHAIN) {
        try {
            // Grab the data at the current block offset
            dataBlock = blockList.remove(currentBlock);
            blocks.add(dataBlock);
            // Now figure out which block we go to next
            currentBlock = _entries.get(currentBlock);
            firstPass = false;
        } catch(IOException e) {
            if(currentBlock == headerPropertiesStartBlock) {
                // Special case where things are in the wrong order
                _logger.log(POILogger.WARN, "Warning, header block comes after data blocks in POIFS block listing");
                currentBlock = POIFSConstants.END_OF_CHAIN;
            } else if(currentBlock == 0 && firstPass) {
                // Special case where the termination isn't done right
                //  on an empty set
                _logger.log(POILogger.WARN, "Warning, incorrectly terminated empty data blocks in POIFS block listing (should end at -2, ended at 0)");
                currentBlock = POIFSConstants.END_OF_CHAIN;
            } else {
                // Ripple up
                throw e;
            }
        }
    }

    return blocks.toArray(new ListManagedBlock[blocks.size()]);
}
 
Example #17
Source File: XlsReader.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * returns a {@link BlockStoreInputStream} that exposes all workbook sectors in their correct order
 *
 * @param is XLS InputStream
 * @return {@link BlockStoreInputStream} that wraps the workbook's stream
 *
 * @throws IOException if the data doesn't contain a proper MS-CFB header
 * @throws OldExcelFormatException if the file is too old to be supported
 */
static InputStream createWorkbookInputStream(final XlsInputStream is) throws IOException {
  final XlsReader xlsReader = new XlsReader(is);
  DocumentEntry workBookEntry = xlsReader.getWorkbookEntry();
  DocumentNode workbookNode = (DocumentNode) workBookEntry;

  // use proper blockStore
  final boolean useMiniStore = workbookNode.getSize() < POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE;
  final BlockStore blockStore = useMiniStore ? xlsReader.miniStore : xlsReader.difats;

  return new BlockStoreInputStream(is, blockStore, workbookNode.getProperty().getStartBlock());
}
 
Example #18
Source File: SmallBlockTableWriter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates new SmallBlockTable
 *
 * @param documents a List of POIFSDocument instances
 * @param root the Filesystem's root property
 */
public SmallBlockTableWriter(final POIFSBigBlockSize bigBlockSize,
                             final List<OPOIFSDocument> documents,
                             final RootProperty root)
{
    _sbat         = new BlockAllocationTableWriter(bigBlockSize);
    _small_blocks = new ArrayList<SmallDocumentBlock>();
    _root         = root;

    for (OPOIFSDocument doc : documents)
    {
        SmallDocumentBlock[] blocks = doc.getSmallBlocks();

        if (blocks.length != 0)
        {
            doc.setStartBlock(_sbat.allocateSpace(blocks.length));
            for (int j = 0; j < blocks.length; j++)
            {
                _small_blocks.add(blocks[ j ]);
            }
        } else {
        	doc.setStartBlock(POIFSConstants.END_OF_CHAIN);
        }
    }
    _sbat.simpleCreateBlocks();
    _root.setSize(_small_blocks.size());
    _big_block_count = SmallDocumentBlock.fill(bigBlockSize,_small_blocks);
}
 
Example #19
Source File: DocumentBlock.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * create a document block from a raw data block
 *
 * @param block the raw data block
 *
 * @exception IOException
 */

public DocumentBlock(final RawDataBlock block)
    throws IOException
{
    super(
          block.getBigBlockSize() == POIFSConstants.SMALLER_BIG_BLOCK_SIZE ?
                POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS :
                POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS
    );
    _data       = block.getData();
    _bytes_read = _data.length;
}
 
Example #20
Source File: BlockAllocationTableWriter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * create a BlockAllocationTableWriter
 */
public BlockAllocationTableWriter(POIFSBigBlockSize bigBlockSize)
{
   _bigBlockSize = bigBlockSize; 
    _start_block  = POIFSConstants.END_OF_CHAIN;
    _entries      = new IntList();
    _blocks       = new BATBlock[ 0 ];
}
 
Example #21
Source File: BATBlock.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a single instance initialized with default values
 */
private BATBlock(POIFSBigBlockSize bigBlockSize)
{
    super(bigBlockSize);
    
    int _entries_per_block = bigBlockSize.getBATEntriesPerBlock();
    _values = new int[_entries_per_block];
    _has_free_sectors = true;

    Arrays.fill(_values, POIFSConstants.UNUSED_BLOCK);
}
 
Example #22
Source File: BATBlock.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void recomputeFree() {
   boolean hasFree = false;
   for(int k=0; k<_values.length; k++) {
      if(_values[k] == POIFSConstants.UNUSED_BLOCK) {
         hasFree = true;
         break;
      }
   }
   _has_free_sectors = hasFree;
}
 
Example #23
Source File: BATBlock.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a single BATBlock, with all the values set to empty.
 */
public static BATBlock createEmptyBATBlock(final POIFSBigBlockSize bigBlockSize, boolean isXBAT) {
   BATBlock block = new BATBlock(bigBlockSize);
   if(isXBAT) {
      block.setXBATChain(bigBlockSize, POIFSConstants.END_OF_CHAIN);
   }
   return block;
}
 
Example #24
Source File: BATBlock.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create an array of XBATBlocks from an array of int block
 * allocation table entries
 *
 * @param entries the array of int entries
 * @param startBlock the start block of the array of XBAT blocks
 *
 * @return the newly created array of BATBlocks
 */

public static BATBlock [] createXBATBlocks(final POIFSBigBlockSize bigBlockSize,
                                           final int [] entries,
                                           final int startBlock)
{
    int        block_count =
        calculateXBATStorageRequirements(bigBlockSize, entries.length);
    BATBlock[] blocks      = new BATBlock[ block_count ];
    int        index       = 0;
    int        remaining   = entries.length;

    int _entries_per_xbat_block = bigBlockSize.getXBATEntriesPerBlock();
    if (block_count != 0)
    {
        for (int j = 0; j < entries.length; j += _entries_per_xbat_block)
        {
            blocks[ index++ ] =
                new BATBlock(bigBlockSize, entries, j,
                             (remaining > _entries_per_xbat_block)
                             ? j + _entries_per_xbat_block
                             : entries.length);
            remaining         -= _entries_per_xbat_block;
        }
        for (index = 0; index < blocks.length - 1; index++)
        {
            blocks[ index ].setXBATChain(bigBlockSize, startBlock + index + 1);
        }
        blocks[ index ].setXBATChain(bigBlockSize, POIFSConstants.END_OF_CHAIN);
    }
    return blocks;
}
 
Example #25
Source File: BATBlock.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * How many sectors in this block are taken?
 * Note that calling {@link #hasFreeSectors()} is much quicker
 */
public int getUsedSectors(boolean isAnXBAT) {
    int usedSectors = 0;
    int toCheck = _values.length;
    if (isAnXBAT) toCheck--; // Last is a chain location
    for(int k=0; k<toCheck; k++) {
        if(_values[k] != POIFSConstants.UNUSED_BLOCK) {
            usedSectors ++;
        }
    }
    return usedSectors;
}
 
Example #26
Source File: BATBlock.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void setValueAt(int relativeOffset, int value) {
   int oldValue = _values[relativeOffset];
   _values[relativeOffset] = value;
   
   // Do we need to re-compute the free?
   if(value == POIFSConstants.UNUSED_BLOCK) {
      _has_free_sectors = true;
      return;
   }
   if(oldValue == POIFSConstants.UNUSED_BLOCK) {
      recomputeFree();
   }
}
 
Example #27
Source File: MiniStore.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public MiniStore(final XlsInputStream is, DirectoryProperty root, HeaderBlock header, BlockStore difats) {
  super(is, header, POIFSConstants.SMALL_BLOCK_SIZE);
  this.root = root;
  this.difats = difats;

  // load all mini-FAT blocks
  int nextAt = header.getSBATStart();
  for(int i = 0; i < header.getSBATCount() && nextAt != POIFSConstants.END_OF_CHAIN; i++) {
    BATBlock sfat = BATBlock.createBATBlock(header.getBigBlockSize(), difats.getBlockBuffer(nextAt));
    sfat.setOurBlockIndex(nextAt);
    blocks.add(sfat);
    nextAt = difats.getNextBlock(nextAt);
  }
}
 
Example #28
Source File: PropertyTable.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private void processSector(int sectorOffset) {
  final int property_count = sectorSize / POIFSConstants.PROPERTY_SIZE;

  byte[] bytes = new byte[POIFSConstants.PROPERTY_SIZE];

  int index = properties.size();
  is.seek(sectorOffset);
  for (int k = 0; k < property_count; k++, index++) {
    try {
      is.read(bytes, 0, bytes.length);
    } catch (IOException e) {
      // shouldn't be possible in a well formatted xls stream
      throw new IllegalStateException("Couldn't read from stream");
    }

    switch (bytes[PropertyConstants.PROPERTY_TYPE_OFFSET ]) {
      case PropertyConstants.DIRECTORY_TYPE :
      case PropertyConstants.ROOT_TYPE :
        properties.add(new DirectoryProperty(index, bytes));
        break;
      case PropertyConstants.DOCUMENT_TYPE :
        properties.add(new DocumentProperty(index, bytes));
        break;
      default :
        // add a null as we'll need to access properties by index later (or do we ?)
        properties.add(null);
        break;
    }
  }
}
 
Example #29
Source File: TikaOfficeDetectParser.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void parse(InputStream stream,
      ContentHandler handler, Metadata metadata,
      ParseContext parseContext) throws IOException, SAXException,
      TikaException 
{
   byte[] initial4 = new byte[4];
   InputStream wrapped;
   // Preserve TikaInputStreams as TikaInputStreams as they require less memory to process
   if (stream.markSupported())
   {
      stream.mark(initial4.length);
      IOUtils.readFully(stream, initial4);
      stream.reset();
      wrapped = stream;
   }
   else
   {
      PushbackInputStream inp = new PushbackInputStream(stream, 4);
      IOUtils.readFully(inp, initial4);
      inp.unread(initial4);
      wrapped = inp;
   }
   
   // Which is it?
   if(initial4[0] == POIFSConstants.OOXML_FILE_HEADER[0] &&
      initial4[1] == POIFSConstants.OOXML_FILE_HEADER[1] &&
      initial4[2] == POIFSConstants.OOXML_FILE_HEADER[2] &&
      initial4[3] == POIFSConstants.OOXML_FILE_HEADER[3])
   {
      ooxmlParser.parse(wrapped, handler, metadata, parseContext);
   }
   else
   {
      ole2Parser.parse(wrapped, handler, metadata, parseContext);
   }
}
 
Example #30
Source File: Property.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected Property()
{
    _raw_data = new byte[ POIFSConstants.PROPERTY_SIZE ];
    Arrays.fill(_raw_data, _default_fill);
    _name_size         = new ShortField(_name_size_offset);
    _property_type     =
        new ByteField(PropertyConstants.PROPERTY_TYPE_OFFSET);
    _node_color        = new ByteField(_node_color_offset);
    _previous_property = new IntegerField(_previous_property_offset,
                                          _NO_INDEX, _raw_data);
    _next_property     = new IntegerField(_next_property_offset,
                                          _NO_INDEX, _raw_data);
    _child_property    = new IntegerField(_child_property_offset,
                                          _NO_INDEX, _raw_data);
    _storage_clsid     = new ClassID(_raw_data,_storage_clsid_offset);
    _user_flags        = new IntegerField(_user_flags_offset, 0, _raw_data);
    _seconds_1         = new IntegerField(_seconds_1_offset, 0,
                                          _raw_data);
    _days_1            = new IntegerField(_days_1_offset, 0, _raw_data);
    _seconds_2         = new IntegerField(_seconds_2_offset, 0,
                                          _raw_data);
    _days_2            = new IntegerField(_days_2_offset, 0, _raw_data);
    _start_block       = new IntegerField(_start_block_offset);
    _size              = new IntegerField(_size_offset, 0, _raw_data);
    _index             = _NO_INDEX;
    setName("");
    setNextChild(null);
    setPreviousChild(null);
}