Skip to content

Commit

Permalink
Restore JDK 8 runtime compatibility, fixes jline#888
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Oct 31, 2023
1 parent ecbc73a commit f63a20f
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 40 deletions.
3 changes: 2 additions & 1 deletion builtins/src/main/java/org/jline/builtins/Tmux.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -2077,7 +2078,7 @@ public synchronized void flush() throws IOException {
}
}
if (out.position() > 0) {
out.flip();
((Buffer) out).clear();
terminal.write(out);
masterInputOutput.write(terminal.read().getBytes());
}
Expand Down
1 change: 0 additions & 1 deletion demo/jline-gogo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ echo "Launching Gogo JLine..."
echo "Classpath: $cp"
set mouse=a
java -cp $cp \
--enable-preview \
$opts \
-Dgosh.home="${DIRNAME}" \
-Djava.util.logging.config.file="${logconf}" \
Expand Down
19 changes: 12 additions & 7 deletions terminal/src/main/java/org/jline/utils/InputStreamReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import java.nio.charset.MalformedInputException;
import java.nio.charset.UnmappableCharacterException;

import static org.jline.utils.NonBlocking.limit;
import static org.jline.utils.NonBlocking.position;

/**
*
* NOTE for JLine: the default InputStreamReader that comes from the JRE
Expand Down Expand Up @@ -69,7 +72,9 @@ public InputStreamReader(InputStream in) {
.newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
bytes.limit(0);
int newLimit = 0;
ByteBuffer bytes1 = bytes;
limit(bytes1, newLimit);
}

/**
Expand Down Expand Up @@ -101,7 +106,7 @@ public InputStreamReader(InputStream in, final String enc) throws UnsupportedEnc
} catch (IllegalArgumentException e) {
throw (UnsupportedEncodingException) new UnsupportedEncodingException(enc).initCause(e);
}
bytes.limit(0);
limit(bytes, 0);
}

/**
Expand All @@ -118,7 +123,7 @@ public InputStreamReader(InputStream in, CharsetDecoder dec) {
dec.averageCharsPerByte();
this.in = in;
decoder = dec;
bytes.limit(0);
limit(bytes, 0);
}

/**
Expand All @@ -136,7 +141,7 @@ public InputStreamReader(InputStream in, Charset charset) {
decoder = charset.newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
bytes.limit(0);
limit(bytes, 0);
}

/**
Expand Down Expand Up @@ -273,7 +278,7 @@ public int read(char[] buf, int offset, int length) throws IOException {
} else if (was_red == 0) {
break;
}
bytes.limit(bytes.limit() + was_red);
limit(bytes, bytes.limit() + was_red);
}

// decode bytes
Expand All @@ -283,8 +288,8 @@ public int read(char[] buf, int offset, int length) throws IOException {
// compact the buffer if no space left
if (bytes.limit() == bytes.capacity()) {
bytes.compact();
bytes.limit(bytes.position());
bytes.position(0);
limit(bytes, bytes.position());
position(bytes, 0);
}
needInput = true;
} else {
Expand Down
55 changes: 36 additions & 19 deletions terminal/src/main/java/org/jline/utils/NonBlocking.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -78,8 +79,8 @@ private NonBlockingReaderInputStream(NonBlockingReader reader, Charset charset)
this.bytes = ByteBuffer.allocate(4);
this.chars = CharBuffer.allocate(2);
// No input available after initialization
this.bytes.limit(0);
this.chars.limit(0);
limit(this.bytes, 0);
limit(this.chars, 0);
}

@Override
Expand All @@ -102,15 +103,15 @@ public int read(long timeout, boolean isPeek) throws IOException {
}
if (c >= 0) {
if (!chars.hasRemaining()) {
chars.position(0);
chars.limit(0);
position(chars, 0);
limit(chars, 0);
}
int l = chars.limit();
chars.array()[chars.arrayOffset() + l] = (char) c;
chars.limit(l + 1);
bytes.clear();
limit(chars, l + 1);
clear(bytes);
encoder.encode(chars, bytes, false);
bytes.flip();
flip(bytes);
}
}
if (bytes.hasRemaining()) {
Expand Down Expand Up @@ -146,8 +147,8 @@ public NonBlockingInputStreamReader(NonBlockingInputStream input, CharsetDecoder
this.decoder = decoder;
this.bytes = ByteBuffer.allocate(2048);
this.chars = CharBuffer.allocate(1024);
this.bytes.limit(0);
this.chars.limit(0);
limit(this.bytes, 0);
limit(this.chars, 0);
}

@Override
Expand All @@ -160,15 +161,15 @@ protected int read(long timeout, boolean isPeek) throws IOException {
}
if (b >= 0) {
if (!bytes.hasRemaining()) {
bytes.position(0);
bytes.limit(0);
position(bytes, 0);
limit(bytes, 0);
}
int l = bytes.limit();
bytes.array()[bytes.arrayOffset() + l] = (byte) b;
bytes.limit(l + 1);
chars.clear();
limit(bytes, l + 1);
clear(chars);
decoder.decode(bytes, chars, false);
chars.flip();
flip(chars);
}
}
if (chars.hasRemaining()) {
Expand Down Expand Up @@ -198,18 +199,18 @@ public int readBuffered(char[] b, int off, int len, long timeout) throws IOExcep
Timeout t = new Timeout(timeout);
while (!chars.hasRemaining() && !t.elapsed()) {
if (!bytes.hasRemaining()) {
bytes.position(0);
bytes.limit(0);
position(bytes, 0);
limit(bytes, 0);
}
int nb = input.readBuffered(
bytes.array(), bytes.limit(), bytes.capacity() - bytes.limit(), t.timeout());
if (nb < 0) {
return nb;
}
bytes.limit(bytes.limit() + nb);
chars.clear();
limit(bytes, bytes.limit() + nb);
clear(chars);
decoder.decode(bytes, chars, false);
chars.flip();
flip(chars);
}
int nb = Math.min(len, chars.remaining());
chars.get(b, off, nb);
Expand All @@ -227,4 +228,20 @@ public void close() throws IOException {
input.close();
}
}

static void limit(Buffer buffer, int limit) {
buffer.limit(limit);
}

static void position(Buffer buffer, int pos) {
buffer.position(pos);
}

static void clear(Buffer buffer) {
buffer.clear();
}

static void flip(Buffer buffer) {
buffer.flip();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.io.OutputStream;
import java.nio.ByteBuffer;

import static org.jline.utils.NonBlocking.limit;

public class NonBlockingPumpInputStream extends NonBlockingInputStream {

private static final int DEFAULT_BUFFER_SIZE = 4096;
Expand All @@ -37,7 +39,7 @@ public NonBlockingPumpInputStream(int bufferSize) {
this.writeBuffer = ByteBuffer.wrap(buf);
this.output = new NbpOutputStream();
// There are no bytes available to read after initialization
readBuffer.limit(0);
limit(readBuffer, 0);
}

public OutputStream getOutputStream() {
Expand All @@ -63,12 +65,12 @@ private int wait(ByteBuffer buffer, long timeout) throws IOException {
private static boolean rewind(ByteBuffer buffer, ByteBuffer other) {
// Extend limit of other buffer if there is additional input/output available
if (buffer.position() > other.position()) {
other.limit(buffer.position());
limit(other, buffer.position());
}
// If we have reached the end of the buffer, rewind and set the new limit
if (buffer.position() == buffer.capacity()) {
buffer.rewind();
buffer.limit(other.position());
limit(buffer, other.position());
return true;
} else {
return false;
Expand Down
19 changes: 12 additions & 7 deletions terminal/src/main/java/org/jline/utils/PumpReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;

import static org.jline.utils.NonBlocking.clear;
import static org.jline.utils.NonBlocking.flip;
import static org.jline.utils.NonBlocking.limit;
import static org.jline.utils.NonBlocking.position;

public class PumpReader extends Reader {

private static final int EOF = -1;
Expand All @@ -42,7 +47,7 @@ public PumpReader(int bufferSize) {
this.writer = new Writer(this);

// There are no bytes available to read after initialization
readBuffer.limit(0);
limit(readBuffer, 0);
}

public java.io.Writer getWriter() {
Expand Down Expand Up @@ -139,13 +144,13 @@ private void waitForBufferSpace() throws InterruptedIOException, ClosedException
private static boolean rewind(CharBuffer buffer, CharBuffer other) {
// Extend limit of other buffer if there is additional input/output available
if (buffer.position() > other.position()) {
other.limit(buffer.position());
limit(other, buffer.position());
}

// If we have reached the end of the buffer, rewind and set the new limit
if (buffer.position() == buffer.capacity()) {
buffer.rewind();
buffer.limit(other.position());
limit(buffer, other.position());
return true;
} else {
return false;
Expand Down Expand Up @@ -322,7 +327,7 @@ synchronized void write(String str, int off, int len) throws IOException {
int count = Math.min(len, writeBuffer.remaining());
// CharBuffer.put(String) doesn't use getChars so do it manually
str.getChars(off, off + count, buf, writeBuffer.position());
writeBuffer.position(writeBuffer.position() + count);
position(writeBuffer, writeBuffer.position() + count);

off += count;
len -= count;
Expand Down Expand Up @@ -399,7 +404,7 @@ private InputStream(PumpReader reader, Charset charset) {
this.buffer = ByteBuffer.allocate((int) Math.ceil(encoder.maxBytesPerChar() * 2));

// No input available after initialization
buffer.limit(0);
limit(buffer, 0);
}

@Override
Expand All @@ -417,9 +422,9 @@ public int read() throws IOException {
}

private boolean readUsingBuffer() throws IOException {
buffer.clear(); // Reset buffer
clear(buffer); // Reset buffer
reader.readBytes(encoder, buffer);
buffer.flip();
flip(buffer);
return buffer.hasRemaining();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;

import static org.jline.utils.NonBlocking.flip;

/**
* Redirects an {@link OutputStream} to a {@link Writer} by decoding the data
* using the specified {@link Charset}.
Expand Down Expand Up @@ -90,7 +92,7 @@ public void close() throws IOException {
*/
private void processInput(final boolean endOfInput) throws IOException {
// Prepare decoderIn for reading
decoderIn.flip();
flip(decoderIn);
CoderResult coderResult;
while (true) {
coderResult = decoder.decode(decoderIn, decoderOut, endOfInput);
Expand Down
3 changes: 2 additions & 1 deletion terminal/src/test/java/org/jline/utils/DisplayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import static org.jline.utils.InfoCmp.Capability.enter_ca_mode;
import static org.jline.utils.InfoCmp.Capability.exit_ca_mode;
import static org.jline.utils.NonBlocking.flip;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class DisplayTest {
Expand Down Expand Up @@ -164,7 +165,7 @@ public synchronized void flush() throws IOException {
}
}
if (out.position() > 0) {
out.flip();
flip(out);
virtual.write(out);
masterInputOutput.write(virtual.read().getBytes());
}
Expand Down

0 comments on commit f63a20f

Please sign in to comment.