KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > storage > PropertyBlock


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.poifs.storage;
20
21 import java.io.*;
22
23 import java.util.*;
24
25 import org.apache.poi.poifs.common.POIFSConstants;
26 import org.apache.poi.poifs.property.Property;
27 import org.apache.poi.util.IntegerField;
28 import org.apache.poi.util.LittleEndian;
29 import org.apache.poi.util.LittleEndianConsts;
30
31 /**
32  * A block of Property instances
33  *
34  * @author Marc Johnson (mjohnson at apache dot org)
35  */

36
37 public class PropertyBlock
38     extends BigBlock
39 {
40     private static final int _properties_per_block =
41         POIFSConstants.BIG_BLOCK_SIZE / POIFSConstants.PROPERTY_SIZE;
42     private Property[] _properties;
43
44     /**
45      * Create a single instance initialized with default values
46      *
47      * @param properties the properties to be inserted
48      * @param offset the offset into the properties array
49      */

50
51     private PropertyBlock(final Property [] properties, final int offset)
52     {
53         _properties = new Property[ _properties_per_block ];
54         for (int j = 0; j < _properties_per_block; j++)
55         {
56             _properties[ j ] = properties[ j + offset ];
57         }
58     }
59
60     /**
61      * Create an array of PropertyBlocks from an array of Property
62      * instances, creating empty Property instances to make up any
63      * shortfall
64      *
65      * @param properties the Property instances to be converted into
66      * PropertyBlocks, in a java List
67      *
68      * @return the array of newly created PropertyBlock instances
69      */

70
71     public static BlockWritable [] createPropertyBlockArray(
72             final List properties)
73     {
74         int block_count =
75             (properties.size() + _properties_per_block - 1)
76             / _properties_per_block;
77         Property[] to_be_written =
78             new Property[ block_count * _properties_per_block ];
79
80         System.arraycopy(properties.toArray(new Property[ 0 ]), 0,
81                          to_be_written, 0, properties.size());
82         for (int j = properties.size(); j < to_be_written.length; j++)
83         {
84
85             // create an instance of an anonymous inner class that
86
// extends Property
87
to_be_written[ j ] = new Property()
88             {
89                 protected void preWrite()
90                 {
91                 }
92
93                 public boolean isDirectory()
94                 {
95                     return false;
96                 }
97             };
98         }
99         BlockWritable[] rvalue = new BlockWritable[ block_count ];
100
101         for (int j = 0; j < block_count; j++)
102         {
103             rvalue[ j ] = new PropertyBlock(to_be_written,
104                                             j * _properties_per_block);
105         }
106         return rvalue;
107     }
108
109     /* ********** START extension of BigBlock ********** */
110
111     /**
112      * Write the block's data to an OutputStream
113      *
114      * @param stream the OutputStream to which the stored data should
115      * be written
116      *
117      * @exception IOException on problems writing to the specified
118      * stream
119      */

120
121     void writeData(final OutputStream stream)
122         throws IOException
123     {
124         for (int j = 0; j < _properties_per_block; j++)
125         {
126             _properties[ j ].writeData(stream);
127         }
128     }
129
130     /* ********** END extension of BigBlock ********** */
131 } // end public class PropertyBlock
132

133
Popular Tags