2019-08-23 11:41:30 +00:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
2019-08-22 12:03:43 +00:00
|
|
|
use bytes::BytesMut;
|
2019-05-14 08:05:29 +00:00
|
|
|
use failure::*;
|
2019-08-23 11:41:30 +00:00
|
|
|
use futures::ready;
|
|
|
|
use futures::stream::{Stream, TryStream};
|
2019-05-14 08:05:29 +00:00
|
|
|
|
2019-08-22 12:03:43 +00:00
|
|
|
use super::Chunker;
|
2019-05-22 07:39:02 +00:00
|
|
|
|
2019-05-18 08:46:29 +00:00
|
|
|
/// Split input stream into dynamic sized chunks
|
2019-08-23 11:41:30 +00:00
|
|
|
pub struct ChunkStream<S: Unpin> {
|
2019-05-14 08:05:29 +00:00
|
|
|
input: S,
|
|
|
|
chunker: Chunker,
|
2019-05-22 07:39:02 +00:00
|
|
|
buffer: BytesMut,
|
|
|
|
scan_pos: usize,
|
2019-05-14 08:05:29 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 11:41:30 +00:00
|
|
|
impl<S: Unpin> ChunkStream<S> {
|
2019-05-30 11:28:24 +00:00
|
|
|
pub fn new(input: S, chunk_size: Option<usize>) -> Self {
|
|
|
|
Self { input, chunker: Chunker::new(chunk_size.unwrap_or(4*1024*1024)), buffer: BytesMut::new(), scan_pos: 0}
|
2019-05-14 08:05:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 11:41:30 +00:00
|
|
|
impl<S: Unpin> Unpin for ChunkStream<S> {}
|
|
|
|
|
|
|
|
impl<S: Unpin> Stream for ChunkStream<S>
|
|
|
|
where
|
|
|
|
S: TryStream,
|
|
|
|
S::Ok: AsRef<[u8]>,
|
|
|
|
S::Error: Into<Error>,
|
2019-05-22 07:18:05 +00:00
|
|
|
{
|
2019-05-14 08:05:29 +00:00
|
|
|
|
2019-08-23 11:41:30 +00:00
|
|
|
type Item = Result<BytesMut, Error>;
|
2019-05-14 08:05:29 +00:00
|
|
|
|
2019-08-23 11:41:30 +00:00
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
|
|
|
let this = self.get_mut();
|
2019-05-14 08:05:29 +00:00
|
|
|
loop {
|
2019-08-23 11:41:30 +00:00
|
|
|
if this.scan_pos < this.buffer.len() {
|
|
|
|
let boundary = this.chunker.scan(&this.buffer[this.scan_pos..]);
|
2019-05-22 06:05:27 +00:00
|
|
|
|
2019-08-23 11:41:30 +00:00
|
|
|
let chunk_size = this.scan_pos + boundary;
|
2019-05-22 06:05:27 +00:00
|
|
|
|
|
|
|
if boundary == 0 {
|
2019-08-23 11:41:30 +00:00
|
|
|
this.scan_pos = this.buffer.len();
|
2019-05-22 06:05:27 +00:00
|
|
|
// continue poll
|
2019-08-23 11:41:30 +00:00
|
|
|
} else if chunk_size <= this.buffer.len() {
|
|
|
|
let result = this.buffer.split_to(chunk_size);
|
|
|
|
this.scan_pos = 0;
|
|
|
|
return Poll::Ready(Some(Ok(result)));
|
2019-05-22 06:05:27 +00:00
|
|
|
} else {
|
|
|
|
panic!("got unexpected chunk boundary from chunker");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 11:41:30 +00:00
|
|
|
match ready!(Pin::new(&mut this.input).try_poll_next(cx)) {
|
|
|
|
Some(Err(err)) => {
|
|
|
|
return Poll::Ready(Some(Err(err.into())));
|
2019-05-14 08:05:29 +00:00
|
|
|
}
|
2019-08-23 11:41:30 +00:00
|
|
|
None => {
|
|
|
|
this.scan_pos = 0;
|
|
|
|
if this.buffer.len() > 0 {
|
|
|
|
return Poll::Ready(Some(Ok(this.buffer.take())));
|
2019-05-19 09:05:56 +00:00
|
|
|
} else {
|
2019-08-23 11:41:30 +00:00
|
|
|
return Poll::Ready(None);
|
2019-05-19 09:05:56 +00:00
|
|
|
}
|
2019-05-14 08:05:29 +00:00
|
|
|
}
|
2019-08-23 11:41:30 +00:00
|
|
|
Some(Ok(data)) => {
|
|
|
|
this.buffer.extend_from_slice(data.as_ref());
|
|
|
|
}
|
2019-05-14 08:05:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-18 08:46:29 +00:00
|
|
|
|
|
|
|
/// Split input stream into fixed sized chunks
|
2019-08-23 11:41:30 +00:00
|
|
|
pub struct FixedChunkStream<S: Unpin> {
|
2019-05-18 08:46:29 +00:00
|
|
|
input: S,
|
|
|
|
chunk_size: usize,
|
2019-05-22 07:05:35 +00:00
|
|
|
buffer: BytesMut,
|
2019-05-18 08:46:29 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 11:41:30 +00:00
|
|
|
impl<S: Unpin> FixedChunkStream<S> {
|
2019-05-18 08:46:29 +00:00
|
|
|
pub fn new(input: S, chunk_size: usize) -> Self {
|
2019-05-22 07:05:35 +00:00
|
|
|
Self { input, chunk_size, buffer: BytesMut::new() }
|
2019-05-18 08:46:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 11:41:30 +00:00
|
|
|
impl<S: Unpin> Unpin for FixedChunkStream<S> {}
|
2019-05-18 08:46:29 +00:00
|
|
|
|
2019-08-23 11:41:30 +00:00
|
|
|
impl<S: Unpin> Stream for FixedChunkStream<S>
|
|
|
|
where
|
|
|
|
S: TryStream,
|
|
|
|
S::Ok: AsRef<[u8]>,
|
|
|
|
{
|
|
|
|
type Item = Result<BytesMut, S::Error>;
|
2019-05-18 08:46:29 +00:00
|
|
|
|
2019-08-23 11:41:30 +00:00
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Result<BytesMut, S::Error>>> {
|
|
|
|
let this = self.get_mut();
|
2019-05-18 08:46:29 +00:00
|
|
|
loop {
|
2019-08-23 11:41:30 +00:00
|
|
|
if this.buffer.len() == this.chunk_size {
|
|
|
|
return Poll::Ready(Some(Ok(this.buffer.take())));
|
|
|
|
} else if this.buffer.len() > this.chunk_size {
|
|
|
|
let result = this.buffer.split_to(this.chunk_size);
|
|
|
|
return Poll::Ready(Some(Ok(result)));
|
2019-05-22 07:05:35 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 11:41:30 +00:00
|
|
|
match ready!(Pin::new(&mut this.input).try_poll_next(cx)) {
|
|
|
|
Some(Err(err)) => {
|
|
|
|
return Poll::Ready(Some(Err(err)));
|
2019-05-18 08:46:29 +00:00
|
|
|
}
|
2019-08-23 11:41:30 +00:00
|
|
|
None => {
|
2019-05-18 08:46:29 +00:00
|
|
|
// last chunk can have any size
|
2019-08-23 11:41:30 +00:00
|
|
|
if this.buffer.len() > 0 {
|
|
|
|
return Poll::Ready(Some(Ok(this.buffer.take())));
|
2019-05-18 08:46:29 +00:00
|
|
|
} else {
|
2019-08-23 11:41:30 +00:00
|
|
|
return Poll::Ready(None);
|
2019-05-18 08:46:29 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-23 11:41:30 +00:00
|
|
|
Some(Ok(data)) => {
|
|
|
|
this.buffer.extend_from_slice(data.as_ref());
|
2019-05-22 07:05:35 +00:00
|
|
|
}
|
2019-05-18 08:46:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|