37 |
|
|
38 |
package java.nio.channels; |
package java.nio.channels; |
39 |
|
|
40 |
import java.io.*; |
import java.nio.channels.spi.AbstractSelectableChannel; |
41 |
import java.net.*; |
import java.nio.channels.spi.SelectorProvider; |
42 |
import java.nio.*; |
import java.nio.ByteBuffer; |
43 |
import java.nio.channels.spi.*; |
import java.io.IOException; |
44 |
|
import java.net.Socket; |
45 |
|
import java.net.SocketAddress; |
46 |
|
|
47 |
|
/** |
48 |
|
* @author Michael Koch |
49 |
|
* @since 1.4 |
50 |
|
*/ |
51 |
abstract public class SocketChannel extends AbstractSelectableChannel |
abstract public class SocketChannel extends AbstractSelectableChannel |
52 |
{ |
{ |
53 |
static SelectorProvider sys_provider; |
/** |
54 |
|
* Initializes this socket. |
55 |
protected SocketChannel(SelectorProvider provider) |
*/ |
56 |
{ |
protected SocketChannel (SelectorProvider provider) |
57 |
super (provider); |
{ |
58 |
} |
super (provider); |
59 |
|
} |
60 |
|
|
61 |
public static SocketChannel open() throws IOException |
/** |
62 |
{ |
* Opens a socket channel. |
63 |
return SelectorProvider.provider().openSocketChannel(); |
*/ |
64 |
} |
public static SocketChannel open () throws IOException |
65 |
|
{ |
66 |
public static SocketChannel open(SocketAddress remote) throws IOException |
return SelectorProvider.provider ().openSocketChannel (); |
67 |
{ |
} |
68 |
SocketChannel ch = open(); |
|
69 |
|
/** |
70 |
|
* Opens a channel and connects it to a remote address. |
71 |
|
*/ |
72 |
|
public static SocketChannel open (SocketAddress remote) throws IOException |
73 |
|
{ |
74 |
|
SocketChannel ch = open (); |
75 |
|
|
76 |
if (ch.connect(remote)) |
if (ch.connect (remote)) |
77 |
{ |
{ |
78 |
} |
} |
79 |
return ch; |
|
80 |
} |
return ch; |
81 |
|
} |
82 |
|
|
83 |
public long read(ByteBuffer[] dsts) |
/** |
84 |
{ |
* Reads data from the channel. |
85 |
long b = 0; |
*/ |
86 |
for (int i=0;i<dsts.length;i++) |
public final long read (ByteBuffer[] dsts) |
87 |
{ |
{ |
88 |
b+=read(dsts[i]); |
long b = 0; |
89 |
} |
|
90 |
return b; |
for (int i = 0; i < dsts.length; i++) |
91 |
} |
{ |
92 |
|
b += read (dsts [i]); |
93 |
public long write(ByteBuffer[] dsts) |
} |
94 |
{ |
|
95 |
long b = 0; |
return b; |
96 |
for (int i=0;i<dsts.length;i++) |
} |
97 |
{ |
|
98 |
b+=write(dsts[i]); |
/** |
99 |
} |
* Writes data to the channel. |
100 |
return b; |
*/ |
101 |
} |
public final long write (ByteBuffer[] dsts) |
102 |
|
{ |
103 |
public int validOps() |
long b = 0; |
104 |
{ |
|
105 |
return SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE; |
for (int i= 0; i < dsts.length; i++) |
106 |
} |
{ |
107 |
|
b += write (dsts [i]); |
108 |
|
} |
109 |
public abstract int read(ByteBuffer dst); |
|
110 |
public abstract boolean connect(SocketAddress remote) throws IOException; |
return b; |
111 |
public abstract boolean finishConnect(); |
} |
112 |
public abstract boolean isConnected(); |
|
113 |
public abstract boolean isConnectionPending(); |
/** |
114 |
public abstract long read(ByteBuffer[] dsts, int offset, int length); |
* Retrieves the valid operations for this channel. |
115 |
public abstract Socket socket(); |
*/ |
116 |
public abstract int write(ByteBuffer src); |
public final int validOps () |
117 |
public abstract long write(ByteBuffer[] srcs, int offset, int length); |
{ |
118 |
|
return SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE; |
119 |
|
} |
120 |
|
|
121 |
|
/** |
122 |
|
* Reads data from the channel. |
123 |
|
*/ |
124 |
|
public abstract int read (ByteBuffer dst); |
125 |
|
|
126 |
|
/** |
127 |
|
* Connects the channel's socket to the remote address. |
128 |
|
*/ |
129 |
|
public abstract boolean connect (SocketAddress remote) throws IOException; |
130 |
|
|
131 |
|
/** |
132 |
|
* Finishes the process of connecting a socket channel. |
133 |
|
*/ |
134 |
|
public abstract boolean finishConnect (); |
135 |
|
|
136 |
|
/** |
137 |
|
* Tells whether or not the channel's socket is connected. |
138 |
|
*/ |
139 |
|
public abstract boolean isConnected (); |
140 |
|
|
141 |
|
/** |
142 |
|
* Tells whether or not a connection operation is in progress on this channel. |
143 |
|
*/ |
144 |
|
public abstract boolean isConnectionPending (); |
145 |
|
|
146 |
|
/** |
147 |
|
* Reads data from the channel. |
148 |
|
*/ |
149 |
|
public abstract long read (ByteBuffer[] dsts, int offset, int length); |
150 |
|
|
151 |
|
/** |
152 |
|
* Retrieves the channel's socket. |
153 |
|
*/ |
154 |
|
public abstract Socket socket (); |
155 |
|
|
156 |
|
/** |
157 |
|
* Writes data to the channel. |
158 |
|
*/ |
159 |
|
public abstract int write (ByteBuffer src); |
160 |
|
|
161 |
|
/** |
162 |
|
* Writes data to the channel. |
163 |
|
*/ |
164 |
|
public abstract long write (ByteBuffer[] srcs, int offset, int length); |
165 |
} |
} |