刚从github下载了源码,发现里面依赖的data, proto包没有类。然后从maven上下载jar包,将里面的data, proto包反编译为java源文件。
正好这两个包里面的类不多,先来研究一下
data包
ACL用于管理权限, 格式为scheme:id:permission
public class ACL implements Record {
private int perms;
private Id id;
public class Id implements Record {
private String scheme;
private String id;
public class Stat implements Record {
private long czxid;
private long mzxid;
private long ctime;
private long mtime;
private int version;
private int cversion;
private int aversion;
private long ephemeralOwner;
private int dataLength;
private int numChildren;
private long pzxid;
public class StatPersisted implements Record {
private long czxid;
private long mzxid;
private long ctime;
private long mtime;
private int version;
private int cversion;
private int aversion;
private long ephemeralOwner;
private long pzxid;
proto包
里面每个类,都代表一个客户端的请求和响应。这些类和data包的类都是实现了Record接口
public interface Record {
public void serialize (OutputArchive archive, String tag)
throws IOException;
public void deserialize (InputArchive archive, String tag)
throws IOException;
}
public void serialize (OutputArchive a_, String tag) throws IOException {
a_.startRecord(this , tag);
a_.writeInt(this .perms, "perms" );
a_.writeRecord(this .id, "id" );
a_.endRecord(this , tag);
}
public void deserialize (InputArchive a_, String tag) throws IOException {
a_.startRecord(tag);
this .perms = a_.readInt("perms" );
this .id = new Id();
a_.readRecord(this .id, "id" );
a_.endRecord(tag);
}
public void write (DataOutput out ) throws IOException {
BinaryOutputArchive archive = new BinaryOutputArchive(out );
this .serialize(archive, "" );
}
public void readFields (DataInput in ) throws IOException {
BinaryInputArchive archive = new BinaryInputArchive(in );
this .deserialize(archive, "" );
}
public void startRecord(Record r, String tag ) throws IOException {
if (tag != null && ! "" . equals (tag )) {
printCommaUnlessFirst();
stream. print("s{" );
isFirst = true ;
}
}
public void endRecord(Record r, String tag ) throws IOException {
if (tag == null || "" . equals (tag )) {
stream. print("\n" );
isFirst = true ;
} else {
stream. print("}" );
isFirst = false ;
}
}
里面的toString()也很有特色,利用CSV的序列化,增强可读性
public String toString () {
try {
ByteArrayOutputStream s = new ByteArrayOutputStream();
CsvOutputArchive a_ = new CsvOutputArchive(s);
a_.startRecord(this , "" );
a_.writeInt(this .perms, "perms" );
a_.writeRecord(this .id, "id" );
a_.endRecord(this , "" );
return new String(s.toByteArray(), "UTF-8" );
} catch (Throwable var3) {
var3.printStackTrace();
return "ERROR" ;
}
}
最后看看Object原生方法
public int compareTo (Object peer_) throws ClassCastException {
if (!(peer_ instanceof ACL)) {
throw new ClassCastException("Comparing different types of records." );
} else {
ACL peer = (ACL)peer_;
int ret = 0 ;
ret = this .perms == peer.perms ? 0 : (this .perms < peer.perms ? -1 : 1 );
if (ret != 0 ) {
return ret;
} else {
ret = this .id.compareTo(peer.id);
return ret != 0 ? ret : ret;
}
}
}
public boolean equals (Object peer_) {
if (!(peer_ instanceof ACL)) {
return false ;
} else if (peer_ == this ) {
return true ;
} else {
ACL peer = (ACL)peer_;
boolean ret = false ;
ret = this .perms == peer.perms;
if (!ret) {
return ret;
} else {
ret = this .id.equals(peer.id);
return !ret ? ret : ret;
}
}
}
public int hashCode () {
int result = 17 ;
int ret = this .perms;
result = 37 * result + ret;
ret = this .id.hashCode();
result = 37 * result + ret;
return result;
}