0N/A/*
3261N/A * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.nio.ch;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.nio.*;
0N/Aimport java.nio.channels.*;
0N/A
0N/A
0N/A// Make a datagram-socket channel look like a datagram socket.
0N/A//
0N/A// The methods in this class are defined in exactly the same order as in
0N/A// java.net.DatagramSocket so as to simplify tracking future changes to that
0N/A// class.
0N/A//
0N/A
0N/Apublic class DatagramSocketAdaptor
0N/A extends DatagramSocket
0N/A{
0N/A
0N/A // The channel being adapted
0N/A private final DatagramChannelImpl dc;
0N/A
0N/A // Timeout "option" value for receives
0N/A private volatile int timeout = 0;
0N/A
0N/A // ## super will create a useless impl
0N/A private DatagramSocketAdaptor(DatagramChannelImpl dc) throws IOException {
0N/A // Invoke the DatagramSocketAdaptor(SocketAddress) constructor,
0N/A // passing a dummy DatagramSocketImpl object to aovid any native
0N/A // resource allocation in super class and invoking our bind method
0N/A // before the dc field is initialized.
0N/A super(dummyDatagramSocket);
0N/A this.dc = dc;
0N/A }
0N/A
0N/A public static DatagramSocket create(DatagramChannelImpl dc) {
0N/A try {
0N/A return new DatagramSocketAdaptor(dc);
0N/A } catch (IOException x) {
0N/A throw new Error(x);
0N/A }
0N/A }
0N/A
0N/A private void connectInternal(SocketAddress remote)
0N/A throws SocketException
0N/A {
0N/A InetSocketAddress isa = Net.asInetSocketAddress(remote);
0N/A int port = isa.getPort();
0N/A if (port < 0 || port > 0xFFFF)
0N/A throw new IllegalArgumentException("connect: " + port);
0N/A if (remote == null)
0N/A throw new IllegalArgumentException("connect: null address");
524N/A if (isClosed())
0N/A return;
0N/A try {
0N/A dc.connect(remote);
0N/A } catch (Exception x) {
0N/A Net.translateToSocketException(x);
0N/A }
0N/A }
0N/A
0N/A public void bind(SocketAddress local) throws SocketException {
0N/A try {
0N/A if (local == null)
0N/A local = new InetSocketAddress(0);
0N/A dc.bind(local);
0N/A } catch (Exception x) {
0N/A Net.translateToSocketException(x);
0N/A }
0N/A }
0N/A
0N/A public void connect(InetAddress address, int port) {
0N/A try {
0N/A connectInternal(new InetSocketAddress(address, port));
0N/A } catch (SocketException x) {
0N/A // Yes, j.n.DatagramSocket really does this
0N/A }
0N/A }
0N/A
0N/A public void connect(SocketAddress remote) throws SocketException {
0N/A if (remote == null)
0N/A throw new IllegalArgumentException("Address can't be null");
0N/A connectInternal(remote);
0N/A }
0N/A
0N/A public void disconnect() {
0N/A try {
0N/A dc.disconnect();
0N/A } catch (IOException x) {
0N/A throw new Error(x);
0N/A }
0N/A }
0N/A
0N/A public boolean isBound() {
524N/A return dc.localAddress() != null;
0N/A }
0N/A
0N/A public boolean isConnected() {
524N/A return dc.remoteAddress() != null;
0N/A }
0N/A
0N/A public InetAddress getInetAddress() {
0N/A return (isConnected()
0N/A ? Net.asInetSocketAddress(dc.remoteAddress()).getAddress()
0N/A : null);
0N/A }
0N/A
0N/A public int getPort() {
0N/A return (isConnected()
0N/A ? Net.asInetSocketAddress(dc.remoteAddress()).getPort()
0N/A : -1);
0N/A }
0N/A
0N/A public void send(DatagramPacket p) throws IOException {
0N/A synchronized (dc.blockingLock()) {
0N/A if (!dc.isBlocking())
0N/A throw new IllegalBlockingModeException();
0N/A try {
0N/A synchronized (p) {
0N/A ByteBuffer bb = ByteBuffer.wrap(p.getData(),
0N/A p.getOffset(),
0N/A p.getLength());
0N/A if (dc.isConnected()) {
0N/A if (p.getAddress() == null) {
0N/A // Legacy DatagramSocket will send in this case
0N/A // and set address and port of the packet
0N/A InetSocketAddress isa = (InetSocketAddress)
524N/A dc.remoteAddress();
0N/A p.setPort(isa.getPort());
0N/A p.setAddress(isa.getAddress());
0N/A dc.write(bb);
0N/A } else {
0N/A // Target address may not match connected address
0N/A dc.send(bb, p.getSocketAddress());
0N/A }
0N/A } else {
0N/A // Not connected so address must be valid or throw
0N/A dc.send(bb, p.getSocketAddress());
0N/A }
0N/A }
0N/A } catch (IOException x) {
0N/A Net.translateException(x);
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Must hold dc.blockingLock()
0N/A //
2494N/A private SocketAddress receive(ByteBuffer bb) throws IOException {
0N/A if (timeout == 0) {
2494N/A return dc.receive(bb);
0N/A }
0N/A
0N/A // Implement timeout with a selector
0N/A SelectionKey sk = null;
0N/A Selector sel = null;
0N/A dc.configureBlocking(false);
0N/A try {
0N/A int n;
2494N/A SocketAddress sender;
2494N/A if ((sender = dc.receive(bb)) != null)
2494N/A return sender;
0N/A sel = Util.getTemporarySelector(dc);
0N/A sk = dc.register(sel, SelectionKey.OP_READ);
0N/A long to = timeout;
0N/A for (;;) {
0N/A if (!dc.isOpen())
0N/A throw new ClosedChannelException();
0N/A long st = System.currentTimeMillis();
0N/A int ns = sel.select(to);
0N/A if (ns > 0 && sk.isReadable()) {
2494N/A if ((sender = dc.receive(bb)) != null)
2494N/A return sender;
0N/A }
0N/A sel.selectedKeys().remove(sk);
0N/A to -= System.currentTimeMillis() - st;
0N/A if (to <= 0)
0N/A throw new SocketTimeoutException();
0N/A
0N/A }
0N/A } finally {
0N/A if (sk != null)
0N/A sk.cancel();
0N/A if (dc.isOpen())
0N/A dc.configureBlocking(true);
0N/A if (sel != null)
0N/A Util.releaseTemporarySelector(sel);
0N/A }
0N/A }
0N/A
0N/A public void receive(DatagramPacket p) throws IOException {
0N/A synchronized (dc.blockingLock()) {
0N/A if (!dc.isBlocking())
0N/A throw new IllegalBlockingModeException();
0N/A try {
0N/A synchronized (p) {
0N/A ByteBuffer bb = ByteBuffer.wrap(p.getData(),
0N/A p.getOffset(),
0N/A p.getLength());
2494N/A SocketAddress sender = receive(bb);
2494N/A p.setSocketAddress(sender);
0N/A p.setLength(bb.position() - p.getOffset());
0N/A }
0N/A } catch (IOException x) {
0N/A Net.translateException(x);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public InetAddress getLocalAddress() {
0N/A if (isClosed())
0N/A return null;
524N/A SocketAddress local = dc.localAddress();
524N/A if (local == null)
524N/A local = new InetSocketAddress(0);
524N/A InetAddress result = ((InetSocketAddress)local).getAddress();
524N/A SecurityManager sm = System.getSecurityManager();
524N/A if (sm != null) {
524N/A try {
524N/A sm.checkConnect(result.getHostAddress(), -1);
524N/A } catch (SecurityException x) {
524N/A return new InetSocketAddress(0).getAddress();
524N/A }
0N/A }
524N/A return result;
0N/A }
0N/A
0N/A public int getLocalPort() {
0N/A if (isClosed())
0N/A return -1;
0N/A try {
524N/A SocketAddress local = dc.getLocalAddress();
524N/A if (local != null) {
524N/A return ((InetSocketAddress)local).getPort();
524N/A }
0N/A } catch (Exception x) {
0N/A }
524N/A return 0;
0N/A }
0N/A
0N/A public void setSoTimeout(int timeout) throws SocketException {
0N/A this.timeout = timeout;
0N/A }
0N/A
0N/A public int getSoTimeout() throws SocketException {
0N/A return timeout;
0N/A }
0N/A
524N/A private void setBooleanOption(SocketOption<Boolean> name, boolean value)
524N/A throws SocketException
524N/A {
524N/A try {
524N/A dc.setOption(name, value);
524N/A } catch (IOException x) {
524N/A Net.translateToSocketException(x);
524N/A }
524N/A }
524N/A
524N/A private void setIntOption(SocketOption<Integer> name, int value)
524N/A throws SocketException
524N/A {
524N/A try {
524N/A dc.setOption(name, value);
524N/A } catch (IOException x) {
524N/A Net.translateToSocketException(x);
524N/A }
524N/A }
524N/A
524N/A private boolean getBooleanOption(SocketOption<Boolean> name) throws SocketException {
524N/A try {
524N/A return dc.getOption(name).booleanValue();
524N/A } catch (IOException x) {
524N/A Net.translateToSocketException(x);
524N/A return false; // keep compiler happy
524N/A }
524N/A }
524N/A
524N/A private int getIntOption(SocketOption<Integer> name) throws SocketException {
524N/A try {
524N/A return dc.getOption(name).intValue();
524N/A } catch (IOException x) {
524N/A Net.translateToSocketException(x);
524N/A return -1; // keep compiler happy
524N/A }
0N/A }
0N/A
0N/A public void setSendBufferSize(int size) throws SocketException {
524N/A if (size <= 0)
524N/A throw new IllegalArgumentException("Invalid send size");
4216N/A setIntOption(StandardSocketOptions.SO_SNDBUF, size);
0N/A }
0N/A
0N/A public int getSendBufferSize() throws SocketException {
4216N/A return getIntOption(StandardSocketOptions.SO_SNDBUF);
0N/A }
0N/A
0N/A public void setReceiveBufferSize(int size) throws SocketException {
524N/A if (size <= 0)
524N/A throw new IllegalArgumentException("Invalid receive size");
4216N/A setIntOption(StandardSocketOptions.SO_RCVBUF, size);
0N/A }
0N/A
0N/A public int getReceiveBufferSize() throws SocketException {
4216N/A return getIntOption(StandardSocketOptions.SO_RCVBUF);
0N/A }
0N/A
0N/A public void setReuseAddress(boolean on) throws SocketException {
4216N/A setBooleanOption(StandardSocketOptions.SO_REUSEADDR, on);
0N/A }
0N/A
0N/A public boolean getReuseAddress() throws SocketException {
4216N/A return getBooleanOption(StandardSocketOptions.SO_REUSEADDR);
524N/A
0N/A }
0N/A
0N/A public void setBroadcast(boolean on) throws SocketException {
4216N/A setBooleanOption(StandardSocketOptions.SO_BROADCAST, on);
0N/A }
0N/A
0N/A public boolean getBroadcast() throws SocketException {
4216N/A return getBooleanOption(StandardSocketOptions.SO_BROADCAST);
0N/A }
0N/A
0N/A public void setTrafficClass(int tc) throws SocketException {
4216N/A setIntOption(StandardSocketOptions.IP_TOS, tc);
0N/A }
0N/A
0N/A public int getTrafficClass() throws SocketException {
4216N/A return getIntOption(StandardSocketOptions.IP_TOS);
0N/A }
0N/A
0N/A public void close() {
0N/A try {
0N/A dc.close();
0N/A } catch (IOException x) {
0N/A throw new Error(x);
0N/A }
0N/A }
0N/A
0N/A public boolean isClosed() {
0N/A return !dc.isOpen();
0N/A }
0N/A
0N/A public DatagramChannel getChannel() {
0N/A return dc;
0N/A }
0N/A
0N/A /*
0N/A * A dummy implementation of DatagramSocketImpl that can be passed to the
0N/A * DatagramSocket constructor so that no native resources are allocated in
0N/A * super class.
0N/A */
0N/A private static final DatagramSocketImpl dummyDatagramSocket
0N/A = new DatagramSocketImpl()
0N/A {
0N/A protected void create() throws SocketException {}
0N/A
0N/A protected void bind(int lport, InetAddress laddr) throws SocketException {}
0N/A
0N/A protected void send(DatagramPacket p) throws IOException {}
0N/A
0N/A protected int peek(InetAddress i) throws IOException { return 0; }
0N/A
0N/A protected int peekData(DatagramPacket p) throws IOException { return 0; }
0N/A
0N/A protected void receive(DatagramPacket p) throws IOException {}
0N/A
0N/A protected void setTTL(byte ttl) throws IOException {}
0N/A
0N/A protected byte getTTL() throws IOException { return 0; }
0N/A
0N/A protected void setTimeToLive(int ttl) throws IOException {}
0N/A
0N/A protected int getTimeToLive() throws IOException { return 0;}
0N/A
0N/A protected void join(InetAddress inetaddr) throws IOException {}
0N/A
0N/A protected void leave(InetAddress inetaddr) throws IOException {}
0N/A
0N/A protected void joinGroup(SocketAddress mcastaddr,
0N/A NetworkInterface netIf) throws IOException {}
0N/A
0N/A protected void leaveGroup(SocketAddress mcastaddr,
0N/A NetworkInterface netIf) throws IOException {}
0N/A
0N/A protected void close() {}
0N/A
0N/A public Object getOption(int optID) throws SocketException { return null;}
0N/A
0N/A public void setOption(int optID, Object value) throws SocketException {}
0N/A };
0N/A}