diff --git a/builtins/src/main/java/org/jline/builtins/Tmux.java b/builtins/src/main/java/org/jline/builtins/Tmux.java index 486c616f..be02c3f7 100644 --- a/builtins/src/main/java/org/jline/builtins/Tmux.java +++ b/builtins/src/main/java/org/jline/builtins/Tmux.java @@ -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; @@ -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()); } diff --git a/demo/jline-gogo.sh b/demo/jline-gogo.sh index c6c8325b..7ba3ca9a 100755 --- a/demo/jline-gogo.sh +++ b/demo/jline-gogo.sh @@ -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}" \ diff --git a/pom.xml b/pom.xml index 1d06f975..77139c15 100644 --- a/pom.xml +++ b/pom.xml @@ -117,6 +117,8 @@ 21.2.0 --add-opens java.base/java.io=ALL-UNNAMED + + false @@ -713,6 +715,36 @@ com.diffplug.spotless spotless-maven-plugin + + + org.codehaus.mojo + animal-sniffer-maven-plugin + 1.23 + + + org.ow2.asm + asm + 9.6 + + + + + check + + check + + + + org.codehaus.mojo.signature + java18 + 1.0 + + ${animal-sniffer.skip} + true + + + + diff --git a/terminal-ffm/pom.xml b/terminal-ffm/pom.xml index 9811ea01..90ac4746 100644 --- a/terminal-ffm/pom.xml +++ b/terminal-ffm/pom.xml @@ -25,6 +25,7 @@ 21 org.jline.terminal.ffm + true diff --git a/terminal/src/main/java/org/jline/utils/InputStreamReader.java b/terminal/src/main/java/org/jline/utils/InputStreamReader.java index 141bb17e..ef8d1910 100644 --- a/terminal/src/main/java/org/jline/utils/InputStreamReader.java +++ b/terminal/src/main/java/org/jline/utils/InputStreamReader.java @@ -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 @@ -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); } /** @@ -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); } /** @@ -118,7 +123,7 @@ public InputStreamReader(InputStream in, CharsetDecoder dec) { dec.averageCharsPerByte(); this.in = in; decoder = dec; - bytes.limit(0); + limit(bytes, 0); } /** @@ -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); } /** @@ -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 @@ -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 { diff --git a/terminal/src/main/java/org/jline/utils/NonBlocking.java b/terminal/src/main/java/org/jline/utils/NonBlocking.java index 4d2721b2..b3e32f55 100644 --- a/terminal/src/main/java/org/jline/utils/NonBlocking.java +++ b/terminal/src/main/java/org/jline/utils/NonBlocking.java @@ -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; @@ -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 @@ -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()) { @@ -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 @@ -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()) { @@ -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); @@ -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(); + } } diff --git a/terminal/src/main/java/org/jline/utils/NonBlockingPumpInputStream.java b/terminal/src/main/java/org/jline/utils/NonBlockingPumpInputStream.java index 745ee6f3..2c5cbfc6 100644 --- a/terminal/src/main/java/org/jline/utils/NonBlockingPumpInputStream.java +++ b/terminal/src/main/java/org/jline/utils/NonBlockingPumpInputStream.java @@ -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; @@ -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() { @@ -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; diff --git a/terminal/src/main/java/org/jline/utils/PumpReader.java b/terminal/src/main/java/org/jline/utils/PumpReader.java index bc1d4ed0..57dbf994 100644 --- a/terminal/src/main/java/org/jline/utils/PumpReader.java +++ b/terminal/src/main/java/org/jline/utils/PumpReader.java @@ -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; @@ -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() { @@ -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; @@ -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; @@ -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 @@ -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(); } diff --git a/terminal/src/main/java/org/jline/utils/WriterOutputStream.java b/terminal/src/main/java/org/jline/utils/WriterOutputStream.java index 02b314be..e5fd2a6a 100644 --- a/terminal/src/main/java/org/jline/utils/WriterOutputStream.java +++ b/terminal/src/main/java/org/jline/utils/WriterOutputStream.java @@ -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}. @@ -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); @@ -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); } } } diff --git a/terminal/src/test/java/org/jline/utils/DisplayTest.java b/terminal/src/test/java/org/jline/utils/DisplayTest.java index 55b9bd33..c90c3582 100644 --- a/terminal/src/test/java/org/jline/utils/DisplayTest.java +++ b/terminal/src/test/java/org/jline/utils/DisplayTest.java @@ -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 { @@ -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()); }