BufferedReader直接到byte []

时间:2013-02-27 08:14:53

标签: java sockets bytearray inputstream bufferedreader

我的后续BufferedReader是否有可能将输入直接放入byte []?

public static Runnable reader() throws IOException {
    Log.e("Communication", "reader");
    din = new DataInputStream(sock.getInputStream());
    brdr = new BufferedReader(new InputStreamReader(din), 300);
    boolean done = false;
    while (!done) {
       try {
       char[] buffer = new char[200];
           int length = brdr.read(buffer, 0, 200);
           String message = new String(buffer, 0, length);
           btrar = message.getBytes("ISO-8859-1");                      
           int i=0;
           for (int counter = 0; counter < message.length(); counter++) {
              i++;  
              System.out.println(btrar[counter] + " = " + " btrar "  + i);
           }
    ...

那是读者的一部分,请看看。

我想直接输入到btrar,

1 个答案:

答案 0 :(得分:22)

  

我的后续BufferedReader是否有可能将输入直接放入byte []?

任何Reader都旨在让您阅读字符,而不是字节。要读取二进制数据,只需使用InputStream - 使用BufferedInputStream缓冲它,如果需要。

目前还不清楚你要做什么,但你可以使用类似的东西:

BufferedInputStream input = new BufferedInputStream(sock.getInputStream());
while (!done) {
    // TODO: Rename btrar to something more meaningful
    int bytesRead = input.read(btrar);
    // Do something with the data...
}