From bd1678db3edccc4bc0ea6abcdfb0328a252c2aa6 Mon Sep 17 00:00:00 2001 From: Glavo Date: Mon, 2 Oct 2023 22:43:19 +0800 Subject: [PATCH] Modernize code (#267) # Conflicts: # src/main/java/org/fusesource/jansi/AnsiConsole.java # src/main/java/org/fusesource/jansi/internal/OSInfo.java --- src/main/java/org/fusesource/jansi/Ansi.java | 9 ++---- .../org/fusesource/jansi/AnsiConsole.java | 21 +++++--------- .../java/org/fusesource/jansi/AnsiMain.java | 4 ++- .../jansi/internal/JansiLoader.java | 29 +++---------------- .../org/fusesource/jansi/internal/OSInfo.java | 20 ++++++------- .../fusesource/jansi/io/AnsiOutputStream.java | 8 +++-- .../jansi/io/FastBufferedOutputStream.java | 2 +- .../java/org/fusesource/jansi/AnsiTest.java | 22 ++++---------- .../org/fusesource/jansi/EncodingTest.java | 10 +++---- .../jansi/io/AnsiOutputStreamTest.java | 4 +-- 10 files changed, 45 insertions(+), 84 deletions(-) diff --git a/src/main/java/org/fusesource/jansi/Ansi.java b/src/main/java/org/fusesource/jansi/Ansi.java index 803604cb..576d8d53 100644 --- a/src/main/java/org/fusesource/jansi/Ansi.java +++ b/src/main/java/org/fusesource/jansi/Ansi.java @@ -149,17 +149,14 @@ public int value() { } } + @FunctionalInterface public interface Consumer { void apply(Ansi ansi); } public static final String DISABLE = Ansi.class.getName() + ".disable"; - private static Callable detector = new Callable() { - public Boolean call() throws Exception { - return !Boolean.getBoolean(DISABLE); - } - }; + private static Callable detector = () -> !Boolean.getBoolean(DISABLE); public static void setDetector(final Callable detector) { if (detector == null) throw new IllegalArgumentException(); @@ -374,7 +371,7 @@ public Ansi reset() { } private final StringBuilder builder; - private final ArrayList attributeOptions = new ArrayList(5); + private final ArrayList attributeOptions = new ArrayList<>(5); public Ansi() { this(new StringBuilder(80)); diff --git a/src/main/java/org/fusesource/jansi/AnsiConsole.java b/src/main/java/org/fusesource/jansi/AnsiConsole.java index 7fe9271d..7cd0acd8 100644 --- a/src/main/java/org/fusesource/jansi/AnsiConsole.java +++ b/src/main/java/org/fusesource/jansi/AnsiConsole.java @@ -294,19 +294,13 @@ public int getTerminalWidth() { SetConsoleMode(console, mode[0]); // set it back for now, but we know it works processor = null; type = AnsiType.VirtualTerminal; - installer = new AnsiOutputStream.IoRunnable() { - @Override - public void run() throws IOException { - virtualProcessing++; - SetConsoleMode(console, mode[0] | ENABLE_VIRTUAL_TERMINAL_PROCESSING); - } + installer = () -> { + virtualProcessing++; + SetConsoleMode(console, mode[0] | ENABLE_VIRTUAL_TERMINAL_PROCESSING); }; - uninstaller = new AnsiOutputStream.IoRunnable() { - @Override - public void run() throws IOException { - if (--virtualProcessing == 0) { - SetConsoleMode(console, mode[0]); - } + uninstaller = () -> { + if (--virtualProcessing == 0) { + SetConsoleMode(console, mode[0]); } }; width = kernel32Width; @@ -460,8 +454,7 @@ static boolean getBoolean(String name) { try { String val = System.getProperty(name); result = val.isEmpty() || Boolean.parseBoolean(val); - } catch (IllegalArgumentException e) { - } catch (NullPointerException e) { + } catch (IllegalArgumentException | NullPointerException ignored) { } return result; } diff --git a/src/main/java/org/fusesource/jansi/AnsiMain.java b/src/main/java/org/fusesource/jansi/AnsiMain.java index d9a3aad8..4cbe52fe 100644 --- a/src/main/java/org/fusesource/jansi/AnsiMain.java +++ b/src/main/java/org/fusesource/jansi/AnsiMain.java @@ -23,6 +23,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintStream; +import java.nio.charset.StandardCharsets; import java.util.Properties; import org.fusesource.jansi.Ansi.Attribute; @@ -31,6 +32,7 @@ import org.fusesource.jansi.internal.Kernel32; import org.fusesource.jansi.internal.MingwSupport; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.fusesource.jansi.Ansi.ansi; import static org.fusesource.jansi.internal.Kernel32.GetConsoleScreenBufferInfo; @@ -309,7 +311,7 @@ private static String getPomPropertiesVersion(String path) throws IOException { private static void printJansiLogoDemo() throws IOException { BufferedReader in = - new BufferedReader(new InputStreamReader(AnsiMain.class.getResourceAsStream("jansi.txt"), "UTF-8")); + new BufferedReader(new InputStreamReader(AnsiMain.class.getResourceAsStream("jansi.txt"), UTF_8)); try { String l; while ((l = in.readLine()) != null) { diff --git a/src/main/java/org/fusesource/jansi/internal/JansiLoader.java b/src/main/java/org/fusesource/jansi/internal/JansiLoader.java index f705620c..1b4494f3 100644 --- a/src/main/java/org/fusesource/jansi/internal/JansiLoader.java +++ b/src/main/java/org/fusesource/jansi/internal/JansiLoader.java @@ -37,8 +37,9 @@ import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.util.Arrays; import java.util.LinkedList; import java.util.List; @@ -198,9 +199,7 @@ private static boolean extractAndLoadLibraryFile( if (!extractedLckFile.exists()) { new FileOutputStream(extractedLckFile).close(); } - try (OutputStream out = new FileOutputStream(extractedLibFile)) { - copy(in, out); - } + Files.copy(in, extractedLibFile.toPath(), StandardCopyOption.REPLACE_EXISTING); } finally { // Delete the extracted lib file on JVM exit. extractedLibFile.deleteOnExit(); @@ -239,14 +238,6 @@ private static String randomUUID() { return Long.toHexString(new Random().nextLong()); } - private static void copy(InputStream in, OutputStream out) throws IOException { - byte[] buf = new byte[8192]; - int n; - while ((n = in.read(buf)) > 0) { - out.write(buf, 0, n); - } - } - /** * Loads native library using the given path and name of the library. * @@ -358,7 +349,7 @@ private static void loadJansiNativeLibrary() throws Exception { throw new Exception(String.format( "No native library found for os.name=%s, os.arch=%s, paths=[%s]", - OSInfo.getOSName(), OSInfo.getArchName(), join(triedPaths, File.pathSeparator))); + OSInfo.getOSName(), OSInfo.getArchName(), String.join(File.pathSeparator, triedPaths))); } private static boolean hasResource(String path) { @@ -401,16 +392,4 @@ public static String getVersion() { } return version; } - - private static String join(List list, String separator) { - StringBuilder sb = new StringBuilder(); - boolean first = true; - for (String item : list) { - if (first) first = false; - else sb.append(separator); - - sb.append(item); - } - return sb.toString(); - } } diff --git a/src/main/java/org/fusesource/jansi/internal/OSInfo.java b/src/main/java/org/fusesource/jansi/internal/OSInfo.java index 8c9999ca..9409656b 100644 --- a/src/main/java/org/fusesource/jansi/internal/OSInfo.java +++ b/src/main/java/org/fusesource/jansi/internal/OSInfo.java @@ -34,6 +34,8 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.HashMap; import java.util.Locale; @@ -122,19 +124,15 @@ public static boolean isAndroid() { public static boolean isAlpine() { try { - Process p = Runtime.getRuntime().exec("cat /etc/os-release | grep ^ID"); - p.waitFor(); - - InputStream in = p.getInputStream(); - try { - return readFully(in).toLowerCase().contains("alpine"); - } finally { - in.close(); + for (String line : Files.readAllLines(Paths.get("/etc/os-release"))) { + if (line.startsWith("ID") && line.toLowerCase(Locale.ROOT).contains("alpine")) { + return true; + } } - - } catch (Throwable e) { - return false; + } catch (Throwable ignored) { } + + return false; } static String getHardwareName() { diff --git a/src/main/java/org/fusesource/jansi/io/AnsiOutputStream.java b/src/main/java/org/fusesource/jansi/io/AnsiOutputStream.java index d925d728..9c45afef 100644 --- a/src/main/java/org/fusesource/jansi/io/AnsiOutputStream.java +++ b/src/main/java/org/fusesource/jansi/io/AnsiOutputStream.java @@ -25,6 +25,8 @@ import org.fusesource.jansi.AnsiMode; import org.fusesource.jansi.AnsiType; +import static java.nio.charset.StandardCharsets.US_ASCII; + /** * A ANSI print stream extracts ANSI escape codes written to * an output stream and calls corresponding AnsiProcessor.process* methods. @@ -38,12 +40,14 @@ */ public class AnsiOutputStream extends FilterOutputStream { - public static final byte[] RESET_CODE = "\033[0m".getBytes(); + public static final byte[] RESET_CODE = "\033[0m".getBytes(US_ASCII); + @FunctionalInterface public interface IoRunnable { void run() throws IOException; } + @FunctionalInterface public interface WidthSupplier { int getTerminalWidth(); } @@ -79,7 +83,7 @@ public int getTerminalWidth() { private final byte[] buffer = new byte[MAX_ESCAPE_SEQUENCE_LENGTH]; private int pos = 0; private int startOfValue; - private final ArrayList options = new ArrayList(); + private final ArrayList options = new ArrayList<>(); private int state = LOOKING_FOR_FIRST_ESC_CHAR; private final Charset cs; diff --git a/src/main/java/org/fusesource/jansi/io/FastBufferedOutputStream.java b/src/main/java/org/fusesource/jansi/io/FastBufferedOutputStream.java index 823e8019..e436c35e 100644 --- a/src/main/java/org/fusesource/jansi/io/FastBufferedOutputStream.java +++ b/src/main/java/org/fusesource/jansi/io/FastBufferedOutputStream.java @@ -24,7 +24,7 @@ */ public class FastBufferedOutputStream extends FilterOutputStream { - protected final byte buf[] = new byte[8192]; + protected final byte[] buf = new byte[8192]; protected int count; public FastBufferedOutputStream(OutputStream out) { diff --git a/src/test/java/org/fusesource/jansi/AnsiTest.java b/src/test/java/org/fusesource/jansi/AnsiTest.java index 1d8af409..4e9dcfa6 100644 --- a/src/test/java/org/fusesource/jansi/AnsiTest.java +++ b/src/test/java/org/fusesource/jansi/AnsiTest.java @@ -29,6 +29,8 @@ import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Tests for the {@link Ansi} class. @@ -38,20 +40,10 @@ public class AnsiTest { @Test public void testSetEnabled() throws Exception { Ansi.setEnabled(false); - new Thread() { - @Override - public void run() { - assertEquals(false, Ansi.isEnabled()); - } - }.run(); + new Thread(() -> assertFalse(Ansi.isEnabled())).run(); Ansi.setEnabled(true); - new Thread() { - @Override - public void run() { - assertEquals(true, Ansi.isEnabled()); - } - }.run(); + new Thread(() -> assertTrue(Ansi.isEnabled())).run(); } @Test @@ -67,11 +59,7 @@ public void testApply() { assertEquals( "test", Ansi.ansi() - .apply(new Ansi.Consumer() { - public void apply(Ansi ansi) { - ansi.a("test"); - } - }) + .apply(ansi -> ansi.a("test")) .toString()); } diff --git a/src/test/java/org/fusesource/jansi/EncodingTest.java b/src/test/java/org/fusesource/jansi/EncodingTest.java index 4cccc88f..d6befd2d 100644 --- a/src/test/java/org/fusesource/jansi/EncodingTest.java +++ b/src/test/java/org/fusesource/jansi/EncodingTest.java @@ -18,7 +18,7 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.io.UnsupportedEncodingException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.concurrent.atomic.AtomicReference; import org.fusesource.jansi.io.AnsiOutputStream; @@ -32,7 +32,7 @@ public class EncodingTest { @Test public void testEncoding8859() throws UnsupportedEncodingException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final AtomicReference newLabel = new AtomicReference(); + final AtomicReference newLabel = new AtomicReference<>(); PrintStream ansi = new AnsiPrintStream( new AnsiOutputStream( baos, @@ -46,7 +46,7 @@ protected void processChangeWindowTitle(String label) { }, AnsiType.Emulation, AnsiColors.TrueColor, - Charset.forName("ISO-8859-1"), + StandardCharsets.ISO_8859_1, null, null, false), @@ -61,7 +61,7 @@ protected void processChangeWindowTitle(String label) { @Test public void testEncodingUtf8() throws UnsupportedEncodingException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final AtomicReference newLabel = new AtomicReference(); + final AtomicReference newLabel = new AtomicReference<>(); PrintStream ansi = new PrintStream( new AnsiOutputStream( baos, @@ -75,7 +75,7 @@ protected void processChangeWindowTitle(String label) { }, AnsiType.Emulation, AnsiColors.TrueColor, - Charset.forName("UTF-8"), + StandardCharsets.UTF_8, null, null, false), diff --git a/src/test/java/org/fusesource/jansi/io/AnsiOutputStreamTest.java b/src/test/java/org/fusesource/jansi/io/AnsiOutputStreamTest.java index 49e6dae1..96353408 100644 --- a/src/test/java/org/fusesource/jansi/io/AnsiOutputStreamTest.java +++ b/src/test/java/org/fusesource/jansi/io/AnsiOutputStreamTest.java @@ -17,7 +17,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.fusesource.jansi.AnsiColors; import org.fusesource.jansi.AnsiMode; @@ -38,7 +38,7 @@ void canHandleSgrsWithMultipleOptions() throws IOException { null, AnsiType.Emulation, AnsiColors.TrueColor, - Charset.forName("UTF-8"), + StandardCharsets.UTF_8, null, null, false);