From 626e01a4e3287ef2cfe417d834d4a70218c6cb50 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sun, 19 Dec 2010 06:35:54 +0000 Subject: [PATCH] Move more of the logic from HeaderBlockWriter to HeaderBlock git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1050767 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/poifs/storage/HeaderBlock.java | 84 +++++++++++--- .../poi/poifs/storage/HeaderBlockWriter.java | 109 ++++-------------- 2 files changed, 91 insertions(+), 102 deletions(-) diff --git a/src/java/org/apache/poi/poifs/storage/HeaderBlock.java b/src/java/org/apache/poi/poifs/storage/HeaderBlock.java index e12ec447d5..b3bf9a5a16 100644 --- a/src/java/org/apache/poi/poifs/storage/HeaderBlock.java +++ b/src/java/org/apache/poi/poifs/storage/HeaderBlock.java @@ -17,17 +17,6 @@ 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; @@ -37,7 +26,6 @@ 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; @@ -51,7 +39,7 @@ import org.apache.poi.util.ShortField; /** * The block containing the archive header */ -public final class HeaderBlock { +public final class HeaderBlock implements HeaderBlockConstants { private static final POILogger _logger = POILogFactory.getLogger(HeaderBlock.class); @@ -183,7 +171,7 @@ public final class HeaderBlock { /** * Create a single instance initialized with default values */ - public HeaderBlock(POIFSBigBlockSize bigBlockSize) throws IOException + public HeaderBlock(POIFSBigBlockSize bigBlockSize) { this.bigBlockSize = bigBlockSize; @@ -255,6 +243,14 @@ public final class HeaderBlock { public int getPropertyStart() { return _property_start; } + /** + * Set start of Property Table + * + * @param startBlock the index of the first block of the Property Table + */ + public void setPropertyStart(final int startBlock) { + _property_start = startBlock; + } /** * @return start of small block (MiniFAT) allocation table @@ -265,6 +261,25 @@ public final class HeaderBlock { public int getSBATCount() { return _sbat_count; } + + /** + * Set start of small block allocation table + * + * @param startBlock the index of the first big block of the small + * block allocation table + */ + public void setSBATStart(final int startBlock) { + _sbat_start = startBlock; + } + /** + * Set count of SBAT blocks + * + * @param count the number of SBAT blocks + */ + public void setSBATBlockCount(final int count) + { + _sbat_count = count; + } /** * @return number of BAT blocks @@ -272,6 +287,13 @@ public final class HeaderBlock { public int getBATCount() { return _bat_count; } + /** + * Sets the number of BAT blocks that are used. + * This is the number used in both the BAT and XBAT. + */ + public void setBATCount(final int count) { + _bat_count = count; + } /** * Returns the offsets to the first (up to) 109 @@ -282,7 +304,7 @@ public final class HeaderBlock { */ public int[] getBATArray() { // Read them in - int[] result = new int[ _bat_count ]; + int[] result = new int[ Math.min(_bat_count,_max_bats_in_header) ]; int offset = _bat_array_offset; for (int j = 0; j < _bat_count; j++) { result[ j ] = LittleEndian.getInt(_data, offset); @@ -290,6 +312,24 @@ public final class HeaderBlock { } return result; } + /** + * 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 _max_bats_in_header) { int excess_blocks = blockCount - _max_bats_in_header; @@ -144,14 +84,14 @@ public class HeaderBlockWriter } rvalue = BATBlock.createXBATBlocks(bigBlockSize, excess_block_array, startBlock + blockCount); - _xbat_start.set(startBlock + blockCount, _data); + _header_block.setXBATStart(startBlock + blockCount); } else { rvalue = BATBlock.createXBATBlocks(bigBlockSize, new int[ 0 ], 0); - _xbat_start.set(POIFSConstants.END_OF_CHAIN, _data); + _header_block.setXBATStart(POIFSConstants.END_OF_CHAIN); } - _xbat_count.set(rvalue.length, _data); + _header_block.setXBATCount(rvalue.length); return rvalue; } @@ -161,10 +101,9 @@ public class HeaderBlockWriter * @param startBlock the index of the first block of the Property * Table */ - public void setPropertyStart(final int startBlock) { - _property_start.set(startBlock, _data); + _header_block.setPropertyStart(startBlock); } /** @@ -173,10 +112,9 @@ public class HeaderBlockWriter * @param startBlock the index of the first big block of the small * block allocation table */ - public void setSBATStart(final int startBlock) { - _sbat_start.set(startBlock, _data); + _header_block.setSBATStart(startBlock); } /** @@ -184,10 +122,9 @@ public class HeaderBlockWriter * * @param count the number of SBAT blocks */ - public void setSBATBlockCount(final int count) { - _sbat_block_count.set(count, _data); + _header_block.setSBATBlockCount(count); } /** @@ -219,10 +156,10 @@ public class HeaderBlockWriter * stream */ - void writeData(final OutputStream stream) + public void writeBlocks(final OutputStream stream) throws IOException { - doWriteData(stream, _data); + _header_block.writeData(stream); } /* ********** END extension of BigBlock ********** */ -- 2.39.5