diff --git a/src/main/java/org/fusesource/jansi/WindowsSupport.java b/src/main/java/org/fusesource/jansi/WindowsSupport.java index 010f527e..ac031395 100644 --- a/src/main/java/org/fusesource/jansi/WindowsSupport.java +++ b/src/main/java/org/fusesource/jansi/WindowsSupport.java @@ -15,27 +15,21 @@ */ package org.fusesource.jansi; -import java.io.UnsupportedEncodingException; - -import static org.fusesource.jansi.internal.Kernel32.FORMAT_MESSAGE_FROM_SYSTEM; -import static org.fusesource.jansi.internal.Kernel32.FormatMessageW; -import static org.fusesource.jansi.internal.Kernel32.GetLastError; +import org.fusesource.jansi.internal.Kernel32; +/** + * @deprecated Use org.fusesource.jansi.internal.Kernel32 if needed + */ +@Deprecated public class WindowsSupport { + @Deprecated public static String getLastErrorMessage() { - int errorCode = GetLastError(); - return getErrorMessage(errorCode); + return Kernel32.getLastErrorMessage(); } + @Deprecated public static String getErrorMessage(int errorCode) { - int bufferSize = 160; - byte data[] = new byte[bufferSize]; - FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, 0, errorCode, 0, data, bufferSize, null); - try { - return new String(data, "UTF-16LE").trim(); - } catch (UnsupportedEncodingException e) { - throw new IllegalStateException(e); - } + return Kernel32.getErrorMessage(errorCode); } } diff --git a/src/main/java/org/fusesource/jansi/internal/Kernel32.java b/src/main/java/org/fusesource/jansi/internal/Kernel32.java index 1d45de1f..d4884caf 100644 --- a/src/main/java/org/fusesource/jansi/internal/Kernel32.java +++ b/src/main/java/org/fusesource/jansi/internal/Kernel32.java @@ -16,8 +16,7 @@ package org.fusesource.jansi.internal; import java.io.IOException; - -import org.fusesource.jansi.WindowsSupport; +import java.io.UnsupportedEncodingException; /** * Interface to access Win32 base APIs. @@ -473,7 +472,7 @@ public static INPUT_RECORD[] readConsoleInputHelper(long handle, int count, bool ? PeekConsoleInputW(handle, inputRecordPtr, count, length) : ReadConsoleInputW(handle, inputRecordPtr, count, length); if (res == 0) { - throw new IOException("ReadConsoleInputW failed: " + WindowsSupport.getLastErrorMessage()); + throw new IOException("ReadConsoleInputW failed: " + getLastErrorMessage()); } if (length[0] <= 0) { return new INPUT_RECORD[0]; @@ -518,4 +517,20 @@ public static INPUT_RECORD[] readConsoleKeyInput(long handle, int count, boolean } } } + + public static String getLastErrorMessage() { + int errorCode = GetLastError(); + return getErrorMessage(errorCode); + } + + public static String getErrorMessage(int errorCode) { + int bufferSize = 160; + byte data[] = new byte[bufferSize]; + FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, 0, errorCode, 0, data, bufferSize, null); + try { + return new String(data, "UTF-16LE").trim(); + } catch (UnsupportedEncodingException e) { + throw new IllegalStateException(e); + } + } } diff --git a/src/main/java/org/fusesource/jansi/io/WindowsAnsiProcessor.java b/src/main/java/org/fusesource/jansi/io/WindowsAnsiProcessor.java index 74a178a5..8aac49aa 100644 --- a/src/main/java/org/fusesource/jansi/io/WindowsAnsiProcessor.java +++ b/src/main/java/org/fusesource/jansi/io/WindowsAnsiProcessor.java @@ -18,7 +18,7 @@ import java.io.IOException; import java.io.OutputStream; -import org.fusesource.jansi.WindowsSupport; +import org.fusesource.jansi.internal.Kernel32; import org.fusesource.jansi.internal.Kernel32.CONSOLE_SCREEN_BUFFER_INFO; import org.fusesource.jansi.internal.Kernel32.COORD; @@ -115,7 +115,7 @@ public WindowsAnsiProcessor(OutputStream ps) throws IOException { private void getConsoleInfo() throws IOException { os.flush(); if (GetConsoleScreenBufferInfo(console, info) == 0) { - throw new IOException("Could not get the screen info: " + WindowsSupport.getLastErrorMessage()); + throw new IOException("Could not get the screen info: " + Kernel32.getLastErrorMessage()); } if (negative) { info.attributes = invertAttributeColors(info.attributes); @@ -129,7 +129,7 @@ private void applyAttribute() throws IOException { attributes = invertAttributeColors(attributes); } if (SetConsoleTextAttribute(console, attributes) == 0) { - throw new IOException(WindowsSupport.getLastErrorMessage()); + throw new IOException(Kernel32.getLastErrorMessage()); } } @@ -145,7 +145,7 @@ private short invertAttributeColors(short attributes) { private void applyCursorPosition() throws IOException { if (SetConsoleCursorPosition(console, info.cursorPosition.copy()) == 0) { - throw new IOException(WindowsSupport.getLastErrorMessage()); + throw new IOException(Kernel32.getLastErrorMessage()); } } @@ -397,7 +397,7 @@ protected void processInsertLine(int optionInt) throws IOException { info.attributes = originalColors; info.unicodeChar = ' '; if (ScrollConsoleScreenBuffer(console, scroll, scroll, org, info) == 0) { - throw new IOException(WindowsSupport.getLastErrorMessage()); + throw new IOException(Kernel32.getLastErrorMessage()); } } @@ -413,7 +413,7 @@ protected void processDeleteLine(int optionInt) throws IOException { info.attributes = originalColors; info.unicodeChar = ' '; if (ScrollConsoleScreenBuffer(console, scroll, scroll, org, info) == 0) { - throw new IOException(WindowsSupport.getLastErrorMessage()); + throw new IOException(Kernel32.getLastErrorMessage()); } } diff --git a/src/test/java/org/fusesource/jansi/WindowsSupportTest.java b/src/test/java/org/fusesource/jansi/internal/Kernel32Test.java similarity index 77% rename from src/test/java/org/fusesource/jansi/WindowsSupportTest.java rename to src/test/java/org/fusesource/jansi/internal/Kernel32Test.java index f0ccba9b..36df03dc 100644 --- a/src/test/java/org/fusesource/jansi/WindowsSupportTest.java +++ b/src/test/java/org/fusesource/jansi/internal/Kernel32Test.java @@ -13,19 +13,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.fusesource.jansi; +package org.fusesource.jansi.internal; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.condition.OS; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assumptions.assumeTrue; -public class WindowsSupportTest { +public class Kernel32Test { @Test + @EnabledOnOs(OS.WINDOWS) public void testErrorMessage() { - assumeTrue(AnsiConsole.IS_WINDOWS); - String msg = WindowsSupport.getErrorMessage(500); + String msg = Kernel32.getErrorMessage(500); assertEquals(msg, "User profile cannot be loaded."); } }