OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-dev/] [fsf-gcc-snapshot-1-mar-12/] [or1k-gcc/] [libjava/] [gnu/] [java/] [nio/] [SocketChannelImpl.java] - Diff between revs 756 and 783

Only display areas with differences | Details | Blame | View Log

Rev 756 Rev 783
/* SocketChannelImpl.java --
/* SocketChannelImpl.java --
   Copyright (C) 2002, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
   Copyright (C) 2002, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
 
 
This file is part of GNU Classpath.
This file is part of GNU Classpath.
 
 
GNU Classpath is free software; you can redistribute it and/or modify
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
the Free Software Foundation; either version 2, or (at your option)
any later version.
any later version.
 
 
GNU Classpath is distributed in the hope that it will be useful, but
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
02110-1301 USA.
 
 
Linking this library statically or dynamically with other modules is
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
conditions of the GNU General Public License cover the whole
combination.
combination.
 
 
As a special exception, the copyright holders of this library give you
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */
exception statement from your version. */
 
 
 
 
package gnu.java.nio;
package gnu.java.nio;
 
 
import gnu.java.net.PlainSocketImpl;
import gnu.java.net.PlainSocketImpl;
 
 
import java.io.IOException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.SocketTimeoutException;
import java.nio.ByteBuffer;
import java.nio.ByteBuffer;
import java.nio.channels.AlreadyConnectedException;
import java.nio.channels.AlreadyConnectedException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ConnectionPendingException;
import java.nio.channels.ConnectionPendingException;
import java.nio.channels.NoConnectionPendingException;
import java.nio.channels.NoConnectionPendingException;
import java.nio.channels.NotYetConnectedException;
import java.nio.channels.NotYetConnectedException;
import java.nio.channels.SelectionKey;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.UnresolvedAddressException;
import java.nio.channels.UnresolvedAddressException;
import java.nio.channels.UnsupportedAddressTypeException;
import java.nio.channels.UnsupportedAddressTypeException;
import java.nio.channels.spi.SelectorProvider;
import java.nio.channels.spi.SelectorProvider;
 
 
public final class SocketChannelImpl extends SocketChannel
public final class SocketChannelImpl extends SocketChannel
{
{
  private PlainSocketImpl impl;
  private PlainSocketImpl impl;
  private NIOSocket socket;
  private NIOSocket socket;
  private boolean connectionPending;
  private boolean connectionPending;
 
 
  SocketChannelImpl (SelectorProvider provider)
  SocketChannelImpl (SelectorProvider provider)
    throws IOException
    throws IOException
  {
  {
    super (provider);
    super (provider);
    impl = new PlainSocketImpl();
    impl = new PlainSocketImpl();
    impl.create(true);
    impl.create(true);
    socket = new NIOSocket (impl, this);
    socket = new NIOSocket (impl, this);
    configureBlocking(true);
    configureBlocking(true);
  }
  }
 
 
  SocketChannelImpl (SelectorProvider provider,
  SocketChannelImpl (SelectorProvider provider,
                     NIOSocket socket)
                     NIOSocket socket)
    throws IOException
    throws IOException
  {
  {
    super (provider);
    super (provider);
    this.impl = socket.getPlainSocketImpl();
    this.impl = socket.getPlainSocketImpl();
    this.socket = socket;
    this.socket = socket;
  }
  }
 
 
  public void finalizer()
  public void finalizer()
  {
  {
    if (isConnected())
    if (isConnected())
      {
      {
        try
        try
          {
          {
            close ();
            close ();
          }
          }
        catch (Exception e)
        catch (Exception e)
          {
          {
          }
          }
      }
      }
  }
  }
 
 
  PlainSocketImpl getPlainSocketImpl()
  PlainSocketImpl getPlainSocketImpl()
  {
  {
    return impl;
    return impl;
  }
  }
 
 
  int getNativeFD()
  int getNativeFD()
  {
  {
    return socket.getPlainSocketImpl().getNativeFD();
    return socket.getPlainSocketImpl().getNativeFD();
  }
  }
 
 
  protected void implCloseSelectableChannel () throws IOException
  protected void implCloseSelectableChannel () throws IOException
  {
  {
    socket.close();
    socket.close();
  }
  }
 
 
  protected void implConfigureBlocking (boolean blocking) throws IOException
  protected void implConfigureBlocking (boolean blocking) throws IOException
  {
  {
    socket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT);
    socket.setSoTimeout (blocking ? 0 : NIOConstants.DEFAULT_TIMEOUT);
  }
  }
 
 
  public boolean connect (SocketAddress remote) throws IOException
  public boolean connect (SocketAddress remote) throws IOException
  {
  {
    if (!isOpen())
    if (!isOpen())
      throw new ClosedChannelException();
      throw new ClosedChannelException();
 
 
    if (isConnected())
    if (isConnected())
      throw new AlreadyConnectedException();
      throw new AlreadyConnectedException();
 
 
    if (connectionPending)
    if (connectionPending)
      throw new ConnectionPendingException();
      throw new ConnectionPendingException();
 
 
    if (!(remote instanceof InetSocketAddress))
    if (!(remote instanceof InetSocketAddress))
      throw new UnsupportedAddressTypeException();
      throw new UnsupportedAddressTypeException();
 
 
    if (((InetSocketAddress) remote).isUnresolved())
    if (((InetSocketAddress) remote).isUnresolved())
      throw new UnresolvedAddressException();
      throw new UnresolvedAddressException();
 
 
    try
    try
      {
      {
        socket.getPlainSocketImpl().setInChannelOperation(true);
        socket.getPlainSocketImpl().setInChannelOperation(true);
          // indicate that a channel is initiating the accept operation
          // indicate that a channel is initiating the accept operation
          // so that the socket ignores the fact that we might be in
          // so that the socket ignores the fact that we might be in
          // non-blocking mode.
          // non-blocking mode.
 
 
        if (isBlocking())
        if (isBlocking())
          {
          {
            // Do blocking connect.
            // Do blocking connect.
            socket.connect (remote);
            socket.connect (remote);
            return true;
            return true;
          }
          }
 
 
        // Do non-blocking connect.
        // Do non-blocking connect.
        try
        try
          {
          {
            socket.connect (remote, NIOConstants.DEFAULT_TIMEOUT);
            socket.connect (remote, NIOConstants.DEFAULT_TIMEOUT);
            return true;
            return true;
          }
          }
        catch (SocketTimeoutException e)
        catch (SocketTimeoutException e)
          {
          {
            connectionPending = true;
            connectionPending = true;
            return false;
            return false;
          }
          }
      }
      }
    finally
    finally
      {
      {
        socket.getPlainSocketImpl().setInChannelOperation(false);
        socket.getPlainSocketImpl().setInChannelOperation(false);
      }
      }
  }
  }
 
 
  public boolean finishConnect ()
  public boolean finishConnect ()
    throws IOException
    throws IOException
  {
  {
    if (!isOpen())
    if (!isOpen())
      throw new ClosedChannelException();
      throw new ClosedChannelException();
 
 
    if (!isConnected() && !connectionPending)
    if (!isConnected() && !connectionPending)
      throw new NoConnectionPendingException();
      throw new NoConnectionPendingException();
 
 
    if (isConnected())
    if (isConnected())
      return true;
      return true;
 
 
    // FIXME: Handle blocking/non-blocking mode.
    // FIXME: Handle blocking/non-blocking mode.
 
 
    Selector selector = provider().openSelector();
    Selector selector = provider().openSelector();
    register(selector, SelectionKey.OP_CONNECT);
    register(selector, SelectionKey.OP_CONNECT);
 
 
    if (isBlocking())
    if (isBlocking())
      {
      {
        selector.select(); // blocking until channel is connected.
        selector.select(); // blocking until channel is connected.
        connectionPending = false;
        connectionPending = false;
        return true;
        return true;
      }
      }
 
 
    int ready = selector.selectNow(); // non-blocking
    int ready = selector.selectNow(); // non-blocking
    if (ready == 1)
    if (ready == 1)
      {
      {
        connectionPending = false;
        connectionPending = false;
        return true;
        return true;
      }
      }
 
 
    return false;
    return false;
  }
  }
 
 
  public boolean isConnected ()
  public boolean isConnected ()
  {
  {
    return socket.isConnected();
    return socket.isConnected();
  }
  }
 
 
  public boolean isConnectionPending ()
  public boolean isConnectionPending ()
  {
  {
    return connectionPending;
    return connectionPending;
  }
  }
 
 
  public Socket socket ()
  public Socket socket ()
  {
  {
    return socket;
    return socket;
  }
  }
 
 
  public int read(ByteBuffer dst) throws IOException
  public int read(ByteBuffer dst) throws IOException
  {
  {
    if (!isConnected())
    if (!isConnected())
      throw new NotYetConnectedException();
      throw new NotYetConnectedException();
 
 
    byte[] data;
    byte[] data;
    int offset = 0;
    int offset = 0;
    InputStream input = socket.getInputStream();
    InputStream input = socket.getInputStream();
    int available = input.available();
    int available = input.available();
    int len = dst.remaining();
    int len = dst.remaining();
 
 
    if ((! isBlocking()) && available == 0)
    if ((! isBlocking()) && available == 0)
      return 0;
      return 0;
 
 
    if (dst.hasArray())
    if (dst.hasArray())
      {
      {
        offset = dst.arrayOffset() + dst.position();
        offset = dst.arrayOffset() + dst.position();
        data = dst.array();
        data = dst.array();
      }
      }
    else
    else
      {
      {
        data = new byte [len];
        data = new byte [len];
      }
      }
 
 
    int readBytes = 0;
    int readBytes = 0;
    boolean completed = false;
    boolean completed = false;
 
 
    try
    try
      {
      {
        begin();
        begin();
        socket.getPlainSocketImpl().setInChannelOperation(true);
        socket.getPlainSocketImpl().setInChannelOperation(true);
        readBytes = input.read (data, offset, len);
        readBytes = input.read (data, offset, len);
        completed = true;
        completed = true;
      }
      }
    finally
    finally
      {
      {
        end (completed);
        end (completed);
        socket.getPlainSocketImpl().setInChannelOperation(false);
        socket.getPlainSocketImpl().setInChannelOperation(false);
      }
      }
 
 
    if (readBytes > 0)
    if (readBytes > 0)
      if (dst.hasArray())
      if (dst.hasArray())
        {
        {
          dst.position (dst.position() + readBytes);
          dst.position (dst.position() + readBytes);
        }
        }
      else
      else
        {
        {
          dst.put (data, offset, readBytes);
          dst.put (data, offset, readBytes);
        }
        }
 
 
    return readBytes;
    return readBytes;
  }
  }
 
 
  public long read (ByteBuffer[] dsts, int offset, int length)
  public long read (ByteBuffer[] dsts, int offset, int length)
    throws IOException
    throws IOException
  {
  {
    if (!isConnected())
    if (!isConnected())
      throw new NotYetConnectedException();
      throw new NotYetConnectedException();
 
 
    if ((offset < 0)
    if ((offset < 0)
        || (offset > dsts.length)
        || (offset > dsts.length)
        || (length < 0)
        || (length < 0)
        || (length > (dsts.length - offset)))
        || (length > (dsts.length - offset)))
      throw new IndexOutOfBoundsException();
      throw new IndexOutOfBoundsException();
 
 
    long readBytes = 0;
    long readBytes = 0;
 
 
    for (int index = offset; index < length; index++)
    for (int index = offset; index < length; index++)
      readBytes += read (dsts [index]);
      readBytes += read (dsts [index]);
 
 
    return readBytes;
    return readBytes;
  }
  }
 
 
  public int write (ByteBuffer src)
  public int write (ByteBuffer src)
    throws IOException
    throws IOException
  {
  {
    if (!isConnected())
    if (!isConnected())
      throw new NotYetConnectedException();
      throw new NotYetConnectedException();
 
 
    byte[] data;
    byte[] data;
    int offset = 0;
    int offset = 0;
    int len = src.remaining();
    int len = src.remaining();
 
 
    if (!src.hasArray())
    if (!src.hasArray())
      {
      {
        data = new byte [len];
        data = new byte [len];
        src.get (data, 0, len);
        src.get (data, 0, len);
      }
      }
    else
    else
      {
      {
        offset = src.arrayOffset() + src.position();
        offset = src.arrayOffset() + src.position();
        data = src.array();
        data = src.array();
      }
      }
 
 
    OutputStream output = socket.getOutputStream();
    OutputStream output = socket.getOutputStream();
    boolean completed = false;
    boolean completed = false;
 
 
    try
    try
      {
      {
        begin();
        begin();
        socket.getPlainSocketImpl().setInChannelOperation(true);
        socket.getPlainSocketImpl().setInChannelOperation(true);
        output.write (data, offset, len);
        output.write (data, offset, len);
        completed = true;
        completed = true;
      }
      }
    finally
    finally
      {
      {
        end (completed);
        end (completed);
        socket.getPlainSocketImpl().setInChannelOperation(false);
        socket.getPlainSocketImpl().setInChannelOperation(false);
      }
      }
 
 
    if (src.hasArray())
    if (src.hasArray())
      {
      {
        src.position (src.position() + len);
        src.position (src.position() + len);
      }
      }
 
 
    return len;
    return len;
  }
  }
 
 
  public long write (ByteBuffer[] srcs, int offset, int length)
  public long write (ByteBuffer[] srcs, int offset, int length)
    throws IOException
    throws IOException
  {
  {
    if (!isConnected())
    if (!isConnected())
      throw new NotYetConnectedException();
      throw new NotYetConnectedException();
 
 
    if ((offset < 0)
    if ((offset < 0)
        || (offset > srcs.length)
        || (offset > srcs.length)
        || (length < 0)
        || (length < 0)
        || (length > (srcs.length - offset)))
        || (length > (srcs.length - offset)))
      throw new IndexOutOfBoundsException();
      throw new IndexOutOfBoundsException();
 
 
    long writtenBytes = 0;
    long writtenBytes = 0;
 
 
    for (int index = offset; index < length; index++)
    for (int index = offset; index < length; index++)
      writtenBytes += write (srcs [index]);
      writtenBytes += write (srcs [index]);
 
 
    return writtenBytes;
    return writtenBytes;
  }
  }
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.