MappedByteBuffer protobuf

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();
    }
相關文章
相關標籤/搜索