nio Buffer 三属性以及flip、clear、compact方法

12 篇文章 0 订阅
package c.ct.io.nio;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NioDemo {
    public static void main(String[] args) throws IOException {
        //  write();
        //  read();
        //  m3();
        //   m4();
        // m5();
        m6();
    }

    public static void m6() {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        //存入数据
        buffer.put((byte) 'A');
        buffer.put((byte) 'B');
        buffer.put((byte) 'C');
        buffer.put((byte) 'D');
        buffer.compact();
        System.out.print(buffer.capacity() + "\t");
        System.out.print(buffer.limit() + "\t");
        System.out.println(buffer.position() + "\t");
        buffer.put((byte) 'E');
        buffer.put((byte) 'F');
        System.out.print(buffer.capacity() + "\t");
        System.out.print(buffer.limit() + "\t");
        System.out.println(buffer.position() + "\t");
        buffer.flip();
        System.out.print(buffer.capacity() + "\t");
        System.out.print(buffer.limit() + "\t");
        System.out.println(buffer.position() + "\t");

        System.out.println(buffer.get(1020));
        System.out.println(buffer.get(1021));
        System.out.println(buffer.get(0));
        System.out.println(buffer.get(1022));//Exception in thread "main" java.lang.IndexOutOfBoundsException

    }

    public static void m5() {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        //存入数据
        buffer.put((byte) 'A');
        buffer.put((byte) 'B');
        buffer.compact();
        System.out.println(buffer.capacity());
        System.out.println(buffer.limit());
        System.out.println(buffer.position());
        buffer.put((byte) 'C');
        buffer.put((byte) 'D');
        System.out.println(new String(buffer.array()));
        System.out.println(buffer.capacity());
        System.out.println(buffer.limit());
        System.out.println(buffer.position());
        buffer.flip();
        System.out.println((char) buffer.get(1021));
        System.out.println((char) buffer.get(1022));
        System.out.println((char) buffer.get(1023));
        System.out.println("压缩后get:" + new String(buffer.array()));

    }

    public static void m4() {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put((byte) 'A');
        buffer.put((byte) 'B');
        System.out.println(buffer.capacity());
        System.out.println(buffer.limit());
        System.out.println(buffer.position());
        buffer.flip();
        System.out.println(buffer.capacity());
        System.out.println(buffer.limit());
        System.out.println(buffer.position());

        System.out.println((char) buffer.get());
        System.out.println((char) buffer.get());

    }

    public static void m3() {
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        System.out.println("写入之前");
        System.out.println("容量:" + buffer.capacity());
        System.out.println("限制:" + buffer.limit());
        System.out.println("位置:" + buffer.position());
        //  System.out.println("标记: "+buffer.mark());
        buffer.put("hello".getBytes());
        System.out.println("写入之后");
        System.out.println("容量:" + buffer.capacity());
        System.out.println("限制:" + buffer.limit());
        System.out.println("位置:" + buffer.position());
        //    System.out.println("标记: "+buffer.mark());
        buffer.flip();
        System.out.println("改为读取模式");
        System.out.println("容量:" + buffer.capacity());
        System.out.println("限制:" + buffer.limit());
        System.out.println("位置:" + buffer.position());

        System.out.println("读取之后");
        byte[] buf = new byte[2];
        buffer.get(buf);
        String s = new String(buf, 0, 2);
        System.out.println(s);
        System.out.println("容量:" + buffer.capacity());
        System.out.println("限制:" + buffer.limit());
        System.out.println("位置:" + buffer.position());

        System.out.println("------rewind之后--------");
        buffer.rewind();
        System.out.println("==================");
        System.out.println("容量:" + buffer.capacity());
        System.out.println("限制:" + buffer.limit());
        System.out.println("位置:" + buffer.position());

        System.out.println("------读取2个字节--------");
        buffer.get(buf);
        String ste = new String(buf);

        System.out.println(ste);
        System.out.println("容量:" + buffer.capacity());
        System.out.println("限制:" + buffer.limit());
        System.out.println("位置:" + buffer.position());

        System.out.println("-------------compact之后------------");
        buffer.compact();//将没读到两个的放到前两位 当前位置不变
        System.out.println("容量:" + buffer.capacity());
        System.out.println("限制:" + buffer.limit());
        System.out.println("位置:" + buffer.position());

        System.out.println("before flip");
        buffer.flip();
        System.out.println("容量:" + buffer.capacity());
        System.out.println("限制:" + buffer.limit());
        System.out.println("位置:" + buffer.position());
        String s6 = new String(buffer.array(), 0, 3);
        System.out.println(s6);

    }

    public static void write() throws FileNotFoundException, UnsupportedEncodingException {
        FileOutputStream fileOutputStream = new FileOutputStream("src/nio.txt", true);

        FileChannel fileChannel = fileOutputStream.getChannel();
        ByteBuffer buf = ByteBuffer.allocate(100);
        buf.put("hello\r\n".getBytes("UTF-8"));
        try {
            buf.flip();
            fileChannel.write(buf);
            fileChannel.close();
            fileOutputStream.close();
        } catch (IOException e) {

        }
    }

    public static void read() {
        RandomAccessFile aFile = null;
        try {
            aFile = new RandomAccessFile("src/nio.txt", "rw");
            FileChannel fileChannel = aFile.getChannel();
            ByteBuffer buf = ByteBuffer.allocate(100);
            while (fileChannel.read(buf) != -1) {
                buf.flip();
                while (buf.hasRemaining()) {
                    System.out.print((char) buf.get());
                }
                buf.clear();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (aFile != null) {
                    aFile.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

有部分参考csdn,具体链接不知道了,如有侵权,请告知删除。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值