// Path: src/main/java/org/fusesource/jansi/WindowsSupport.java
// public class WindowsSupport {
// 
//     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);
//         }
//     }
// 
// }


import java.io.IOException;
import org.fusesource.jansi.WindowsSupport;
    /**
     * see: http://msdn.microsoft.com/en-us/library/ms683207(v=VS.85).aspx
     */
    public static native int GetNumberOfConsoleInputEvents(
            long handle,
            int[] numberOfEvents);

    /**
     * see: http://msdn.microsoft.com/en-us/library/ms683147(v=VS.85).aspx
     */
    public static native int FlushConsoleInputBuffer(
            long handle);

    /**
     * Return console input events.
     */
    public static INPUT_RECORD[] readConsoleInputHelper(
            long handle, int count, boolean peek) throws IOException {
        int[] length = new int[1];
        int res;
        long inputRecordPtr = 0;
        try {
            inputRecordPtr = malloc(INPUT_RECORD.SIZEOF * count);
            if (inputRecordPtr == 0) {
                throw new IOException("cannot allocate memory with JNI");
            }
            res = peek ?
                    PeekConsoleInputW(handle, inputRecordPtr, count, length)
                    : ReadConsoleInputW(handle, inputRecordPtr, count, length);
            if (res == 0) {
// Path: src/main/java/org/fusesource/jansi/WindowsSupport.java
// public class WindowsSupport {
// 
//     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);
//         }
//     }
// 
// }


// Path: src/main/java/org/fusesource/jansi/internal/Kernel32.java
import java.io.IOException;
import org.fusesource.jansi.WindowsSupport;

    /**
     * see: http://msdn.microsoft.com/en-us/library/ms683207(v=VS.85).aspx
     */
    public static native int GetNumberOfConsoleInputEvents(
            long handle,
            int[] numberOfEvents);

    /**
     * see: http://msdn.microsoft.com/en-us/library/ms683147(v=VS.85).aspx
     */
    public static native int FlushConsoleInputBuffer(
            long handle);

    /**
     * Return console input events.
     */
    public static INPUT_RECORD[] readConsoleInputHelper(
            long handle, int count, boolean peek) throws IOException {
        int[] length = new int[1];
        int res;
        long inputRecordPtr = 0;
        try {
            inputRecordPtr = malloc(INPUT_RECORD.SIZEOF * count);
            if (inputRecordPtr == 0) {
                throw new IOException("cannot allocate memory with JNI");
            }
            res = peek ?
                    PeekConsoleInputW(handle, inputRecordPtr, count, length)
                    : ReadConsoleInputW(handle, inputRecordPtr, count, length);
            if (res == 0) {
                throw new IOException("ReadConsoleInputW failed: " + WindowsSupport.getLastErrorMessage());