內存映射文件MappedByteBuffer和Buffer的Scattering與Gathering

上一篇講到的DirectByteBuffer繼承自MappedByteBufferjava

1、MappedByteBuffer緩存

MappedByteBuffer的定義:app

A direct byte buffer whose content is a memory-mapped region of a file.dom

直接緩存,內容是一個內存映射文件。socket

建立測試類測試

public class NioTest9 {
    public static void main(String[] args) throws  Exception {
        RandomAccessFile randomAccessFile = new RandomAccessFile("NioTest9.txt","rw");
        FileChannel fileChannel =  randomAccessFile.getChannel();
        MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0,5);
        mappedByteBuffer.put(0,(byte)'a');
        mappedByteBuffer.put(3,(byte)'b');

        randomAccessFile.close();
    }
}

 建立NioTest9.tex文件操作系統

 

 運行程序,用記事本打開3d

操做的是堆外內存,堆外內存寫入到文件由操做系統控制。server

 

2、排他鎖和共享鎖blog

實際用的比較少

/**
 * 
 * 共享鎖: 全部程序都能多共享的部分進行讀
 * 排他鎖: 只有一個程序對鎖定的部分進行寫操做
 * 
 */
public class NioTest10 {

    public static void main(String[] args) throws Exception {
        RandomAccessFile randomAccessFile = new RandomAccessFile("NioTest10.txt", "rw");

        FileChannel fileChannel = randomAccessFile.getChannel();

       FileLock fileLock = fileChannel.lock(3,6,true);

        System.out.println("valid:" + fileLock.isValid());

        System.out.println("lock type:" + fileLock.isShared());

        fileLock.release();

        randomAccessFile.close();
    }
}

  

 

3、關於Buffer的Scattering(散開)與Gathering(收集)

/**
 *
 * 關於Buffer的Scattering(散開)與Gathering(收集)
 *
 */
public class NioTest11 {

    public static void main(String[] args) throws Exception {

        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

        InetSocketAddress address = new InetSocketAddress(8899);

        serverSocketChannel.socket().bind(address);

        int messageLength = 2 + 3 + 4;
        ByteBuffer[] buffers = new ByteBuffer[3];
        buffers[0] = ByteBuffer.allocate(2);
        buffers[1] = ByteBuffer.allocate(3);
        buffers[2] = ByteBuffer.allocate(4);

        SocketChannel socketChannel = serverSocketChannel.accept();

        while (true){
            int byteRead = 0;
            while (byteRead < messageLength){
                long r = socketChannel.read(buffers);
                byteRead += r;

                System.out.println("bytesRead:" + byteRead);

                Arrays.asList(buffers).stream().map(buffer -> "position:" + buffer.position() +",limit:" + buffer.limit() + " 值:" + buffer.toString()).forEach(System.out::println);
            }

            Arrays.asList(buffers).forEach(buffer -> {
                buffer.flip();
            });

            long byteWritten = 0;
            while (byteWritten < messageLength){
                long r = socketChannel.write(buffers);
                byteWritten += r;
            }


            Arrays.asList(buffers).forEach(bufffer -> {
                bufffer.clear();
            });

            System.out.println("bytesRead:" + byteRead + ",byteWritten: " + byteWritten +", messageLength:" + messageLength);
        }

    }
}

  使用Telnet發送,輸出結果以下圖:

相關文章
相關標籤/搜索