org.pkl.thirdparty.jline.terminal.impl.ExternalTerminal Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pkl-tools Show documentation
Show all versions of pkl-tools Show documentation
Fat Jar containing pkl-cli, pkl-codegen-java, pkl-codegen-kotlin, pkl-config-java, pkl-core, pkl-doc, and their shaded third-party dependencies.
/*
* Copyright (c) 2002-2018, the original author(s).
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
*
* https://opensource.org/licenses/BSD-3-Clause
*/
package org.pkl.thirdparty.jline.terminal.impl;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.IntConsumer;
import org.pkl.thirdparty.jline.terminal.Attributes;
import org.pkl.thirdparty.jline.terminal.Cursor;
import org.pkl.thirdparty.jline.terminal.Size;
/**
* Console implementation with embedded line disciplined.
*
* This terminal is well-suited for supporting incoming external
* connections, such as from the network (through telnet, ssh,
* or any kind of protocol).
* The terminal will start consuming the input in a separate thread
* to generate interruption events.
*
* @see LineDisciplineTerminal
*/
public class ExternalTerminal extends LineDisciplineTerminal {
protected final AtomicBoolean closed = new AtomicBoolean();
protected final InputStream masterInput;
protected final Object lock = new Object();
protected boolean paused = true;
protected Thread pumpThread;
public ExternalTerminal(
String name, String type, InputStream masterInput, OutputStream masterOutput, Charset encoding)
throws IOException {
this(name, type, masterInput, masterOutput, encoding, SignalHandler.SIG_DFL);
}
public ExternalTerminal(
String name,
String type,
InputStream masterInput,
OutputStream masterOutput,
Charset encoding,
SignalHandler signalHandler)
throws IOException {
this(name, type, masterInput, masterOutput, encoding, signalHandler, false);
}
public ExternalTerminal(
String name,
String type,
InputStream masterInput,
OutputStream masterOutput,
Charset encoding,
SignalHandler signalHandler,
boolean paused)
throws IOException {
this(name, type, masterInput, masterOutput, encoding, signalHandler, paused, null, null);
}
public ExternalTerminal(
String name,
String type,
InputStream masterInput,
OutputStream masterOutput,
Charset encoding,
SignalHandler signalHandler,
boolean paused,
Attributes attributes,
Size size)
throws IOException {
super(name, type, masterOutput, encoding, signalHandler);
this.masterInput = masterInput;
if (attributes != null) {
setAttributes(attributes);
}
if (size != null) {
setSize(size);
}
if (!paused) {
resume();
}
}
protected void doClose() throws IOException {
if (closed.compareAndSet(false, true)) {
pause();
super.doClose();
}
}
@Override
public boolean canPauseResume() {
return true;
}
@Override
public void pause() {
synchronized (lock) {
paused = true;
}
}
@Override
public void pause(boolean wait) throws InterruptedException {
Thread p;
synchronized (lock) {
paused = true;
p = pumpThread;
}
if (p != null) {
p.interrupt();
p.join();
}
}
@Override
public void resume() {
synchronized (lock) {
paused = false;
if (pumpThread == null) {
pumpThread = new Thread(this::pump, toString() + " input pump thread");
pumpThread.setDaemon(true);
pumpThread.start();
}
}
}
@Override
public boolean paused() {
synchronized (lock) {
return paused;
}
}
public void pump() {
try {
byte[] buf = new byte[1024];
while (true) {
int c = masterInput.read(buf);
if (c >= 0) {
processInputBytes(buf, 0, c);
}
if (c < 0 || closed.get()) {
break;
}
synchronized (lock) {
if (paused) {
pumpThread = null;
return;
}
}
}
} catch (IOException e) {
processIOException(e);
} finally {
synchronized (lock) {
pumpThread = null;
}
}
try {
slaveInput.close();
} catch (IOException e) {
// ignore
}
}
@Override
public Cursor getCursorPosition(IntConsumer discarded) {
return CursorSupport.getCursorPosition(this, discarded);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy