gt:             int        block_count = bmo.countBlocks();
context: /*
   * #%L
   * Fork of Apache Jakarta POI.
   * %%
   * Copyright (C) 2008 - 2016 Open Microscopy Environment:
   *   - Board of Regents of the University of Wisconsin-Madison
   *   - Glencoe Software, Inc.
   *   - University of Dundee
   * %%
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   * #L%
   */
  
  /* ====================================================================
     Licensed to the Apache Software Foundation (ASF) under one or more
     contributor license agreements.  See the NOTICE file distributed with
     this work for additional information regarding copyright ownership.
     The ASF licenses this file to You under the Apache License, Version 2.0
     (the "License"); you may not use this file except in compliance with
     the License.  You may obtain a copy of the License at
  
         http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
  ==================================================================== */
          
  
  package loci.poi.poifs.filesystem;
  
  import java.io.*;
  
  import java.util.*;
  
  import loci.common.*;
  
  import loci.poi.poifs.common.POIFSConstants;
  import loci.poi.poifs.dev.POIFSViewable;
  import loci.poi.poifs.property.DirectoryProperty;
  import loci.poi.poifs.property.DocumentProperty;
  import loci.poi.poifs.property.Property;
  import loci.poi.poifs.property.PropertyTable;
  import loci.poi.poifs.storage.BATBlock;
  import loci.poi.poifs.storage.BlockAllocationTableReader;
  import loci.poi.poifs.storage.BlockAllocationTableWriter;
  import loci.poi.poifs.storage.BlockList;
  import loci.poi.poifs.storage.BlockWritable;
  import loci.poi.poifs.storage.HeaderBlockReader;
  import loci.poi.poifs.storage.HeaderBlockWriter;
  import loci.poi.poifs.storage.RawDataBlock;
  import loci.poi.poifs.storage.RawDataBlockList;
  import loci.poi.poifs.storage.SmallBlockTableReader;
  import loci.poi.poifs.storage.SmallBlockTableWriter;
  import loci.poi.poifs.storage.SmallDocumentBlock;
  import loci.poi.poifs.storage.ListManagedBlock;
  import loci.poi.poifs.storage.SmallDocumentBlockList;
  import loci.poi.util.*;
  
  /**
   * This is the main class of the POIFS system; it manages the entire
   * life cycle of the filesystem.
   *
   * @author Marc Johnson (mjohnson at apache dot org)
   */
  
  public class POIFSFileSystem
      implements POIFSViewable
  {
      private PropertyTable _property_table;
      private List          _documents;
      private DirectoryNode _root;
      private int bigBlockSize;
      private RandomAccessInputStream stream;
  
      /**
       * Constructor, intended for writing
       */
  
      public POIFSFileSystem()
      {
          _property_table = new PropertyTable();
          _documents      = new ArrayList();
          _root           = null;
      }
  
      /**
       * Create a POIFSFileSystem from an InputStream
       *
       * @param stream the InputStream from which to read the data
       *
       * @exception IOException on errors reading, or on invalid data
       */
  
      public POIFSFileSystem(final RandomAccessInputStream stream, int size)
          throws IOException
      {
          this();
          this.stream = stream;
  
          // read the header block from the stream
          HeaderBlockReader header_block_reader = new HeaderBlockReader(stream,
            size);
  
          // read the rest of the stream into blocks
          RawDataBlockList  data_blocks         = new RawDataBlockList(stream,
            size);
  
          // set up the block allocation table (necessary for the
          // data_blocks to be manageable
          new BlockAllocationTableReader(header_block_reader.getBATCount(),
                                         header_block_reader.getBATArray(),
                                         header_block_reader.getXBATCount(),
                                         header_block_reader.getXBATIndex(),
                                         data_blocks, size);
  
          // get property table from the document
          PropertyTable properties =
              new PropertyTable(header_block_reader.getPropertyStart(),
                                data_blocks);
  
          // init documents
          BlockList small = SmallBlockTableReader.getSmallDocumentBlocks(
            data_blocks, properties.getRoot(),
            header_block_reader.getSBATStart());
  
          processProperties(small, data_blocks,
            properties.getRoot().getChildren(), null);
      }
  
      /**
       * Create a new document to be added to the root directory
       *
       * @param stream the InputStream from which the document's data
       *               will be obtained
       * @param name the name of the new POIFSDocument
       *
       * @return the new DocumentEntry
       *
       * @exception IOException on error creating the new POIFSDocument
       */
  
      public DocumentEntry createDocument(final RandomAccessInputStream stream,
                                          final String name)
          throws IOException
      {
          return getRoot().createDocument(name, stream);
      }
  
      /**
       * create a new DocumentEntry in the root entry; the data will be
       * provided later
       *
       * @param name the name of the new DocumentEntry
       * @param size the size of the new DocumentEntry
       * @param writer the writer of the new DocumentEntry
       *
       * @return the new DocumentEntry
       *
       * @exception IOException
       */
  
      public DocumentEntry createDocument(final String name, final int size,
                                          final POIFSWriterListener writer)
          throws IOException
      {
          return getRoot().createDocument(name, size, writer);
      }
  
      /**
       * create a new DirectoryEntry in the root directory
       *
       * @param name the name of the new DirectoryEntry
       *
       * @return the new DirectoryEntry
       *
       * @exception IOException on name duplication
       */
  
      public DirectoryEntry createDirectory(final String name)
          throws IOException
      {
          return getRoot().createDirectory(name);
      }
  
      /**
       * Write the filesystem out
       *
       * @param stream the OutputStream to which the filesystem will be
       *               written
       *
       * @exception IOException thrown on errors writing to the stream
       */
  
      public void writeFilesystem(final OutputStream stream)
          throws IOException
      {
  
          // get the property table ready
          _property_table.preWrite();
  
          // create the small block store, and the SBAT
          SmallBlockTableWriter      sbtw       =
              new SmallBlockTableWriter(_documents, _property_table.getRoot());
  
          // create the block allocation table
          BlockAllocationTableWriter bat        =
              new BlockAllocationTableWriter();
  
          // create a list of BATManaged objects: the documents plus the
          // property table and the small block table
          List                       bm_objects = new ArrayList();
  
          bm_objects.addAll(_documents);
          bm_objects.add(_property_table);
          bm_objects.add(sbtw);
          bm_objects.add(sbtw.getSBAT());
  
          // walk the list, allocating space for each and assigning each
          // a starting block number
          Iterator iter = bm_objects.iterator();
  
          while (iter.hasNext())
          {
              BATManaged bmo         = ( BATManaged ) iter.next();
  