0

当我在Android中调试包含SocketChannel write的代码时,我得到了IllegalArgumentException,但是在windows中相同的代码没有这个异常,在SocketChannel write中Android和windows之间有区别吗?

更新:(代码是开源项目frostwire-android的一部分(这个文件在github),这部分和vuze 4.5一样,我只是添加一个try{})

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;     
}
4

1 回答 1

0

该行为由相同的合同(标准库文档)定义,并且该文档没有为任何特定于实现的解释提供空间,因此您的问题的答案必须是,不,Android 上的行为和Windows 上的行为。

顺便说一句,文档并没有说该方法可能会抛出IllegalArgumentException. 您确定该方法引发了异常吗?我建议您为此提供 SSCCE。

于 2012-03-12T10:18:15.237 回答