<commit_before>/*
 * Copyright 2010 <NAME>.
 *
 * Licensed 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:
 *
 *    http://www.apache.org/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.freeswitch.esl.client.transport.message;

import org.freeswitch.esl.client.transport.HeaderParser;
import org.freeswitch.esl.client.transport.message.EslHeaders.Name;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.frame.TooLongFrameException;
import org.jboss.netty.handler.codec.replay.ReplayingDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Decoder used by the IO processing pipeline. Client consumers should never need to use
 * this class.
 * <p/>
 * Follows the following decode algorithm (from FreeSWITCH wiki)
 * <pre>
 *    Look for \n\n in your receive buffer
 *
 *    Examine data for existence of Content-Length
 *
 *    If NOT present, process event and remove from receive buffer
 *
 *    IF present, Shift buffer to remove 'header'
 *    Evaluate content-length value
 *
 *    Loop until receive buffer size is >= Content-length
 *    Extract content-length bytes from buffer and process
 * </pre>
 */
public class EslFrameDecoder extends ReplayingDecoder<EslFrameDecoder.State> {
	/**
	 * Line feed character
	 */
	static final byte LF = 10;

	protected static enum State {
		READ_HEADER,
		READ_BODY,
	}

	private final Logger log = LoggerFactory.getLogger(this.getClass());
	private final int maxHeaderSize;
	private EslMessage currentMessage;
	private boolean treatUnknownHeadersAsBody = false;

	public EslFrameDecoder(int maxHeaderSize) {
		super(State.READ_HEADER);
		if (maxHeaderSize <= 0) {
			throw new IllegalArgumentException(
				"maxHeaderSize must be a positive integer: " +
					maxHeaderSize);
		}
		this.maxHeaderSize = maxHeaderSize;
	}

	public EslFrameDecoder(int maxHeaderSize, boolean treatUnknownHeadersAsBody) {
		this(maxHeaderSize);
		this.treatUnknownHeadersAsBody = treatUnknownHeadersAsBody;
	}

	@Override
	protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer,
	                        State state) throws Exception {
		log.trace("decode() : state [{}]", state);<commit_msg>EslFrameDecoder: Remove unnecessary static modifier from enum<commit_after>/*
 * Copyright 2010 <NAME>.
 *
 * Licensed 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:
 *
 *    http://www.apache.org/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.freeswitch.esl.client.transport.message;

import org.freeswitch.esl.client.transport.HeaderParser;
import org.freeswitch.esl.client.transport.message.EslHeaders.Name;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.frame.TooLongFrameException;
import org.jboss.netty.handler.codec.replay.ReplayingDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Decoder used by the IO processing pipeline. Client consumers should never need to use
 * this class.
 * <p/>
 * Follows the following decode algorithm (from FreeSWITCH wiki)
 * <pre>
 *    Look for \n\n in your receive buffer
 *
 *    Examine data for existence of Content-Length
 *
 *    If NOT present, process event and remove from receive buffer
 *
 *    IF present, Shift buffer to remove 'header'
 *    Evaluate content-length value
 *
 *    Loop until receive buffer size is >= Content-length
 *    Extract content-length bytes from buffer and process
 * </pre>
 */
public class EslFrameDecoder extends ReplayingDecoder<EslFrameDecoder.State> {
	/**
	 * Line feed character
	 */
	static final byte LF = 10;

	protected enum State {
		READ_HEADER,
		READ_BODY,
	}

	private final Logger log = LoggerFactory.getLogger(this.getClass());
	private final int maxHeaderSize;
	private EslMessage currentMessage;
	private boolean treatUnknownHeadersAsBody = false;

	public EslFrameDecoder(int maxHeaderSize) {
		super(State.READ_HEADER);
		if (maxHeaderSize <= 0) {
			throw new IllegalArgumentException(
				"maxHeaderSize must be a positive integer: " +
					maxHeaderSize);
		}
		this.maxHeaderSize = maxHeaderSize;
	}

	public EslFrameDecoder(int maxHeaderSize, boolean treatUnknownHeadersAsBody) {
		this(maxHeaderSize);
		this.treatUnknownHeadersAsBody = treatUnknownHeadersAsBody;
	}

	@Override
	protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer,
	                        State state) throws Exception {
		log.trace("decode() : state [{}]", state);
{'fraction_non_alphanumeric': 0.08477037574877473, 'fraction_numerical': 0.00471954982755491, 'mean_word_length': 4.159176029962547, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1697505', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1523, 'n_words': 512}