KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > demo > PropertySetsReader


1 /*********************************************************************
2 *
3 * Copyright (C) 2002 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.demo;
21
22 import java.io.OutputStream JavaDoc;
23 import java.io.BufferedWriter JavaDoc;
24 import java.io.OutputStreamWriter JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 import jxl.WorkbookSettings;
29 import jxl.read.biff.CompoundFile;
30 import jxl.read.biff.BiffException;
31 import jxl.biff.BaseCompoundFile;
32
33 /**
34  * Generates a biff dump of the specified excel file
35  */

36 class PropertySetsReader
37 {
38   private BufferedWriter JavaDoc writer;
39   private CompoundFile compoundFile;
40
41   /**
42    * Constructor
43    *
44    * @param file the file
45    * @param propertySet the property set to read
46    * @param os the output stream
47    * @exception IOException
48    * @exception BiffException
49    */

50   public PropertySetsReader(java.io.File JavaDoc file, String JavaDoc propertySet,
51                             OutputStream JavaDoc os)
52     throws IOException JavaDoc, BiffException
53   {
54     writer = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(os));
55     FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
56
57     int initialFileSize = 1024*1024; // 1mb
58
int arrayGrowSize = 1024*1024;// 1mb
59

60     byte[] d = new byte[initialFileSize];
61     int bytesRead = fis.read(d);
62     int pos = bytesRead;
63
64     while (bytesRead != -1)
65     {
66       if (pos >= d.length)
67       {
68         // Grow the array
69
byte newArray[] = new byte[d.length + arrayGrowSize];
70         System.arraycopy(d, 0, newArray, 0, d.length);
71         d = newArray;
72       }
73       bytesRead = fis.read(d, pos, d.length - pos);
74       pos += bytesRead;
75     }
76
77     bytesRead = pos + 1;
78
79     compoundFile = new CompoundFile(d, new WorkbookSettings());
80     fis.close();
81
82     if (propertySet == null)
83     {
84       displaySets();
85     }
86     else
87     {
88       displayPropertySet(propertySet, os);
89     }
90   }
91
92   /**
93    * Displays the properties to the output stream
94    */

95   void displaySets() throws IOException JavaDoc
96   {
97     String JavaDoc[] sets = compoundFile.getPropertySetNames();
98
99     for (int i = 0; i < sets.length ; i++)
100     {
101       BaseCompoundFile.PropertyStorage ps = compoundFile.getPropertySet(sets[i]);
102       writer.write(Integer.toString(i));
103       writer.write(") ");
104       writer.write(sets[i]);
105       writer.write("(type ");
106       writer.write(Integer.toString(ps.type));
107       writer.write(" size ");
108       writer.write(Integer.toString(ps.size));
109       writer.write(" prev " );
110       writer.write(Integer.toString(ps.previous));
111       writer.write(" next " );
112       writer.write(Integer.toString(ps.next));
113       writer.write(" child " );
114       writer.write(Integer.toString(ps.child));
115       writer.write(" start block " );
116       writer.write(Integer.toString(ps.startBlock));
117       writer.write(")");
118       writer.newLine();
119     }
120
121     writer.flush();
122     writer.close();
123   }
124
125   /**
126    * Write the property stream to the output stream
127    */

128   void displayPropertySet(String JavaDoc ps, OutputStream JavaDoc os)
129     throws IOException JavaDoc,BiffException
130   {
131     if (ps.equalsIgnoreCase("SummaryInformation"))
132     {
133       ps = BaseCompoundFile.SUMMARY_INFORMATION_NAME;
134     }
135     else if (ps.equalsIgnoreCase("DocumentSummaryInformation"))
136     {
137       ps = BaseCompoundFile.DOCUMENT_SUMMARY_INFORMATION_NAME;
138     }
139     else if (ps.equalsIgnoreCase("CompObj"))
140     {
141       ps = BaseCompoundFile.COMP_OBJ_NAME;
142     }
143
144     byte[] stream = compoundFile.getStream(ps);
145     os.write(stream);
146   }
147
148 }
149
Popular Tags