43 |
import java.nio.ByteBuffer; |
import java.nio.ByteBuffer; |
44 |
import java.nio.channels.spi.AbstractSelectableChannel; |
import java.nio.channels.spi.AbstractSelectableChannel; |
45 |
|
|
46 |
|
/** |
47 |
|
* @since 1.4 |
48 |
|
*/ |
49 |
public abstract class DatagramChannel |
public abstract class DatagramChannel |
50 |
extends AbstractSelectableChannel |
extends AbstractSelectableChannel |
51 |
implements ByteChannel, ScatteringByteChannel, GatheringByteChannel |
implements ByteChannel, ScatteringByteChannel, GatheringByteChannel |
52 |
{ |
{ |
53 |
|
/** |
54 |
|
* Initializes the channel. |
55 |
|
*/ |
56 |
protected DatagramChannel (SelectorProvider provider) |
protected DatagramChannel (SelectorProvider provider) |
57 |
{ |
{ |
58 |
super (provider); |
super (provider); |
59 |
} |
} |
60 |
|
|
61 |
|
/** |
62 |
|
* Opens a datagram channel. |
63 |
|
*/ |
64 |
public static DatagramChannel open () throws IOException |
public static DatagramChannel open () throws IOException |
65 |
{ |
{ |
66 |
return SelectorProvider.provider ().openDatagramChannel (); |
return SelectorProvider.provider ().openDatagramChannel (); |
67 |
} |
} |
68 |
|
|
69 |
public final long read (ByteBuffer[] dsts) |
/** |
70 |
|
* Reads data from this channel. |
71 |
|
*/ |
72 |
|
public final long read (ByteBuffer[] dsts) throws IOException |
73 |
{ |
{ |
74 |
long b = 0; |
long b = 0; |
75 |
for (int i=0;i<dsts.length;i++) |
|
76 |
b += read(dsts[i]); |
for (int i = 0; i < dsts.length; i++) |
77 |
|
b += read (dsts[i]); |
78 |
|
|
79 |
return b; |
return b; |
80 |
} |
} |
81 |
|
|
82 |
|
/** |
83 |
|
* Writes data to this channel. |
84 |
|
*/ |
85 |
public final long write (ByteBuffer[] srcs) |
public final long write (ByteBuffer[] srcs) |
86 |
{ |
{ |
87 |
long b = 0; |
long b = 0; |
88 |
for (int i=0;i<srcs.length;i++) |
|
89 |
b += write(srcs[i]); |
for (int i = 0;i < srcs.length; i++) |
90 |
|
b += write (srcs[i]); |
91 |
|
|
92 |
return b; |
return b; |
93 |
} |
} |
94 |
|
|
95 |
|
/** |
96 |
|
* Connects this channel's socket. |
97 |
|
*/ |
98 |
public abstract DatagramChannel connect (SocketAddress remote); |
public abstract DatagramChannel connect (SocketAddress remote); |
99 |
|
|
100 |
|
/** |
101 |
|
* Disonnects this channel's socket. |
102 |
|
*/ |
103 |
public abstract DatagramChannel disconnect (); |
public abstract DatagramChannel disconnect (); |
104 |
|
|
105 |
|
/** |
106 |
|
* Tells whether or not this channel's socket is connected. |
107 |
|
*/ |
108 |
public abstract boolean isConnected (); |
public abstract boolean isConnected (); |
109 |
|
|
110 |
|
/** |
111 |
|
* Reads data from this channel. |
112 |
|
*/ |
113 |
public abstract int read (ByteBuffer dst); |
public abstract int read (ByteBuffer dst); |
114 |
|
|
115 |
|
/** |
116 |
|
* Reads data from this channel. |
117 |
|
*/ |
118 |
public abstract long read (ByteBuffer[] dsts, int offset, int length); |
public abstract long read (ByteBuffer[] dsts, int offset, int length); |
119 |
|
|
120 |
|
/** |
121 |
|
* Receives a datagram via this channel. |
122 |
|
*/ |
123 |
public abstract SocketAddress receive (ByteBuffer dst); |
public abstract SocketAddress receive (ByteBuffer dst); |
124 |
|
|
125 |
|
/** |
126 |
|
* Sends a datagram via this channel. |
127 |
|
*/ |
128 |
public abstract int send (ByteBuffer src, SocketAddress target); |
public abstract int send (ByteBuffer src, SocketAddress target); |
129 |
|
|
130 |
|
/** |
131 |
|
* Retrieves the channel's socket. |
132 |
|
*/ |
133 |
public abstract DatagramSocket socket (); |
public abstract DatagramSocket socket (); |
134 |
|
|
135 |
|
/** |
136 |
|
* Writes data to this channel. |
137 |
|
*/ |
138 |
public abstract int write (ByteBuffer src); |
public abstract int write (ByteBuffer src); |
139 |
|
|
140 |
|
/** |
141 |
|
* Writes data to this channel. |
142 |
|
*/ |
143 |
public abstract long write (ByteBuffer[] srcs, int offset, int length); |
public abstract long write (ByteBuffer[] srcs, int offset, int length); |
144 |
|
|
145 |
public final int validOps() |
/** |
146 |
|
* Retrieves the valid operations for this channel. |
147 |
|
*/ |
148 |
|
public final int validOps () |
149 |
{ |
{ |
150 |
return SelectionKey.OP_READ | SelectionKey.OP_WRITE; |
return SelectionKey.OP_READ | SelectionKey.OP_WRITE; |
151 |
} |
} |
|
|
|
152 |
} |
} |