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

The following examples show how to use org.apache.poi.poifs.common.POIFSConstants. These examples are extracted from open source projects. 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 Project: lams   Source File: POIFSHeaderDumper.java    License: 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 2
Source Project: lams   Source File: NPOIFSDocument.java    License: 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 3
Source Project: lams   Source File: NPOIFSFileSystem.java    License: 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 4
Source Project: lams   Source File: NPOIFSMiniStore.java    License: 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 5
Source Project: lams   Source File: OPOIFSDocument.java    License: 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 6
Source Project: lams   Source File: HeaderBlock.java    License: 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 7
/**
 * 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 8
/**
 * 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 9
Source Project: dremio-oss   Source File: MiniStore.java    License: 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 10
Source Project: dremio-oss   Source File: BlockStoreInputStream.java    License: 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 11
Source Project: dremio-oss   Source File: PropertyTable.java    License: 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 12
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 13
Source Project: lams   Source File: NPOIFSDocument.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor for an existing Document 
 */
public NPOIFSDocument(DocumentProperty property, NPOIFSFileSystem filesystem) 
   throws IOException
{
   this._property = property;
   this._filesystem = filesystem;

   if(property.getSize() < POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE) {
      _stream = new NPOIFSStream(_filesystem.getMiniStore(), property.getStartBlock());
      _block_size = _filesystem.getMiniStore().getBlockStoreBlockSize();
   } else {
      _stream = new NPOIFSStream(_filesystem, property.getStartBlock());
      _block_size = _filesystem.getBlockStoreBlockSize();
   }
}
 
Example 14
Source Project: lams   Source File: NPOIFSDocument.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Stores the given data for this Document
 */
private int store(InputStream stream) throws IOException {
    final int bigBlockSize = POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE;
    BufferedInputStream bis = new BufferedInputStream(stream, bigBlockSize+1);
    bis.mark(bigBlockSize);

    // Do we need to store as a mini stream or a full one?
    if(bis.skip(bigBlockSize) < bigBlockSize) {
       _stream = new NPOIFSStream(_filesystem.getMiniStore());
       _block_size = _filesystem.getMiniStore().getBlockStoreBlockSize();
    } else {
       _stream = new NPOIFSStream(_filesystem);
       _block_size = _filesystem.getBlockStoreBlockSize();
    }

    // start from the beginning 
    bis.reset();
    
    // Store it
    OutputStream os = _stream.getOutputStream();
    byte buf[] = new byte[1024];
    int length = 0;
    
    for (int readBytes; (readBytes = bis.read(buf)) != -1; length += readBytes) {
        os.write(buf, 0, readBytes);
    }
    
    // Pad to the end of the block with -1s
    int usedInBlock = length % _block_size;
    if (usedInBlock != 0 && usedInBlock != _block_size) {
        int toBlockEnd = _block_size - usedInBlock;
        byte[] padding = new byte[toBlockEnd];
        Arrays.fill(padding, (byte)0xFF);
        os.write(padding);
    }
    
    // Tidy and return the length
    os.close();
    return length;
}
 
Example 15
Source Project: lams   Source File: NDocumentOutputStream.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void checkBufferSize() throws IOException {
    // Have we gone over the mini stream limit yet?
    if (_buffer.size() > POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE) {
        // Will need to be in the main stream
        byte[] data = _buffer.toByteArray();
        _buffer = null;
        write(data, 0, data.length);
    } else {
        // So far, mini stream will work, keep going
    }
}
 
Example 16
Source Project: lams   Source File: OPOIFSDocument.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor from small blocks
 *
 * @param name the name of the POIFSDocument
 * @param blocks the small blocks making up the POIFSDocument
 * @param length the actual length of the POIFSDocument
 */
public OPOIFSDocument(String name, SmallDocumentBlock[] blocks, int length) {
	_size = length;
	
	if(blocks.length == 0) {
	   _bigBigBlockSize = POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS;
	} else {
	   _bigBigBlockSize = blocks[0].getBigBlockSize();
	}

	_big_store = new BigBlockStore(_bigBigBlockSize, EMPTY_BIG_BLOCK_ARRAY);
	_property = new DocumentProperty(name, _size);
	_small_store = new SmallBlockStore(_bigBigBlockSize, blocks);
	_property.setDocument(this);
}
 
