一、MappedByteBuffer 映射字节缓冲区
----------------------------------------------------
1.将硬盘的一部分空间映射到内存中进行读写操作,提高了效率
/**
* 测试映射字节缓冲区
*/
@Test
public void tsMappedByteBuffer()
{
try {
File f = new File("d:\\a.txt");
FileWriter fw = new FileWriter(f);
fw.write("0123456789");
fw.flush();
fw.close();
RandomAccessFile raf = new RandomAccessFile("d:\\a.txt","rw");
FileChannel fc = raf.getChannel();
MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, 2, 6);
System.out.println(mbb.get(0));
System.out.println(mbb.get(1));
System.out.println(mbb.get(2));
mbb.put(0, (byte)97);
mbb.put(1, (byte)98);
mbb.put(2, (byte)99);
System.out.println(mbb.get(0));
System.out.println(mbb.get(1));
System.out.println(mbb.get(2));
} catch (Exception e) {
e.printStackTrace();
}
}
二、Google Protocol Buffers使用详解
-------------------------------------------------------
1.设计对象
2.描述对象(文本文件)
//初始配置选项:导包,创建类
package tutorial;
option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos";
message Person
{
//定义基本字段:基本字符串,int32, 可选字符串
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.编译描述
$ protoc --java_out=d:\\protobuf addressbook.proto
-- java_out=$DST_DIR //输出文件夹路径
-- addressbook.proto //.proto file路径
4.获得生成的源代码
输出路径下会生产源码:com.example.tutorial.AddressBookProtos.java
5.导入对象到工程
1.将文件夹com,直接拷贝到工程src下
2.引入类库 com.google.xxxx(导入jar包 : protobuf-java-2.5.0.jar)
-- 软件安装包,jar包,类库源码,和说明文档,请移步到我上传的的资源protobuf.zip
6.实例化对象
--导入包
import com.example.tutorial.AddressBookProtos.Person;
import com.example.tutorial.AddressBookProtos.AddressBook;
--构建对象
//构建一个Person
Person p = Person.newBuilder().setId(12345)
.setName("john")
.setEmail("john@cc.com")
.addPhone(PhoneNumber.newBuilder().setNumber("12345667111")
.setType(PhoneType.HOME)
.build())
.build();
7.串行化存储对象:p.writeTo(fos);
//使用pb串行化数据
try {
FileOutputStream fos = new FileOutputStream("D:\\Program Files (x86)\\protoc-2.5.0\\protobuf_examples\\excmple1\\person_protoc.dat");
long start = System.nanoTime();
p.writeTo(fos);
System.out.println("protoc序列化耗时:" + (System.nanoTime() - start)); //print: protoc序列化耗时:3638703
fos.close();
File f = new File("D:\\Program Files (x86)\\protoc-2.5.0\\protobuf_examples\\excmple1\\person_protoc.dat");
System.out.println("protoc file size :" + f.length()); //protoc file size :39
fos = new FileOutputStream("D:\\Program Files (x86)\\protoc-2.5.0\\protobuf_examples\\excmple1\\person_java.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
start = System.nanoTime();
oos.writeObject(p);
System.out.println("java序列化耗时:" + (System.nanoTime() - start)); //java序列化耗时:12746035
oos.close();
fos.close();
f = new File("D:\\Program Files (x86)\\protoc-2.5.0\\protobuf_examples\\excmple1\\person_java.dat");
System.out.println("java file size :" + f.length()); //java file size :241
} catch (Exception e) {
e.printStackTrace();
}
8.反串行化加载对象:Person.parseFrom(fis);
//使用pb反串行
try {
String protoPath = "D:\\Program Files (x86)\\protoc-2.5.0\\protobuf_examples\\excmple1\\person_protoc.dat";
String javaPath = "D:\\Program Files (x86)\\protoc-2.5.0\\protobuf_examples\\excmple1\\person_java.dat";
long start = System.nanoTime();
Person newP1 = Person.parseFrom(new FileInputStream(protoPath));
System.out.println("pb反序列化耗时:" + (System.nanoTime() - start)); //pb反序列化耗时:16652044
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(javaPath));
start = System.nanoTime();
Person newP2 = (Person)ois.readObject();
System.out.println("java反序列化耗时:" + (System.nanoTime() - start)); //java反序列化耗时:10047459
} catch (Exception e) {
e.printStackTrace();
}