import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
public class Demo01 {
public static void main(String[] args) {
testServerSOcketChannel();
}
public static void testServerSOcketChannel() {
try {
// 1. eg: 创建 ServerSocketChannel 实例
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
// System.out.println(serverSocketChannel.getClass());//sun.nio.ch.ServerSocketChannelImpl
// 2. eg: 要绑定ip和端口
InetSocketAddress address = new InetSocketAddress(7777);
// 3. eg: 通过 ServerSocketChannel 创建 ServerSocket
ServerSocket serverSocket = serverSocketChannel.socket();
// 4. eg: 绑定 ServerSocket到指定的ip和端口
serverSocket.bind(address);
// 5. eg: 创建 ByteBuffer 数组
ByteBuffer[] bufs = { ByteBuffer.allocate(5), ByteBuffer.allocate(3) };
// 6. eg: 创建 SocketChannel 实例
SocketChannel socketChannel = serverSocketChannel.accept();
int messageLength = 8;
while (true) {
int byteRead = 0;
while (byteRead < messageLength) {
// socketChannel 可以将数据读入到 ByteBuffer 数组里面
long read = socketChannel.read(bufs);
byteRead += read;
System.out.println("byteRead:" + byteRead);
// 1. 遍历输出 ByteBuffer 数组
Arrays.asList(bufs).stream().map(buf -> "position:" + buf.position() + ",limit:" + buf.limit())
.forEach(System.out::println);
}
// 2. 将所有的ByteBuffer 元素 flip
Arrays.asList(bufs).forEach(ByteBuffer::flip);
System.out.println("flip............");
//3. 读出ByteBuffer的数据
int byteWrite = 0;
while (byteWrite < messageLength) {
long write = socketChannel.write(bufs);
byteWrite += write;
System.out.println("byteWrite:" + byteWrite);
}
// 4. 将所有的 ByteBuffer 元素 复位
Arrays.asList(bufs).forEach(ByteBuffer::clear);
Arrays.asList(bufs).stream().map(buf -> "position:" + buf.position() + ",limit:" + buf.limit())
.forEach(System.out::println);
Arrays.asList(bufs).stream().map(buf -> new String(buf.array(),0,buf.limit()))
.forEach(System.out::println);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 1. 测试内存映射
*/
public static void testMapByteBuffer() {
try {
RandomAccessFile randomAccessFile = new RandomAccessFile("1.txt", "rw");
FileChannel channel = randomAccessFile.getChannel();
MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 5);
mappedByteBuffer.put(0, (byte) 'a');
mappedByteBuffer.put(1, (byte) 'b');
mappedByteBuffer.put(2, (byte) 'c');
randomAccessFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 1. 测试只读ByteBuffer
*/
public static void testReadOnlyBuffer() {
ByteBuffer buffer = ByteBuffer.allocate(5);
System.out.println(buffer.getClass());// HeapByteBuffer
ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();//
System.out.println(buffer.getClass());
System.out.println(readOnlyBuffer.getClass());// HeapByteBufferR
readOnlyBuffer.put((byte) 1);// ReadOnlyBufferException
}
public static void testCopyFile02() {
try {
FileInputStream fis = new FileInputStream("1.avi");
FileChannel channel01 = fis.getChannel();
FileOutputStream fos = new FileOutputStream("2.avi");
FileChannel channel02 = fos.getChannel();
// channel01.transferFrom(src, position, count);
channel02.transferFrom(channel01, 0, channel01.size());
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void testCopyFile01() {
try {
FileInputStream fis = new FileInputStream("a.txt");
FileChannel channel01 = fis.getChannel();
FileOutputStream fos = new FileOutputStream("2.txt");
FileChannel channel02 = fos.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(5);
while (true) {
// buffer 复位
buffer.clear();
int read = channel01.read(buffer);
if (read == -1) {
break;
}
buffer.flip();
channel02.write(buffer);
// (read = channel01.read(buffer)) != -1
}
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void testChannelRead() {
try {
FileInputStream fis = new FileInputStream("a.txt");
FileChannel channel = fis.getChannel();
ByteBuffer dst = ByteBuffer.allocate(1024);
int read = channel.read(dst);
System.out.println(new String(dst.array(), 0, dst.position()));
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void testChannelWrite() {
try {
FileOutputStream fos = new FileOutputStream("a.txt");
FileChannel channel = fos.getChannel();
ByteBuffer buffer = ByteBuffer.allocate("aaabbbccc".getBytes().length);
buffer.put("aaabbbccc".getBytes());
buffer.flip();
channel.write(buffer);
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 01.
*/
public static void testBuffer() {
/**
* 1. IntBuffer:
*
*/
IntBuffer buffer = IntBuffer.allocate(5);
buffer.put(1);
buffer.put(2);
buffer.put(3);
// buffer.put(4);
// buffer.put(5);
System.out.println("1: position:" + buffer.position());// 3
System.out.println("1: limit:" + buffer.limit());// 5
buffer.flip();
System.out.println("1: position:" + buffer.position());// 0
System.out.println("1: limit:" + buffer.limit());// 3
while (buffer.hasRemaining()) {
int i = buffer.get();
System.out.println(i);
}
}
}
网友评论