KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hpsf > basic > Util


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.hpsf.basic;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.EOFException JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.LinkedList JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 import org.apache.poi.hpsf.PropertySet;
37 import org.apache.poi.poifs.eventfilesystem.POIFSReader;
38 import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
39 import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
40
41
42
43 /**
44  * <p>Static utility methods needed by the HPSF test cases.</p>
45  *
46  * @author Rainer Klute (klute@rainer-klute.de)
47  * @since 2002-07-20
48  * @version $Id: Util.java,v 1.9 2004/04/09 13:05:34 glens Exp $
49  */

50 public class Util
51 {
52
53     /**
54      * <p>Reads bytes from an input stream and writes them to an
55      * output stream until end of file is encountered.</p>
56      *
57      * @param in the input stream to read from
58      *
59      * @param out the output stream to write to
60      *
61      * @exception IOException if an I/O exception occurs
62      */

63     public static void copy(final InputStream JavaDoc in, final OutputStream JavaDoc out)
64         throws IOException JavaDoc
65     {
66         final int BUF_SIZE = 1000;
67         byte[] b = new byte[BUF_SIZE];
68         int read;
69         boolean eof = false;
70         while (!eof)
71         {
72             try
73             {
74                 read = in.read(b, 0, BUF_SIZE);
75                 if (read > 0)
76                     out.write(b, 0, read);
77                 else
78                     eof = true;
79             }
80             catch (EOFException JavaDoc ex)
81             {
82                 eof = true;
83             }
84         }
85     }
86
87
88
89     /**
90      * <p>Reads all files from a POI filesystem and returns them as an
91      * array of {@link POIFile} instances. This method loads all files
92      * into memory and thus does not cope well with large POI
93      * filessystems.</p>
94      *
95      * @param poiFs The name of the POI filesystem as seen by the
96      * operating system. (This is the "filename".)
97      *
98      * @return The POI files. The elements are ordered in the same way
99      * as the files in the POI filesystem.
100      *
101      * @exception FileNotFoundException if the file containing the POI
102      * filesystem does not exist
103      *
104      * @exception IOException if an I/O exception occurs
105      */

106     public static POIFile[] readPOIFiles(final File JavaDoc poiFs)
107         throws FileNotFoundException JavaDoc, IOException JavaDoc
108     {
109         return readPOIFiles(poiFs, null);
110     }
111
112
113
114     /**
115      * <p>Reads a set of files from a POI filesystem and returns them
116      * as an array of {@link POIFile} instances. This method loads all
117      * files into memory and thus does not cope well with large POI
118      * filessystems.</p>
119      *
120      * @param poiFs The name of the POI filesystem as seen by the
121      * operating system. (This is the "filename".)
122      *
123      * @param poiFiles The names of the POI files to be read.
124      *
125      * @return The POI files. The elements are ordered in the same way
126      * as the files in the POI filesystem.
127      *
128      * @exception FileNotFoundException if the file containing the POI
129      * filesystem does not exist
130      *
131      * @exception IOException if an I/O exception occurs
132      */

133     public static POIFile[] readPOIFiles(final File JavaDoc poiFs,
134                                          final String JavaDoc[] poiFiles)
135         throws FileNotFoundException JavaDoc, IOException JavaDoc
136     {
137         final List JavaDoc files = new ArrayList JavaDoc();
138         POIFSReader r = new POIFSReader();
139         POIFSReaderListener pfl = new POIFSReaderListener()
140         {
141             public void processPOIFSReaderEvent(final POIFSReaderEvent event)
142             {
143                 try
144                 {
145                     final POIFile f = new POIFile();
146                     f.setName(event.getName());
147                     f.setPath(event.getPath());
148                     final InputStream JavaDoc in = event.getStream();
149                     final ByteArrayOutputStream JavaDoc out =
150                         new ByteArrayOutputStream JavaDoc();
151                     Util.copy(in, out);
152                     out.close();
153                     f.setBytes(out.toByteArray());
154                     files.add(f);
155                 }
156                 catch (IOException JavaDoc ex)
157                 {
158                     ex.printStackTrace();
159                     throw new RuntimeException JavaDoc(ex.getMessage());
160                 }
161             }
162         };
163         if (poiFiles == null)
164             /* Register the listener for all POI files. */
165             r.registerListener(pfl);
166         else
167             /* Register the listener for the specified POI files
168              * only. */

169             for (int i = 0; i < poiFiles.length; i++)
170                 r.registerListener(pfl, poiFiles[i]);
171
172         /* Read the POI filesystem. */
173         r.read(new FileInputStream JavaDoc(poiFs));
174         POIFile[] result = new POIFile[files.size()];
175         for (int i = 0; i < result.length; i++)
176             result[i] = (POIFile) files.get(i);
177         return result;
178     }
179
180
181
182     /**
183      * <p>Read all files from a POI filesystem which are property set streams
184      * and returns them as an array of {@link org.apache.poi.hpsf.PropertySet}
185      * instances.</p>
186      *
187      * @param poiFs The name of the POI filesystem as seen by the
188      * operating system. (This is the "filename".)
189      *
190      * @return The property sets. The elements are ordered in the same way
191      * as the files in the POI filesystem.
192      *
193      * @exception FileNotFoundException if the file containing the POI
194      * filesystem does not exist
195      *
196      * @exception IOException if an I/O exception occurs
197      */

198     public static POIFile[] readPropertySets(final File JavaDoc poiFs)
199         throws FileNotFoundException JavaDoc, IOException JavaDoc
200     {
201         final List JavaDoc files = new ArrayList JavaDoc(7);
202         final POIFSReader r = new POIFSReader();
203         POIFSReaderListener pfl = new POIFSReaderListener()
204         {
205             public void processPOIFSReaderEvent(final POIFSReaderEvent event)
206             {
207                 try
208                 {
209                     final POIFile f = new POIFile();
210                     f.setName(event.getName());
211                     f.setPath(event.getPath());
212                     final InputStream JavaDoc in = event.getStream();
213                     if (PropertySet.isPropertySetStream(in))
214                     {
215                         final ByteArrayOutputStream JavaDoc out =
216                             new ByteArrayOutputStream JavaDoc();
217                         Util.copy(in, out);
218                         out.close();
219                         f.setBytes(out.toByteArray());
220                         files.add(f);
221                     }
222                 }
223                 catch (Exception JavaDoc ex)
224                 {
225                     ex.printStackTrace();
226                     throw new RuntimeException JavaDoc(ex.getMessage());
227                 }
228             }
229         };
230
231         /* Register the listener for all POI files. */
232         r.registerListener(pfl);
233
234         /* Read the POI filesystem. */
235         r.read(new FileInputStream JavaDoc(poiFs));
236         POIFile[] result = new POIFile[files.size()];
237         for (int i = 0; i < result.length; i++)
238             result[i] = (POIFile) files.get(i);
239         return result;
240     }
241
242
243
244     /**
245      * <p>Prints the system properties to System.out.</p>
246      */

247     public static void printSystemProperties()
248     {
249         final Properties JavaDoc p = System.getProperties();
250         final List JavaDoc names = new LinkedList JavaDoc();
251         for (Iterator JavaDoc i = p.keySet().iterator(); i.hasNext();)
252             names.add(i.next());
253         Collections.sort(names);
254         for (final Iterator JavaDoc i = names.iterator(); i.hasNext();)
255         {
256             String JavaDoc name = (String JavaDoc) i.next();
257             String JavaDoc value = (String JavaDoc) p.get(name);
258             System.out.println(name + ": " + value);
259         }
260         System.out.println("Current directory: " +
261                            System.getProperty("user.dir"));
262     }
263
264
265
266 }
267
Popular Tags