<commit_before>/*
 * Author: <NAME>
 */

package pcap.reconst.reconstructor;

import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import pcap.reconst.beans.TcpConnection;
import pcap.reconst.beans.TcpSequenceCounter;
import pcap.reconst.beans.TimestampPair;
import pcap.reconst.beans.packet.PlaceholderTcpPacket;
import pcap.reconst.beans.packet.TcpPacket;

public class TcpReassembler {
	private static Log log = LogFactory.getLog(TcpReassembler.class);

	private TcpSequenceCounter reqCounter = null, respCounter = null;
	private List<TcpPacket> orderedPackets = new ArrayList<TcpPacket>();
	private List<Integer> reqIndexes = new ArrayList<Integer>();
	private List<Integer> respIndexes = new ArrayList<Integer>();
	private String packetData = null;
	private Map<Integer, Integer> packetPositions = new HashMap<Integer, Integer>();

	private boolean rebuildData = true;

	public boolean isIncomplete() {
		for (TcpPacket packet : orderedPackets) {
			if (packet instanceof PlaceholderTcpPacket) {
				return true;
			}
		}
		return false;
	}

	public boolean isEmpty() {
		return orderedPackets.isEmpty();
	}

	public String getOrderedPacketData() {
		if (rebuildData || packetData == null) {
			buildPacketData();
			rebuildData = false;
		}
		return packetData;
	}

	public void buildPacketData() {
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < orderedPackets.size(); i++) {
			byte[] data = orderedPackets.get(i).getData();
			if (data != null && data.length > 0) {
				int startpos = buf.length();
				buf.append(new String(data));
				packetPositions.put(buf.length(), i);
				if (log.isInfoEnabled()) {
					log.info("Start position: " + startpos + " End position: "
							+ buf.length() + "\n" + new String(data));
				}
			}
		}
		packetData = buf.toString();
	}

	public TimestampPair getTimestampRange(String needle) {
		if (rebuildData || packetData == null) {
			buildPacketData();
			rebuildData = false;
		}

		int beginIndex = this.packetData.indexOf(needle);
		int endIndex = beginIndex + needle.length();

		if (log.isInfoEnabled()) {
			log.info("Find timestamp for:\n" + needle + "\nbegin index: "
					+ beginIndex + " end index: " + endIndex);
		}

		double startTS = -1.0, endTS = -1.0;

		List<Integer> positions = new ArrayList<Integer>(
				packetPositions.keySet());
		Collections.sort(positions);

		for (int pos : positions) {<commit_msg>Changed the logging level for some of the messages in TcpReassembler.<commit_after>/*
 * Author: <NAME>
 */

package pcap.reconst.reconstructor;

import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import pcap.reconst.beans.TcpConnection;
import pcap.reconst.beans.TcpSequenceCounter;
import pcap.reconst.beans.TimestampPair;
import pcap.reconst.beans.packet.PlaceholderTcpPacket;
import pcap.reconst.beans.packet.TcpPacket;

public class TcpReassembler {
	private static Log log = LogFactory.getLog(TcpReassembler.class);

	private TcpSequenceCounter reqCounter = null, respCounter = null;
	private List<TcpPacket> orderedPackets = new ArrayList<TcpPacket>();
	private List<Integer> reqIndexes = new ArrayList<Integer>();
	private List<Integer> respIndexes = new ArrayList<Integer>();
	private String packetData = null;
	private Map<Integer, Integer> packetPositions = new HashMap<Integer, Integer>();

	private boolean rebuildData = true;

	public boolean isIncomplete() {
		for (TcpPacket packet : orderedPackets) {
			if (packet instanceof PlaceholderTcpPacket) {
				return true;
			}
		}
		return false;
	}

	public boolean isEmpty() {
		return orderedPackets.isEmpty();
	}

	public String getOrderedPacketData() {
		if (rebuildData || packetData == null) {
			buildPacketData();
			rebuildData = false;
		}
		return packetData;
	}

	public void buildPacketData() {
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < orderedPackets.size(); i++) {
			byte[] data = orderedPackets.get(i).getData();
			if (data != null && data.length > 0) {
				int startpos = buf.length();
				buf.append(new String(data));
				packetPositions.put(buf.length(), i);
				if (log.isDebugEnabled()) {
					log.debug("Start position: " + startpos + " End position: "
							+ buf.length() + "\n" + new String(data));
				}
			}
		}
		packetData = buf.toString();
	}

	public TimestampPair getTimestampRange(String needle) {
		if (rebuildData || packetData == null) {
			buildPacketData();
			rebuildData = false;
		}

		int beginIndex = this.packetData.indexOf(needle);
		int endIndex = beginIndex + needle.length();

		if (log.isDebugEnabled()) {
			log.debug("Find timestamp for:\n" + needle + "\nbegin index: "
					+ beginIndex + " end index: " + endIndex);
		}

		double startTS = -1.0, endTS = -1.0;

		List<Integer> positions = new ArrayList<Integer>(
				packetPositions.keySet());
		Collections.sort(positions);

		for (int pos : positions) {
{'fraction_non_alphanumeric': 0.12680060652009098, 'fraction_numerical': 0.002274450341167551, 'mean_word_length': 4.245526838966203, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '7314858', 'n_tokens_mistral': 1823, 'n_tokens_neox': 1647, 'n_words': 402}