harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sergey Dmitriev (JIRA)" <j...@apache.org>
Subject [jira] Created: (HARMONY-3475) java.nio.SocketChannel's OutputStream cannot write a sibgle byte
Date Thu, 22 Mar 2007 15:30:32 GMT
java.nio.SocketChannel's OutputStream cannot write a sibgle byte
----------------------------------------------------------------

                 Key: HARMONY-3475
                 URL: https://issues.apache.org/jira/browse/HARMONY-3475
             Project: Harmony
          Issue Type: Bug
          Components: Classlib
            Reporter: Sergey Dmitriev


java.nio.SocketChannelImpl$SocketChannelOutputStream.write(int) does not actually write a
byte.

        public void write(int oneByte) throws IOException {
            if (!channel.isBlocking()) {
                throw new IllegalBlockingModeException();
            }
            ByteBuffer buffer = ByteBuffer.allocate(1);
            buffer.put((byte) (oneByte & 0xFF));
            channel.write(buffer);
        }

As you can see the buffer created via allocate & put hence the buffer has no remaining
data as expected (remaining() == 0). So there is actually nothing to write.

As one of bug fix variants we can make a replace:

-            buffer.put((byte) (oneByte & 0xFF));
----
+            buffer.put(0, (byte) (oneByte & 0xFF));


Here is a simple demo with respect to this:

--- server.java ---
import java.net.*;
import java.nio.*;
import java.nio.channels.*;

public class server {
    public static void main(String args[]) throws Exception {
        int port = 9999;
        InetSocketAddress addr = new InetSocketAddress(port);
        ServerSocketChannel sch = ServerSocketChannel.open();
        sch.socket().bind(addr);

        System.out.println("listening on " + port);
        SocketChannel ch = sch.accept();
        System.out.println("accepted");
        try {
            while (true) {
                ByteBuffer buf = ByteBuffer.allocate(100);
                int res = ch.read(buf);
                if (res == -1) {
                    System.out.println("received eof");
                    break;
                }
                System.out.println("received " + res + " byte(s)");
            }
        } finally {
            ch.close();
        }

    }
}


--- client.java ---
import java.net.*;
import java.io.*;
import java.nio.channels.*;

public class client {
    public static void main(String args[]) throws Exception {
        int port = 9999;
        InetSocketAddress addr = new InetSocketAddress("localhost", port);
        SocketChannel ch = SocketChannel.open(addr);
        Socket socket = ch.socket();
        OutputStream os = socket.getOutputStream();
        os.write(1);
        System.out.println("sent 1 byte to " +port);
        ch.close();
        System.out.println("sent eof to " +port);
    }
}

--- console1 ---
] ~ ~/jdk150/bin/java server
listening on 9999
accepted
received eof

--- console2 ---
] ~ ~/harmony/bin/java client
sent 1 byte to 9999
sent eof to 9999

As you can see from the server's log 1 byte is not received. You can run SUN JDK on client
side and compare the outputs.

PS As for the summary "cannot write a sibgle byte" - of course this stream CAN write. :)


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message