/*
* Copyright 2011 Daniel Rendall
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package uk.co.sromo.blister;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
/**
* TODO: Documentation
*/
public class BinaryPlistEncoder implements BPVisitor {
private final static Logger log = Logger.getLogger(BinaryPlistEncoder.class.getSimpleName());
private final static byte[] EMPTY = new byte[0];
private final Map<Integer, byte[]> objectData = new LinkedHashMap<Integer, byte[]>(); // object ref -> bytes for that object
private final Set<Integer> collectionTypes = new HashSet<Integer>(); // object refs of collection types which need special treatment
private final Map<BPItem, Integer> objectRefs = new LinkedHashMap<BPItem, Integer>(); // BPItem -> object ref
int currentObjectRef = 0;
public void visit(BPArray item) {
if (!objectRefs.containsKey(item)) {
// store this with an empty representation so that it gets a reference number
int myRef = store(item, EMPTY);
// ensure all the children are stored
for(BPItem child : item) {
child.accept(this);
}
// so now we have object references for all children.
// we can store the array (temporarily) with 4-byte int offsets.
ByteBuffer bb = ByteBuffer.allocate(item.size() * 4 + 1);
// identify the object type for later...
bb.put((byte) BinaryPlist.ARRAY);
for(BPItem child : item) {
if (!objectRefs.containsKey(child)) {
throw new EncodingException("Expected an object ref for " + child + " after visiting array");
}
bb.putInt(objectRefs.get(child));
}
// now we store the list of item references - note that this is not the true representation of the array
objectData.put(myRef, bb.array());
collectionTypes.add(myRef);
}
}
public void visit(BPBoolean item) {
if (!objectRefs.containsKey(item)) {
byte[] bytes = new byte[] { item.getValue() ? (byte) BinaryPlist.BOOL_TRUE : (byte) BinaryPlist.BOOL_FALSE};
store(item, bytes);
}
}
public void visit(BPData item) {
if (!objectRefs.containsKey(item)) {
byte[] itemBytes = item.getData();
byte[] itemHeader = getObjectHeader(BinaryPlist.DATA, itemBytes.length);
store(item, itemHeader, itemBytes);
}
}
public void visit(BPDate item) {
if (!objectRefs.containsKey(item)) {
// TODO - implement this properly
byte[] bytes = new byte[] {(byte)BinaryPlist.DATE, 0,0,0,0,0,0,0,0};
store(item, bytes);
}
}
public void visit(BPDict item) {
if (!objectRefs.containsKey(item)) {
// store this with an empty representation so that it gets a reference number
int myRef = store(item, EMPTY);
// ensure all the children are stored
for(BPString key : item.keySet()) {
key.accept(this);
BPItem value = item.get(key);
value.accept(this);
}
// so now we have object references for all children.
// we can store the array (temporarily) with 4-byte int offsets.
ByteBuffer bb = ByteBuffer.allocate(item.size() * 8 + 1);
// identify the object type for later...
bb.put((byte) BinaryPlist.DICT);
for(BPString key : item.keySet()) {
if (!objectRefs.containsKey(key)) {
throw new EncodingException("Expected an object ref for key " + key + " after visiting dict");
}
bb.putInt(objectRefs.get(key));
}
for(BPString key : item.keySet()) {
BPItem value = item.get(key);
if (!objectRefs.containsKey(value)) {
throw new EncodingException("Expected an object ref for value " + value + " after visiting dict");
}
bb.putInt(objectRefs.get(value));
}
// now we store the list of item references - note that this is not the true representation of the array
objectData.put(myRef, bb.array());
collectionTypes.add(myRef);
}
}
public void visit(BPInt item) {
if (!objectRefs.containsKey(item)) {
long lValue = item.getLongValue();
if (lValue < 0 || item.getSize() == BPInt.Size.Long) {
store(item, new byte[] {(byte) (BinaryPlist.INT | 0x03),
(byte) ((lValue >> 56) & 0xff),
(byte) ((lValue >> 48) & 0xff),
(byte) ((lValue >> 40) & 0xff),
(byte) ((lValue >> 32) & 0xff),
(byte) ((lValue >> 24) & 0xff),
(byte) ((lValue >> 16) & 0xff),
(byte) ((lValue >> 8) & 0xff),
(byte) (lValue & 0xff) });
} else {
int value = item.getValue();
if (value < 256) {
store(item, new byte[] {(byte) (BinaryPlist.INT | 0x00),
(byte) value});
} else if (value < 65536) {
store(item, new byte[] {(byte) (BinaryPlist.INT | 0x01),
(byte) ((value >> 8) & 0xff),
(byte) (value & 0xff) });
} else {
store(item, new byte[] {(byte) (BinaryPlist.INT | 0x02),
(byte) ((value >> 24) & 0xff),
(byte) ((value >> 16) & 0xff),
(byte) ((value >> 8) & 0xff),
(byte) (value & 0xff) });
}
}
}
}
public void visit(BPNull item) {
if (!objectRefs.containsKey(item)) {
store(item, new byte[] {(byte) BinaryPlist.NULL});
}
}
public void visit(BPReal item) {
if (!objectRefs.containsKey(item)) {
// TODO - implement this properly
store(item, new byte[] {(byte) (BinaryPlist.REAL | 0x00), 0});
}
}
public void visit(BPSet item) {
if (!objectRefs.containsKey(item)) {
// store this with an empty representation so that it gets a reference number
int myRef = store(item, EMPTY);
// ensure all the children are stored
for(BPItem child : item) {
child.accept(this);
}
// so now we have object references for all children.
// we can store the array (temporarily) with 4-byte int offsets.
ByteBuffer bb = ByteBuffer.allocate(item.size() * 4 + 1);
// identify the object type for later...
bb.put((byte) BinaryPlist.SET);
for(BPItem child : item) {
if (!objectRefs.containsKey(child)) {
throw new EncodingException("Expected an object ref for " + child + " after visiting set");
}
bb.putInt(objectRefs.get(child));
}
// now
没有合适的资源?快使用搜索试试~ 我知道了~
Java的PList类库 Blister.7z
共54个文件
java:30个
plist:15个
xml:4个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 23 浏览量
2022-07-06
10:31:43
上传
评论
收藏 34KB 7Z 举报
温馨提示
Java的PList类库 Blister.7z
资源推荐
资源详情
资源评论
收起资源包目录
Java的PList类库 Blister.7z (54个子文件)
Java的PList类库 Blister
danielrendall-Blister-650766e
docs
CFBinaryPList.c 53KB
README 125B
code
modules
pom.xml 3KB
src
test
resources
Numbers.xml 1KB
Numbers.plist 445B
Unicode.plist 317B
Key4096.plist 4KB
IntArray.xml 3KB
Longs.plist 3KB
BinaryInfo.plist 1KB
BinaryKey4095.plist 4KB
BinarySingleKey.plist 66B
SingleLongKey.plist 272B
Key4095.plist 4KB
BinaryKey4096.plist 4KB
Longs.xml 7KB
Info.plist 3KB
BinaryUnicode.plist 139B
BinarySingleLongKey.plist 104B
SingleKey.plist 238B
IntArray.plist 722B
java
uk
co
sromo
blister
TestBPItems.java 15KB
TestPerformance.java 2KB
TestNumbers.java 2KB
Numerals.java 874B
Keys.java 849B
main
resources
log4j.properties 1KB
java
uk
co
sromo
blister
BPInt.java 6KB
BPReal.java 5KB
BPBoolean.java 2KB
BPString.java 4KB
BinaryPlist.java 4KB
BinaryPlistException.java 894B
BPVisitor.java 1KB
BPArray.java 4KB
BPExpandableItem.java 1KB
BPData.java 1KB
package.html 835B
BPUid.java 1KB
App.java 3KB
BPDict.java 10KB
BinaryPlistTrailer.java 3KB
BinaryPlistDecoder.java 11KB
BinaryPlistEncoder.java 17KB
BPItem.java 2KB
BinaryPlistOffsetReader.java 2KB
BinaryPlistHeader.java 2KB
util
DumpVisitor.java 3KB
BPVisitorSupport.java 1KB
BPSet.java 4KB
BinaryPlistOffsetTable.java 2KB
BPDate.java 1KB
BPNull.java 1KB
.gitignore 66B
共 54 条
- 1
资源评论
BryanDing
- 粉丝: 299
- 资源: 5583
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于Java的本科探索学习项目设计源码 - 本科探索
- 基于Javascript和Python的微商城项目设计源码 - MicroMall
- 基于Java的网上订餐系统设计源码 - online ordering system
- 基于Javascript的超级美眉网络资源管理应用模块设计源码
- 基于Typescript和PHP的编程知识储备库设计源码 - study-php
- Screenshot_2024-05-28-11-40-58-177_com.tencent.mm.jpg
- 基于Dart的Flutter小提琴调音器APP设计源码 - violinhelper
- 基于JavaScript和CSS的随寻订购网页设计源码 - web-order
- 基于MATLAB的声纹识别系统设计源码 - VoiceprintRecognition
- 基于Java的微服务插件集合设计源码 - wsy-plugins
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功