| ByteBuffer byteBuf = ByteBuffer.allocate(1024); IntBuffer intBuf = IntBuffer.allocate(1024); LongBuffer longBuf = LongBuffer.allocate(1024); |
| public static ByteBuffer wrap(byte[] array) { ... } |
| // 填充一个 byte 值 public abstract ByteBuffer put(byte b); // 在指定位置填充一个 int 值 public abstract ByteBuffer put(int index, byte b); // 将一个数组中的值填充进去 public final ByteBuffer put(byte[] src) {...} public ByteBuffer put(byte[] src, int offset, int length) {...} |
| int num = channel.read(buf); |
| public final Buffer flip() { limit = position; // 将 limit 设置为实际写入的数据数量 position = 0; // 重置 position 为 0 mark = -1; // mark 之后再说 return this; } |
| // 根据 position 来获取数据 public abstract byte get(); // 获取指定位置的数据 public abstract byte get(int index); // 将 Buffer 中的数据写入到数组中 public ByteBuffer get(byte[] dst) |
| new String(buffer.array()).trim(); |
| int num = channel.write(buf); |
| public final Buffer mark() { mark = position; return this; } |
| public final Buffer reset() { int m = mark; if (m < 0) throw new InvalidMarkException(); position = m; return this; } |
| public final Buffer rewind() { position = 0; mark = -1; return this; } |
| public final Buffer clear() { position = 0; limit = capacity; mark = -1; return this; } |
| FileInputStream inputStream = new FileInputStream(new File("/data.txt")); FileChannel fileChannel = inputStream.getChannel(); |
| ByteBuffer buffer = ByteBuffer.allocate(1024); int num = fileChannel.read(buffer); |
| ByteBuffer buffer = ByteBuffer.allocate(1024); buffer.put("随机写入一些内容到 Buffer 中".getBytes()); // Buffer 切换为读模式 buffer.flip(); while(buffer.hasRemaining()) { // 将 Buffer 中的内容写入文件 fileChannel.write(buffer); } |
| SocketChannel socketChannel = SocketChannel<br> .open(newInetSocketAddress("127.0.0.1" , 80)); |
| // 打开一个通道 SocketChannel socketChannel = SocketChannel.open(); // 发起连接 socketChannel.connect(new InetSocketAddress("127.0.0.1", 80)); |
| // 读取数据 socketChannel.read(buffer); // 写入数据到网络连接中 while(buffer.hasRemaining()) { socketChannel.write(buffer); } |
| // 实例化 ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); // 监听 8080 端口 serverSocketChannel.socket().bind(new InetSocketAddress(8080)); while (true) { // 一旦有一个 TCP 连接进来,就对应创建一个 SocketChannel 进行处理 SocketChannel socketChannel = serverSocketChannel.accept(); } |
| DatagramChannel channel = DatagramChannel.open(); channel.socket().bind(new InetSocketAddress(9090)); ByteBuffer buf = ByteBuffer.allocate(48); channel.receive(buf); |
| String newData = "New String to write to file..." + System.currentTimeMillis(); ByteBuffer buf = ByteBuffer.allocate(48); buf.put(newData.getBytes()); buf.flip(); int bytesSent = channel.send(buf, newInetSocketAddress("jenkov.com" , 80)); |
| Selector selector = Selector.open(); |
| // 将通道设置为非阻塞模式,因为默认都是阻塞模式的 channel.configureBlocking(false); // 注册 SelectionKey key = channel.register(selector, SelectionKey.OP_READ); |
| Selector selector = Selector.open(); channel.configureBlocking(false); SelectionKey key = channel.register(selector, SelectionKey.OP_READ); while(true) { // 判断是否有事件准备好 int readyChannels = selector.select(); if(readyChannels == 0) continue ; // 遍历 Set<SelectionKey> selectedKeys = selector.selectedKeys(); Iterator<SelectionKey> keyIterator = selectedKeys.iterator(); while(keyIterator.hasNext()) { SelectionKey key = keyIterator.next(); if(key.isAcceptable()) { // a connection was accepted by a ServerSocketChannel. } else if (key.isConnectable()) { // a connection was established with a remote server. } else if (key.isReadable()) { // a channel is ready for reading } else if (key.isWritable()) { // a channel is ready for writing } keyIterator.remove(); } } |
~(。≧3≦)ノ⌒☆
| 欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |