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.wicket.serialize.java; 018 019import java.io.IOException; 020import java.io.InputStream; 021import java.io.ObjectInputStream; 022import java.io.ObjectOutputStream; 023import java.io.OutputStream; 024import java.util.zip.Deflater; 025import java.util.zip.DeflaterOutputStream; 026import java.util.zip.Inflater; 027import java.util.zip.InflaterInputStream; 028 029/** 030 * A {@link JavaSerializer} that deflates the outputstream on the fly, reducing page store size by 031 * up to a factor 8. Be advised that deflating serialized objects comes at a price of about 2-20ms 032 * per page request, depending on the size of the page and the cpu power of the machine. 033 * 034 * <p> 035 * To use this serializer, put the following code in your application's init: 036 * 037 * <pre> 038 * getFrameworkSettings().setSerializer(new DeflatedJavaSerializer(getApplicationKey())); 039 * </pre> 040 * 041 * @author papegaaij 042 */ 043public class DeflatedJavaSerializer extends JavaSerializer 044{ 045 private static final int COMPRESS_BUF_SIZE = 4 * 1024; 046 047 /** 048 * Construct. 049 * 050 * @param applicationKey 051 */ 052 public DeflatedJavaSerializer(String applicationKey) 053 { 054 super(applicationKey); 055 } 056 057 @Override 058 protected ObjectOutputStream newObjectOutputStream(OutputStream out) throws IOException 059 { 060 return super.newObjectOutputStream(new DeflaterOutputStream(out, createDeflater(), 061 COMPRESS_BUF_SIZE)); 062 } 063 064 /** 065 * Creates the {@code Deflater}. Override this method to customize the deflater, for example to 066 * change the compression level and/or strategy. 067 * 068 * @return the {@code Deflater} 069 */ 070 protected Deflater createDeflater() 071 { 072 return new Deflater(Deflater.BEST_SPEED); 073 } 074 075 @Override 076 protected ObjectInputStream newObjectInputStream(InputStream in) throws IOException 077 { 078 return super.newObjectInputStream(new InflaterInputStream(in, new Inflater(), 079 COMPRESS_BUF_SIZE)); 080 } 081}