???????????????
?????????Java?д??????I/O????????????????????????????????????????????????????


????
???

??????????server???????????????????????????????????????????????????????????????ЩCPU?????????????????????????????????I/O????????????Ч??????CPU??
??????. ??I/O
????1?? Buffer
?????????I/O?????????????????????String??????I/O??????Buffer??д????????????????Buffer??????????????????????????????????????????????????????
????java.nio.Buffer ????д????
????java.nio.ByteBuffer ???????????? ?????ReadableByteChannel?ж??? WritableByteChannel??д
????java.nio.MappedByteBuffer ?????????????????????????????
????java.nio.CharBuffer ????????????????д?????
????java.nio.DoubleBuffer ????double?????????д?????
????java.nio.FloatBuffer ????float????
????java.nio.IntBuffer ????int????
????java.nio.LongBuffer ????long????
????java.nio.ShortBuffer ????short????
??????????????allocate(int capacity)????????allocateDirect(int capacity)???????????Buffer????????????????MappedBytesBuffer???????FileChannel.map(int mode??long position??int size)??????direct??buffer??????з????????????鰱??????????????д??????????(nondirect)buffer??????Java?е????????????д???????????????÷????????????ò???????wrap????????ByteBuffer.wrap(byte[])????Java????????????buffer??
????2?? ???????
??????ByteBuffer?д???????漰??????????????????????????ByteBuffer??????ByteOrder???????????????????????д????????????????ByteBuffer???????????дString??
????Java.nio.charset.Charset?????????????????????????CharsetEncoder??CharsetDecoder?????????????????????????
????3?? ???(Channel)
???????????????е?java.io???????????????дBuffer?????????NIO??????Channel??????дBuffer????????????????????????????????豸?????????????????????????????????????????


????
???

???????ReadableByteChannel??WritableByteChannel????????д??
????GatheringByteChannel??????????ν????Buffer?е?????д????????????ScatteringByteChannel???????ν???????????????Buffer?С???????????????????????????I/O????????
???????????????????I/O???????Channel????????????????Stream??Reader
????4?? Selector
?????????????I/O?У?????????????????????stream?ж???д????????????????stream?????????????????÷?????????????????Щ???????????????????????????NIO???У????Selector????????????SelectableChannel????????????????????????????????????????????????????????????Selector??????????selection?????????????????????????в??????????????????????????selector?????????????????????


????
???

???????????????????????????????SelectionKey?????????????????λ??????????Ρ??????????????SelectableChannel.register(Selector sel??int op)????????????????????????????????????????????С???????SelectionKey????????SelectionKey??readyOps()????????????????????????λ????SelectableChannel??validOps????????????????????????????????????????????IllegalArgumentException?????±??г???SelectableChannel???????????????
????ServerSocketChannel OP_ACCEPT
????SocketChannel OP_CONNECT?? OP_READ?? OP_WRITE
????DatagramChannel OP_READ?? OP_WRITE
????Pipe.SourceChannel OP_READ
????Pipe.SinkChannel OP_WRITE
??????. ???????
????1?? ?????????????
?????????????easy????SocketChannelReader???SocketChannel??????????????HTML?????
package examples.nio;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.net.InetSocketAddress;
import java.io.IOException;
public class SocketChannelReader{
private Charset charset=Charset.forName("UTF-8");//????UTF-8?????
private SocketChannel channel;
public void getHTMLContent(){
try{
connect();
sendRequest();
readResponse();
}catch(IOException e){
System.err.println(e.toString());
}finally{
if(channel!=null){
try{
channel.close();
}catch(IOException e){}
}
}
}
private void connect()throws IOException{//?????CSDN
InetSocketAddress socketAddress=
new InetSocketAddress("http://www.csdn.net"??80/);
channel=SocketChannel.open(socketAddress);
//??ù???????open???????channel???????????????????
//????SocketChannel.open().connect(socketAddress);????
}
private void sendRequest()throws IOException{
channel.write(charset.encode("GET "
+"/document"
+" "));//????GET????CSDN?????????
//???channel.write???????????CharByte?????????????
//Charset.encode(String)??????????????
}
private void readResponse()throws IOException{//??????
ByteBuffer buffer=ByteBuffer.allocate(1024);//????1024???????
while(channel.read(buffer)!=-1){
buffer.flip();//flip????????????????????????á?
System.out.println(charset.decode(buffer));
//???Charset.decode??????????????????
buffer.clear();//??????
}
}
public static void main(String [] args){
new SocketChannelReader().getHTMLContent();
}