Android中的Java SocketChannel write(ByteBuffer源代码)与windows中的不同吗?
当我在Android中调试包含SocketCannel write的代码时,我得到了IllegalArgumentException,但windows中的相同代码没有此例外,Android和windows在SocketCannel write中有区别吗 更新: (代码是开源项目frostwire-android()的一部分,这部分与vuze 4.5相同,我只是添加了一个try{})Android中的Java SocketChannel write(ByteBuffer源代码)与windows中的不同吗?,java,android,socketchannel,Java,Android,Socketchannel,当我在Android中调试包含SocketCannel write的代码时,我得到了IllegalArgumentException,但windows中的相同代码没有此例外,Android和windows在SocketCannel write中有区别吗 更新: (代码是开源项目frostwire-android()的一部分,这部分与vuze 4.5相同,我只是添加了一个try{}) private int channelWrite(ByteBuffer buf)引发IOException { i
private int channelWrite(ByteBuffer buf)引发IOException
{
int-writed=0;
while(remainingBytesToScatter>0&&buf.remaining()>0)
{
int currentwrited=0;
试一试{
currentWrite=channel.write((ByteBuffer)(buf.slice().limit(Math.min)(50+rnd.nextInt(100),buf.remaining()));
}捕获(例外e){
if(IOException的实例){
Log.d(“,”chanel write IOException“+e.getMessage());
}else if(例如IOException的实例){
Log.d(“,”chanel write AsynchronousCloseException“+e.getMessage());
}else if(例如ClosedByInterruptException的实例){
Log.d(“,”chanel write ClosedByInterruptException“+e.getMessage());
}else if(关闭通道异常的实例){
Log.d(“,“chanel write ClosedChannel Exception”+e.getMessage());
}else if(例如NotYetConnectedException的实例){
Log.d(“,“chanel write ClosedChannel Exception”+e.getMessage());
}否则{
//第二次,到达这里
Log.d(“,“chanel write unknown”+e.getMessage());
}
}
如果(CurrentWrite==0)
打破
基本位置(基本位置()+当前写入);
remainingBytesToScatter-=CurrentWrite;
如果(剩余的测试模式为0)
写入+=通道写入(buf);
书面回报;
}
该行为由同一合同(标准库文档)定义,并且该文档没有为任何特定于实现的解释留出空间,因此您的问题的答案必须是:不,Android上的行为和Windows上的行为之间不应有区别
顺便说一句,并不是说该方法可能会抛出一个IllegalArgumentException
。您确定该异常是从该方法引发的吗?我建议您为此提供一个SSCCE
private int channelWrite(ByteBuffer buf) throws IOException
{
int written = 0;
while(remainingBytesToScatter > 0 && buf.remaining() > 0)
{
int currentWritten = 0;
try{
currentWritten = channel.write((ByteBuffer)(buf.slice().limit(Math.min(50+rnd.nextInt(100),buf.remaining()))));
}catch( Exception e ) {
if(e instanceof IOException) {
Log.d("", "chanel write IOException " + e.getMessage());
}else if(e instanceof IOException) {
Log.d("", "chanel write AsynchronousCloseException " + e.getMessage());
}else if(e instanceof ClosedByInterruptException) {
Log.d("", "chanel write ClosedByInterruptException " + e.getMessage());
}else if(e instanceof ClosedChannelException) {
Log.d("", "chanel write ClosedChannelException " + e.getMessage());
}else if(e instanceof NotYetConnectedException) {
Log.d("", "chanel write ClosedChannelException " + e.getMessage());
}else {
// while in second time, reach here
Log.d("", "chanel write unknown " + e.getMessage());
}
}
if(currentWritten == 0)
break;
buf.position(buf.position()+currentWritten);
remainingBytesToScatter -= currentWritten;
if(remainingBytesToScatter <= 0)
{
remainingBytesToScatter = 0;
try
{
channel.socket().setTcpNoDelay(false);
} catch (SocketException e)
{
Debug.printStackTrace(e);
}
}
written += currentWritten;
}
if(buf.remaining() > 0)
written += channel.write(buf);
return written;
}