[Ejtools-cvs] libraries/common/src/main/org/ejtools/archive/io ByteReader.java,NONE,1.1 DirectoryWri
Brought to you by:
letiemble
|
From: <let...@us...> - 2003-09-15 23:14:35
|
Update of /cvsroot/ejtools/libraries/common/src/main/org/ejtools/archive/io In directory sc8-pr-cvs1:/tmp/cvs-serv359/libraries/common/src/main/org/ejtools/archive/io Added Files: ByteReader.java DirectoryWriter.java FileReader.java FileWriter.java Reader.java StreamReader.java StreamWriter.java Writer.java Log Message: Add support for Archive manipulation : reading, writing, modification and iteration. The collection of classes will be the base of the Deployment Browser --- NEW FILE: ByteReader.java --- /* * EJTools, the Enterprise Java Tools * * Distributable under LGPL license. * See terms of license at www.gnu.org. */ package org.ejtools.archive.io; import java.io.ByteArrayInputStream; import java.util.zip.ZipInputStream; /** * @author Laurent Etiemble * @version $Revision: 1.1 $ */ public class ByteReader extends StreamReader { /** *Constructor for the FileMapper object * * @param content Description of the Parameter */ public ByteReader(byte[] content) { try { ByteArrayInputStream bais = new ByteArrayInputStream(content); this.pushZipInputStream(new ZipInputStream(bais)); } catch (Exception ioe) { ioe.printStackTrace(); } } } --- NEW FILE: DirectoryWriter.java --- /* * EJTools, the Enterprise Java Tools * * Distributable under LGPL license. * See terms of license at www.gnu.org. */ package org.ejtools.archive.io; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import java.util.Stack; import org.ejtools.archive.Archive; import org.ejtools.archive.Entry; /** * @author Laurent Etiemble * @version $Revision: 1.1 $ */ public class DirectoryWriter implements Writer { /** Description of the Field */ private Stack outDirs = new Stack(); /** *Constructor for the ArchiveWriter object * * @param basedir Description of the Parameter */ public DirectoryWriter(File basedir) { this.pushURI(basedir); } /** * Description of the Method * * @param archive Description of the Parameter */ public void visit(Archive archive) { File uri = this.getURI(); if (uri == null) { throw new IllegalStateException("No initial uri"); } for (Iterator iterator = archive.iterator(); iterator.hasNext(); ) { Entry entry = (Entry) iterator.next(); if (entry.isArchive()) { File newURI = new File(uri, entry.getURI()); this.pushURI(newURI); entry.accept(this); this.popURI(); } else { entry.accept(this); } } } /** * Description of the Method * * @param entry Description of the Parameter */ public void visit(Entry entry) { try { File uri = this.getURI(); if (uri == null) { throw new IllegalStateException("No initial uri"); } File file = new File(uri, entry.getURI()); file.getParentFile().mkdirs(); FileOutputStream fos = new FileOutputStream(file); fos.write(entry.getContent()); fos.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } /** * Gets the stream attribute of the FileMapper object * * @return The stream value */ protected File getURI() { return (File) this.outDirs.lastElement(); } /** * Gets the stream attribute of the FileMapper object * * @return Description of the Return Value */ protected File popURI() { if (this.outDirs.isEmpty()) { return null; } else { return (File) this.outDirs.pop(); } } /** * Description of the Method * * @param uri Description of the Parameter */ protected void pushURI(File uri) { this.outDirs.push(uri); } } --- NEW FILE: FileReader.java --- /* * EJTools, the Enterprise Java Tools * * Distributable under LGPL license. * See terms of license at www.gnu.org. */ package org.ejtools.archive.io; import java.io.File; import java.io.FileInputStream; import java.util.zip.ZipInputStream; /** * @author Laurent Etiemble * @version $Revision: 1.1 $ */ public class FileReader extends StreamReader { /** *Constructor for the FileMapper object * * @param file Description of the Parameter */ public FileReader(File file) { try { FileInputStream fis = new FileInputStream(file); this.pushZipInputStream(new ZipInputStream(fis)); } catch (Exception ioe) { ioe.printStackTrace(); } } } --- NEW FILE: FileWriter.java --- /* * EJTools, the Enterprise Java Tools * * Distributable under LGPL license. * See terms of license at www.gnu.org. */ package org.ejtools.archive.io; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipOutputStream; /** * @author Laurent Etiemble * @version $Revision: 1.1 $ */ public class FileWriter extends StreamWriter { /** *Constructor for the ArchiveWriter object * * @param file Description of the Parameter */ public FileWriter(File file) { try { FileOutputStream fos = new FileOutputStream(file); this.pushZipOutputStream(new ZipOutputStream(fos)); } catch (IOException ioe) { ioe.printStackTrace(); } } } --- NEW FILE: Reader.java --- /* * EJTools, the Enterprise Java Tools * * Distributable under LGPL license. * See terms of license at www.gnu.org. */ package org.ejtools.archive.io; import org.ejtools.archive.Archive; import org.ejtools.archive.Entry; /** * @author Laurent Etiemble * @version $Revision: 1.1 $ */ public interface Reader { /** * Description of the Method * * @param archive Description of the Parameter */ public void visit(Archive archive); /** * Description of the Method * * @param entry Description of the Parameter */ public void visit(Entry entry); } --- NEW FILE: StreamReader.java --- /* * EJTools, the Enterprise Java Tools * * Distributable under LGPL license. * See terms of license at www.gnu.org. */ package org.ejtools.archive.io; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Stack; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import org.ejtools.archive.Archive; import org.ejtools.archive.Entry; import org.ejtools.archive.JarArchive; import org.ejtools.archive.JarEntry; /** * @author Laurent Etiemble * @version $Revision: 1.1 $ */ public abstract class StreamReader implements Reader { /** Description of the Field */ private Stack inStreams = new Stack(); /** * Description of the Method * * @param entry Description of the Parameter */ public void visit(Entry entry) { } /** * Description of the Method * * @param archive Description of the Parameter */ public void visit(Archive archive) { try { ZipInputStream stream = this.getZipInputStream(); if (stream == null) { throw new IllegalStateException("No initial input stream"); } ZipEntry ze = null; while ((ze = stream.getNextEntry()) != null) { if ((ze != null) && (!ze.isDirectory())) { Entry entry = this.createEntry(ze.getName()); if (entry != null) { entry.addTo(archive); } } stream.closeEntry(); } stream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } /** * Description of the Method * * @param uri Description of the Parameter * @return Description of the Return Value */ protected Entry createEntry(String uri) { try { ZipInputStream stream = this.getZipInputStream(); if (stream == null) { throw new IllegalStateException("No initial input stream"); } byte[] buffer = new byte[1024]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); int read = 0; while ((read = stream.read(buffer)) > 0) { baos.write(buffer, 0, read); } byte[] content = baos.toByteArray(); try { ZipInputStream jis = new ZipInputStream(new ByteArrayInputStream(content)); ZipEntry ze = jis.getNextEntry(); jis.close(); if (ze != null) { Archive archive = new JarArchive(uri); // // Archive should not have its content !!! // // archive.setContent(content); this.pushZipInputStream(new ZipInputStream(new ByteArrayInputStream(content))); archive.accept(this); this.popZipInputStream(); return archive; } else { Entry entry = new JarEntry(uri); entry.setContent(content); return entry; } } catch (Exception ioe) { Entry entry = new JarEntry(uri); entry.setContent(content); return entry; } } catch (IOException ioe) { ioe.printStackTrace(); } return null; } /** * Gets the stream attribute of the FileMapper object * * @return The stream value */ protected ZipInputStream getZipInputStream() { return (ZipInputStream) this.inStreams.lastElement(); } /** * Gets the stream attribute of the FileMapper object * * @return The stream value * @exception IOException Description of the Exception */ protected ZipInputStream popZipInputStream() throws IOException { if (this.inStreams.isEmpty()) { return null; } else { return (ZipInputStream) this.inStreams.pop(); } } /** * Description of the Method * * @param stream Description of the Parameter * @exception IOException Description of the Exception */ protected void pushZipInputStream(ZipInputStream stream) throws IOException { this.inStreams.push(stream); } } --- NEW FILE: StreamWriter.java --- /* * EJTools, the Enterprise Java Tools * * Distributable under LGPL license. * See terms of license at www.gnu.org. */ package org.ejtools.archive.io; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Iterator; import java.util.Stack; import java.util.jar.JarEntry; import java.util.zip.ZipOutputStream; import org.ejtools.archive.Archive; import org.ejtools.archive.Entry; /** * @author Laurent Etiemble * @version $Revision: 1.1 $ */ public class StreamWriter implements Writer { /** Description of the Field */ private Stack outStreams = new Stack(); /** * Description of the Method * * @param archive Description of the Parameter */ public void visit(Archive archive) { try { ZipOutputStream stream = this.getZipOutputStream(); if (stream == null) { throw new IllegalStateException("No initial input stream"); } for (Iterator iterator = archive.iterator(); iterator.hasNext(); ) { Entry entry = (Entry) iterator.next(); if (entry.isArchive()) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream jos = new ZipOutputStream(baos); this.pushZipOutputStream(jos); entry.accept(this); this.popZipOutputStream(); JarEntry je = new JarEntry(entry.getURI()); stream.putNextEntry(je); stream.write(baos.toByteArray()); } else { entry.accept(this); } } stream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } /** * Description of the Method * * @param entry Description of the Parameter */ public void visit(Entry entry) { try { ZipOutputStream stream = this.getZipOutputStream(); if (stream == null) { throw new IllegalStateException("No initial input stream"); } JarEntry je = new JarEntry(entry.getURI()); stream.putNextEntry(je); stream.write(entry.getContent()); } catch (IOException ioe) { ioe.printStackTrace(); } } /** * Gets the stream attribute of the FileMapper object * * @return The stream value */ protected ZipOutputStream getZipOutputStream() { return (ZipOutputStream) this.outStreams.lastElement(); } /** * Gets the stream attribute of the FileMapper object * * @return The stream value * @exception IOException Description of the Exception */ protected ZipOutputStream popZipOutputStream() throws IOException { if (this.outStreams.isEmpty()) { return null; } else { return (ZipOutputStream) this.outStreams.pop(); } } /** * Description of the Method * * @param stream Description of the Parameter * @exception IOException Description of the Exception */ protected void pushZipOutputStream(ZipOutputStream stream) throws IOException { this.outStreams.push(stream); } } --- NEW FILE: Writer.java --- /* * EJTools, the Enterprise Java Tools * * Distributable under LGPL license. * See terms of license at www.gnu.org. */ package org.ejtools.archive.io; import org.ejtools.archive.Archive; import org.ejtools.archive.Entry; /** * @author Laurent Etiemble * @version $Revision: 1.1 $ */ public interface Writer { /** * Description of the Method * * @param archive Description of the Parameter */ public void visit(Archive archive); /** * Description of the Method * * @param entry Description of the Parameter */ public void visit(Entry entry); } |