poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1050764 - in /poi/trunk/src: java/org/apache/poi/poifs/dev/ java/org/apache/poi/poifs/eventfilesystem/ java/org/apache/poi/poifs/filesystem/ java/org/apache/poi/poifs/storage/ testcases/org/apache/poi/poifs/filesystem/ testcases/org/apache...
Date Sun, 19 Dec 2010 05:54:11 GMT
Author: nick
Date: Sun Dec 19 05:54:11 2010
New Revision: 1050764

URL: http://svn.apache.org/viewvc?rev=1050764&view=rev
Log:
Start to merge the POIFS classes HeaderBlockReader and HeaderBlockWriter into a common HeaderBlock
class

Added:
    poi/trunk/src/java/org/apache/poi/poifs/storage/HeaderBlock.java
    poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockReading.java
      - copied, changed from r1050757, poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockReader.java
    poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockWriting.java
      - copied, changed from r1050757, poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockWriter.java
Removed:
    poi/trunk/src/java/org/apache/poi/poifs/storage/HeaderBlockReader.java
    poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockReader.java
    poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockWriter.java
Modified:
    poi/trunk/src/java/org/apache/poi/poifs/dev/POIFSHeaderDumper.java
    poi/trunk/src/java/org/apache/poi/poifs/eventfilesystem/POIFSReader.java
    poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java
    poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java
    poi/trunk/src/testcases/org/apache/poi/poifs/storage/AllPOIFSStorageTests.java
    poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestBlockAllocationTableReader.java

Modified: poi/trunk/src/java/org/apache/poi/poifs/dev/POIFSHeaderDumper.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/dev/POIFSHeaderDumper.java?rev=1050764&r1=1050763&r2=1050764&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/dev/POIFSHeaderDumper.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/dev/POIFSHeaderDumper.java Sun Dec 19 05:54:11
2010
@@ -18,21 +18,16 @@
 package org.apache.poi.poifs.dev;
 
 import java.io.FileInputStream;
-import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
-import java.util.Iterator;
 
 import org.apache.poi.poifs.common.POIFSBigBlockSize;
 import org.apache.poi.poifs.common.POIFSConstants;
-import org.apache.poi.poifs.filesystem.DirectoryNode;
-import org.apache.poi.poifs.filesystem.DocumentNode;
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.poifs.property.PropertyTable;
 import org.apache.poi.poifs.storage.BlockAllocationTableReader;
 import org.apache.poi.poifs.storage.BlockList;
-import org.apache.poi.poifs.storage.HeaderBlockReader;
+import org.apache.poi.poifs.storage.HeaderBlock;
 import org.apache.poi.poifs.storage.ListManagedBlock;
 import org.apache.poi.poifs.storage.RawDataBlockList;
 import org.apache.poi.poifs.storage.SmallBlockTableReader;
@@ -67,51 +62,50 @@ public class POIFSHeaderDumper {
 		InputStream inp = new FileInputStream(filename);
 		
 		// Header
-		HeaderBlockReader header_block_reader = 
-		   new HeaderBlockReader(inp);
-		displayHeader(header_block_reader);
+		HeaderBlock header_block = new HeaderBlock(inp);
+		displayHeader(header_block);
 		
 		// Raw blocks
-      POIFSBigBlockSize bigBlockSize = header_block_reader.getBigBlockSize();
+      POIFSBigBlockSize bigBlockSize = header_block.getBigBlockSize();
       RawDataBlockList data_blocks = new RawDataBlockList(inp, bigBlockSize);
       displayRawBlocksSummary(data_blocks);
       
       // Main FAT Table
       BlockAllocationTableReader batReader =
          new BlockAllocationTableReader(
-            header_block_reader.getBigBlockSize(),
-            header_block_reader.getBATCount(),
-            header_block_reader.getBATArray(),
-            header_block_reader.getXBATCount(),
-            header_block_reader.getXBATIndex(),
+            header_block.getBigBlockSize(),
+            header_block.getBATCount(),
+            header_block.getBATArray(),
+            header_block.getXBATCount(),
+            header_block.getXBATIndex(),
             data_blocks);
       displayBATReader(batReader);
 
       // Properties Table
       PropertyTable properties =
          new PropertyTable(
-               header_block_reader.getBigBlockSize(),
-               header_block_reader.getPropertyStart(),
+               header_block.getBigBlockSize(),
+               header_block.getPropertyStart(),
                data_blocks);
       
       // Mini Fat
       BlockList sbat = 
          SmallBlockTableReader.getSmallDocumentBlocks(
                bigBlockSize, data_blocks, properties.getRoot(),
-               header_block_reader.getSBATStart()
+               header_block.getSBATStart()
          );
    }
 
