设为首页  
联系我们  
加入收藏  
网页制作 冲浪宝典 图形图像 操作系统 软件教学 编程开发 认证考试 安全技术 站长专区 文学驿站 娱乐天地 游戏天地 办公软件
文章搜索
您的位置: 首页 >> 文章首页 >> 编程开发 >> Java >> New I/0 Functionality for JavaTM 2 Standard Edition 1.4
精品推荐
Java点击TOP10
·java笔试题
·《Thinking in Java》读书笔记
·JSP的mysql_jdbc驱动程序
·异常java.sql.SQLException: Io exception:The Network Adapter could not establish connection
·Java Coder 常用软件下载地址
·org.apache.commons.dbcp.SQLNestedException解决办法
·Java图形界面开发:SWT全接触
·如何使用Java POI生成Excel表文件 !
·功能强大的在线网页编辑器
·一些非常不错的Struts 例子下载
编程开发点击TOP10
·ASP.NET 程序中常用的三十三种代码
·利用ASP.NET构建网上考试系统
·C#版的网站新闻发布系统
·(转)23种设计模式汇集
·设计ASP.NET新闻管理系统
·深山红叶袖珍PE工具箱V16正式版
·我的.NET书架 (入门篇)
·java笔试题
·C++经典电子书下载
·.NET:是什么?将走向哪里?
精选专题

New I/0 Functionality for JavaTM 2 Standard Edition 1.4

作者: 来源:网络文章 时间:2005-12-16 22:39:39

New I/0 Functionality for JavaTM 2 Standard Edition 1.4(8) Once connected, you can read from or write to the channel with ByteBuffer objects. For instance, you can wrap a String in a CharBuffer with the help of an CharsetEncoder to send an HTTP request:

Charset charset = Charset.forName("ISO-8859-1");CharsetEncoder encoder = charset.newEncoder();String request = "GET / \n\r\n\r";channel.write(encoder.encode(CharBuffer.wrap(request)));

You can then read the response from the channel. Since the response for this HTTP request will be text, you'll need to convert that response into a CharBuffer through a CharsetDecoder. By creating just a CharBuffer to start, you can keep reusing the object to avoid unnecessary garbage collection between reads:

ByteBuffer buffer = ByteBuffer.allocateDirect(1024);CharBuffer charBuffer = CharBuffer.allocate(1024);while ((channel.read(buffer)) != -1) {buffer.flip();decoder.decode(buffer, charBuffer, false);charBuffer.flip();System.out.println(charBuffer);buffer.clear();charBuffer.clear();}

The following program connects all these pieces to read the main page of a Web site through an HTTP request. Feel free to save the output to a file to compare the results to viewing the page with a browser.

import Java.io.*;import Java.net.*;import Java.nio.*;import Java.nio.channels.*;import Java.nio.charset.*;public class ReadURL {public static void main(String args[]) { String host = args[0]; SocketChannel channel = null; try {// SetupInetSocketAddress socketAddress =new InetSocketAddress(host, 80);Charset charset =Charset.forName("ISO-8859-1");CharsetDecoder decoder =charset.newDecoder();CharsetEncoder encoder =charset.newEncoder();// Allocate buffersByteBuffer buffer =ByteBuffer.allocateDirect(1024);CharBuffer charBuffer =CharBuffer.allocate(1024);// Connectchannel = SocketChannel.open();channel.connect(socketAddress);// Send requestString request = "GET / \n\r\n\r";channel.write(encoder.encode(CharBuffer.wrap(request)));// Read responsewhile ((channel.read(buffer)) != -1) {buffer.flip();// Decode bufferdecoder.decode(buffer, charBuffer, false);// DisplaycharBuffer.flip();System.out.println(charBuffer);buffer.clear();charBuffer.clear();} } catch (UnknownHostException e) {System.err.println(e); } catch (IOException e) {System.err.println(e); } finally {if (channel != null) {try { channel.close();} catch (IOException ignored) {}} }}}

Non-Blocking Reads

1.New I/0 Functionality for JavaTM 2 Standard Edition 1.4(1)
2.New I/0 Functionality for JavaTM 2 Standard Edition 1.4(2)
3.New I/0 Functionality for JavaTM 2 Standard Edition 1.4(3)
4.New I/0 Functionality for JavaTM 2 Standard Edition 1.4(4)
5.New I/0 Functionality for JavaTM 2 Standard Edition 1.4(5)
6.New I/0 Functionality for JavaTM 2 Standard Edition 1.4(6)
7.New I/0 Functionality for JavaTM 2 Standard Edition 1.4(7)
8.New I/0 Functionality for JavaTM 2 Standard Edition 1.4(8)
9.New I/0 Functionality for JavaTM 2 Standard Edition 1.4(9)
10.New I/0 Functionality for JavaTM 2 Standard Edition 1.4(10)
11.New I/0 Functionality for JavaTM 2 Standard Edition 1.4(11)
12.New I/0 Functionality for JavaTM 2 Standard Edition 1.4(12)
13.New I/0 Functionality for JavaTM 2 Standard Edition 1.4(13)
共13页 9 7 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [138 :>

New I/0 Functionality for JavaTM 2 Standard Edition 1.4 相关文章:
New I/0 Functionality for JavaTM 2 Standard Edition 1.4 相关软件:
特别声明:本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
转载请注明来源:http://www.xgdown.com