Example 17
Source Project: lams   Source File: NPOIFSStream.java    License: GNU General Public License v2.0 5 votes vote down vote up
public Iterator<ByteBuffer> getBlockIterator() {
   if(startBlock == POIFSConstants.END_OF_CHAIN) {
      throw new IllegalStateException(
            "Can't read from a new stream before it has been written to"
      );
   }
   return new StreamBlockByteBufferIterator(startBlock);
}
 
Example 18
Source Project: lams   Source File: NPOIFSStream.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void free(ChainLoopDetector loopDetector) {
   int nextBlock = startBlock;
   while(nextBlock != POIFSConstants.END_OF_CHAIN) {
      int thisBlock = nextBlock;
      loopDetector.claim(thisBlock);
      nextBlock = blockStore.getNextBlock(thisBlock);
      blockStore.setNextBlock(thisBlock, POIFSConstants.UNUSED_BLOCK);
   }
   this.startBlock = POIFSConstants.END_OF_CHAIN;
}
 
Example 19
Source Project: lams   Source File: NPOIFSStream.java    License: GNU General Public License v2.0 5 votes vote down vote up
public ByteBuffer next() {
   if(nextBlock == POIFSConstants.END_OF_CHAIN) {
      throw new IndexOutOfBoundsException("Can't read past the end of the stream");
   }
   
   try {
      loopDetector.claim(nextBlock);
      ByteBuffer data = blockStore.getBlockAt(nextBlock);
      nextBlock = blockStore.getNextBlock(nextBlock);
      return data;
   } catch(IOException e) {
      throw new RuntimeException(e);
   }
}
 
Example 20
Source Project: lams   Source File: NPOIFSStream.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected void createBlockIfNeeded() throws IOException {
    if (buffer != null && buffer.hasRemaining()) return;
    
    int thisBlock = nextBlock;
    
    // Allocate a block if needed, otherwise figure
    //  out what the next block will be
    if(thisBlock == POIFSConstants.END_OF_CHAIN) {
       thisBlock = blockStore.getFreeBlock();
       loopDetector.claim(thisBlock);
       
       // We're on the end of the chain
       nextBlock = POIFSConstants.END_OF_CHAIN;
       
       // Mark the previous block as carrying on to us if needed
       if(prevBlock != POIFSConstants.END_OF_CHAIN) {
          blockStore.setNextBlock(prevBlock, thisBlock);
       }
       blockStore.setNextBlock(thisBlock, POIFSConstants.END_OF_CHAIN);
       
       // If we've just written the first block on a 
       //  new stream, save the start block offset
       if(startBlock == POIFSConstants.END_OF_CHAIN) {
          startBlock = thisBlock;
       }
    } else {
       loopDetector.claim(thisBlock);
       nextBlock = blockStore.getNextBlock(thisBlock);
    }

    buffer = blockStore.createBlockIfNeeded(thisBlock);
    
    // Update pointers
    prevBlock = thisBlock;
}
 
Example 21
Source Project: lams   Source File: NPOIFSStream.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void close() throws IOException {
    // If we're overwriting, free any remaining blocks
    NPOIFSStream toFree = new NPOIFSStream(blockStore, nextBlock);
    toFree.free(loopDetector);
    
    // Mark the end of the stream, if we have any data
    if (prevBlock != POIFSConstants.END_OF_CHAIN) {
        blockStore.setNextBlock(prevBlock, POIFSConstants.END_OF_CHAIN);
    }
}
 
Example 22
Source Project: lams   Source File: NPropertyTable.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return the number of BigBlock's this instance uses
 *
 * @return count of BigBlock instances
 */
public int countBlocks()
{
   long rawSize = _properties.size() * (long)POIFSConstants.PROPERTY_SIZE;
   int blkSize = _bigBigBlockSize.getBigBlockSize();
   int numBlocks = (int)(rawSize / blkSize);
   if ((rawSize % blkSize) != 0) {
       numBlocks++;
   }
   return numBlocks;
}
 
Example 23
Source Project: lams   Source File: Property.java    License: 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);
}
 
Example 24
Source Project: lams   Source File: RootProperty.java    License: 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 25
Source Project: lams   Source File: PropertyFactory.java    License: 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 26
Source Project: lams   Source File: HeaderBlock.java    License: 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 27
Source Project: lams   Source File: HeaderBlock.java    License: 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 28
/**
 * 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 29
Source Project: lams   Source File: SmallBlockTableWriter.java    License: 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 30
Source Project: lams   Source File: DocumentBlock.java    License: 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;
}