简体   繁体   中英

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 ?

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{} )

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.

Btw, the documentation does not say that the method may throw an IllegalArgumentException . Are you sure the exception is thrown from that method? I suggest you provide an SSCCE for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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