001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.classreader;
034:
035: import java.io.*;
036: import java.util.*;
037: import java.util.zip.*;
038:
039: import org.apache.log4j.*;
040:
041: public class ZipClassfileLoader extends ClassfileLoaderDecorator {
042: private static final int BUFFER_SIZE = 16 * 1024;
043:
044: public ZipClassfileLoader(ClassfileLoader loader) {
045: super (loader);
046: }
047:
048: protected void load(String filename) {
049: Logger.getLogger(getClass()).debug(
050: "Starting group in file " + filename);
051:
052: ZipFile zipfile = null;
053: try {
054: zipfile = new ZipFile(filename);
055:
056: fireBeginGroup(filename, zipfile.size());
057:
058: Logger.getLogger(getClass()).debug(
059: "Loading ZipFile " + filename);
060: load(zipfile);
061: Logger.getLogger(getClass()).debug(
062: "Loaded ZipFile " + filename);
063:
064: fireEndGroup(filename);
065: } catch (IOException ex) {
066: Logger.getLogger(getClass()).error(
067: "Cannot load Zip file \"" + filename + "\"", ex);
068: } finally {
069: if (zipfile != null) {
070: try {
071: zipfile.close();
072: } catch (IOException ex) {
073: // Ignore
074: }
075: }
076: }
077: }
078:
079: protected void load(String filename, InputStream in) {
080: Logger.getLogger(getClass()).debug(
081: "Starting group in stream " + filename);
082:
083: ZipInputStream zipfile = null;
084: try {
085: zipfile = new ZipInputStream(in);
086:
087: fireBeginGroup(filename, -1);
088:
089: Logger.getLogger(getClass()).debug(
090: "Loading ZipInputStream " + filename);
091: load(zipfile);
092: Logger.getLogger(getClass()).debug(
093: "Loaded ZipInputStream " + filename);
094:
095: fireEndGroup(filename);
096: } catch (IOException ex) {
097: Logger.getLogger(getClass()).error(
098: "Cannot load Zip file \"" + filename + "\"", ex);
099: } finally {
100: if (zipfile != null) {
101: try {
102: zipfile.close();
103: } catch (IOException ex) {
104: // Ignore
105: }
106: }
107: }
108: }
109:
110: protected void load(ZipFile zipfile) throws IOException {
111: Enumeration entries = zipfile.entries();
112: while (entries.hasMoreElements()) {
113: ZipEntry entry = (ZipEntry) entries.nextElement();
114:
115: fireBeginFile(entry.getName());
116:
117: Logger.getLogger(getClass()).debug(
118: "Starting file " + entry.getName() + " ("
119: + entry.getSize() + " bytes)");
120:
121: byte[] bytes = null;
122: InputStream in = null;
123: try {
124: in = zipfile.getInputStream(entry);
125: bytes = readBytes(in);
126: } finally {
127: if (in != null) {
128: try {
129: in.close();
130: } catch (IOException ex) {
131: // Ignore
132: }
133: }
134: }
135:
136: Logger.getLogger(getClass()).debug(
137: "Passing up file " + entry.getName() + " ("
138: + bytes.length + " bytes)");
139: getLoader().load(entry.getName(),
140: new ByteArrayInputStream(bytes));
141:
142: fireEndFile(entry.getName());
143: }
144: }
145:
146: protected void load(ZipInputStream in) throws IOException {
147: ZipEntry entry;
148: while ((entry = in.getNextEntry()) != null) {
149: fireBeginFile(entry.getName());
150:
151: Logger.getLogger(getClass()).debug(
152: "Starting file " + entry.getName() + " ("
153: + entry.getSize() + " bytes)");
154: byte[] bytes = readBytes(in);
155:
156: Logger.getLogger(getClass()).debug(
157: "Passing up file " + entry.getName() + " ("
158: + bytes.length + " bytes)");
159: getLoader().load(entry.getName(),
160: new ByteArrayInputStream(bytes));
161:
162: fireEndFile(entry.getName());
163: }
164: }
165:
166: private byte[] readBytes(InputStream in) {
167: byte[] result = null;
168:
169: try {
170: ByteArrayOutputStream out = new ByteArrayOutputStream();
171: byte[] buffer = new byte[BUFFER_SIZE];
172: int bytesRead = 0;
173: while ((bytesRead = in.read(buffer, 0, BUFFER_SIZE)) != -1) {
174: out.write(buffer, 0, bytesRead);
175: }
176: out.close();
177:
178: result = out.toByteArray();
179: } catch (IOException ex) {
180: Logger.getLogger(getClass()).debug(
181: "Error loading Zip entry", ex);
182: }
183:
184: return (result);
185: }
186: }
|