This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
binary.rs
89 lines (79 loc) · 2.41 KB
/
binary.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use std::collections::VecDeque;
use std::io::{Read, Seek};
use crate::array::{BinaryArray, Offset};
use crate::buffer::Buffer;
use crate::datatypes::DataType;
use crate::error::{Error, Result};
use super::super::read_basic::*;
use super::super::{Compression, IpcBuffer, Node, OutOfSpecKind};
#[allow(clippy::too_many_arguments)]
pub fn read_binary<O: Offset, R: Read + Seek>(
field_nodes: &mut VecDeque<Node>,
data_type: DataType,
buffers: &mut VecDeque<IpcBuffer>,
reader: &mut R,
block_offset: u64,
is_little_endian: bool,
compression: Option<Compression>,
scratch: &mut Vec<u8>,
) -> Result<BinaryArray<O>> {
let field_node = field_nodes.pop_front().ok_or_else(|| {
Error::oos(format!(
"IPC: unable to fetch the field for {:?}. The file or stream is corrupted.",
data_type
))
})?;
let validity = read_validity(
buffers,
field_node,
reader,
block_offset,
is_little_endian,
compression,
scratch,
)?;
let length: usize = field_node
.length()
.try_into()
.map_err(|_| Error::from(OutOfSpecKind::NegativeFooterLength))?;
let offsets: Buffer<O> = read_buffer(
buffers,
1 + length,
reader,
block_offset,
is_little_endian,
compression,
scratch,
)
// Older versions of the IPC format sometimes do not report an offset
.or_else(|_| Result::Ok(Buffer::<O>::from(vec![O::default()])))?;
let last_offset = offsets.as_slice()[offsets.len() - 1].to_usize();
let values = read_buffer(
buffers,
last_offset,
reader,
block_offset,
is_little_endian,
compression,
scratch,
)?;
BinaryArray::<O>::try_new(data_type, offsets, values, validity)
}
pub fn skip_binary(
field_nodes: &mut VecDeque<Node>,
buffers: &mut VecDeque<IpcBuffer>,
) -> Result<()> {
let _ = field_nodes.pop_front().ok_or_else(|| {
Error::oos("IPC: unable to fetch the field for binary. The file or stream is corrupted.")
})?;
let _ = buffers
.pop_front()
.ok_or_else(|| Error::oos("IPC: missing validity buffer."))?;
let _ = buffers
.pop_front()
.ok_or_else(|| Error::oos("IPC: missing offsets buffer."))?;
let _ = buffers
.pop_front()
.ok_or_else(|| Error::oos("IPC: missing values buffer."))?;
Ok(())
}