
org.testifyproject.netty.handler.codec.serialization.ObjectDecoder Maven / Gradle / Ivy
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in org.testifyproject.testifyprojectpliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org.testifyproject/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.testifyproject.testifyproject.netty.handler.codec.serialization;
import org.testifyproject.testifyproject.netty.buffer.ByteBuf;
import org.testifyproject.testifyproject.netty.buffer.ByteBufInputStream;
import org.testifyproject.testifyproject.netty.channel.ChannelHandlerContext;
import org.testifyproject.testifyproject.netty.handler.codec.LengthFieldBasedFrameDecoder;
import java.org.testifyproject.testifyproject.ObjectInputStream;
import java.org.testifyproject.testifyproject.ObjectOutputStream;
import java.org.testifyproject.testifyproject.StreamCorruptedException;
/**
* A org.testifyproject.testifyprojectcoder which org.testifyproject.testifyprojectserializes the received {@link ByteBuf}s into Java
* objects.
*
* Please note that the serialized form this org.testifyproject.testifyprojectcoder expects is not
* org.testifyproject.testifyprojectpatible with the standard {@link ObjectOutputStream}. Please use
* {@link ObjectEncoder} or {@link ObjectEncoderOutputStream} to ensure the
* interoperability with this org.testifyproject.testifyprojectcoder.
*/
public class ObjectDecoder extends LengthFieldBasedFrameDecoder {
private final ClassResolver classResolver;
/**
* Creates a new org.testifyproject.testifyprojectcoder whose maximum object size is {@code 1048576}
* bytes. If the size of the received object is greater than
* {@code 1048576} bytes, a {@link StreamCorruptedException} will be
* raised.
*
* @param classResolver the {@link ClassResolver} to use for this org.testifyproject.testifyprojectcoder
*/
public ObjectDecoder(ClassResolver classResolver) {
this(1048576, classResolver);
}
/**
* Creates a new org.testifyproject.testifyprojectcoder with the specified maximum object size.
*
* @param maxObjectSize the maximum byte length of the serialized object.
* if the length of the received object is greater
* than this value, {@link StreamCorruptedException}
* will be raised.
* @param classResolver the {@link ClassResolver} which will load the class
* of the serialized object
*/
public ObjectDecoder(int maxObjectSize, ClassResolver classResolver) {
super(maxObjectSize, 0, 4, 0, 4);
this.classResolver = classResolver;
}
@Override
protected Object org.testifyproject.testifyprojectcode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
ByteBuf frame = (ByteBuf) super.org.testifyproject.testifyprojectcode(ctx, in);
if (frame == null) {
return null;
}
ObjectInputStream is = new CompactObjectInputStream(new ByteBufInputStream(frame), classResolver);
Object result = is.readObject();
is.close();
return result;
}
@Override
protected ByteBuf extractFrame(ChannelHandlerContext ctx, ByteBuf buffer, int index, int length) {
return buffer.slice(index, length);
}
}