Skip to content

Commit 6911f21

Browse files
Glavognodet
authored andcommittedOct 12, 2023
Modernize code (#267)
1 parent 937ce1c commit 6911f21

File tree

10 files changed

+44
-88
lines changed

10 files changed

+44
-88
lines changed
 

‎src/main/java/org/fusesource/jansi/Ansi.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,14 @@ public int value() {
149149
}
150150
}
151151

152+
@FunctionalInterface
152153
public interface Consumer {
153154
void apply(Ansi ansi);
154155
}
155156

156157
public static final String DISABLE = Ansi.class.getName() + ".disable";
157158

158-
private static Callable<Boolean> detector = new Callable<Boolean>() {
159-
public Boolean call() throws Exception {
160-
return !Boolean.getBoolean(DISABLE);
161-
}
162-
};
159+
private static Callable<Boolean> detector = () -> !Boolean.getBoolean(DISABLE);
163160

164161
public static void setDetector(final Callable<Boolean> detector) {
165162
if (detector == null) throw new IllegalArgumentException();
@@ -374,7 +371,7 @@ public Ansi reset() {
374371
}
375372

376373
private final StringBuilder builder;
377-
private final ArrayList<Integer> attributeOptions = new ArrayList<Integer>(5);
374+
private final ArrayList<Integer> attributeOptions = new ArrayList<>(5);
378375

379376
public Ansi() {
380377
this(new StringBuilder(80));

‎src/main/java/org/fusesource/jansi/AnsiConsole.java

+7-14
Original file line numberDiff line numberDiff line change
@@ -294,19 +294,13 @@ public int getTerminalWidth() {
294294
SetConsoleMode(console, mode[0]); // set it back for now, but we know it works
295295
processor = null;
296296
type = AnsiType.VirtualTerminal;
297-
installer = new AnsiOutputStream.IoRunnable() {
298-
@Override
299-
public void run() throws IOException {
300-
virtualProcessing++;
301-
SetConsoleMode(console, mode[0] | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
302-
}
297+
installer = () -> {
298+
virtualProcessing++;
299+
SetConsoleMode(console, mode[0] | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
303300
};
304-
uninstaller = new AnsiOutputStream.IoRunnable() {
305-
@Override
306-
public void run() throws IOException {
307-
if (--virtualProcessing == 0) {
308-
SetConsoleMode(console, mode[0]);
309-
}
301+
uninstaller = () -> {
302+
if (--virtualProcessing == 0) {
303+
SetConsoleMode(console, mode[0]);
310304
}
311305
};
312306
width = kernel32Width;
@@ -460,8 +454,7 @@ static boolean getBoolean(String name) {
460454
try {
461455
String val = System.getProperty(name);
462456
result = val.isEmpty() || Boolean.parseBoolean(val);
463-
} catch (IllegalArgumentException e) {
464-
} catch (NullPointerException e) {
457+
} catch (IllegalArgumentException | NullPointerException ignored) {
465458
}
466459
return result;
467460
}

‎src/main/java/org/fusesource/jansi/AnsiMain.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.fusesource.jansi.internal.Kernel32;
3232
import org.fusesource.jansi.internal.MingwSupport;
3333

34+
import static java.nio.charset.StandardCharsets.UTF_8;
3435
import static org.fusesource.jansi.Ansi.ansi;
3536
import static org.fusesource.jansi.internal.Kernel32.GetConsoleScreenBufferInfo;
3637

@@ -309,7 +310,7 @@ private static String getPomPropertiesVersion(String path) throws IOException {
309310

310311
private static void printJansiLogoDemo() throws IOException {
311312
BufferedReader in =
312-
new BufferedReader(new InputStreamReader(AnsiMain.class.getResourceAsStream("jansi.txt"), "UTF-8"));
313+
new BufferedReader(new InputStreamReader(AnsiMain.class.getResourceAsStream("jansi.txt"), UTF_8));
313314
try {
314315
String l;
315316
while ((l = in.readLine()) != null) {

‎src/main/java/org/fusesource/jansi/internal/JansiLoader.java

+4-25
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737
import java.io.FilenameFilter;
3838
import java.io.IOException;
3939
import java.io.InputStream;
40-
import java.io.OutputStream;
4140
import java.net.URL;
41+
import java.nio.file.Files;
42+
import java.nio.file.StandardCopyOption;
4243
import java.util.Arrays;
4344
import java.util.LinkedList;
4445
import java.util.List;
@@ -198,9 +199,7 @@ private static boolean extractAndLoadLibraryFile(
198199
if (!extractedLckFile.exists()) {
199200
new FileOutputStream(extractedLckFile).close();
200201
}
201-
try (OutputStream out = new FileOutputStream(extractedLibFile)) {
202-
copy(in, out);
203-
}
202+
Files.copy(in, extractedLibFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
204203
} finally {
205204
// Delete the extracted lib file on JVM exit.
206205
extractedLibFile.deleteOnExit();
@@ -239,14 +238,6 @@ private static String randomUUID() {
239238
return Long.toHexString(new Random().nextLong());
240239
}
241240

242-
private static void copy(InputStream in, OutputStream out) throws IOException {
243-
byte[] buf = new byte[8192];
244-
int n;
245-
while ((n = in.read(buf)) > 0) {
246-
out.write(buf, 0, n);
247-
}
248-
}
249-
250241
/**
251242
* Loads native library using the given path and name of the library.
252243
*
@@ -358,7 +349,7 @@ private static void loadJansiNativeLibrary() throws Exception {
358349

359350
throw new Exception(String.format(
360351
"No native library found for os.name=%s, os.arch=%s, paths=[%s]",
361-
OSInfo.getOSName(), OSInfo.getArchName(), join(triedPaths, File.pathSeparator)));
352+
OSInfo.getOSName(), OSInfo.getArchName(), String.join(File.pathSeparator, triedPaths)));
362353
}
363354

364355
private static boolean hasResource(String path) {
@@ -401,16 +392,4 @@ public static String getVersion() {
401392
}
402393
return version;
403394
}
404-
405-
private static String join(List<String> list, String separator) {
406-
StringBuilder sb = new StringBuilder();
407-
boolean first = true;
408-
for (String item : list) {
409-
if (first) first = false;
410-
else sb.append(separator);
411-
412-
sb.append(item);
413-
}
414-
return sb.toString();
415-
}
416395
}

‎src/main/java/org/fusesource/jansi/internal/OSInfo.java

+9-11
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import java.io.ByteArrayOutputStream;
3535
import java.io.IOException;
3636
import java.io.InputStream;
37+
import java.nio.file.Files;
38+
import java.nio.file.Paths;
3739
import java.util.HashMap;
3840
import java.util.Locale;
3941

@@ -122,19 +124,15 @@ public static boolean isAndroid() {
122124

123125
public static boolean isAlpine() {
124126
try {
125-
Process p = Runtime.getRuntime().exec("cat /etc/os-release | grep ^ID");
126-
p.waitFor();
127-
128-
InputStream in = p.getInputStream();
129-
try {
130-
return readFully(in).toLowerCase().contains("alpine");
131-
} finally {
132-
in.close();
127+
for (String line : Files.readAllLines(Paths.get("/etc/os-release"))) {
128+
if (line.startsWith("ID") && line.toLowerCase(Locale.ROOT).contains("alpine")) {
129+
return true;
130+
}
133131
}
134-
135-
} catch (Throwable e) {
136-
return false;
132+
} catch (Throwable ignored) {
137133
}
134+
135+
return false;
138136
}
139137

140138
static String getHardwareName() {

‎src/main/java/org/fusesource/jansi/io/AnsiOutputStream.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.fusesource.jansi.AnsiMode;
2626
import org.fusesource.jansi.AnsiType;
2727

28+
import static java.nio.charset.StandardCharsets.US_ASCII;
29+
2830
/**
2931
* A ANSI print stream extracts ANSI escape codes written to
3032
* an output stream and calls corresponding <code>AnsiProcessor.process*</code> methods.
@@ -38,12 +40,14 @@
3840
*/
3941
public class AnsiOutputStream extends FilterOutputStream {
4042

41-
public static final byte[] RESET_CODE = "\033[0m".getBytes();
43+
public static final byte[] RESET_CODE = "\033[0m".getBytes(US_ASCII);
4244

45+
@FunctionalInterface
4346
public interface IoRunnable {
4447
void run() throws IOException;
4548
}
4649

50+
@FunctionalInterface
4751
public interface WidthSupplier {
4852
int getTerminalWidth();
4953
}
@@ -79,7 +83,7 @@ public int getTerminalWidth() {
7983
private final byte[] buffer = new byte[MAX_ESCAPE_SEQUENCE_LENGTH];
8084
private int pos = 0;
8185
private int startOfValue;
82-
private final ArrayList<Object> options = new ArrayList<Object>();
86+
private final ArrayList<Object> options = new ArrayList<>();
8387
private int state = LOOKING_FOR_FIRST_ESC_CHAR;
8488
private final Charset cs;
8589

‎src/main/java/org/fusesource/jansi/io/FastBufferedOutputStream.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
public class FastBufferedOutputStream extends FilterOutputStream {
2626

27-
protected final byte buf[] = new byte[8192];
27+
protected final byte[] buf = new byte[8192];
2828
protected int count;
2929

3030
public FastBufferedOutputStream(OutputStream out) {

‎src/test/java/org/fusesource/jansi/AnsiTest.java

+5-21
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import org.junit.jupiter.params.provider.CsvSource;
3030

3131
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertFalse;
33+
import static org.junit.jupiter.api.Assertions.assertTrue;
3234

3335
/**
3436
* Tests for the {@link Ansi} class.
@@ -38,20 +40,10 @@ public class AnsiTest {
3840
@Test
3941
public void testSetEnabled() throws Exception {
4042
Ansi.setEnabled(false);
41-
new Thread() {
42-
@Override
43-
public void run() {
44-
assertEquals(false, Ansi.isEnabled());
45-
}
46-
}.run();
43+
new Thread(() -> assertFalse(Ansi.isEnabled())).run();
4744

4845
Ansi.setEnabled(true);
49-
new Thread() {
50-
@Override
51-
public void run() {
52-
assertEquals(true, Ansi.isEnabled());
53-
}
54-
}.run();
46+
new Thread(() -> assertTrue(Ansi.isEnabled())).run();
5547
}
5648

5749
@Test
@@ -64,15 +56,7 @@ public void testClone() throws CloneNotSupportedException {
6456

6557
@Test
6658
public void testApply() {
67-
assertEquals(
68-
"test",
69-
Ansi.ansi()
70-
.apply(new Ansi.Consumer() {
71-
public void apply(Ansi ansi) {
72-
ansi.a("test");
73-
}
74-
})
75-
.toString());
59+
assertEquals("test", Ansi.ansi().apply(ansi -> ansi.a("test")).toString());
7660
}
7761

7862
@ParameterizedTest

‎src/test/java/org/fusesource/jansi/EncodingTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.io.ByteArrayOutputStream;
1919
import java.io.PrintStream;
2020
import java.io.UnsupportedEncodingException;
21-
import java.nio.charset.Charset;
21+
import java.nio.charset.StandardCharsets;
2222
import java.util.concurrent.atomic.AtomicReference;
2323

2424
import org.fusesource.jansi.io.AnsiOutputStream;
@@ -32,7 +32,7 @@ public class EncodingTest {
3232
@Test
3333
public void testEncoding8859() throws UnsupportedEncodingException {
3434
ByteArrayOutputStream baos = new ByteArrayOutputStream();
35-
final AtomicReference<String> newLabel = new AtomicReference<String>();
35+
final AtomicReference<String> newLabel = new AtomicReference<>();
3636
PrintStream ansi = new AnsiPrintStream(
3737
new AnsiOutputStream(
3838
baos,
@@ -46,7 +46,7 @@ protected void processChangeWindowTitle(String label) {
4646
},
4747
AnsiType.Emulation,
4848
AnsiColors.TrueColor,
49-
Charset.forName("ISO-8859-1"),
49+
StandardCharsets.ISO_8859_1,
5050
null,
5151
null,
5252
false),
@@ -61,7 +61,7 @@ protected void processChangeWindowTitle(String label) {
6161
@Test
6262
public void testEncodingUtf8() throws UnsupportedEncodingException {
6363
ByteArrayOutputStream baos = new ByteArrayOutputStream();
64-
final AtomicReference<String> newLabel = new AtomicReference<String>();
64+
final AtomicReference<String> newLabel = new AtomicReference<>();
6565
PrintStream ansi = new PrintStream(
6666
new AnsiOutputStream(
6767
baos,
@@ -75,7 +75,7 @@ protected void processChangeWindowTitle(String label) {
7575
},
7676
AnsiType.Emulation,
7777
AnsiColors.TrueColor,
78-
Charset.forName("UTF-8"),
78+
StandardCharsets.UTF_8,
7979
null,
8080
null,
8181
false),

‎src/test/java/org/fusesource/jansi/io/AnsiOutputStreamTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import java.io.ByteArrayOutputStream;
1919
import java.io.IOException;
20-
import java.nio.charset.Charset;
20+
import java.nio.charset.StandardCharsets;
2121

2222
import org.fusesource.jansi.AnsiColors;
2323
import org.fusesource.jansi.AnsiMode;
@@ -38,7 +38,7 @@ void canHandleSgrsWithMultipleOptions() throws IOException {
3838
null,
3939
AnsiType.Emulation,
4040
AnsiColors.TrueColor,
41-
Charset.forName("UTF-8"),
41+
StandardCharsets.UTF_8,
4242
null,
4343
null,
4444
false);

0 commit comments

Comments
 (0)
Please sign in to comment.