/** * Copyright 2016 benjobs * <p> * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * 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.opencron.common.serialization; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import java.io.IOException; /** * ${DESCRIPTION} * * @author Ricky Fung */ public class OpencronDecoder<T> extends LengthFieldBasedFrameDecoder { private Serializer serializer = new HessianSerializer(); private Class<T> type; public OpencronDecoder(Class<T> type, int maxFrameLength, int lengthFieldOffset, int lengthFieldLength) throws IOException { super(maxFrameLength, lengthFieldOffset, lengthFieldLength); this.type = type; } @Override protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { ByteBuf buffer = (ByteBuf) super.decode(ctx, in); if (buffer == null) { return null; } if (buffer.readableBytes() < 4) { throw new Exception("error"); } int length = buffer.readInt(); byte[] data = new byte[length]; buffer.readBytes(data); return serializer.decode(data, type); } }