1 22 package org.jboss.util.file; 23 24 import java.util.Iterator ; 25 import java.util.jar.JarInputStream ; 26 import java.util.jar.JarEntry ; 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.io.FileInputStream ; 30 import java.io.InputStream ; 31 import java.io.ByteArrayInputStream ; 32 33 39 public class JarStreamBrowser implements Iterator 40 { 41 JarInputStream jar; 44 JarEntry next; 45 ArchiveBrowser.Filter filter; 46 47 public JarStreamBrowser(File file, ArchiveBrowser.Filter filter) throws IOException 48 { 49 this(new FileInputStream (file), filter); 50 } 51 52 public JarStreamBrowser(InputStream is, ArchiveBrowser.Filter filter) throws IOException 53 { 54 this.filter = filter; 55 jar = new JarInputStream (is); 56 setNext(); 57 } 58 59 public boolean hasNext() 60 { 61 return next != null; 62 } 63 64 private void setNext() 65 { 66 try 67 { 68 if (next != null) jar.closeEntry(); 69 next = null; 70 do 71 { 72 next = jar.getNextJarEntry(); 73 } while (next != null && (next.isDirectory() || !filter.accept(next.getName()))); 74 if (next == null) jar.close(); 75 } 76 catch (IOException e) 77 { 78 throw new RuntimeException ("failed to browse jar", e); 79 } 80 } 81 82 public Object next() 83 { 84 int size = (int) next.getSize(); 85 byte[] buf = new byte[size]; 86 int count = 0; 87 int current = 0; 88 try 89 { 90 while (( 91 ( 92 current = jar.read(buf, count, 93 size - count) 94 ) != -1 95 ) && (count < size)) 96 { 97 count += current; 98 } 99 ByteArrayInputStream bais = new ByteArrayInputStream (buf); 100 setNext(); 101 return bais; 102 } 103 catch (IOException e) 104 { 105 try 106 { 107 jar.close(); 108 } 109 catch (IOException ignored) 110 { 111 112 } 113 throw new RuntimeException (e); 114 } 115 } 116 117 public void remove() 118 { 119 throw new RuntimeException ("Illegal operation on ArchiveBrowser"); 120 } 121 } 122 | Popular Tags |