码迷,mamicode.com
首页 > 移动开发 > 详细

MappedByteBuffer protobuf

时间:2017-05-01 12:54:07      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:test   mil   导入   cep   nbsp   对比   引入   串行化   文档   

NIO

java.nio.MappedByteBuffer
映射字节缓冲区。
    @Test
    public void testFileMapping() {
        try {
            RandomAccessFile raf = new RandomAccessFile("d:/k.txt", "rws");
            FileChannel fc = raf.getChannel();
            MappedByteBuffer buffer = fc.map(MapMode.READ_WRITE, 2, 6);//映射的文件位置
            System.out.println(buffer.get(0));
            System.out.println(buffer.get(1));
            System.out.println(buffer.get(2));
            buffer.put(0, (byte) 97);
            buffer.put(1, (byte) 98);
            buffer.put(2, (byte) 99);
            fc.close();
            raf.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 

protobuf

1.protocal buffer,协议缓冲区.
2.串行化技术。
    java.io.Serializable
    ObjectOutputStream / ObjectInputStream
    transient       //临时的
    transaction     //事务
    truncate        //截断.

串行化

[java串行化]
易于使用
效率不高。
没有语言的互操作性。

[手动二进制编码]
效率高
难
跨语言

[人性化文档结构]
xml/json/txt/sax
低效

[PB]
描述语言
编译器
库
2008年发布.

PB下载和使用

0.安装protobuf-win32.zip
    a.解压即可。
    b.配置环境path变量
        path=%path%:c:\myprograms\protocal-2.5.0

1.设计对象
2.描述对象
    [d:/xxx/addressbook.proto]
    package tutorial;
    option java_package = "com.example.tutorial";
    option java_outer_classname = "AddressBookProtos";
    message Person {
        required string name = 1;
        required int32 id = 2;
        optional string email = 3;
        enum PhoneType {
        MOBILE = 0;
        HOME = 1;
        WORK = 2;
    }
    message PhoneNumber {
        required string number = 1;
        optional PhoneType type = 2 [default = HOME];
    }
    repeated PhoneNumber phone = 4;
    }
    message AddressBook {
        repeated Person person = 1;
    }
3.编译描述(注意addressbook.proto保存时使用ANSI格式)

    cmd>protoc --java_out=d:\protobuf\out addressbook.proto

    -- 会生成源代码.

4.获得生成的源代码
    略
5.导入对象到工程
    a.引入google protobuf类库
    b.复制源代码到eclise中.

6.实例化对象
    package com.example.tutorial;

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;

    import com.example.tutorial.AddressBookProtos.Person;
    import com.example.tutorial.AddressBookProtos.Person.PhoneNumber;
    import com.example.tutorial.AddressBookProtos.Person.PhoneType;

    public class TestPB {

        public static void main(String[] args) throws Exception {
            //使用对象
            PhoneNumber number = Person.PhoneNumber.newBuilder()
                            .setType(PhoneType.MOBILE)
                            .setNumber("123456")
                            .build();

            Person p = Person.newBuilder().setId(100)
                .setName("tom")
                .setEmail("abc@hotmail.com")
                .addPhone(number)
                .build();

            //使用PB串行化对象
            FileOutputStream fos = new FileOutputStream("d:/protobuf/person.dat");
            p.writeTo(fos);
            fos.close();
            System.out.println("over");

        }
    }

串行化技术对比

[Space Size]
java-build-in( 870) > google-protobuf(230) > avro(210)      //3倍多

[Time]
java-build-in(75.3) > avro(12.3) > google-protobuf(6.6)     //10倍多
    @Test
    public void testProtoBuf() throws Exception {
        // 使用对象
        PhoneNumber number = Person.PhoneNumber.newBuilder().setType(PhoneType.MOBILE).setNumber("123456789").build();
        Person p = Person.newBuilder().setId(100).setName("tom").setEmail("bei@163.com").build();
        // 使用PB串行化对象
        long start = System.nanoTime();
        FileOutputStream fos = new FileOutputStream("d:/protobuf/person.dat");
        p.writeTo(fos);
        System.out.println(System.nanoTime() - start);
        fos.close();
        System.out.println("over");
        // 使用java串行化计算
        fos = new FileOutputStream("d:/protobuf/person_java.dat");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        start = System.nanoTime();
        oos.writeObject(p);
        System.out.println(System.nanoTime() - start);
        System.out.println("over1");
        oos.close();
        fos.close();
    }

 

MappedByteBuffer protobuf

标签:test   mil   导入   cep   nbsp   对比   引入   串行化   文档   

原文地址:http://www.cnblogs.com/yihaifutai/p/6791590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!