-	public static void displayHeader(HeaderBlockReader header_block_reader) throws Exception
{
+	public static void displayHeader(HeaderBlock header_block) throws Exception {
 	   System.out.println("Header Details:");
-	   System.out.println(" Block size: " + header_block_reader.getBigBlockSize());
-      System.out.println(" BAT (FAT) header blocks: " + header_block_reader.getBATArray().length);
-      System.out.println(" BAT (FAT) block count: " + header_block_reader.getBATCount());
-      System.out.println(" XBAT (FAT) block count: " + header_block_reader.getXBATCount());
-      System.out.println(" XBAT (FAT) block 1 at: " + header_block_reader.getXBATIndex());
-      System.out.println(" SBAT (MiniFAT) block count: " + header_block_reader.getSBATCount());
-      System.out.println(" SBAT (MiniFAT) block 1 at: " + header_block_reader.getSBATStart());
-      System.out.println(" Property table at: " + header_block_reader.getPropertyStart());
+	   System.out.println(" Block size: " + header_block.getBigBlockSize());
+      System.out.println(" BAT (FAT) header blocks: " + header_block.getBATArray().length);
+      System.out.println(" BAT (FAT) block count: " + header_block.getBATCount());
+      System.out.println(" XBAT (FAT) block count: " + header_block.getXBATCount());
+      System.out.println(" XBAT (FAT) block 1 at: " + header_block.getXBATIndex());
+      System.out.println(" SBAT (MiniFAT) block count: " + header_block.getSBATCount());
+      System.out.println(" SBAT (MiniFAT) block 1 at: " + header_block.getSBATStart());
+      System.out.println(" Property table at: " + header_block.getPropertyStart());
       System.out.println("");
 	}
 

Modified: poi/trunk/src/java/org/apache/poi/poifs/eventfilesystem/POIFSReader.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/eventfilesystem/POIFSReader.java?rev=1050764&r1=1050763&r2=1050764&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/eventfilesystem/POIFSReader.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/eventfilesystem/POIFSReader.java Sun Dec 19 05:54:11
2010
@@ -31,7 +31,7 @@ import org.apache.poi.poifs.property.Pro
 import org.apache.poi.poifs.property.PropertyTable;
 import org.apache.poi.poifs.storage.BlockAllocationTableReader;
 import org.apache.poi.poifs.storage.BlockList;
-import org.apache.poi.poifs.storage.HeaderBlockReader;
+import org.apache.poi.poifs.storage.HeaderBlock;
 import org.apache.poi.poifs.storage.RawDataBlockList;
 import org.apache.poi.poifs.storage.SmallBlockTableReader;
 
@@ -75,32 +75,32 @@ public class POIFSReader
         registryClosed = true;
 
         // read the header block from the stream
-        HeaderBlockReader header_block_reader = new HeaderBlockReader(stream);
+        HeaderBlock header_block = new HeaderBlock(stream);
 
         // read the rest of the stream into blocks
-        RawDataBlockList  data_blocks         = new RawDataBlockList(stream, header_block_reader.getBigBlockSize());
+        RawDataBlockList  data_blocks         = new RawDataBlockList(stream, header_block.getBigBlockSize());
 
         // set up the block allocation table (necessary for the
         // data_blocks to be manageable
-        new BlockAllocationTableReader(header_block_reader.getBigBlockSize(),
-                                       header_block_reader.getBATCount(),
-                                       header_block_reader.getBATArray(),
-                                       header_block_reader.getXBATCount(),
-                                       header_block_reader.getXBATIndex(),
+        new BlockAllocationTableReader(header_block.getBigBlockSize(),
+                                       header_block.getBATCount(),
+                                       header_block.getBATArray(),
+                                       header_block.getXBATCount(),
+                                       header_block.getXBATIndex(),
                                        data_blocks);
 
         // get property table from the document
         PropertyTable properties =
-            new PropertyTable(header_block_reader.getBigBlockSize(),
-                              header_block_reader.getPropertyStart(),
+            new PropertyTable(header_block.getBigBlockSize(),
+                              header_block.getPropertyStart(),
                               data_blocks);
 
         // process documents
         processProperties(SmallBlockTableReader
             .getSmallDocumentBlocks(
-                  header_block_reader.getBigBlockSize(),
+                  header_block.getBigBlockSize(),
                   data_blocks, properties.getRoot(), 
-                  header_block_reader.getSBATStart()), 
+                  header_block.getSBATStart()), 
                   data_blocks, properties.getRoot()
                         .getChildren(), new POIFSDocumentPath());
     }

Modified: poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java?rev=1050764&r1=1050763&r2=1050764&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java Sun Dec 19 05:54:11
2010
@@ -43,7 +43,7 @@ import org.apache.poi.poifs.storage.Bloc
 import org.apache.poi.poifs.storage.BlockList;
 import org.apache.poi.poifs.storage.BlockWritable;
 import org.apache.poi.poifs.storage.HeaderBlockConstants;
-import org.apache.poi.poifs.storage.HeaderBlockReader;
+import org.apache.poi.poifs.storage.HeaderBlock;
 import org.apache.poi.poifs.storage.HeaderBlockWriter;
 import org.apache.poi.poifs.storage.RawDataBlockList;
 import org.apache.poi.poifs.storage.SmallBlockTableReader;
@@ -146,12 +146,12 @@ public class POIFSFileSystem
         this();
         boolean success = false;
 
-        HeaderBlockReader header_block_reader;
+        HeaderBlock header_block;
         RawDataBlockList data_blocks;
         try {
             // read the header block from the stream
-            header_block_reader = new HeaderBlockReader(stream);
-            bigBlockSize = header_block_reader.getBigBlockSize();
+            header_block = new HeaderBlock(stream);
+            bigBlockSize = header_block.getBigBlockSize();
 
             // read the rest of the stream into blocks
             data_blocks = new RawDataBlockList(stream, bigBlockSize);
@@ -163,29 +163,29 @@ public class POIFSFileSystem
 
         // set up the block allocation table (necessary for the
         // data_blocks to be manageable
-        new BlockAllocationTableReader(header_block_reader.getBigBlockSize(),
-                                       header_block_reader.getBATCount(),
-                                       header_block_reader.getBATArray(),
-                                       header_block_reader.getXBATCount(),
-                                       header_block_reader.getXBATIndex(),
+        new BlockAllocationTableReader(header_block.getBigBlockSize(),
+                                       header_block.getBATCount(),
+                                       header_block.getBATArray(),
+                                       header_block.getXBATCount(),
+                                       header_block.getXBATIndex(),
                                        data_blocks);
 
         // get property table from the document
         PropertyTable properties =
             new PropertyTable(bigBlockSize,
-                              header_block_reader.getPropertyStart(),
+                              header_block.getPropertyStart(),
                               data_blocks);
 
         // init documents
         processProperties(
         		SmallBlockTableReader.getSmallDocumentBlocks(
         		      bigBlockSize, data_blocks, properties.getRoot(),
-        				header_block_reader.getSBATStart()
+        				header_block.getSBATStart()
         		),
         		data_blocks,
         		properties.getRoot().getChildren(),
         		null,
-        		header_block_reader.getPropertyStart()
+        		header_block.getPropertyStart()
         );
 
         // For whatever reason CLSID of root is always 0.

Added: poi/trunk/src/java/org/apache/poi/poifs/storage/HeaderBlock.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/storage/HeaderBlock.java?rev=1050764&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/storage/HeaderBlock.java (added)
+++ poi/trunk/src/java/org/apache/poi/poifs/storage/HeaderBlock.java Sun Dec 19 05:54:11 2010
@@ -0,0 +1,343 @@
+/* ====================================================================
+   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 org.apache.poi.poifs.storage;
+
+import static org.apache.poi.poifs.storage.HeaderBlockConstants._bat_array_offset;
+import static org.apache.poi.poifs.storage.HeaderBlockConstants._bat_count_offset;
+import static org.apache.poi.poifs.storage.HeaderBlockConstants._max_bats_in_header;
+import static org.apache.poi.poifs.storage.HeaderBlockConstants._property_start_offset;
+import static org.apache.poi.poifs.storage.HeaderBlockConstants._sbat_start_offset;
+import static org.apache.poi.poifs.storage.HeaderBlockConstants._sbat_block_count_offset;
+import static org.apache.poi.poifs.storage.HeaderBlockConstants._signature;
+import static org.apache.poi.poifs.storage.HeaderBlockConstants._signature_offset;
+import static org.apache.poi.poifs.storage.HeaderBlockConstants._xbat_count_offset;
+import static org.apache.poi.poifs.storage.HeaderBlockConstants._xbat_start_offset;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+import org.apache.poi.poifs.common.POIFSBigBlockSize;
+import org.apache.poi.poifs.common.POIFSConstants;
+import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.util.HexDump;
+import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.IntegerField;
+import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianConsts;
+import org.apache.poi.util.LongField;
+import org.apache.poi.util.POILogFactory;
+import org.apache.poi.util.POILogger;
+import org.apache.poi.util.ShortField;
+
+/**
+ * The block containing the archive header
+ */
+public final class HeaderBlock {
+   private static final POILogger _logger =
+      POILogFactory.getLogger(HeaderBlock.class);
+   
+	/**
+	 * What big block size the file uses. Most files
+	 *  use 512 bytes, but a few use 4096
+	 */
+	private final POIFSBigBlockSize bigBlockSize;
+
+	/** 
+	 * number of big block allocation table blocks (int).
+	 * (Number of FAT Sectors in Microsoft parlance) 
+	 */
+	private int _bat_count;
+
+	/** 
+	 * Start of the property set block (int index of the property set
+	 * chain's first big block).
+	 */
+	private int _property_start;
+
+	/** 
+	 * start of the small block allocation table (int index of small
+	 * block allocation table's first big block)
+	 */
+	private int _sbat_start;
+	/**
+	 * Number of small block allocation table blocks (int)
+	 * (Number of MiniFAT Sectors in Microsoft parlance)
+	 */
+	private int _sbat_count;
+
+	/** 
+	 * Big block index for extension to the big block allocation table
+	 */
+	private int _xbat_start;
+	/**
+	 * Number of big block allocation table blocks (int)
+	 * (Number of DIFAT Sectors in Microsoft parlance)
+	 */
+	private int _xbat_count;
+	
+	/**
+	 * The data. Only ever 512 bytes, because 4096 byte
+	 *  files use zeros for the extra header space.
+	 */
+	private final byte[] _data;
+	
+   private static final byte _default_value = ( byte ) 0xFF;
+
+	/**
+	 * create a new HeaderBlockReader from an InputStream
+	 *
+	 * @param stream the source InputStream
+	 *
+	 * @exception IOException on errors or bad data
+	 */
+	public HeaderBlock(InputStream stream) throws IOException {
+		// Grab the first 512 bytes
+	   // (For 4096 sized blocks, the remaining 3584 bytes are zero)
+		// Then, process the contents
+		this(readFirst512(stream));
+		
+		// Fetch the rest of the block if needed
+		if(bigBlockSize.getBigBlockSize() != 512) {
+		   int rest = bigBlockSize.getBigBlockSize() - 512;
+		   byte[] tmp = new byte[rest];
+		   IOUtils.readFully(stream, tmp);
+		}
+	}
+	
+	public HeaderBlock(ByteBuffer buffer) throws IOException {
+	   this(buffer.array());
+	}
+	
+	private HeaderBlock(byte[] data) throws IOException {
+	   this._data = data;
+	   
+		// verify signature
+		long signature = LittleEndian.getLong(_data, _signature_offset);
+
+		if (signature != _signature) {
+			// Is it one of the usual suspects?
+			byte[] OOXML_FILE_HEADER = POIFSConstants.OOXML_FILE_HEADER;
+			if(_data[0] == OOXML_FILE_HEADER[0] &&
+				_data[1] == OOXML_FILE_HEADER[1] &&
+				_data[2] == OOXML_FILE_HEADER[2] &&
+				_data[3] == OOXML_FILE_HEADER[3]) {
+				throw new OfficeXmlFileException("The supplied data appears to be in the Office 2007+
XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call
a different part of POI to process this data (eg XSSF instead of HSSF)");
+			}
+			if ((signature & 0xFF8FFFFFFFFFFFFFL) == 0x0010000200040009L) {
+				// BIFF2 raw stream starts with BOF (sid=0x0009, size=0x0004, data=0x00t0)
+				throw new IllegalArgumentException("The supplied data appears to be in BIFF2 format.
 "
+						+ "POI only supports BIFF8 format");
+			}
+
+			// Give a generic error
+			throw new IOException("Invalid header signature; read "
+				                  + longToHex(signature) + ", expected "
+				                  + longToHex(_signature));
+		}
+
+
+		// Figure out our block size
+		if (_data[30] == 12) {
+			this.bigBlockSize = POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS;
+		} else if(_data[30] == 9) {
+			this.bigBlockSize = POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS;
+		} else {
+		   throw new IOException("Unsupported blocksize  (2^"+ _data[30] + "). Expected 2^9 or
2^12.");
+		}
+
+	   // Setup the fields to read and write the counts and starts
+      _bat_count      = new IntegerField(_bat_count_offset, data).get();
+      _property_start = new IntegerField(_property_start_offset,_data).get();
+      _sbat_start = new IntegerField(_sbat_start_offset, _data).get();
+      _sbat_count = new IntegerField(_sbat_block_count_offset, _data).get();
+      _xbat_start = new IntegerField(_xbat_start_offset, _data).get();
+      _xbat_count = new IntegerField(_xbat_count_offset, _data).get();
+      
+      // Sanity check values
+      if(_bat_count > _max_bats_in_header) {
+         _logger.log(POILogger.WARN, "Too many BAT blocks listed in header, found " 
+                     + _bat_count + " but the maximum is " + _max_bats_in_header);
+         _bat_count = _max_bats_in_header;
+      }
+	}
+	
+   /**
+    * Create a single instance initialized with default values
+    */
+   public HeaderBlock(POIFSBigBlockSize bigBlockSize) throws IOException
+   {
+      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);
+      
+      // Initialise 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;
+   }
+   
+	private static byte[] readFirst512(InputStream stream) throws IOException {
+      // Grab the first 512 bytes
+      // (For 4096 sized blocks, the remaining 3584 bytes are zero)
+      byte[] data = new byte[512];
+      int bsCount = IOUtils.readFully(stream, data);
+      if(bsCount != 512) {
+         throw alertShortRead(bsCount, 512);
+      }
+      return data;
+	}
+
+	private static String longToHex(long value) {
+		return new String(HexDump.longToHex(value));
+	}
+
+	private static IOException alertShortRead(int pRead, int expectedReadSize) {
+		int read;
+		if (pRead < 0) {
+			//Can't have -1 bytes read in the error message!
+			read = 0;
+		} else {
+			read = pRead;
+		}
+		String type = " byte" + (read == 1 ? (""): ("s"));
+
+		return new IOException("Unable to read entire header; "
+				+ read + type + " read; expected "
+				+ expectedReadSize + " bytes");
+	}
+
+	/**
+	 * get start of Property Table
+	 *
+	 * @return the index of the first block of the Property Table
+	 */
+	public int getPropertyStart() {
+		return _property_start;
+	}
+
+	/**
+	 * @return start of small block (MiniFAT) allocation table
+	 */
+	public int getSBATStart() {
+		return _sbat_start;
+	}
+	public int getSBATCount() {
+	   return _sbat_count;
+	}
+
+	/**
+	 * @return number of BAT blocks
+	 */
+	public int getBATCount() {
+		return _bat_count;
+	}
+
+	/**
+	 * Returns the offsets to the first (up to) 109
+	 *  BAT sectors.
+	 * Any additional BAT sectors are held in the XBAT (DIFAT)
+	 *  sectors in a chain.
+	 * @return BAT offset array
+	 */
+	public int[] getBATArray() {
+      // Read them in
+		int[] result = new int[ _bat_count ];
+		int offset = _bat_array_offset;
+		for (int j = 0; j < _bat_count; j++) {
+			result[ j ] = LittleEndian.getInt(_data, offset);
+			offset     += LittleEndianConsts.INT_SIZE;
+		}
+		return result;
+	}
+
+	/**
+	 * @return XBAT (DIFAT) count
+	 */
+	public int getXBATCount() {
+		return _xbat_count;
+	}
+
+	/**
+	 * @return XBAT (DIFAT) index
+	 */
+	public int getXBATIndex() {
+		return _xbat_start;
+	}
+
+	/**
+	 * @return The Big Block size, normally 512 bytes, sometimes 4096 bytes
+	 */
+	public POIFSBigBlockSize getBigBlockSize() {
+		return bigBlockSize;
+	}
+	
+   /**
+    * 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<POIFSConstants.LARGER_BIG_BLOCK_SIZE;
i++) {
+         stream.write(0);
+      }
+   }
+}

Modified: poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java?rev=1050764&r1=1050763&r2=1050764&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java Sun Dec
19 05:54:11 2010
@@ -29,7 +29,7 @@ import junit.framework.TestCase;
 import org.apache.poi.POIDataSamples;
 import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.poifs.common.POIFSBigBlockSize;
-import org.apache.poi.poifs.storage.HeaderBlockReader;
+import org.apache.poi.poifs.storage.HeaderBlock;
 import org.apache.poi.poifs.storage.RawDataBlockList;
 
 /**
@@ -184,14 +184,14 @@ public final class TestPOIFSFileSystem e
 	   InputStream inp = _samples.openResourceAsStream("BlockSize4096.zvi");
 	   
 	   // First up, check that we can process the header properly
-      HeaderBlockReader header_block_reader = new HeaderBlockReader(inp);
-      POIFSBigBlockSize bigBlockSize = header_block_reader.getBigBlockSize();
+      HeaderBlock header_block = new HeaderBlock(inp);
+      POIFSBigBlockSize bigBlockSize = header_block.getBigBlockSize();
       assertEquals(4096, bigBlockSize.getBigBlockSize());
       
       // Check the fat info looks sane
-      assertEquals(109, header_block_reader.getBATArray().length);
-      assertTrue(header_block_reader.getBATCount() > 0);
-      assertEquals(0, header_block_reader.getXBATCount());
+      assertEquals(1, header_block.getBATArray().length);
+      assertEquals(1, header_block.getBATCount());
+      assertEquals(0, header_block.getXBATCount());
       
       // Now check we can get the basic fat
       RawDataBlockList data_blocks = new RawDataBlockList(inp, bigBlockSize);

Modified: poi/trunk/src/testcases/org/apache/poi/poifs/storage/AllPOIFSStorageTests.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/storage/AllPOIFSStorageTests.java?rev=1050764&r1=1050763&r2=1050764&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/storage/AllPOIFSStorageTests.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/storage/AllPOIFSStorageTests.java Sun Dec
19 05:54:11 2010
@@ -33,8 +33,8 @@ public final class AllPOIFSStorageTests 
 		result.addTestSuite(TestBlockAllocationTableWriter.class);
 		result.addTestSuite(TestBlockListImpl.class);
 		result.addTestSuite(TestDocumentBlock.class);
-		result.addTestSuite(TestHeaderBlockReader.class);
-		result.addTestSuite(TestHeaderBlockWriter.class);
+		result.addTestSuite(TestHeaderBlockReading.class);
+		result.addTestSuite(TestHeaderBlockWriting.class);
 		result.addTestSuite(TestPropertyBlock.class);
 		result.addTestSuite(TestRawDataBlock.class);
 		result.addTestSuite(TestRawDataBlockList.class);

Modified: poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestBlockAllocationTableReader.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestBlockAllocationTableReader.java?rev=1050764&r1=1050763&r2=1050764&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestBlockAllocationTableReader.java
(original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestBlockAllocationTableReader.java
Sun Dec 19 05:54:11 2010
@@ -405,10 +405,10 @@ public final class TestBlockAllocationTa
 
 		// similar code to POIFSFileSystem.<init>:
 		InputStream stream = new ByteArrayInputStream(data);
-		HeaderBlockReader hb;
+		HeaderBlock hb;
 		RawDataBlockList dataBlocks;
 		try {
-			hb = new HeaderBlockReader(stream);
+			hb = new HeaderBlock(stream);
 			dataBlocks = new RawDataBlockList(stream, bigBlockSize);
 		} catch (IOException e) {
 			throw new RuntimeException(e);
@@ -419,7 +419,11 @@ public final class TestBlockAllocationTa
 					hb.getXBATIndex(), dataBlocks);
 		} catch (IOException e) {
 			// expected during successful test
-			assertEquals("Block count 538976257 is too high. POI maximum is 65535.", e.getMessage());
+			assertEquals(
+			      "Your file contains 0 sectors, but the initial DIFAT array at index 0 referenced
block # 538976288. This isn't allowed and  your file is corrupt", 
+			      e.getMessage()
+			);
+//       assertEquals("Block count 538976257 is too high. POI maximum is 65535.", e.getMessage());
 		} catch (OutOfMemoryError e) {
 			if (e.getStackTrace()[1].getMethodName().equals("testBadSectorAllocationTableSize")) {
 				throw new AssertionFailedError("Identified bug 48085");

Copied: poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockReading.java (from
r1050757, poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockReader.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockReading.java?p2=poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockReading.java&p1=poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockReader.java&r1=1050757&r2=1050764&rev=1050764&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockReader.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockReading.java Sun Dec
19 05:54:11 2010
@@ -27,7 +27,7 @@ import junit.framework.TestCase;
  *
  * @author Marc Johnson
  */
-public final class TestHeaderBlockReader extends TestCase {
+public final class TestHeaderBlockReading extends TestCase {
 
 	public void testConstructors() throws IOException {
 		String[] hexData = {
@@ -49,7 +49,7 @@ public final class TestHeaderBlockReader
 			"FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
FF FF FF",
 		};
 		byte[] content = RawDataUtil.decode(hexData);
-		HeaderBlockReader block = new HeaderBlockReader(new ByteArrayInputStream(content));
+		HeaderBlock block = new HeaderBlock(new ByteArrayInputStream(content));
 
 		assertEquals(-2, block.getPropertyStart());
 
@@ -58,7 +58,7 @@ public final class TestHeaderBlockReader
 
 		System.arraycopy(content, 0, shortblock, 0, 511);
 		try {
-			block = new HeaderBlockReader(new ByteArrayInputStream(shortblock));
+			block = new HeaderBlock(new ByteArrayInputStream(shortblock));
 			fail("Should have caught IOException reading a short block");
 		} catch (IOException ignored) {
 
@@ -69,7 +69,7 @@ public final class TestHeaderBlockReader
 		for (int index = 0; index < 8; index++) {
 			content[index] = (byte) (content[index] - 1);
 			try {
-				block = new HeaderBlockReader(new ByteArrayInputStream(content));
+				block = new HeaderBlock(new ByteArrayInputStream(content));
 				fail("Should have caught IOException corrupting byte " + index);
 			} catch (IOException ignored) {
 

Copied: poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockWriting.java (from
r1050757, poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockWriter.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockWriting.java?p2=poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockWriting.java&p1=poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockWriter.java&r1=1050757&r2=1050764&rev=1050764&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockWriter.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockWriting.java Sun Dec
19 05:54:11 2010
@@ -32,7 +32,7 @@ import org.apache.poi.util.LittleEndianC
  *
  * @author Marc Johnson
  */
-public final class TestHeaderBlockWriter extends TestCase {
+public final class TestHeaderBlockWriting extends TestCase {
 
 	private static void confirmEqual(String[] expectedDataHexDumpLines, byte[] actual) {
 		byte[] expected = RawDataUtil.decode(expectedDataHexDumpLines);
@@ -78,8 +78,8 @@ public final class TestHeaderBlockWriter
 		block.setPropertyStart(0x87654321);
 		output = new ByteArrayOutputStream(512);
 		block.writeBlocks(output);
-		assertEquals(0x87654321, new HeaderBlockReader(new ByteArrayInputStream(output
-				.toByteArray())).getPropertyStart());
+		assertEquals(0x87654321, new HeaderBlock(
+		      new ByteArrayInputStream(output.toByteArray())).getPropertyStart());
 	}
 
 	/**



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message