• R/O
  • SSH

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

アルファ版。新版→https://osdn.jp/users/tacticsrealize/pf/ChlorophyllUploader/wiki/FrontPage


File Info

修訂. 82b375367456635f711775be0d76dcafef88ca32
大小 2,360 bytes
時間 2015-07-08 20:15:01
作者
Log Message

新ファイル形式に対応しArduinoとの連携

Content

package ants.chlorofilsender;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.IntConsumer;

import mirrg.util.UTF8StreamReader;

public class InputChlorofilSender extends UTF8StreamReader
{

	public InputChlorofilSender(InputStream in, boolean blocking)
	{
		super(in, blocking);
	}

	protected ArrayList<String> messageBlockBuffer;
	protected String messageBlockSentinel = null;

	public void stream(
		IntConsumer consumerAcceptedChar,
		BiConsumer<EnumTypeMessage, String> consumerMessage,
		Consumer<ArrayList<String>> consumerMessageBlock,
		Consumer<Exception> consumerInternalException)
	{
		stream((Consumer<String>) line -> {

			if (messageBlockSentinel != null) {
				if (line.equals(messageBlockSentinel)) {
					messageBlockSentinel = null;
					consumerMessageBlock.accept(messageBlockBuffer);
					messageBlockBuffer = new ArrayList<>();
				} else {
					messageBlockBuffer.add(line);
				}

				return;
			}

			{
				String res;

				if ((res = getProperty(line, "C")) != null) {
					int i;
					try {
						i = Integer.parseInt(res, 10);
					} catch (NumberFormatException e) {
						consumerInternalException.accept(e);
						return;
					}
					consumerAcceptedChar.accept(i);
					return;
				}
				if ((res = getProperty(line, "Message")) != null) {
					consumerMessage.accept(EnumTypeMessage.MESSAGE, res);
					return;
				}
				if ((res = getProperty(line, "Error")) != null) {
					consumerMessage.accept(EnumTypeMessage.ERROR, res);
					return;
				}
				if ((res = getProperty(line, "Warnings")) != null) {
					consumerMessage.accept(EnumTypeMessage.WARNINGS, res);
					return;
				}
				if ((res = getProperty(line, "MessageBlock")) != null) {
					messageBlockSentinel = res;
					messageBlockBuffer = new ArrayList<>();
					return;
				}

			}

		});
	}

	protected String getProperty(String string, String prefix)
	{
		prefix += ":";

		if (string.startsWith(prefix)) {
			return string.substring(prefix.length());
		} else {
			return null;
		}
	}

	public static enum EnumTypeMessage
	{
		MESSAGE, ERROR, WARNINGS,
	}

	public void close()
	{
		// TODO 自動生成されたメソッド・スタブ
		
	}

}