1 2 17 18 19 package org.apache.poi.hpsf.basic; 20 21 import java.io.ByteArrayOutputStream ; 22 import java.io.EOFException ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileNotFoundException ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.util.ArrayList ; 30 import java.util.Collections ; 31 import java.util.Iterator ; 32 import java.util.LinkedList ; 33 import java.util.List ; 34 import java.util.Properties ; 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 50 public class Util 51 { 52 53 63 public static void copy(final InputStream in, final OutputStream out) 64 throws IOException 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 ex) 81 { 82 eof = true; 83 } 84 } 85 } 86 87 88 89 106 public static POIFile[] readPOIFiles(final File poiFs) 107 throws FileNotFoundException , IOException 108 { 109 return readPOIFiles(poiFs, null); 110 } 111 112 113 114 133 public static POIFile[] readPOIFiles(final File poiFs, 134 final String [] poiFiles) 135 throws FileNotFoundException , IOException 136 { 137 final List files = new ArrayList (); 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 in = event.getStream(); 149 final ByteArrayOutputStream out = 150 new ByteArrayOutputStream (); 151 Util.copy(in, out); 152 out.close(); 153 f.setBytes(out.toByteArray()); 154 files.add(f); 155 } 156 catch (IOException ex) 157 { 158 ex.printStackTrace(); 159 throw new RuntimeException (ex.getMessage()); 160 } 161 } 162 }; 163 if (poiFiles == null) 164 165 r.registerListener(pfl); 166 else 167 169 for (int i = 0; i < poiFiles.length; i++) 170 r.registerListener(pfl, poiFiles[i]); 171 172 173 r.read(new FileInputStream (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 198 public static POIFile[] readPropertySets(final File poiFs) 199 throws FileNotFoundException , IOException 200 { 201 final List files = new ArrayList (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 in = event.getStream(); 213 if (PropertySet.isPropertySetStream(in)) 214 { 215 final ByteArrayOutputStream out = 216 new ByteArrayOutputStream (); 217 Util.copy(in, out); 218 out.close(); 219 f.setBytes(out.toByteArray()); 220 files.add(f); 221 } 222 } 223 catch (Exception ex) 224 { 225 ex.printStackTrace(); 226 throw new RuntimeException (ex.getMessage()); 227 } 228 } 229 }; 230 231 232 r.registerListener(pfl); 233 234 235 r.read(new FileInputStream (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 247 public static void printSystemProperties() 248 { 249 final Properties p = System.getProperties(); 250 final List names = new LinkedList (); 251 for (Iterator i = p.keySet().iterator(); i.hasNext();) 252 names.add(i.next()); 253 Collections.sort(names); 254 for (final Iterator i = names.iterator(); i.hasNext();) 255 { 256 String name = (String ) i.next(); 257 String value = (String ) 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 |