All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jtransc.io.JTranscConsolePrintStream Maven / Gradle / Ivy

Go to download

JVM AOT compiler currently generating JavaScript, C++, Haxe, with initial focus on Kotlin and games.

There is a newer version: 0.6.8
Show newest version
package com.jtransc.io;

import com.jtransc.annotation.JTranscMethodBody;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

public class JTranscConsolePrintStream extends PrintStream {
	final boolean error;
	final ConsoleBaseStream stream;

	public JTranscConsolePrintStream(final boolean error) {
		this(error ? new ConsoleErrorStream() : new ConsoleOutputStream(), error);
	}

	private JTranscConsolePrintStream(ConsoleBaseStream stream, final boolean error) {
		super(stream);
		this.stream = stream;
		this.error = error;
	}

	@Override
	public void println(String x) {
		JTranscConsole.logOrError(stream.sb.toString() + x, error);
		stream.sb.setLength(0);
	}

	static private abstract class ConsoleBaseStream extends OutputStream {
		public StringBuilder sb = new StringBuilder();
		private final boolean error;

		public ConsoleBaseStream(boolean error) {
			this.error = error;
		}

		protected void _write(int b) throws IOException {
			char c = (char)b;
			if (c == '\n') {
				JTranscConsole.logOrError(sb.toString(), error);
				sb.setLength(0);
			} else {
				sb.append(c);
			}
		}
	}

	static private class ConsoleOutputStream extends ConsoleBaseStream {
		public ConsoleOutputStream() {
			super(false);
		}

		@Override
		@JTranscMethodBody(target = "dart", value = "stdout.writeCharCode(p0);")
		public void write(int b) throws IOException {
			_write(b);
		}
	}

	static private class ConsoleErrorStream extends ConsoleBaseStream {
		public ConsoleErrorStream() {
			super(true);
		}

		@Override
		@JTranscMethodBody(target = "dart", value = "stderr.writeCharCode(p0);")
		public void write(int b) throws IOException {
			_write(b);
		}
	}
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy