Skip to content

Commit a9dc2a3

Browse files
committedOct 12, 2023
Deprecate WindowsSupport
1 parent 6911f21 commit a9dc2a3

File tree

4 files changed

+39
-29
lines changed

4 files changed

+39
-29
lines changed
 

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

+9-15
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,21 @@
1515
*/
1616
package org.fusesource.jansi;
1717

18-
import java.io.UnsupportedEncodingException;
19-
20-
import static org.fusesource.jansi.internal.Kernel32.FORMAT_MESSAGE_FROM_SYSTEM;
21-
import static org.fusesource.jansi.internal.Kernel32.FormatMessageW;
22-
import static org.fusesource.jansi.internal.Kernel32.GetLastError;
18+
import org.fusesource.jansi.internal.Kernel32;
2319

20+
/**
21+
* @deprecated Use org.fusesource.jansi.internal.Kernel32 if needed
22+
*/
23+
@Deprecated
2424
public class WindowsSupport {
2525

26+
@Deprecated
2627
public static String getLastErrorMessage() {
27-
int errorCode = GetLastError();
28-
return getErrorMessage(errorCode);
28+
return Kernel32.getLastErrorMessage();
2929
}
3030

31+
@Deprecated
3132
public static String getErrorMessage(int errorCode) {
32-
int bufferSize = 160;
33-
byte data[] = new byte[bufferSize];
34-
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, 0, errorCode, 0, data, bufferSize, null);
35-
try {
36-
return new String(data, "UTF-16LE").trim();
37-
} catch (UnsupportedEncodingException e) {
38-
throw new IllegalStateException(e);
39-
}
33+
return Kernel32.getErrorMessage(errorCode);
4034
}
4135
}

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

+18-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
package org.fusesource.jansi.internal;
1717

1818
import java.io.IOException;
19-
20-
import org.fusesource.jansi.WindowsSupport;
19+
import java.io.UnsupportedEncodingException;
2120

2221
/**
2322
* Interface to access Win32 base APIs.
@@ -473,7 +472,7 @@ public static INPUT_RECORD[] readConsoleInputHelper(long handle, int count, bool
473472
? PeekConsoleInputW(handle, inputRecordPtr, count, length)
474473
: ReadConsoleInputW(handle, inputRecordPtr, count, length);
475474
if (res == 0) {
476-
throw new IOException("ReadConsoleInputW failed: " + WindowsSupport.getLastErrorMessage());
475+
throw new IOException("ReadConsoleInputW failed: " + getLastErrorMessage());
477476
}
478477
if (length[0] <= 0) {
479478
return new INPUT_RECORD[0];
@@ -518,4 +517,20 @@ public static INPUT_RECORD[] readConsoleKeyInput(long handle, int count, boolean
518517
}
519518
}
520519
}
520+
521+
public static String getLastErrorMessage() {
522+
int errorCode = GetLastError();
523+
return getErrorMessage(errorCode);
524+
}
525+
526+
public static String getErrorMessage(int errorCode) {
527+
int bufferSize = 160;
528+
byte data[] = new byte[bufferSize];
529+
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, 0, errorCode, 0, data, bufferSize, null);
530+
try {
531+
return new String(data, "UTF-16LE").trim();
532+
} catch (UnsupportedEncodingException e) {
533+
throw new IllegalStateException(e);
534+
}
535+
}
521536
}

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.io.IOException;
1919
import java.io.OutputStream;
2020

21-
import org.fusesource.jansi.WindowsSupport;
21+
import org.fusesource.jansi.internal.Kernel32;
2222
import org.fusesource.jansi.internal.Kernel32.CONSOLE_SCREEN_BUFFER_INFO;
2323
import org.fusesource.jansi.internal.Kernel32.COORD;
2424

@@ -115,7 +115,7 @@ public WindowsAnsiProcessor(OutputStream ps) throws IOException {
115115
private void getConsoleInfo() throws IOException {
116116
os.flush();
117117
if (GetConsoleScreenBufferInfo(console, info) == 0) {
118-
throw new IOException("Could not get the screen info: " + WindowsSupport.getLastErrorMessage());
118+
throw new IOException("Could not get the screen info: " + Kernel32.getLastErrorMessage());
119119
}
120120
if (negative) {
121121
info.attributes = invertAttributeColors(info.attributes);
@@ -129,7 +129,7 @@ private void applyAttribute() throws IOException {
129129
attributes = invertAttributeColors(attributes);
130130
}
131131
if (SetConsoleTextAttribute(console, attributes) == 0) {
132-
throw new IOException(WindowsSupport.getLastErrorMessage());
132+
throw new IOException(Kernel32.getLastErrorMessage());
133133
}
134134
}
135135

@@ -145,7 +145,7 @@ private short invertAttributeColors(short attributes) {
145145

146146
private void applyCursorPosition() throws IOException {
147147
if (SetConsoleCursorPosition(console, info.cursorPosition.copy()) == 0) {
148-
throw new IOException(WindowsSupport.getLastErrorMessage());
148+
throw new IOException(Kernel32.getLastErrorMessage());
149149
}
150150
}
151151

@@ -397,7 +397,7 @@ protected void processInsertLine(int optionInt) throws IOException {
397397
info.attributes = originalColors;
398398
info.unicodeChar = ' ';
399399
if (ScrollConsoleScreenBuffer(console, scroll, scroll, org, info) == 0) {
400-
throw new IOException(WindowsSupport.getLastErrorMessage());
400+
throw new IOException(Kernel32.getLastErrorMessage());
401401
}
402402
}
403403

@@ -413,7 +413,7 @@ protected void processDeleteLine(int optionInt) throws IOException {
413413
info.attributes = originalColors;
414414
info.unicodeChar = ' ';
415415
if (ScrollConsoleScreenBuffer(console, scroll, scroll, org, info) == 0) {
416-
throw new IOException(WindowsSupport.getLastErrorMessage());
416+
throw new IOException(Kernel32.getLastErrorMessage());
417417
}
418418
}
419419

‎src/test/java/org/fusesource/jansi/WindowsSupportTest.java ‎src/test/java/org/fusesource/jansi/internal/Kernel32Test.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.fusesource.jansi;
16+
package org.fusesource.jansi.internal;
1717

1818
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.api.condition.EnabledOnOs;
20+
import org.junit.jupiter.api.condition.OS;
1921

2022
import static org.junit.jupiter.api.Assertions.assertEquals;
21-
import static org.junit.jupiter.api.Assumptions.assumeTrue;
2223

23-
public class WindowsSupportTest {
24+
public class Kernel32Test {
2425

2526
@Test
27+
@EnabledOnOs(OS.WINDOWS)
2628
public void testErrorMessage() {
27-
assumeTrue(AnsiConsole.IS_WINDOWS);
28-
String msg = WindowsSupport.getErrorMessage(500);
29+
String msg = Kernel32.getErrorMessage(500);
2930
assertEquals(msg, "User profile cannot be loaded.");
3031
}
3132
}

0 commit comments

Comments
 (0)
Please sign in to comment.