Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore JDK 8 runtime compatibility, fixes #888 #890

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 32 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@
<graal.plugin.version>21.2.0</graal.plugin.version>

<surefire.argLine>--add-opens java.base/java.io=ALL-UNNAMED</surefire.argLine>

<animal-sniffer.skip>false</animal-sniffer.skip>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -713,6 +715,36 @@
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
<version>1.23</version>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>9.6</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<signature>
<groupId>org.codehaus.mojo.signature</groupId>
<artifactId>java18</artifactId>
<version>1.0</version>
</signature>
<skip>${animal-sniffer.skip}</skip>
<ignoreDependencies>true</ignoreDependencies>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
1 change: 1 addition & 0 deletions terminal-ffm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<properties>
<java.target.version>21</java.target.version>
<automatic.module.name>org.jline.terminal.ffm</automatic.module.name>
<animal-sniffer.skip>true</animal-sniffer.skip>
</properties>

<dependencies>
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
59 changes: 40 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,24 @@ 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();
}

static void rewind(Buffer buffer) {
buffer.rewind();
}
}
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());
NonBlocking.rewind(buffer);
limit(buffer, other.position());
return true;
} else {
return false;
Expand Down
21 changes: 13 additions & 8 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());
NonBlocking.rewind(buffer);
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,9 @@
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;

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

/**
* Redirects an {@link OutputStream} to a {@link Writer} by decoding the data
* using the specified {@link Charset}.
Expand Down Expand Up @@ -90,7 +93,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 All @@ -116,7 +119,7 @@ private void processInput(final boolean endOfInput) throws IOException {
private void flushOutput() throws IOException {
if (decoderOut.position() > 0) {
out.write(decoderOut.array(), 0, decoderOut.position());
decoderOut.rewind();
rewind(decoderOut);
}
}
}
Loading
Loading