当我在Android中调试包含SocketChannel写入的代码时,我得到了IllegalArgumentException,但是同样的代码在windows中没有这个异常,在Android和windows中在SocketChannel写入上有区别吗?
更新:(代码是开源项目frostwire-android(this file in github)的一部分,这部分和vuze 4.5一样,我只是增加了一次尝试{})
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;
}发布于 2012-03-12 18:18:15
行为是由相同的合同(标准库文档)定义的,文档不会给任何特定于实现的解释留出空间,所以你的问题的答案一定是,不,Android上的行为和Windows上的行为应该没有区别。
顺便说一句,the documentation并没有说该方法可能会抛出IllegalArgumentException。你确定异常是从那个方法抛出的吗?我建议您为此提供一个SSCCE。
https://stackoverflow.com/questions/9665112
复制相似问题