可以在 Julia 中读取长度分隔文件,但不能在 Rust 中读取
Can read length delimited file in Julia but not in Rust
在 julia 中,我可以使用此代码从文件中读取前 4 个字节。 (就是要阅读的消息长度)
# Read the length in first 4 bytes
msglen = read(R.buffer, UInt32)
# Then read up to that length
bytes = read(R.buffer, msglen)
但是当我尝试在 Rust 中读取同一个文件时,长度值变得太大了:
let mut f = std::fs::File::open("../20181002.bytes").unwrap();
let mut buf = Vec::new();
f.read_to_end(&mut buf).expect("file reading failed");
let mut dst = [0u8; 4];
let mut read_index = 0usize;
// select first 4 bytes of buf and clone into dst
dst.clone_from_slice(&buf[read_index..(read_index+4)]);
println!("Dst: {:?}", &buf[read_index..(read_index+4)]);
let length = u32::from_le_bytes(dst);
println!("Len: {}", length);
Dst: [31, 139, 8, 0]
Len: 559903
I think the first 4 bytes should be 1f8b 0800 0000 0000
如果有帮助,这是用 C# 编写的:
public static void Write(Stream stream, byte[] bytes)
{
var lengthBuffer = BitConverter.GetBytes(bytes.Length);
// Write object length.
stream.Write(lengthBuffer, offset: 0, count: 4);
// Write object.
stream.Write(bytes, offset: 0, count: bytes.Length);
}
1f8b 是 gzip 编码文件的幻数。抱歉浪费您的时间 - 留下代码以防有人感兴趣。
在 julia 中,我可以使用此代码从文件中读取前 4 个字节。 (就是要阅读的消息长度)
# Read the length in first 4 bytes
msglen = read(R.buffer, UInt32)
# Then read up to that length
bytes = read(R.buffer, msglen)
但是当我尝试在 Rust 中读取同一个文件时,长度值变得太大了:
let mut f = std::fs::File::open("../20181002.bytes").unwrap();
let mut buf = Vec::new();
f.read_to_end(&mut buf).expect("file reading failed");
let mut dst = [0u8; 4];
let mut read_index = 0usize;
// select first 4 bytes of buf and clone into dst
dst.clone_from_slice(&buf[read_index..(read_index+4)]);
println!("Dst: {:?}", &buf[read_index..(read_index+4)]);
let length = u32::from_le_bytes(dst);
println!("Len: {}", length);
Dst: [31, 139, 8, 0]
Len: 559903
I think the first 4 bytes should be 1f8b 0800 0000 0000
如果有帮助,这是用 C# 编写的:
public static void Write(Stream stream, byte[] bytes)
{
var lengthBuffer = BitConverter.GetBytes(bytes.Length);
// Write object length.
stream.Write(lengthBuffer, offset: 0, count: 4);
// Write object.
stream.Write(bytes, offset: 0, count: bytes.Length);
}
1f8b 是 gzip 编码文件的幻数。抱歉浪费您的时间 - 留下代码以防有人感兴趣。