- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在处理之前提出的 question 时遇到了这个问题。
这可能特定于 ObjectInputStream
而不是一般的二进制读取,因此标题可能具有误导性。
从那里开始的问题基本上是这样的:作者已经将字符串的 HashMap 序列化为 double 。作者对 hashmap 中每个条目的 custom serialization format 非常简单
int n // length of string key as a 4-byte integer
byte[n] key // a string of length n
double value // the value associated with the key
现在由于某种原因,在序列化过程中,字符串之一 2010-00-008.html
被序列化了两个额外的字节,如下所示。
因此,不是写入 16 个字节,而是写入 18 个字节。这势必会导致问题,因为它仍然表示该字符串的长度为 16 个字节。
但是,出于某种原因,您可以完美地将散列映射写出并读回!似乎给定一个 18 字节的字符串,您可以读取 16 个字节并仍然读取整个内容。
这是代码。它基本上是另一个问题中的代码,除了我制作它以便您应该能够更改路径并运行它。运行它之后,您将获得一系列写入语句,然后是一系列读取语句。检查该文件,您应该会注意到字符串中的额外字节,但程序不会崩溃。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
public class Test {
// customize the path as needed
public static String path = "C:\\temp\\sample.dat";
HashMap<String, Double> map = new HashMap<String, Double>();
public Test() {
map.put("2010-00-027.html",21732.994621513037); map.put("2010-00-020.html",3466.5169348296736); map.put("2010-00-051.html",12528.648992702407); map.put("2010-00-062.html",3354.8950010256385);
map.put("2010-00-024.html",10295.095511718278); map.put("2010-00-052.html",5381.513344679818); map.put("2010-00-007.html",16466.33813960735); map.put("2010-00-017.html",9484.969198176652);
map.put("2010-00-054.html",15423.873112634772); map.put("2010-00-022.html",8123.842752870753); map.put("2010-00-033.html",21238.496665104063); map.put("2010-00-028.html",7578.792651786424);
map.put("2010-00-048.html",3566.4118233046393); map.put("2010-00-040.html",2681.0799941861724); map.put("2010-00-049.html",14308.090890746222); map.put("2010-00-058.html",5911.342406606804);
map.put("2010-00-045.html",2284.118716145881); map.put("2010-00-031.html",2859.565771680721); map.put("2010-00-046.html",4555.187022907964); map.put("2010-00-036.html",8479.709295569426);
map.put("2010-00-061.html",846.8292195815125); map.put("2010-00-023.html",14108.644025417952); map.put("2010-00-041.html",22686.232732684934); map.put("2010-00-025.html",9513.539663409734);
map.put("2010-00-012.html",459.6427911376829); map.put("2010-00-005.html",0.0); map.put("2010-00-013.html",2646.403220496738); map.put("2010-00-065.html",5808.86423609936);
map.put("2010-00-056.html",12154.250518054876); map.put("2010-00-008.html",10811.15198506469); map.put("2010-00-042.html",9271.006516004005); map.put("2010-00-000.html",4387.4162586468965);
map.put("2010-00-059.html",4456.211623469774); map.put("2010-00-055.html",3534.7511584735325); map.put("2010-00-057.html",8745.640098512009); map.put("2010-00-032.html",4993.295735075575);
map.put("2010-00-021.html",3852.5805998017922); map.put("2010-00-043.html",4108.020033536286); map.put("2010-00-053.html",2.2446400279239946); map.put("2010-00-030.html",17853.541210836203);
}
public void write() {
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
oos.writeInt(map.size()); // write size of the map
for (Map.Entry<String, Double> entry : map.entrySet()) { // iterate entries
System.out.println("writing ("+ entry.getKey() +","+ entry.getValue() +")");
byte[] bytes = entry.getKey().getBytes();
oos.writeInt(bytes.length); // length of key string
oos.write(bytes); // key string bytes
oos.writeDouble(entry.getValue()); // value
}
oos.close();
} catch (Exception e) {
}
}
public void read() {
try {
FileInputStream f = new FileInputStream(path);
ObjectInputStream ois = new ObjectInputStream(f);
int size = ois.readInt(); // read size of the map
HashMap<String, Double> newMap = new HashMap<>(size);
for (int i = 0; i < size; i++) { // iterate entries
int length = ois.readInt(); // length of key string
byte[] bytes = new byte[length];
ois.readFully(bytes, 0, length);
//ois.read(bytes);
String key = new String(bytes);
double value = ois.readDouble(); // value
newMap.put(key, value);
System.out.println("read ("+ key +","+ value +")");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Test t = new Test();
t.write();
t.read();
}
}
最佳答案
您需要阅读 Protocol chapter of the Object Serialization Specification .除了实际数据之外,流中还充满了类型和 block 标记。这是其中之一,在正确读取流时被ObjectInputStream
过滤掉。
EDIT 额外的字节是 77 64
,这意味着 TC_BLOCK_DATA
的大小为 0x64。
关于java - ObjectOutputStream 文件中的这两个额外字节是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23945586/
我配置了我的RouteInitializer如下: class AppRouteInitializer implements RouteInitializer { init(Router rout
我正在尝试从 Android 应用程序发送短信。我正在使用 PendingIntent 以便我可以使用 Broadcast Receiver 检查它是否发送正常。由于 sendTextMessage
目录 简介 1 "额外"字段是什么 1.1 "额外"是指与业务无关 1.2 产生
应用程序读取 JSON 数据。然后它会将其放入 ListView (正确),但在按下某个项目后,我总是会得到显示的相同值。下面的代码我认为是问题所在,但我找不到。 try{ JSONArray
我正在使用以下代码 (Kotlin) 创建通知 val builder = NotificationCompat.Builder(ctx) ........ .set
我有一个问题。现在我正在使用 3 个面板,mainPanel 和其他 2 个面板(btnPanel 和 iconPanel)。所以问题是当我按下“重置”按钮时,我删除了 iconPanel 并再次添加
这是我的 html: Settings Export Import 和CSS: span.button { float:right; margin-righ
我正在尝试将一个结构编码为 JSON,然后将其插入我的 Mongo 数据库,但不断出现此错误:%!(EXTRA main.Test={575590180 Me})。我究竟做错了什么?我完全从我从事的另
嘿,我遇到了这些 latex 格式问题,有人可以提供一些帮助吗? .tex 文件: \begin{table}{} \renewcommand{\arraystretch}{1.1} \c
我在 FragmentPagerAdapter 中使用了 Fragment 的 ArrayList。 我想在 saveState() 中保存此 ArrayList 的状态,并在 restoreStat
我做了this MapKit-教程 一切正常,但如何为我的 pin 添加额外的属性? 这是我的课车: import Foundation import MapKit class Car: NSObje
关于 Android intent 将提供的附加功能有哪些文档? 更新: 我做了一些进一步的调查。我知道我们可以假设每个 Intent 都不会解析任何数据或额外内容,除非有明确记录。此外,一些(但不是
我在 python3.4.3 上使用 SqlAlchemy 来管理 MySQL 数据库。我正在创建一个表: from datetime import datetime from sqlalchemy
我正在使用 bootstrap 创建网页。我在两个 block (内容和标题)上派生了正文。在内容 block 中,我有 div 类 .container .sameTable 在里面我有 div 类
我在Windows 7上的MinGW和MSYS下使用gfortran构建了一些fortran程序。但是当我在未安装MinGW和MSYS的其他计算机上运行它们时,系统总是要求一些dll,例如libgfo
第一个元素的右侧似乎有额外的间距,我不知道它是从哪里来的。有人可以帮助我吗? 这是我使用的代码: http://jsfiddle.net/srabeat/tenx4y1c/1/ for (i = 0;
我使用 fs-extra 收到以下错误: ERROR { [Error: EPERM: operation not permitted, unlink 'C:\Projects\xxx\branche
我正在尝试在 CBC 模式下使用 AES-128 加密 320 字节的二进制数据,并将密码存储到一个文件中。输出文件应该是 320 字节,但我得到了 336 字节。这是我的代码: #include
我有一个特定的要求,我必须从我的 Activity 中触发浏览器上的 url。我可以使用以下代码执行此操作: Intent browserIntent = new Intent( Intent.A
我正在使用 JMS DI 注入(inject)带有注解的服务: use JMS\DiExtraBundle\Annotation as DI; /** * @DI\Service("foo.bar.
我是一名优秀的程序员,十分优秀!