压缩双精度数组时,我对GZIPOutputStream有一个非常奇怪的问题。在第 57 个元素处,当我重新加载数据时,我得到了一个小的差异:
57 > 3.003727492141554E7 3.0037273913440887E7
1900: false
57 > -6.110783629228158E7 -6.110783629076558E7
2000: false
1900 和 2000 是两组不同的双打。左值是原始值。
当我使用没有 GZIP 的简单FileOutputStream时,我不明白这个问题。为什么?GZIP 输出流是否会丢失信息?
编辑
以下是我读取和写入数据的方式:
public static final double[] coefficients = new double[1161289];
...
public static void dump(File f) throws FileNotFoundException, IOException {
OutputStream os = FileUtils.zipContent(f);
byte[] ba = new byte[8];
ByteBuffer BF = ByteBuffer.wrap(ba);
BF.order(ByteOrder.BIG_ENDIAN);
for (int i=0;i<coefficients.length;i++) {
BF.putDouble(0, coefficients[i]);
os.write(ba,0,8);
}
os.close();
}
public static void load(File f) throws FileNotFoundException, IOException {
InputStream is = FileUtils.readZippedContent(f);
byte[] ba = new byte[8];
final ByteBuffer BF = ByteBuffer.wrap(ba);
BF.order(ByteOrder.BIG_ENDIAN);
for (int i=0;i<coefficients.length;i++) {
is.read(ba,0,8);
coefficients[i] = BF.getDouble(0);
}
}
...
public static GZIPOutputStream zipContent(File f)
throws FileNotFoundException, IOException {
return new GZIPOutputStream(new FileOutputStream(f));
}
public static GZIPInputStream readZippedContent(File f)
throws FileNotFoundException, IOException {
return new GZIPInputStream(new FileInputStream(f));
}