001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.impl;
018
019import java.io.InputStream;
020import java.io.OutputStream;
021import java.util.zip.Deflater;
022import java.util.zip.DeflaterOutputStream;
023import java.util.zip.InflaterInputStream;
024
025import org.apache.camel.Exchange;
026import org.apache.camel.converter.stream.OutputStreamBuilder;
027import org.apache.camel.spi.DataFormat;
028import org.apache.camel.spi.DataFormatName;
029import org.apache.camel.util.IOHelper;
030
031/**
032 * "Deflate" compression data format.
033 * See {@link org.apache.camel.model.dataformat.ZipFileDataFormat} for Zip file compression.
034 */
035public class ZipDataFormat extends org.apache.camel.support.ServiceSupport implements DataFormat, DataFormatName {
036
037    private int compressionLevel;
038
039    public ZipDataFormat() {
040        this.compressionLevel = Deflater.BEST_SPEED;
041    }
042
043    public ZipDataFormat(int compressionLevel) {
044        this.compressionLevel = compressionLevel;
045    }
046
047    @Override
048    public String getDataFormatName() {
049        return "zip";
050    }
051
052    public int getCompressionLevel() {
053        return compressionLevel;
054    }
055
056    public void setCompressionLevel(int compressionLevel) {
057        this.compressionLevel = compressionLevel;
058    }
059
060    public void marshal(final Exchange exchange, final Object graph, final OutputStream stream) throws Exception {
061        // ask for a mandatory type conversion to avoid a possible NPE beforehand as we do copy from the InputStream
062        final InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, graph);
063
064        final Deflater deflater = new Deflater(compressionLevel);
065        final DeflaterOutputStream zipOutput = new DeflaterOutputStream(stream, deflater);
066        try {
067            IOHelper.copy(is, zipOutput);
068        } finally {
069            IOHelper.close(is, zipOutput);
070            
071            /*
072            * As we create the Deflater our self and do not use the stream default
073            * (see {@link java.util.zip.DeflaterOutputStream#usesDefaultDeflater})
074            * we need to close the Deflater to not risk a OutOfMemoryException
075            * in native code parts (see {@link java.util.zip.Deflater#end})
076            */
077            deflater.end();
078        }
079    }
080
081    public Object unmarshal(final Exchange exchange, final InputStream inputStream) throws Exception {
082        InflaterInputStream inflaterInputStream = new InflaterInputStream(inputStream);
083        OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
084
085        try {
086            IOHelper.copy(inflaterInputStream, osb);
087            return osb.build();
088        } finally {
089            // must close input streams
090            IOHelper.close(osb, inflaterInputStream, inputStream);
091        }
092    }
093
094    @Override
095    protected void doStart() throws Exception {
096        // noop
097    }
098
099    @Override
100    protected void doStop() throws Exception {
101        // noop
102    }
103}