char
添加参数的正确方法charQueue
是final ConcurrentLinkedQueue<Character>
什么?
甲骨文似乎说它应该工作:
从 char 类型到 Character 类型
http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.7
代码:
package telnet;
import static java.lang.System.out;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Logger;
public class InputStreamWorker {
private final static Logger LOG = Logger.getLogger(InputStreamWorker.class.getName());
public InputStreamWorker() {
}
public void print(final InputStream inputStream, final ConcurrentLinkedQueue<Character> charQueue) {
Thread print = new Thread() {
StringBuilder sb = new StringBuilder();
@Override
public void run() {
try {
char ch = (char) inputStream.read();
sb.append(ch);
while (255 > ch && ch >= 0) {
charQueue.add(ch);
ch = (char) inputStream.read();
System.out.print(ch);
}
} catch (IOException ex) {
out.println("cannot read inputStream:\t" + ex);
}
}
};
print.start();
}
}
从构建结果中提取:
-do-compile:
[mkdir] Created dir: /home/thufir/NetBeansProjects/TelnetConsole/build/empty
[mkdir] Created dir: /home/thufir/NetBeansProjects/TelnetConsole/build/generated-sources/ap-source-output
[javac] Compiling 11 source files to /home/thufir/NetBeansProjects/TelnetConsole/build/classes
[javac] /home/thufir/NetBeansProjects/TelnetConsole/src/telnet/InputStreamWorker.java:28: error: no suitable method found for add(char)
[javac] charQueue.add(ch);
[javac] ^
[javac] method ConcurrentLinkedQueue.add(Character) is not applicable
[javac] (actual argument char cannot be converted to Character by method invocation conversion)
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 1 error