在非堵塞的模式下,服务器发过来一个比较大的数据,在read的时候,有时数据还没结束,read返回了0,有时数据已经结束了read还是返回0 ,这是怎么回事啊,代码如下:
package com.service;import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;import com.data.ServiceDataInfo;
import com.data.publicInfo;import android.os.Handler;
import android.util.Log;
public class ServiceLisThread extends Thread { static final int SOCKET_WAIT = 0, STREAM_SOCKET_CONNECT = 1,
STREAM_SOCKET_SEND = 2, STREAM_SOCKET_RECEIVE = 3;
static int SIZE = 10; static String mhost = null;
static int mPort = 0;
static int mhandle = 0;
static int current_state = SOCKET_WAIT;
static ByteBuffer send_byte = null;
static int send_buflen = 0;
public static Handler childHandler; public void SetConnect(int handle) {
current_state = STREAM_SOCKET_CONNECT;
mhandle = handle;

}
SocketChannel sChannel = null;
public void run() {
try {
//long start = System.currentTimeMillis(); // 打开Socket通道
sChannel= SocketChannel.open();

Selector selector = null ;
ByteBuffer recvbuffer = ByteBuffer.allocateDirect(10*1024);
byte[] buff = new byte[10*1024]; 
InetSocketAddress ip = new InetSocketAddress(
publicInfo.IP,publicInfo.PORT);
Log.v("ydLog","TcpSocket run");
if(selector == null)
{
selector  = Selector.open();
}
// 注册连接服务端socket动作
                            sChannel.configureBlocking(false);
sChannel.connect(ip);
//要设置连接超时   
//sChannel.socket().connect(ip,5 * 1000);   
//设置读超时   
//sChannel.socket().setSoTimeout(5 * 100);   //sChannel.socket().setSoTimeout(5 * 1000);
sChannel.register(selector, SelectionKey.OP_CONNECT);
Log.v("ydLog",  "准备连接"); // 连接
//int total = 0; _FOR: for (;;) {
// 
selector.select();
Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
 
while (iter.hasNext()) 
{
SelectionKey key = (SelectionKey) iter.next();
Log.v("ydLog",  "已连接"); // 连接
iter.remove();

if (key.isConnectable()) 
{

SocketChannel channel = (SocketChannel) key.channel();

if (channel.isConnectionPending()) 
{
channel.finishConnect();
} channel.register(selector, SelectionKey.OP_READ);
}
else if(key.isWritable())
{
SocketChannel channel = (SocketChannel) key.channel();
if(send_byte != null)
{

int len   = channel.write(send_byte);

send_byte.clear();
}
channel.register(selector, SelectionKey.OP_READ);
}
else if (key.isReadable()) 
{
SocketChannel channel = (SocketChannel) key.channel();
recvbuffer.clear();
StringBuffer TEMPVALUE = new StringBuffer(10*1024);
int count = 0;

for (;;)
{
recvbuffer.clear();

count = channel.read(recvbuffer);

recvbuffer.limit( 10*1024);
recvbuffer.position( 0 );
if (count > 0) 
{ //recvbuffer.flip();

recvbuffer.get(buff, 0, count); 
String tempBuf = new String(buff, 0, count, "UTF-8");
                          TEMPVALUE.append(tempBuf); 
             Log.v("ydLog", "TEMPVALUE : " + TEMPVALUE);
             Log.v("ydLog", "TEMPVALUE : " + TEMPVALUE.length());
             key.interestOps(SelectionKey.OP_READ);
                        
}

else if(count == -1) 
{
File path = new File(publicInfo.sdcardUrl + "aa");
File f= new File(publicInfo.sdcardUrl + "aa" + "bb.txt");
if(!path.exists())
{
path.mkdirs();
}
if(!f.exists())
{
f.createNewFile();
}
else
{

f.delete();
f.createNewFile();
}
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream(f));
osw.write(TEMPVALUE.toString());
osw.close();

ServiceDataInfo dataInfo = new ServiceDataInfo();
            // dataInfo.resolveServiceData( TEMPVALUE );
//sChannel.close();
key.interestOps(SelectionKey.OP_READ);
             selector.selectedKeys().remove(key);
break;//; _FOR;

}
}

}
Thread.sleep(300);
}
}
}
catch(SocketTimeoutException e )
{
Log.e("ydLog", "connection timed out");
//run();
sChannel.notify();

}
catch(SocketException e)
{
Log.e("ydLog", e.toString());
//run();
}
catch (Exception e) {

e.printStackTrace();
//run();
}
}
}