<commit_before>package org.menacheri.handlers.netty;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandler.Sharable;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.oneone.OneToOneDecoder;
import org.menacheri.convert.ITransform;
import org.menacheri.convert.flex.AMFDeSerializer;
import org.menacheri.event.IEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import flex.messaging.io.SerializationContext;

/**
 * This class takes a {@link ByteArrayInputStream} containing AMF3 object as
 * input and creates a java object from it using the {@link AMFDeSerializer}
 * class.
 * 
 * @author <NAME>.
 * 
 */
@Sharable
public class AMF3ToJavaObjectDecoder extends OneToOneDecoder implements ITransform<ChannelBuffer, Object>
{
	private static final Logger LOG = LoggerFactory.getLogger(AMF3ToJavaObjectDecoder.class);
	
	@Override
	protected Object decode(ChannelHandlerContext ctx, Channel channel,
			Object msg) throws Exception
	{
		if(null == msg)
		{
			LOG.error("Incoming message is null");
			return msg;
		}
		
		// Check if incoming message is of correct type. If not return null.
		if (!(msg instanceof ByteArrayInputStream))
		{
			LOG.error("Received Invalid object: {} to decode!", msg.getClass()
					.getCanonicalName());
			return null;
		}

		// Cast object to correct type and pass it on to the de serializer
		IEvent event = (IEvent)msg;
		ByteArrayInputStream bis = (ByteArrayInputStream) event.getSource();
		event.setSource(deSerializeObjectFromStream(bis));
		return event;
	}

	@Override
	public Object convert(ChannelBuffer buffer) throws Exception {
		ByteArrayInputStream bis = new ByteArrayInputStream(buffer.array());
		return deSerializeObjectFromStream(bis);
	}
	
	protected Object deSerializeObjectFromStream(ByteArrayInputStream bis) throws Exception
	{
		AMFDeSerializer serializer = new AMFDeSerializer(SerializationContext
				.getSerializationContext());
		Object o = null;
		try<commit_msg>Fixed bug with wrong class cast. Incoming object in this decoder is of type ChannelBuffer not ByteArrayInputstream<commit_after>package org.menacheri.handlers.netty;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandler.Sharable;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.oneone.OneToOneDecoder;
import org.menacheri.convert.ITransform;
import org.menacheri.convert.flex.AMFDeSerializer;
import org.menacheri.event.IEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import flex.messaging.io.SerializationContext;

/**
 * This class takes a {@link ByteArrayInputStream} containing AMF3 object as
 * input and creates a java object from it using the {@link AMFDeSerializer}
 * class.
 * 
 * @author <NAME>.
 * 
 */
@Sharable
public class AMF3ToJavaObjectDecoder extends OneToOneDecoder implements ITransform<ChannelBuffer, Object>
{
	private static final Logger LOG = LoggerFactory.getLogger(AMF3ToJavaObjectDecoder.class);
	
	@Override
	protected Object decode(ChannelHandlerContext ctx, Channel channel,
			Object msg) throws Exception
	{
		if(null == msg)
		{
			LOG.warn("Incoming message is null");
			return msg;
		}
		
		// Cast object to correct type and pass it on to the de serializer
		IEvent event = (IEvent)msg;
		ChannelBuffer buffer = (ChannelBuffer)event.getSource();
		ByteArrayInputStream bis = new ByteArrayInputStream(buffer.array());
		event.setSource(deSerializeObjectFromStream(bis));
		return event;
	}

	@Override
	public Object convert(ChannelBuffer buffer) throws Exception {
		ByteArrayInputStream bis = new ByteArrayInputStream(buffer.array());
		return deSerializeObjectFromStream(bis);
	}
	
	protected Object deSerializeObjectFromStream(ByteArrayInputStream bis) throws Exception
	{
		AMFDeSerializer serializer = new AMFDeSerializer(SerializationContext
				.getSerializationContext());
		Object o = null;
		try
{'fraction_non_alphanumeric': 0.08732057416267942, 'fraction_numerical': 0.0023923444976076554, 'mean_word_length': 5.287218045112782, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2896435', 'n_tokens_mistral': 1293, 'n_tokens_neox': 1210, 'n_words': 319}