001/**
002 *
003 * Copyright 2013-2014 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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.jivesoftware.smack.compression.jzlib;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.io.OutputStream;
022
023import org.jivesoftware.smack.SmackConfiguration;
024import org.jivesoftware.smack.compression.XMPPInputOutputStream;
025
026import com.jcraft.jzlib.DeflaterOutputStream;
027import com.jcraft.jzlib.InflaterInputStream;
028
029/**
030 * This class provides XMPP "zlib" compression with the help of JZLib.
031 *
032 * @author Florian Schmaus
033 * @see <a href="http://www.jcraft.com/jzlib/">JZLib</a>
034 *
035 */
036public class JzlibInputOutputStream extends XMPPInputOutputStream {
037
038    static {
039        SmackConfiguration.addCompressionHandler(new JzlibInputOutputStream());
040    }
041
042    public JzlibInputOutputStream() {
043        super("zlib");
044    }
045
046    @Override
047    public boolean isSupported() {
048        return true;
049    }
050
051    @Override
052    public InputStream getInputStream(InputStream inputStream) throws IOException {
053        final InflaterInputStream is = new InflaterInputStream(inputStream);
054
055        return is;
056    }
057
058    @Override
059    public OutputStream getOutputStream(OutputStream outputStream) throws IOException {
060        final DeflaterOutputStream os = new DeflaterOutputStream(outputStream);
061        if (flushMethod == FlushMethod.SYNC_FLUSH) {
062            os.setSyncFlush(true);
063        }
064
065        return os;
066    }
067}