1 7 package org.ejtools.archive.io; 8 9 import java.io.ByteArrayOutputStream ; 10 import java.io.IOException ; 11 import java.util.Iterator ; 12 import java.util.Stack ; 13 import java.util.jar.JarEntry ; 14 import java.util.zip.ZipOutputStream ; 15 16 import org.ejtools.archive.Archive; 17 import org.ejtools.archive.Entry; 18 19 25 public abstract class StreamWriter implements Writer 26 { 27 28 private Stack outStreams = new Stack (); 29 30 31 36 public void visit(Archive archive) 37 { 38 try 39 { 40 ZipOutputStream stream = this.getZipOutputStream(); 41 if (stream == null) 42 { 43 throw new IllegalStateException ("No initial input stream"); 44 } 45 46 for (Iterator iterator = archive.iterator(); iterator.hasNext(); ) 48 { 49 Entry entry = (Entry ) iterator.next(); 50 51 if (entry.isArchive()) 52 { 53 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 54 ZipOutputStream jos = new ZipOutputStream (baos); 55 56 this.pushZipOutputStream(jos); 57 entry.accept(this); 58 this.popZipOutputStream(); 59 60 JarEntry je = new JarEntry (entry.getURI()); 61 stream.putNextEntry(je); 62 stream.write(baos.toByteArray()); 63 } 64 else 65 { 66 entry.accept(this); 67 } 68 } 69 stream.close(); 70 } 71 catch (IOException ioe) 72 { 73 ioe.printStackTrace(); 74 } 75 } 76 77 78 83 public void visit(Entry entry) 84 { 85 try 86 { 87 ZipOutputStream stream = this.getZipOutputStream(); 89 if (stream == null) 90 { 91 throw new IllegalStateException ("No initial input stream"); 92 } 93 JarEntry je = new JarEntry (entry.getURI()); 94 stream.putNextEntry(je); 95 stream.write(entry.getContent()); 96 } 97 catch (IOException ioe) 98 { 99 ioe.printStackTrace(); 100 } 101 } 102 103 104 109 protected ZipOutputStream getZipOutputStream() 110 { 111 return (ZipOutputStream ) this.outStreams.lastElement(); 112 } 113 114 115 121 protected ZipOutputStream popZipOutputStream() 122 throws IOException 123 { 124 if (this.outStreams.isEmpty()) 125 { 126 return null; 127 } 128 else 129 { 130 return (ZipOutputStream ) this.outStreams.pop(); 131 } 132 } 133 134 135 141 protected void pushZipOutputStream(ZipOutputStream stream) 142 throws IOException 143 { 144 this.outStreams.push(stream); 145 } 146 } 147 | Popular Tags |