Source Code Cross Referenced for Util.java in  » Collaboration » poi-3.0.2-beta2 » org » apache » poi » hpsf » basic » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Collaboration » poi 3.0.2 beta2 » org.apache.poi.hpsf.basic 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.hpsf.basic;
019:
020:        import java.io.ByteArrayOutputStream;
021:        import java.io.EOFException;
022:        import java.io.File;
023:        import java.io.FileInputStream;
024:        import java.io.FileNotFoundException;
025:        import java.io.IOException;
026:        import java.io.InputStream;
027:        import java.io.OutputStream;
028:        import java.util.ArrayList;
029:        import java.util.Collections;
030:        import java.util.Iterator;
031:        import java.util.LinkedList;
032:        import java.util.List;
033:        import java.util.Properties;
034:
035:        import org.apache.poi.hpsf.PropertySet;
036:        import org.apache.poi.poifs.eventfilesystem.POIFSReader;
037:        import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
038:        import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
039:
040:        /**
041:         * <p>Static utility methods needed by the HPSF test cases.</p>
042:         *
043:         * @author Rainer Klute (klute@rainer-klute.de)
044:         * @since 2002-07-20
045:         * @version $Id: Util.java 489730 2006-12-22 19:18:16Z bayard $
046:         */
047:        public class Util {
048:
049:            /**
050:             * <p>Reads bytes from an input stream and writes them to an
051:             * output stream until end of file is encountered.</p>
052:             *
053:             * @param in the input stream to read from
054:             * 
055:             * @param out the output stream to write to
056:             * 
057:             * @exception IOException if an I/O exception occurs
058:             */
059:            public static void copy(final InputStream in, final OutputStream out)
060:                    throws IOException {
061:                final int BUF_SIZE = 1000;
062:                byte[] b = new byte[BUF_SIZE];
063:                int read;
064:                boolean eof = false;
065:                while (!eof) {
066:                    try {
067:                        read = in.read(b, 0, BUF_SIZE);
068:                        if (read > 0)
069:                            out.write(b, 0, read);
070:                        else
071:                            eof = true;
072:                    } catch (EOFException ex) {
073:                        eof = true;
074:                    }
075:                }
076:            }
077:
078:            /**
079:             * <p>Reads all files from a POI filesystem and returns them as an
080:             * array of {@link POIFile} instances. This method loads all files
081:             * into memory and thus does not cope well with large POI
082:             * filessystems.</p>
083:             * 
084:             * @param poiFs The name of the POI filesystem as seen by the
085:             * operating system. (This is the "filename".)
086:             *
087:             * @return The POI files. The elements are ordered in the same way
088:             * as the files in the POI filesystem.
089:             * 
090:             * @exception FileNotFoundException if the file containing the POI 
091:             * filesystem does not exist
092:             * 
093:             * @exception IOException if an I/O exception occurs
094:             */
095:            public static POIFile[] readPOIFiles(final File poiFs)
096:                    throws FileNotFoundException, IOException {
097:                return readPOIFiles(poiFs, null);
098:            }
099:
100:            /**
101:             * <p>Reads a set of files from a POI filesystem and returns them
102:             * as an array of {@link POIFile} instances. This method loads all
103:             * files into memory and thus does not cope well with large POI
104:             * filessystems.</p>
105:             * 
106:             * @param poiFs The name of the POI filesystem as seen by the
107:             * operating system. (This is the "filename".)
108:             *
109:             * @param poiFiles The names of the POI files to be read.
110:             *
111:             * @return The POI files. The elements are ordered in the same way
112:             * as the files in the POI filesystem.
113:             * 
114:             * @exception FileNotFoundException if the file containing the POI 
115:             * filesystem does not exist
116:             * 
117:             * @exception IOException if an I/O exception occurs
118:             */
119:            public static POIFile[] readPOIFiles(final File poiFs,
120:                    final String[] poiFiles) throws FileNotFoundException,
121:                    IOException {
122:                final List files = new ArrayList();
123:                POIFSReader r = new POIFSReader();
124:                POIFSReaderListener pfl = new POIFSReaderListener() {
125:                    public void processPOIFSReaderEvent(
126:                            final POIFSReaderEvent event) {
127:                        try {
128:                            final POIFile f = new POIFile();
129:                            f.setName(event.getName());
130:                            f.setPath(event.getPath());
131:                            final InputStream in = event.getStream();
132:                            final ByteArrayOutputStream out = new ByteArrayOutputStream();
133:                            Util.copy(in, out);
134:                            out.close();
135:                            f.setBytes(out.toByteArray());
136:                            files.add(f);
137:                        } catch (IOException ex) {
138:                            ex.printStackTrace();
139:                            throw new RuntimeException(ex.getMessage());
140:                        }
141:                    }
142:                };
143:                if (poiFiles == null)
144:                    /* Register the listener for all POI files. */
145:                    r.registerListener(pfl);
146:                else
147:                    /* Register the listener for the specified POI files
148:                     * only. */
149:                    for (int i = 0; i < poiFiles.length; i++)
150:                        r.registerListener(pfl, poiFiles[i]);
151:
152:                /* Read the POI filesystem. */
153:                r.read(new FileInputStream(poiFs));
154:                POIFile[] result = new POIFile[files.size()];
155:                for (int i = 0; i < result.length; i++)
156:                    result[i] = (POIFile) files.get(i);
157:                return result;
158:            }
159:
160:            /**
161:             * <p>Read all files from a POI filesystem which are property set streams
162:             * and returns them as an array of {@link org.apache.poi.hpsf.PropertySet}
163:             * instances.</p>
164:             * 
165:             * @param poiFs The name of the POI filesystem as seen by the
166:             * operating system. (This is the "filename".)
167:             *
168:             * @return The property sets. The elements are ordered in the same way
169:             * as the files in the POI filesystem.
170:             * 
171:             * @exception FileNotFoundException if the file containing the POI 
172:             * filesystem does not exist
173:             * 
174:             * @exception IOException if an I/O exception occurs
175:             */
176:            public static POIFile[] readPropertySets(final File poiFs)
177:                    throws FileNotFoundException, IOException {
178:                final List files = new ArrayList(7);
179:                final POIFSReader r = new POIFSReader();
180:                POIFSReaderListener pfl = new POIFSReaderListener() {
181:                    public void processPOIFSReaderEvent(
182:                            final POIFSReaderEvent event) {
183:                        try {
184:                            final POIFile f = new POIFile();
185:                            f.setName(event.getName());
186:                            f.setPath(event.getPath());
187:                            final InputStream in = event.getStream();
188:                            if (PropertySet.isPropertySetStream(in)) {
189:                                final ByteArrayOutputStream out = new ByteArrayOutputStream();
190:                                Util.copy(in, out);
191:                                out.close();
192:                                f.setBytes(out.toByteArray());
193:                                files.add(f);
194:                            }
195:                        } catch (Exception ex) {
196:                            ex.printStackTrace();
197:                            throw new RuntimeException(ex.getMessage());
198:                        }
199:                    }
200:                };
201:
202:                /* Register the listener for all POI files. */
203:                r.registerListener(pfl);
204:
205:                /* Read the POI filesystem. */
206:                r.read(new FileInputStream(poiFs));
207:                POIFile[] result = new POIFile[files.size()];
208:                for (int i = 0; i < result.length; i++)
209:                    result[i] = (POIFile) files.get(i);
210:                return result;
211:            }
212:
213:            /**
214:             * <p>Prints the system properties to System.out.</p>
215:             */
216:            public static void printSystemProperties() {
217:                final Properties p = System.getProperties();
218:                final List names = new LinkedList();
219:                for (Iterator i = p.keySet().iterator(); i.hasNext();)
220:                    names.add(i.next());
221:                Collections.sort(names);
222:                for (final Iterator i = names.iterator(); i.hasNext();) {
223:                    String name = (String) i.next();
224:                    String value = (String) p.get(name);
225:                    System.out.println(name + ": " + value);
226:                }
227:                System.out.println("Current directory: "
228:                        + System.getProperty("user.dir"));
229:            }
230:
231:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.