You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by "Paulex Yang (JIRA)" <ji...@apache.org> on 2007/03/23 09:24:32 UTC

[jira] Assigned: (HARMONY-3475) java.nio.SocketChannel's OutputStream cannot write a single byte

     [ https://issues.apache.org/jira/browse/HARMONY-3475?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Paulex Yang reassigned HARMONY-3475:
------------------------------------

    Assignee: Paulex Yang

> java.nio.SocketChannel's OutputStream cannot write a single byte
> ----------------------------------------------------------------
>
>                 Key: HARMONY-3475
>                 URL: https://issues.apache.org/jira/browse/HARMONY-3475
>             Project: Harmony
>          Issue Type: Bug
>          Components: Classlib
>            Reporter: Sergey Dmitriev
>         Assigned To: Paulex Yang
>         Attachments: 3475.patch
>
>
> org.apache.harmony.nio.internal.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 single 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.