Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-English will be garbled #133

Open
lnkForKing opened this issue Nov 28, 2020 · 1 comment
Open

Non-English will be garbled #133

lnkForKing opened this issue Nov 28, 2020 · 1 comment

Comments

@lnkForKing
Copy link

When executing the command, the encoding has been hard-coded. Use 436 and when receiving the returned result, the string uses the system encoding (my default system encoding is UTF-8). If the result of using the command to execute the script contains non-English, it will return garbled characters.
I hope the author can optimize this point and can specify the encoding instead.

WinRmClient.java

`

public ShellCommand createShell() {
    //  ...
    // The encoding value here is best changed to be settable
    optCodepage.setName("WINRS_CODEPAGE");
    optCodepage.setValue("437");
    optSetCreate.getOption().add(optCodepage);

    ResourceCreated resourceCreated = winrm.create(shell, RESOURCE_URI, MAX_ENVELOPER_SIZE, operationTimeout, locale, optSetCreate);
    String shellId = getShellId(resourceCreated);

    return new ShellCommand(winrm, shellId, operationTimeout, retryReceiveAfterOperationTimeout, locale);
}

`

ShellCommand.java
`

private void getStreams(ReceiveResponse receiveResponse, Writer out, Writer err) {
    List<StreamType> streams = receiveResponse.getStream();
    for (StreamType s : streams) {
        byte[] value = s.getValue();
        if (value == null) continue;
        if (out != null && "stdout".equals(s.getName())) {
            try {
                //TODO use passed locale?
                if (value.length > 0) {
                    // Here it is best to use GBK encoding by default: new String(value, "GBK"), or you can set to use the specified encoding
                    out.write(new String(value));
                    out.flush();
                }
                if (Boolean.TRUE.equals(s.isEnd())) {
                    out.close();
                }
            } catch (IOException e) {
                throw new IllegalStateException(e);
            }
        }
        if (err != null && "stderr".equals(s.getName())) {
            try {
                //TODO use passed locale?
                if (value.length > 0) {
                    // Same here as above
                    err.write(new String(value));
                    err.flush();
                }
                if (Boolean.TRUE.equals(s.isEnd())) {
                    err.close();
                }
            } catch (IOException e) {
                throw new IllegalStateException(e);
            }
        }
    }
}

`

@jcabrerizo
Copy link
Contributor

Hi @lnkForKing this sounds like a good improvement.
We are planning to make a new release later this year with Java 11 compatibility. I'll this to the backlog for that moment.
Fell free to open a PR with your code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants