首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android中的Java SocketChannel write(ByteBuffer源代码)与windows中的不同吗?

Android中的Java SocketChannel write(ByteBuffer源代码)与windows中的不同吗?
EN

Stack Overflow用户
提问于 2012-03-12 18:09:19
回答 1查看 574关注 0票数 0

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

更新:(代码是开源项目frostwire-android(this file in github)的一部分,这部分和vuze 4.5一样,我只是增加了一次尝试{})

代码语言:javascript
复制
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;     
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-12 18:18:15

行为是由相同的合同(标准库文档)定义的,文档不会给任何特定于实现的解释留出空间,所以你的问题的答案一定是,不,Android上的行为和Windows上的行为应该没有区别。

顺便说一句,the documentation并没有说该方法可能会抛出IllegalArgumentException。你确定异常是从那个方法抛出的吗?我建议您为此提供一个SSCCE。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9665112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档