001: /**
002: * $Id: NetFileCompression.java,v 1.7 2005/11/30 11:26:38 ss150821 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.netfile.servlet.java2;
014:
015: import java.io.InputStream;
016: import com.sun.portal.log.common.PortalLogger;
017: import java.io.OutputStream;
018: import java.io.IOException;
019:
020: import java.util.zip.GZIPOutputStream;
021: import java.util.zip.ZipOutputStream;
022: import java.util.zip.Deflater;
023: import java.util.zip.ZipEntry;
024:
025: public class NetFileCompression implements NetFileCompress {
026:
027: private int bufSize = 4096;
028: int compressLevel = 6;
029: String compressMethod = null;
030:
031: public NetFileCompression(int cmpLevel, String cmpMethod) {
032: compressLevel = cmpLevel;
033: compressMethod = cmpMethod;
034: }
035:
036: public NetFileCompression() {
037: compressLevel = 6;
038: compressMethod = "ZIP";
039: }
040:
041: public void setCompressLevel(int i) {
042: this .compressLevel = i;
043: }
044:
045: public void setCompressMethod(String s) {
046: if ((s == null) || (s.equals("")))
047: this .compressMethod = "ZIP";
048: else
049: this .compressMethod = s;
050: }
051:
052: public void setBufferSize(int i) {
053: if (i <= 1024) {
054: this .bufSize = 1024;
055: } else {
056: this .bufSize = 4096;
057: }
058: }
059:
060: private ZipEntry createZipEntry(String name) {
061: return new ZipEntry(name);
062: }
063:
064: public void doCompress(InputStream istream, OutputStream ostream,
065: Object obj) throws IOException {
066: if (istream == null)
067: throw new NullPointerException(
068: "Cannot Read. InputStream is null");
069: if (ostream == null)
070: throw new NullPointerException(
071: "Cannot Write. OutputStream is null");
072:
073: String name = "";
074: if (compressMethod.equalsIgnoreCase("gzip")) {
075: doGZipCompress(istream, ostream, new Deflater(
076: this .compressLevel));
077: } else if (compressMethod.equalsIgnoreCase("zip")) {
078: name = (String) obj;
079: doZipCompress(istream, ostream, name);
080: }
081: }
082:
083: public void doZipCompress(InputStream ist, OutputStream ost,
084: String entry) throws IOException {
085: int len = 0;
086: byte[] buf = new byte[bufSize];
087: ZipOutputStream zos = new ZipOutputStream(ost);
088: zos.closeEntry();
089: zos.putNextEntry(createZipEntry(entry));
090: zos.setMethod(Deflater.DEFLATED);
091: zos.setLevel(this .compressLevel);
092: try {
093: while ((len = ist.read(buf)) > -1) {
094: zos.write(buf, 0, len);
095: }
096: } catch (IOException ioe) {
097: ioe.printStackTrace();
098: throw ioe;
099: } finally {
100: try {
101: if (zos != null)
102: zos.close();
103: if (ist != null)
104: ist.close();
105: } catch (IOException ioe) {
106: }
107: }
108: }
109:
110: public void doGZipCompress(InputStream ist, OutputStream ost,
111: Deflater deflater) throws IOException {
112: int len = 0;
113: byte[] buffer = new byte[bufSize];
114: NetFileGZipOutputStream gzos = new NetFileGZipOutputStream(ost,
115: this .bufSize);
116: gzos.setLevel(this .compressLevel);
117: try {
118: while ((len = ist.read(buffer)) > -1) {
119: gzos.write(buffer, 0, len);
120: }
121: } catch (IOException ioe) {
122: System.out.println(ioe.getMessage());
123: throw ioe;
124: } finally {
125: try {
126: if (gzos != null)
127: gzos.close();
128: if (ist != null)
129: ist.close();
130: } catch (IOException ioe) {
131: }
132: }
133: }
134:
135: }
|