繁体   English   中英

Windows中Android的Java SocketChannel写(ByteBuffer源)不同吗?

[英]is Java SocketChannel write(ByteBuffer source) in Android different in windows?

当我在Android中调试代码包含SocketChannel写时,我得到了IllegalArgumentException,但是Windows中的相同代码没有此异常,Android和SocketChannel写中的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;     
}

行为是由同一合同(标准库文档)定义的,并且该文档不能为任何实现特定的解释留出空间,因此,对您的问题的答案必须是,不,Android上的行为与Windows上的行为。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM