NIO
import org.junit.Test;
import java.io.*;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.*;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Date;
import java.util.Iterator;
import java.util.Scanner;
public class NIOTest {
@Test
public void test1() {
ByteBuffer buf = ByteBuffer.allocate(1024);
ByteBuffer buff = ByteBuffer.allocateDirect(1024);
System.out.println(buf.isDirect());
System.out.println(buff.isDirect());
System.out.println("-----------allocate()------------");
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
String str = "abcde";
buf.put(str.getBytes());
System.out.println("-----------put()------------");
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
buf.flip();
System.out.println("-----------flip()------------");
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
byte[] dest = new byte[buf.limit()];
buf.get(dest);
System.out.println(new String(dest, 0, dest.length));
System.out.println("-----------get()------------");
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
buf.rewind();
System.out.println("-----------rewind()------------");
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
buf.get(dest, 0, 2);
System.out.println("-----------mark()/reset()------------");
System.out.println(new String(dest, 0, 2));
System.out.println(buf.position());
buf.mark();
buf.get(dest, 2, 2);
System.out.println("-----------mark()------------");
System.out.println(new String(dest, 2, 2));
System.out.println(buf.position());
buf.reset();
System.out.println("-----------reset()------------");
System.out.println(buf.position());
if (buf.hasRemaining()) {
System.out.println(buf.remaining());
}
buf.clear();
System.out.println("-----------clear()------------");
System.out.println(buf.position());
System.out.println(buf.limit());
System.out.println(buf.capacity());
System.out.println((char) buf.get());
}
@Test
public void test2() {
FileInputStream fis = null;
FileOutputStream fos = null;
FileChannel in = null;
FileChannel out = null;
try {
fis = new FileInputStream("F:\\Lean\\Text\\src\\main\\resources\\1.jpg");
fos = new FileOutputStream("F:\\Lean\\Text\\src\\main\\resources\\2.jpg");
in = fis.getChannel();
out = fos.getChannel();
ByteBuffer buf = ByteBuffer.allocate(1024);
while (in.read(buf) != -1) {
buf.flip();
out.write(buf);
buf.clear();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test3() {
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
inChannel = FileChannel.open(Paths.get("F:\\Lean\\Text\\src\\main\\resources\\1.jpg"), StandardOpenOption.READ);
outChannel = FileChannel.open(Paths.get("F:\\Lean\\Text\\src\\main\\resources\\3.jpg"), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
MappedByteBuffer inMap = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
MappedByteBuffer outMap = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());
byte[] dest = new byte[inMap.limit()];
inMap.get(dest);
outMap.put(dest);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inChannel != null) {
inChannel.close();
}
if (outChannel != null) {
outChannel.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void test4() throws IOException {
FileChannel in = FileChannel.open(Paths.get("F:\\Lean\\Text\\src\\main\\resources\\1.jpg"), StandardOpenOption.READ);
FileChannel out = FileChannel.open(Paths.get("F:\\Lean\\Text\\src\\main\\resources\\4.jpg"), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
in.transferTo(0, in.size(), out);
in.close();
out.close();
}
@Test
public void test5() throws IOException {
RandomAccessFile raf = new RandomAccessFile("F:\\Lean\\Text\\src\\main\\resources\\IDEA快捷键.txt", "rw");
FileChannel channel = raf.getChannel();
ByteBuffer buf1 = ByteBuffer.allocate(100);
ByteBuffer buf2 = ByteBuffer.allocate(1024);
ByteBuffer[] buf = {buf1, buf2};
channel.read(buf);
for (ByteBuffer byteBuffer : buf) {
byteBuffer.flip();
}
System.out.println(new String(buf[0].array(), 0, buf[0].limit()));
System.out.println("------------------------------");
System.out.println(new String(buf[1].array(), 0, buf[1].limit()));
RandomAccessFile rdaf = new RandomAccessFile("F:\\Lean\\Text\\src\\main\\resources\\IDEA快捷键1.txt", "rw");
FileChannel channel1 = rdaf.getChannel();
channel1.write(buf);
}
@Test
public void test6() throws IOException {
SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("localhost", 9898));
FileChannel infileChannel = FileChannel.open(Paths.get("F:\\Lean\\Text\\src\\main\\resources\\1.jpg"), StandardOpenOption.READ);
ByteBuffer buf = ByteBuffer.allocate(1024);
while ((infileChannel.read(buf) != -1)) {
buf.flip();
socketChannel.write(buf);
buf.clear();
}
socketChannel.shutdownOutput();
int len = 0;
while ((len = socketChannel.read(buf)) != -1) {
buf.flip();
System.out.println(new String(buf.array(), 0, len));
buf.clear();
}
infileChannel.close();
socketChannel.close();
}
@Test
public void test7() throws IOException {
ServerSocketChannel ssc = ServerSocketChannel.open();
FileChannel out = FileChannel.open(Paths.get("F:\\Lean\\Text\\src\\main\\resources\\5.jpg"), StandardOpenOption.WRITE, StandardOpenOption.CREATE);
ssc.bind(new InetSocketAddress(9898));
SocketChannel socketChannel = ssc.accept();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
while ((socketChannel.read(byteBuffer)) != -1) {
byteBuffer.flip();
out.write(byteBuffer);
byteBuffer.clear();
}
byteBuffer.put("服务器接收数据成功".getBytes());
byteBuffer.flip();
socketChannel.write(byteBuffer);
socketChannel.close();
out.close();
ssc.close();
}
@Test
public void test8() throws IOException {
SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("localhost", 9898));
socketChannel.configureBlocking(false);
ByteBuffer buf = ByteBuffer.allocate(1024);
FileChannel infileChannel = FileChannel.open(Paths.get("F:\\Lean\\Text\\src\\main\\resources\\IDEA快捷键.txt"), StandardOpenOption.READ);
while ((infileChannel.read(buf) != -1)) {
buf.flip();
socketChannel.write(buf);
buf.clear();
}
infileChannel.close();
socketChannel.close();
}
@Test
public void test9() throws IOException {
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
ssc.bind(new InetSocketAddress(9898));
Selector selector = Selector.open();
ssc.register(selector, SelectionKey.OP_ACCEPT);
while (selector.select() > 0) {
Iterator<SelectionKey> keyIterator = selector.selectedKeys().iterator();
while (keyIterator.hasNext()) {
SelectionKey key = keyIterator.next();
if (key.isAcceptable()) {
SocketChannel accept = ssc.accept();
accept.configureBlocking(false);
accept.register(selector, SelectionKey.OP_READ);
} else if (key.isReadable()) {
SocketChannel channel = (SocketChannel) key.channel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
int len = 0;
while ((len = channel.read(buffer)) > 0){
buffer.flip();
System.out.println(new String(buffer.array(),0,len));
buffer.clear();
}
}
keyIterator.remove();
}
}
ssc.close();
}
@Test
public void test10() throws IOException {
DatagramChannel dc = DatagramChannel.open();
dc.configureBlocking(false);
ByteBuffer buf =ByteBuffer.allocate(1024);
Scanner scan = new Scanner(System.in);
while (scan.hasNext()){
String str = scan.next();
buf.put((new Date().toString() + "\n" +str).getBytes());
buf.flip();
dc.send(buf,new InetSocketAddress("localhost",9898));
buf.clear();
}
dc.close();
}
@Test
public void test11() throws IOException {
DatagramChannel dc = DatagramChannel.open();
dc.configureBlocking(false);
dc.bind(new InetSocketAddress(9898));
Selector selector = Selector.open();
dc.register(selector,SelectionKey.OP_READ);
while (selector.select()>0){
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
while (it.hasNext()){
SelectionKey sk = it.next();
if (sk.isReadable()){
ByteBuffer buf = ByteBuffer.allocate(1024);
dc.receive(buf);
buf.flip();
System.out.println(new String(buf.array(),0,buf.limit()));
buf.clear();
}
}
it.remove();
}
}
}