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.ByteArrayOutputStream; 020import java.io.InputStream; 021import java.io.OutputStream; 022import java.util.zip.Deflater; 023import java.util.zip.DeflaterOutputStream; 024import java.util.zip.InflaterInputStream; 025 026import org.apache.camel.Exchange; 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(Exchange exchange, Object graph, 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(Exchange exchange, InputStream stream) throws Exception { 073 InputStream is = exchange.getIn().getMandatoryBody(InputStream.class); 074 InflaterInputStream unzipInput = new InflaterInputStream(is); 075 076 // Create an expandable byte array to hold the inflated data 077 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 078 try { 079 IOHelper.copy(unzipInput, bos); 080 return bos.toByteArray(); 081 } finally { 082 // must close input streams 083 IOHelper.close(is, unzipInput); 084 } 085 } 086 087 @Override 088 protected void doStart() throws Exception { 089 // noop 090 } 091 092 @Override 093 protected void doStop() throws Exception { 094 // noop 095 } 096}