@@ -6,11 +6,11 @@ mod header;
66mod packets;
77mod paginate;
88
9- use std:: io:: { Read , Seek , SeekFrom } ;
9+ use std:: io:: { Read , Seek } ;
1010
1111pub use crc:: crc32;
1212pub use error:: { PageError , Result } ;
13- pub use header:: PageHeader ;
13+ pub use header:: { PageHeader , PAGE_HEADER_SIZE } ;
1414pub use packets:: { Packets , PacketsIter } ;
1515pub use paginate:: paginate;
1616
@@ -27,7 +27,6 @@ pub const CONTAINS_LAST_PAGE_OF_BITSTREAM: u8 = 0x04;
2727#[ derive( Clone , PartialEq , Eq , Debug ) ]
2828pub struct Page {
2929 content : Vec < u8 > ,
30- segments : Vec < u8 > ,
3130 header : PageHeader ,
3231 /// The position in the stream the page ended
3332 pub end : u64 ,
@@ -49,7 +48,10 @@ impl Page {
4948 /// NOTE: This will write the checksum as is. It is likely [`Page::gen_crc`] will have
5049 /// to be used prior.
5150 pub fn as_bytes ( & self ) -> Vec < u8 > {
52- let mut bytes = Vec :: with_capacity ( 27 + self . segments . len ( ) + self . content . len ( ) ) ;
51+ let segment_table = & self . header . segments ;
52+ let num_segments = segment_table. len ( ) ;
53+ let mut bytes =
54+ Vec :: with_capacity ( PAGE_HEADER_SIZE + num_segments + self . header . content_size ( ) ) ;
5355
5456 bytes. extend ( b"OggS" ) ;
5557 bytes. push ( 0 ) ; // Version
@@ -58,8 +60,8 @@ impl Page {
5860 bytes. extend ( self . header . stream_serial . to_le_bytes ( ) ) ;
5961 bytes. extend ( self . header . sequence_number . to_le_bytes ( ) ) ;
6062 bytes. extend ( self . header . checksum . to_le_bytes ( ) ) ;
61- bytes. push ( self . segments . len ( ) as u8 ) ;
62- bytes. extend ( & self . segments ) ;
63+ bytes. push ( num_segments as u8 ) ;
64+ bytes. extend ( segment_table ) ;
6365 bytes. extend ( self . content . iter ( ) ) ;
6466
6567 bytes
@@ -73,27 +75,19 @@ impl Page {
7375 ///
7476 /// * [`std::io::Error`]
7577 /// * [`PageError`]
76- pub fn read < V > ( data : & mut V , skip_content : bool ) -> Result < Self >
78+ pub fn read < V > ( data : & mut V ) -> Result < Self >
7779 where
7880 V : Read + Seek ,
7981 {
80- let ( header, segments ) = PageHeader :: read ( data) ?;
82+ let header = PageHeader :: read ( data) ?;
8183
82- let mut content: Vec < u8 > = Vec :: new ( ) ;
83- let content_len: u16 = segments. iter ( ) . map ( |& b| u16:: from ( b) ) . sum ( ) ;
84-
85- if skip_content {
86- data. seek ( SeekFrom :: Current ( i64:: from ( content_len) ) ) ?;
87- } else {
88- content = vec ! [ 0 ; content_len as usize ] ;
89- data. read_exact ( & mut content) ?;
90- }
84+ let mut content = vec ! [ 0 ; header. content_size( ) ] ;
85+ data. read_exact ( & mut content) ?;
9186
9287 let end = data. stream_position ( ) ?;
9388
9489 Ok ( Page {
9590 content,
96- segments,
9791 header,
9892 end,
9993 } )
@@ -131,13 +125,13 @@ impl Page {
131125
132126 let mut p = Page {
133127 content : content[ remaining..] . to_vec ( ) ,
134- segments : segment_table ( remaining) ,
135128 header : PageHeader {
136129 start : self . end ,
137130 header_type_flag : 1 ,
138131 abgp : 0 ,
139132 stream_serial : self . header . stream_serial ,
140133 sequence_number : self . header . sequence_number + 1 ,
134+ segments : segment_table ( remaining) ,
141135 checksum : 0 ,
142136 } ,
143137 end : self . header ( ) . start + content. len ( ) as u64 ,
@@ -201,21 +195,21 @@ mod tests {
201195 0x4F , 0x70 , 0x75 , 0x73 , 0x48 , 0x65 , 0x61 , 0x64 , 0x01 , 0x02 , 0x38 , 0x01 , 0x80 , 0xBB ,
202196 0 , 0 , 0 , 0 , 0 ,
203197 ] ,
204- segments : vec ! [ 19 ] ,
205198 header : PageHeader {
206199 start : 0 ,
207200 header_type_flag : 2 ,
208201 abgp : 0 ,
209202 stream_serial : 1759377061 ,
210203 sequence_number : 0 ,
204+ segments : vec ! [ 19 ] ,
211205 checksum : 3579522525 ,
212206 } ,
213207 end : 47 ,
214208 } ;
215209
216210 let content = std:: fs:: read ( "test_assets/opus_ident_header.page" ) . unwrap ( ) ;
217211
218- let page = Page :: read ( & mut Cursor :: new ( content) , false ) . unwrap ( ) ;
212+ let page = Page :: read ( & mut Cursor :: new ( content) ) . unwrap ( ) ;
219213
220214 assert_eq ! ( expected, page) ;
221215 }
0 commit comments