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        InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, graph);
063
064        DeflaterOutputStream zipOutput = new DeflaterOutputStream(stream, new Deflater(compressionLevel));
065        try {
066            IOHelper.copy(is, zipOutput);
067        } finally {
068            IOHelper.close(is, zipOutput);
069        }
070    }
071
072    public Object unmarshal(final Exchange exchange, final InputStream inputStream) throws Exception {
073        InflaterInputStream inflaterInputStream = new InflaterInputStream(inputStream);
074        OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
075
076        try {
077            IOHelper.copy(inflaterInputStream, osb);
078            return osb.build();
079        } finally {
080            // must close input streams
081            IOHelper.close(osb, inflaterInputStream, inputStream);
082        }
083    }
084
085    @Override
086    protected void doStart() throws Exception {
087        // noop
088    }
089
090    @Override
091    protected void doStop() throws Exception {
092        // noop
093    }
094}