<commit_before>package jp.kshoji.driver.midi.handler;

import java.io.ByteArrayOutputStream;

import jp.kshoji.driver.midi.device.MidiInputDevice;
import jp.kshoji.driver.midi.listener.OnMidiInputEventListener;
import android.os.Handler.Callback;
import android.os.Message;

/**
 * USB MIDI Message parser
 * 
 * @author K.Shoji
 */
public final class MidiMessageCallback implements Callback {

	private final OnMidiInputEventListener midiEventListener;
	private final MidiInputDevice sender;
	private ByteArrayOutputStream systemExclusive = null;

	/**
	 * constructor
	 * 
	 * @param device
	 * @param midiEventListener
	 */
	public MidiMessageCallback(final MidiInputDevice device, final OnMidiInputEventListener midiEventListener) {
		this.midiEventListener = midiEventListener;
		sender = device;
	}

	/*
	 * (non-Javadoc)
	 * @see android.os.Handler.Callback#handleMessage(android.os.Message)
	 */
	@Override
	public boolean handleMessage(final Message msg) {
		if (midiEventListener == null) {
			return false;
		}
		byte[] read = (byte[]) msg.obj;
		int cable;
		int codeIndexNumber;
		int byte1;
		int byte2;
		int byte3;
		for (int i = 0; i < read.length; i += 4) {
			cable = (read[i + 0] >> 4) & 0xf;
			codeIndexNumber = read[i + 0] & 0xf;
			byte1 = read[i + 1] & 0xff;
			byte2 = read[i + 2] & 0xff;
			byte3 = read[i + 3] & 0xff;

			switch (codeIndexNumber) {
				case 0:
					midiEventListener.onMidiMiscellaneousFunctionCodes(sender, cable, byte1, byte2, byte3);
					break;
				case 1:
					midiEventListener.onMidiCableEvents(sender, cable, byte1, byte2, byte3);
					break;
				case 2:
				// system common message with 2 bytes
				{
					byte[] bytes = new byte[]{(byte) byte1, (byte) byte2};
					midiEventListener.onMidiSystemCommonMessage(sender, cable, bytes);
				}
					break;
				case 3:
				// system common message with 3 bytes
				{
					byte[] bytes = new byte[]{(byte) byte1, (byte) byte2, (byte) byte3};
					midiEventListener.onMidiSystemCommonMessage(sender, cable, bytes);
				}
					break;
				case 4:
					// sysex starts, and has next
					synchronized (this) {
						systemExclusive = new ByteArrayOutputStream();
					}
					synchronized (systemExclusive) {
						systemExclusive.write(byte1);
						systemExclusive.write(byte2);
						systemExclusive.write(byte3);<commit_msg>Fixed: The long sysex couldn't received properly. (Issue #2)<commit_after>package jp.kshoji.driver.midi.handler;

import java.io.ByteArrayOutputStream;

import jp.kshoji.driver.midi.device.MidiInputDevice;
import jp.kshoji.driver.midi.listener.OnMidiInputEventListener;
import android.os.Handler.Callback;
import android.os.Message;

/**
 * USB MIDI Message parser
 * 
 * @author K.Shoji
 */
public final class MidiMessageCallback implements Callback {

	private final OnMidiInputEventListener midiEventListener;
	private final MidiInputDevice sender;
	private ByteArrayOutputStream systemExclusive = null;

	/**
	 * constructor
	 * 
	 * @param device
	 * @param midiEventListener
	 */
	public MidiMessageCallback(final MidiInputDevice device, final OnMidiInputEventListener midiEventListener) {
		this.midiEventListener = midiEventListener;
		sender = device;
	}

	/*
	 * (non-Javadoc)
	 * @see android.os.Handler.Callback#handleMessage(android.os.Message)
	 */
	@Override
	public boolean handleMessage(final Message msg) {
		if (midiEventListener == null) {
			return false;
		}
		byte[] read = (byte[]) msg.obj;
		int cable;
		int codeIndexNumber;
		int byte1;
		int byte2;
		int byte3;
		for (int i = 0; i < read.length; i += 4) {
			cable = (read[i + 0] >> 4) & 0xf;
			codeIndexNumber = read[i + 0] & 0xf;
			byte1 = read[i + 1] & 0xff;
			byte2 = read[i + 2] & 0xff;
			byte3 = read[i + 3] & 0xff;

			switch (codeIndexNumber) {
				case 0:
					midiEventListener.onMidiMiscellaneousFunctionCodes(sender, cable, byte1, byte2, byte3);
					break;
				case 1:
					midiEventListener.onMidiCableEvents(sender, cable, byte1, byte2, byte3);
					break;
				case 2:
				// system common message with 2 bytes
				{
					byte[] bytes = new byte[]{(byte) byte1, (byte) byte2};
					midiEventListener.onMidiSystemCommonMessage(sender, cable, bytes);
				}
					break;
				case 3:
				// system common message with 3 bytes
				{
					byte[] bytes = new byte[]{(byte) byte1, (byte) byte2, (byte) byte3};
					midiEventListener.onMidiSystemCommonMessage(sender, cable, bytes);
				}
					break;
				case 4:
					// sysex starts, and has next
					synchronized (this) {
						if (systemExclusive == null) {
							systemExclusive = new ByteArrayOutputStream();
						}
					}
					synchronized (systemExclusive) {
						systemExclusive.write(byte1);
						systemExclusive.write(byte2);
						systemExclusive.write(byte3);
{'fraction_non_alphanumeric': 0.11066525871172123, 'fraction_numerical': 0.017106652587117212, 'mean_word_length': 3.4976258309591644, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6744587', 'n_tokens_mistral': 1873, 'n_tokens_neox': 1518, 'n_words': 378}