001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.serialization;
020
021import java.io.*;
022import java.util.zip.*;
023
024/**
025 * A composite serialization scheme which applies a ZLIB compression/decompression to a concrete {@link JPPFSerialization} implementation.
026 * @author Laurent Cohen
027 */
028public class ZLIBSerialization extends JPPFCompositeSerialization {
029  @Override
030  public void serialize(final Object o, final OutputStream os) throws Exception {
031    final Deflater deflater = new Deflater();
032    final DeflaterOutputStream zlibos = new DeflaterOutputStream(os, deflater);
033    try {
034      getDelegate().serialize(o, zlibos);
035    } finally {
036      zlibos.flush();
037      zlibos.finish();
038      deflater.end(); // required to clear the native/JNI buffers
039    }
040  }
041
042  @Override
043  public Object deserialize(final InputStream is) throws Exception {
044    final Inflater inflater = new Inflater();
045    final InflaterInputStream zlibis = new InflaterInputStream(is, inflater);
046    try {
047      return getDelegate().deserialize(zlibis);
048    } finally {
049      inflater.end(); // required to clear the native/JNI buffers
050    }
051  }
052
053  @Override
054  public String getName() {
055    return "ZLIB";
056  }
057}