简体   繁体   English

Java StringBuilder附加垂直制表符失败

[英]Java StringBuilder Appending Vertical-Tab Character Fails

I receive data from a InputStream and I have a ASCII character 11 which is a vertical tab. 我从InputStream接收数据,我有一个ASCII字符11,这是一个垂直制表符。 I can see the vertical tab 11 in the debugger. 我可以在调试器中看到垂直选项卡11。 As soon as I try to append that character to the StringBuilder it is appended and the length is increased. 当我尝试将该字符附加到StringBuilder时,它就会被附加并且长度会增加。

However, the problem is that when the String is returned the ASCII character is lost but when doing stringBuilder.toString().toCharArray() the ASCII character 11 can be seen. 但是,问题在于,当返回String时,会丢失ASCII字符,但是当执行stringBuilder.toString()。toCharArray()时,可以看到ASCII字符11。

I need to see in the String the ASCII character 11. 我需要在字符串中看到ASCII字符11。

public static void main(String[] args) {
     // Receive data from InputStream
     int read = inputStream.read();
     StringBuilder stringBuilder = new StringBuilder();
     stringBuilder.append((char) read); // /u000b is ' '
     stringBuilder.append("H");
     System.out.println(stringBuilder.toString()); // prints H
     char[] characters = stringBuilder.toString().toCharArray(); // length 2
}

How can this be achived? 如何做到这一点?

EDIT: 编辑:

I need to see the ASCII character in the original String in the debugger. 我需要在调试器的原始字符串中看到ASCII字符。 For example: 例如:

public String getOriginalString() {
    return originalString;
}

public String process(String originalString) {
     return modifiedString;
}

EDIT: 编辑:

public String buildMessage(InputStream inputStream) throws Exception {
    StringBuilder message = null;
    if(inputStream != null) {
        message = new StringBuilder();
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        int byteRead = bufferedInputStream.read();
        while(byteRead != -1) {
            char value = (char) byteRead;
            message.append(value);
            // check how many bytes available
            if(bufferedInputStream.available() != 0) {
                byteRead = bufferedInputStream.read();
            }
            else {
                // to avoid blocking of data
                break;
            }
        }
    }
    char[] characters = message.toString().toCharArray(); // returns length 2
    return message.toString();
}

public static void main(String[] args) {
    String i = buildMessage(inputStream);
    char[] characters = i.toCharArray(); // ASCII characters lost
    StringBuilder stringBuilder = new StringBuider(i);
    char[] characters2 = stringBuilder.toString().toCharArray(); // ASCII characters lost
}

A vertical tab is white-space, so you can not "see it". 垂直制表符是空格,因此您不能“看到它”。 What do you expect the visual appear of a vertical tab to be? 希望 垂直标签的视觉效果如何? The vertical tab ASCII character is a historical artefact , dating to the days to teletypes. 垂直制表符ASCII字符是一种历史文物 ,可追溯到电传打字时代。 It has has no generally accepted special meaning for VDUs and windoing systems displaying text. 对于VDU和显示文本的窗口系统,它没有普遍接受的特殊含义。

The console that shows your output simply doesn't support that character, I guess. 我猜,显示输出的控制台根本不支持该字符。 No way that it is not passed to stdout or that the resulting string doesn't contain your character. 绝对不能将其不传递到stdout或所生成的字符串不包含您的字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM