粗人之玩转Buffer

1、ByteBuffer
public static void main(String args[]) throws Exception {  
    //这用用的是文件IO处理
    FileInputStream fin = new FileInputStream("E://test.txt");
    //创建文件的操作管道
    FileChannel fc = fin.getChannel();  

    //分配一个10个大小缓冲区,说白了就是分配一个10个大小的byte数组
    ByteBuffer buffer = ByteBuffer.allocate(10);  
    output("初始化", buffer);  

    //先读一下
    fc.read(buffer);  
    output("调用read()", buffer);  

    //准备操作之前,先锁定操作范围
    buffer.flip();  
    output("调用flip()", buffer);  

    //判断有没有可读数据
    while (buffer.remaining() > 0) {  
        byte b = buffer.get();  
        // System.out.print(((char)b));  
    }  
    output("调用get()", buffer);  

    //可以理解为解锁
    buffer.clear();  
    output("调用clear()", buffer);  

    //最后把管道关闭
    fin.close();  
}  
//把这个缓冲里面实时状态给答应出来
public static void output(String step, ByteBuffer buffer) {
    System.out.println(step + " : "); 
    //容量,数组大小
    System.out.print("capacity: " + buffer.capacity() + ", ");
    //当前操作数据所在的位置,也可以叫做游标
    System.out.print("position: " + buffer.position() + ", ");
    //锁定值,flip,数据操作范围索引只能在position - limit 之间
    System.out.println("limit: " + buffer.limit());
    System.out.println();
}
2、ByteBuffer#slice
public static public void main( String args[] ) throws Exception {  
    ByteBuffer buffer = ByteBuffer.allocate( 10 );  

    // 缓冲区中的数据0-9  
    for (int i=0; i<buffer.capacity(); ++i) {  
        buffer.put( (byte)i );  
    }  

    // 创建子缓冲区  
    buffer.position( 3 );  
    buffer.limit( 7 );  
    ByteBuffer slice = buffer.slice();  

    // 改变子缓冲区的内容  
    for (int i=0; i<slice.capacity(); ++i) {  
        byte b = slice.get( i );  
        b *= 10;  
        slice.put( i, b );  
    }  

    buffer.position( 0 );  
    buffer.limit( buffer.capacity() );  

    while (buffer.remaining()>0) {  
        System.out.println( buffer.get() );  
    }  
}  
3、ByteBuffer#wrap
public static public void main( String args[] ) throws Exception {  
    // 分配指定大小的缓冲区  
    ByteBuffer buffer1 = ByteBuffer.allocate(10);  

    // 包装一个现有的数组  
    byte array[] = new byte[10];  
    ByteBuffer buffer2 = ByteBuffer.wrap( array );
} 
4、DirectByteBuffer
public static public void main( String args[] ) throws Exception {  

    //在Java里面存的只是缓冲区的引用地址
    //管理效率

    //首先我们从磁盘上读取刚才我们写出的文件内容
    String infile = "E://test.txt";
    FileInputStream fin = new FileInputStream( infile );  
    FileChannel fcin = fin.getChannel();

    //把刚刚读取的内容写入到一个新的文件中
    String outfile = String.format("E://testcopy.txt");
    FileOutputStream fout = new FileOutputStream(outfile);
    FileChannel fcout = fout.getChannel();  

    // 使用allocateDirect,而不是allocate
    ByteBuffer buffer = ByteBuffer.allocateDirect(1024);  

    while (true) {  
        buffer.clear();  

        int r = fcin.read(buffer);  

        if (r==-1) {  
            break;  
        }  

        buffer.flip();  

        fcout.write(buffer);  
    }
}  
5、IntBuffer
public static void main(String[] args) {  
    // 分配新的int缓冲区,参数为缓冲区容量
    // 新缓冲区的当前位置将为零,其界限(限制位置)将为其容量。它将具有一个底层实现数组,其数组偏移量将为零。  
    IntBuffer buffer = IntBuffer.allocate(8);

    for (int i = 0; i < buffer.capacity(); ++i) {  
        int j = 2 * (i + 1);  
        // 将给定整数写入此缓冲区的当前位置,当前位置递增  
        buffer.put(j);  
    }
    // 重设此缓冲区,将限制设置为当前位置,然后将当前位置设置为0  
    buffer.flip();
    // 查看在当前位置和限制位置之间是否有元素  
    while (buffer.hasRemaining()) {  
        // 读取此缓冲区当前位置的整数,然后当前位置递增  
        int j = buffer.get();  
        System.out.print(j + "  ");  
    }
}  
6、MappedByteBuffer
  • IO映射缓存区
public class MappedBuffer {  
    static private final int start = 0;
    static private final int size = 26;

    static public void main( String args[] ) throws Exception {  
        RandomAccessFile raf = new RandomAccessFile( "E://test.txt", "rw" );
        FileChannel fc = raf.getChannel();

        //把缓冲区跟文件系统进行一个映射关联
        //只要操作缓冲区里面的内容,文件内容也会跟着改变
        MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE,start, size );

        mbb.put( 0, (byte)97 );  //a
        mbb.put( 25, (byte)122 );   //z

        raf.close();  
    }  
}
7、xxxxByteBufferR
  • 只读缓存区
public static public void main( String args[] ) throws Exception {  
    ByteBuffer buffer = ByteBuffer.allocate( 10 );  

    // 缓冲区中的数据0-9  
    for (int i=0; i<buffer.capacity(); ++i) {  
        buffer.put( (byte)i );  
    }  

    // 创建只读缓冲区  
    ByteBuffer readonly = buffer.asReadOnlyBuffer();  

    // 改变原缓冲区的内容  
    for (int i=0; i<buffer.capacity(); ++i) {  
        byte b = buffer.get( i );  
        b *= 10;  
        buffer.put( i, b );  
    }  

    readonly.position(0);  
    readonly.limit(buffer.capacity());  

    // 只读缓冲区的内容也随之改变  
    while (readonly.remaining()>0) {  
        System.out.println( readonly.get());  
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值