Skip to content

Commit

Permalink
Deprecate WindowsSupport
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Oct 12, 2023
1 parent 6911f21 commit a9dc2a3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 29 deletions.
24 changes: 9 additions & 15 deletions src/main/java/org/fusesource/jansi/WindowsSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
21 changes: 18 additions & 3 deletions src/main/java/org/fusesource/jansi/internal/Kernel32.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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);
}
}
}
12 changes: 6 additions & 6 deletions src/main/java/org/fusesource/jansi/io/WindowsAnsiProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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());
}
}

Expand All @@ -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());
}
}

Expand Down Expand Up @@ -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());
}
}

Expand All @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}

0 comments on commit a9dc2a3

Please sign in to comment.