Java:1.7 操作系统:Linux(但我不知道它是哪个Linux)
我有一个CharBuffer来包含我从socket的BufferedReader中读取的东西。 Socket建立了肯定,当我试图转储它时,它给了我一个 ERROR_FORMAT 结果。
之后,我从套接字读取数据但导致 java.net.SocketException:连接重置
连接重置问题是我修复的问题,但 CharBuffer.toString()如何提供 ERROR_FORMAT 结果?
以下是我的代码,有什么不对吗?
Socket connectionSocket=xxxxxx; //Connected socket given by other library
connectionSocket.setSoTimeout(75*1000);
CharBuffer charBuffer=CharBuffer.allocate(1024);
BufferedReader bufferedReader=null;
bufferedReader=new BufferedReader(new InputStreamReader(connectionSocket.getInputStream(), "BIG5"));
while((bufferedReader.read(charBuffer))!=-1) // in 2nd time, this line throw a java.net.SocketException: Connection reset
{
charBuffer.flip();
respStr.append(charBuffer);
log.info("CharBuffer: "+charBuffer.toString()); // this line runs just 1 tine, print result: "CharBuffer: 2016102618353211301 : ERROR_FORMAT"
charBuffer.clear();
}
log.info("CharBuffer all: "+charBuffer.toString()); // dodn't been execute
答案 0 :(得分:0)
我无法复制您的问题
public static void main(String[] args) throws IOException {
StringBuilder respStr = new StringBuilder();
CharBuffer charBuffer=CharBuffer.allocate(1024);
BufferedReader bufferedReader=null;
Logger log = Logger.getAnonymousLogger();
// create a byte stream with some big 5 characters in it
// to simulate what a socket should produce
byte[] bytes = new byte[100];
int index = 0;
for(int i = 0xc940;i<0xc940+50;i++){
bytes[index++] = (byte)((i & 0x0ff00)>>8);
bytes[index++] = (byte)((i & 0x0ff));
}
InputStream byteStream = new ByteArrayInputStream(bytes);
// code to process the byte stream
bufferedReader=new BufferedReader(new InputStreamReader(byteStream, "BIG5"));
while((bufferedReader.read(charBuffer))!=-1){
charBuffer.flip();
respStr.append(charBuffer);
log.info("CharBuffer: "+charBuffer.toString());
charBuffer.clear();
}
log.info("CharBuffer all: "+charBuffer.toString());
}
2016年10月27日下午6:27:18 nz.test.buffers.CharBufferTest main
INFO:CharBuffer:&lt;中文字符&gt;
2016年10月27日下午6:27:18 nz.test.buffers.CharBufferTest main
INFO:CharBuffer all:&lt;汉字&gt;