通过BufferedReader的ready()方法来解决,其规约中表述:该方法能告诉我们该流是否准备好被读取,即流中内容不空时返回true,否则返回false。
代码如下:
// 输入流
InputStream is = socket.getInputStream();
StringWriter writer = new StringWriter();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
char[] bt = new char[1024];
do {
reader.read(bt);
} while (reader.ready());
writer.write(bt,0,bt.length);
String request = writer.toString();