<commit_before>package tuwien.inso.mnsa.protocol;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * A protocol based on http://www.win.tue.nl/pinpasjc/docs/apis/offcard/com/ibm/jc/terminal/RemoteJCTerminal.html
 * 
 * <LI>1byte MTY Message type</LI> <LI>1byte LN payload length</LI> <LI>nbyte PLD payload, max 0xFF bytes</LI>
 */
public class Message {

	public static final byte TYPE_TEST = 0x00;
	public static final byte TYPE_APDU_COMMAND = 0x01;
	public static final byte TYPE_APDU_RESPONSE = 0x02;

	private static final short OFFSET_MTY = 0;
	private static final short OFFSET_LN = 1;
	private static final short OFFSET_PY = 2;

	private final byte messageType;
	private final byte length;
	private final byte[] payload;

	private Message(byte messageType, byte length, byte[] payload) {
		this.messageType = messageType;
		this.length = length;
		this.payload = payload;
	}

	public static Message createFrom(byte messageType, byte length, byte[] payload) {
		return new Message(messageType, length, payload);
	}

	public static Message createFrom(InputStream inStream) throws IOException {
		byte[] buffer = new byte[2];
		readExacltyToLength(inStream, buffer, 0, 2);

		byte messageType = buffer[OFFSET_MTY];
		byte length = buffer[OFFSET_LN];

		byte[] payload = new byte[messageType];
		readExacltyToLength(inStream, payload, 0, length);

		return new Message(messageType, length, payload);
	}

	public void write(OutputStream outStream) throws IOException {
		outStream.write(messageType);
		outStream.write(length);
		outStream.write(payload, 0, length);
	}

	public byte getMessageType() {
		return messageType;
	}

	public short getLength() {
		return length;
	}

	public byte[] getPayload() {
		return payload;
	}

	public String toString() {
		return "MTY[" + messageType + "] LN[" + length + "] PY[" + payload + "]";
	}

	private static void readExacltyToLength(InputStream inStream, byte[] output, int offset, int length) throws IOException {
		int total = 0;
		while (offset + total < length) {
			int read = inStream.read(output, offset + total, length - total);
			if (read == -1)
				break;
			total += read;
		}
	}

}<commit_msg>epic fail fixed (length != messege type, omg)<commit_after>package tuwien.inso.mnsa.protocol;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * A protocol based on http://www.win.tue.nl/pinpasjc/docs/apis/offcard/com/ibm/jc/terminal/RemoteJCTerminal.html
 * 
 * <LI>1byte MTY Message type</LI> <LI>1byte LN payload length</LI> <LI>nbyte PLD payload, max 0xFF bytes</LI>
 */
public class Message {

	public static final byte TYPE_TEST = 0x00;
	public static final byte TYPE_APDU_COMMAND = 0x01;
	public static final byte TYPE_APDU_RESPONSE = 0x02;

	private static final short OFFSET_MTY = 0;
	private static final short OFFSET_LN = 1;
	private static final short OFFSET_PY = 2;

	private final byte messageType;
	private final byte length;
	private final byte[] payload;

	private Message(byte messageType, byte length, byte[] payload) {
		this.messageType = messageType;
		this.length = length;
		this.payload = payload;
	}

	public static Message createFrom(byte messageType, byte length, byte[] payload) {
		return new Message(messageType, length, payload);
	}

	public static Message createFrom(InputStream inStream) throws IOException {
		byte[] buffer = new byte[2];
		readExacltyToLength(inStream, buffer, 0, 2);

		byte messageType = buffer[OFFSET_MTY];
		byte length = buffer[OFFSET_LN];

		byte[] payload = new byte[length];
		readExacltyToLength(inStream, payload, 0, length);

		return new Message(messageType, length, payload);
	}

	public void write(OutputStream outStream) throws IOException {
		outStream.write(messageType);
		outStream.write(length);
		outStream.write(payload, 0, length);
	}

	public byte getMessageType() {
		return messageType;
	}

	public short getLength() {
		return length;
	}

	public byte[] getPayload() {
		return payload;
	}

	public String toString() {
		return "MTY[" + messageType + "] LN[" + length + "] PY[" + payload + "]";
	}

	private static void readExacltyToLength(InputStream inStream, byte[] output, int offset, int length) throws IOException {
		int total = 0;
		while (total < length) {
			int read = inStream.read(output, offset + total, length - total);
			if (read == -1)
				break;
			total += read;
		}
	}
}
{'fraction_non_alphanumeric': 0.11017909771026978, 'fraction_numerical': 0.00997506234413965, 'mean_word_length': 4.3870573870573875, 'pattern_counts': {'":': 0, '<': 17, '<?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': '1462898', 'n_tokens_mistral': 1521, 'n_tokens_neox': 1386, 'n_words': 421}