简体   繁体   English

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

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

When i debug code contain SocketChannel write in Android, i got IllegalArgumentException, but the same code in windows no this exception, is there difference between Android and windows in SocketChannel write ? 当我在Android中调试代码包含SocketChannel写时,我得到了IllegalArgumentException,但是Windows中的相同代码没有此异常,Android和SocketChannel写中的Windows之间是否有区别?

UPDATE : (the code is part of open source project frostwire-android( this file in github ), and this part is the same as vuze 4.5, i just add a try{} ) 更新:(代码是开源项目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;     
}

The behavior is defined by the same contract (the standard library documentation) and that documentation does not give room for any implementation specific interpretations, so the answer to your question must be, no, there should be no difference between the behavior on Android and the behavior on Windows. 行为是由同一合同(标准库文档)定义的,并且该文档不能为任何实现特定的解释留出空间,因此,对您的问题的答案必须是,不,Android上的行为与Windows上的行为。

Btw, the documentation does not say that the method may throw an IllegalArgumentException . 顺便说一句, 文档没有说该方法可能抛出IllegalArgumentException Are you sure the exception is thrown from that method? 您确定该方法引发了异常吗? I suggest you provide an SSCCE for this. 我建议您为此提供SSCCE。

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

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