001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.poifs.storage;
019:
020: import java.io.*;
021:
022: import java.util.*;
023:
024: import org.apache.poi.poifs.common.POIFSConstants;
025: import org.apache.poi.poifs.property.Property;
026: import org.apache.poi.util.IntegerField;
027: import org.apache.poi.util.LittleEndian;
028: import org.apache.poi.util.LittleEndianConsts;
029:
030: /**
031: * A block of Property instances
032: *
033: * @author Marc Johnson (mjohnson at apache dot org)
034: */
035:
036: public class PropertyBlock extends BigBlock {
037: private static final int _properties_per_block = POIFSConstants.BIG_BLOCK_SIZE
038: / POIFSConstants.PROPERTY_SIZE;
039: private Property[] _properties;
040:
041: /**
042: * Create a single instance initialized with default values
043: *
044: * @param properties the properties to be inserted
045: * @param offset the offset into the properties array
046: */
047:
048: private PropertyBlock(final Property[] properties, final int offset) {
049: _properties = new Property[_properties_per_block];
050: for (int j = 0; j < _properties_per_block; j++) {
051: _properties[j] = properties[j + offset];
052: }
053: }
054:
055: /**
056: * Create an array of PropertyBlocks from an array of Property
057: * instances, creating empty Property instances to make up any
058: * shortfall
059: *
060: * @param properties the Property instances to be converted into
061: * PropertyBlocks, in a java List
062: *
063: * @return the array of newly created PropertyBlock instances
064: */
065:
066: public static BlockWritable[] createPropertyBlockArray(
067: final List properties) {
068: int block_count = (properties.size() + _properties_per_block - 1)
069: / _properties_per_block;
070: Property[] to_be_written = new Property[block_count
071: * _properties_per_block];
072:
073: System.arraycopy(properties.toArray(new Property[0]), 0,
074: to_be_written, 0, properties.size());
075: for (int j = properties.size(); j < to_be_written.length; j++) {
076:
077: // create an instance of an anonymous inner class that
078: // extends Property
079: to_be_written[j] = new Property() {
080: protected void preWrite() {
081: }
082:
083: public boolean isDirectory() {
084: return false;
085: }
086: };
087: }
088: BlockWritable[] rvalue = new BlockWritable[block_count];
089:
090: for (int j = 0; j < block_count; j++) {
091: rvalue[j] = new PropertyBlock(to_be_written, j
092: * _properties_per_block);
093: }
094: return rvalue;
095: }
096:
097: /* ********** START extension of BigBlock ********** */
098:
099: /**
100: * Write the block's data to an OutputStream
101: *
102: * @param stream the OutputStream to which the stored data should
103: * be written
104: *
105: * @exception IOException on problems writing to the specified
106: * stream
107: */
108:
109: void writeData(final OutputStream stream) throws IOException {
110: for (int j = 0; j < _properties_per_block; j++) {
111: _properties[j].writeData(stream);
112: }
113: }
114:
115: /* ********** END extension of BigBlock ********** */
116: } // end public class PropertyBlock
|