2013-04-17 19:55:21 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Readers and Writers for in-memory buffers
|
|
|
|
//!
|
2013-04-19 14:04:19 -05:00
|
|
|
//! # XXX
|
2013-04-17 19:55:21 -05:00
|
|
|
//!
|
|
|
|
//! * Should probably have something like this for strings.
|
|
|
|
//! * Should they implement Closable? Would take extra state.
|
|
|
|
|
2013-05-24 21:35:29 -05:00
|
|
|
use cmp::min;
|
2013-04-17 19:55:21 -05:00
|
|
|
use prelude::*;
|
|
|
|
use super::*;
|
2013-05-24 21:35:29 -05:00
|
|
|
use vec;
|
2013-04-17 19:55:21 -05:00
|
|
|
|
|
|
|
/// Writes to an owned, growable byte vector
|
|
|
|
pub struct MemWriter {
|
2013-10-21 19:11:42 -05:00
|
|
|
priv buf: ~[u8],
|
|
|
|
priv pos: uint,
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MemWriter {
|
2013-10-21 19:11:42 -05:00
|
|
|
pub fn new() -> MemWriter {
|
|
|
|
MemWriter { buf: vec::with_capacity(128), pos: 0 }
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Writer for MemWriter {
|
2013-04-20 19:25:00 -05:00
|
|
|
fn write(&mut self, buf: &[u8]) {
|
2013-10-21 19:11:42 -05:00
|
|
|
// Make sure the internal buffer is as least as big as where we
|
|
|
|
// currently are
|
|
|
|
let difference = self.pos as i64 - self.buf.len() as i64;
|
|
|
|
if difference > 0 {
|
|
|
|
self.buf.grow(difference as uint, &0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out what bytes will be used to overwrite what's currently
|
|
|
|
// there (left), and what will be appended on the end (right)
|
|
|
|
let cap = self.buf.len() - self.pos;
|
|
|
|
let (left, right) = if cap <= buf.len() {
|
|
|
|
(buf.slice_to(cap), buf.slice_from(cap))
|
|
|
|
} else {
|
|
|
|
(buf, &[])
|
|
|
|
};
|
|
|
|
|
|
|
|
// Do the necessary writes
|
|
|
|
if left.len() > 0 {
|
|
|
|
vec::bytes::copy_memory(self.buf.mut_slice_from(self.pos),
|
|
|
|
left, left.len());
|
|
|
|
}
|
|
|
|
if right.len() > 0 {
|
|
|
|
self.buf.push_all(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bump us forward
|
|
|
|
self.pos += buf.len();
|
2013-04-20 19:25:00 -05:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-04-19 16:58:21 -05:00
|
|
|
impl Seek for MemWriter {
|
2013-10-21 19:11:42 -05:00
|
|
|
fn tell(&self) -> u64 { self.pos as u64 }
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-10-21 19:11:42 -05:00
|
|
|
fn seek(&mut self, pos: i64, style: SeekStyle) {
|
|
|
|
match style {
|
|
|
|
SeekSet => { self.pos = pos as uint; }
|
|
|
|
SeekEnd => { self.pos = self.buf.len() + pos as uint; }
|
|
|
|
SeekCur => { self.pos += pos as uint; }
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
}
|
2013-10-21 19:11:42 -05:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-10-21 19:11:42 -05:00
|
|
|
impl Decorator<~[u8]> for MemWriter {
|
|
|
|
fn inner(self) -> ~[u8] { self.buf }
|
|
|
|
fn inner_ref<'a>(&'a self) -> &'a ~[u8] { &self.buf }
|
|
|
|
fn inner_mut_ref<'a>(&'a mut self) -> &'a mut ~[u8] { &mut self.buf }
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-04-19 14:04:19 -05:00
|
|
|
/// Reads from an owned byte vector
|
2013-04-17 19:55:21 -05:00
|
|
|
pub struct MemReader {
|
2013-09-26 23:22:22 -05:00
|
|
|
priv buf: ~[u8],
|
|
|
|
priv pos: uint
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MemReader {
|
|
|
|
pub fn new(buf: ~[u8]) -> MemReader {
|
|
|
|
MemReader {
|
|
|
|
buf: buf,
|
|
|
|
pos: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader for MemReader {
|
2013-04-20 19:25:00 -05:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
|
|
|
|
{ if self.eof() { return None; } }
|
|
|
|
|
|
|
|
let write_len = min(buf.len(), self.buf.len() - self.pos);
|
|
|
|
{
|
|
|
|
let input = self.buf.slice(self.pos, self.pos + write_len);
|
2013-06-27 04:48:50 -05:00
|
|
|
let output = buf.mut_slice(0, write_len);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(input.len(), output.len());
|
2013-04-20 19:25:00 -05:00
|
|
|
vec::bytes::copy_memory(output, input, write_len);
|
|
|
|
}
|
|
|
|
self.pos += write_len;
|
|
|
|
assert!(self.pos <= self.buf.len());
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-04-20 19:25:00 -05:00
|
|
|
return Some(write_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eof(&mut self) -> bool { self.pos == self.buf.len() }
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-04-19 16:58:21 -05:00
|
|
|
impl Seek for MemReader {
|
2013-04-20 19:25:00 -05:00
|
|
|
fn tell(&self) -> u64 { self.pos as u64 }
|
2013-10-21 15:08:31 -05:00
|
|
|
fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail!() }
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-11-13 13:17:58 -06:00
|
|
|
impl Buffer for MemReader {
|
|
|
|
fn fill<'a>(&'a mut self) -> &'a [u8] { self.buf.slice_from(self.pos) }
|
|
|
|
fn consume(&mut self, amt: uint) { self.pos += amt; }
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-11-13 13:17:58 -06:00
|
|
|
impl Decorator<~[u8]> for MemReader {
|
|
|
|
fn inner(self) -> ~[u8] { self.buf }
|
|
|
|
fn inner_ref<'a>(&'a self) -> &'a ~[u8] { &self.buf }
|
|
|
|
fn inner_mut_ref<'a>(&'a mut self) -> &'a mut ~[u8] { &mut self.buf }
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-04-19 14:04:19 -05:00
|
|
|
|
2013-04-17 19:55:21 -05:00
|
|
|
/// Writes to a fixed-size byte slice
|
2013-09-25 21:42:02 -05:00
|
|
|
pub struct BufWriter<'self> {
|
2013-09-26 23:22:22 -05:00
|
|
|
priv buf: &'self mut [u8],
|
|
|
|
priv pos: uint
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
2013-04-19 14:04:19 -05:00
|
|
|
|
2013-04-17 19:55:21 -05:00
|
|
|
impl<'self> BufWriter<'self> {
|
|
|
|
pub fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a> {
|
|
|
|
BufWriter {
|
|
|
|
buf: buf,
|
|
|
|
pos: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self> Writer for BufWriter<'self> {
|
2013-10-21 15:08:31 -05:00
|
|
|
fn write(&mut self, _buf: &[u8]) { fail!() }
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
fn flush(&mut self) { fail!() }
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-04-19 16:58:21 -05:00
|
|
|
impl<'self> Seek for BufWriter<'self> {
|
2013-10-21 15:08:31 -05:00
|
|
|
fn tell(&self) -> u64 { fail!() }
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail!() }
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-19 14:04:19 -05:00
|
|
|
/// Reads from a fixed-size byte slice
|
2013-09-25 21:42:02 -05:00
|
|
|
pub struct BufReader<'self> {
|
2013-09-26 23:22:22 -05:00
|
|
|
priv buf: &'self [u8],
|
|
|
|
priv pos: uint
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self> BufReader<'self> {
|
|
|
|
pub fn new<'a>(buf: &'a [u8]) -> BufReader<'a> {
|
|
|
|
BufReader {
|
|
|
|
buf: buf,
|
|
|
|
pos: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self> Reader for BufReader<'self> {
|
2013-09-02 13:23:57 -05:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
|
|
|
|
{ if self.eof() { return None; } }
|
|
|
|
|
|
|
|
let write_len = min(buf.len(), self.buf.len() - self.pos);
|
|
|
|
{
|
|
|
|
let input = self.buf.slice(self.pos, self.pos + write_len);
|
|
|
|
let output = buf.mut_slice(0, write_len);
|
|
|
|
assert_eq!(input.len(), output.len());
|
|
|
|
vec::bytes::copy_memory(output, input, write_len);
|
|
|
|
}
|
|
|
|
self.pos += write_len;
|
|
|
|
assert!(self.pos <= self.buf.len());
|
|
|
|
|
|
|
|
return Some(write_len);
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-09-02 13:23:57 -05:00
|
|
|
fn eof(&mut self) -> bool { self.pos == self.buf.len() }
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-04-19 16:58:21 -05:00
|
|
|
impl<'self> Seek for BufReader<'self> {
|
2013-09-02 13:23:57 -05:00
|
|
|
fn tell(&self) -> u64 { self.pos as u64 }
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail!() }
|
2013-04-20 19:25:00 -05:00
|
|
|
}
|
|
|
|
|
2013-11-13 13:17:58 -06:00
|
|
|
impl<'self> Buffer for BufReader<'self> {
|
|
|
|
fn fill<'a>(&'a mut self) -> &'a [u8] { self.buf.slice_from(self.pos) }
|
|
|
|
fn consume(&mut self, amt: uint) { self.pos += amt; }
|
|
|
|
}
|
|
|
|
|
2013-08-24 02:22:40 -05:00
|
|
|
///Calls a function with a MemWriter and returns
|
|
|
|
///the writer's stored vector.
|
|
|
|
pub fn with_mem_writer(writeFn:&fn(&mut MemWriter)) -> ~[u8] {
|
2013-09-26 23:22:22 -05:00
|
|
|
let mut writer = MemWriter::new();
|
|
|
|
writeFn(&mut writer);
|
|
|
|
writer.inner()
|
2013-08-24 02:22:40 -05:00
|
|
|
}
|
|
|
|
|
2013-04-20 19:25:00 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use prelude::*;
|
|
|
|
use super::*;
|
2013-11-11 00:46:32 -06:00
|
|
|
use io::*;
|
2013-04-20 19:25:00 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mem_writer() {
|
|
|
|
let mut writer = MemWriter::new();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(writer.tell(), 0);
|
2013-04-20 19:25:00 -05:00
|
|
|
writer.write([0]);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(writer.tell(), 1);
|
2013-04-20 19:25:00 -05:00
|
|
|
writer.write([1, 2, 3]);
|
|
|
|
writer.write([4, 5, 6, 7]);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(writer.tell(), 8);
|
2013-10-21 19:11:42 -05:00
|
|
|
assert_eq!(*writer.inner_ref(), ~[0, 1, 2, 3, 4, 5, 6, 7]);
|
|
|
|
|
|
|
|
writer.seek(0, SeekSet);
|
|
|
|
assert_eq!(writer.tell(), 0);
|
|
|
|
writer.write([3, 4]);
|
|
|
|
assert_eq!(*writer.inner_ref(), ~[3, 4, 2, 3, 4, 5, 6, 7]);
|
|
|
|
|
|
|
|
writer.seek(1, SeekCur);
|
|
|
|
writer.write([0, 1]);
|
|
|
|
assert_eq!(*writer.inner_ref(), ~[3, 4, 2, 0, 1, 5, 6, 7]);
|
|
|
|
|
|
|
|
writer.seek(-1, SeekEnd);
|
|
|
|
writer.write([1, 2]);
|
|
|
|
assert_eq!(*writer.inner_ref(), ~[3, 4, 2, 0, 1, 5, 6, 1, 2]);
|
|
|
|
|
|
|
|
writer.seek(1, SeekEnd);
|
|
|
|
writer.write([1]);
|
|
|
|
assert_eq!(*writer.inner_ref(), ~[3, 4, 2, 0, 1, 5, 6, 1, 2, 0, 1]);
|
2013-04-20 19:25:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mem_reader() {
|
|
|
|
let mut reader = MemReader::new(~[0, 1, 2, 3, 4, 5, 6, 7]);
|
|
|
|
let mut buf = [];
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(reader.read(buf), Some(0));
|
|
|
|
assert_eq!(reader.tell(), 0);
|
2013-04-20 19:25:00 -05:00
|
|
|
let mut buf = [0];
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(reader.read(buf), Some(1));
|
|
|
|
assert_eq!(reader.tell(), 1);
|
|
|
|
assert_eq!(buf, [0]);
|
2013-04-20 19:25:00 -05:00
|
|
|
let mut buf = [0, ..4];
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(reader.read(buf), Some(4));
|
|
|
|
assert_eq!(reader.tell(), 5);
|
|
|
|
assert_eq!(buf, [1, 2, 3, 4]);
|
|
|
|
assert_eq!(reader.read(buf), Some(3));
|
|
|
|
assert_eq!(buf.slice(0, 3), [5, 6, 7]);
|
2013-04-20 19:25:00 -05:00
|
|
|
assert!(reader.eof());
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(reader.read(buf), None);
|
2013-04-20 19:25:00 -05:00
|
|
|
assert!(reader.eof());
|
|
|
|
}
|
2013-09-02 13:23:57 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_buf_reader() {
|
|
|
|
let in_buf = ~[0, 1, 2, 3, 4, 5, 6, 7];
|
|
|
|
let mut reader = BufReader::new(in_buf);
|
|
|
|
let mut buf = [];
|
|
|
|
assert_eq!(reader.read(buf), Some(0));
|
|
|
|
assert_eq!(reader.tell(), 0);
|
|
|
|
let mut buf = [0];
|
|
|
|
assert_eq!(reader.read(buf), Some(1));
|
|
|
|
assert_eq!(reader.tell(), 1);
|
|
|
|
assert_eq!(buf, [0]);
|
|
|
|
let mut buf = [0, ..4];
|
|
|
|
assert_eq!(reader.read(buf), Some(4));
|
|
|
|
assert_eq!(reader.tell(), 5);
|
|
|
|
assert_eq!(buf, [1, 2, 3, 4]);
|
|
|
|
assert_eq!(reader.read(buf), Some(3));
|
|
|
|
assert_eq!(buf.slice(0, 3), [5, 6, 7]);
|
|
|
|
assert!(reader.eof());
|
|
|
|
assert_eq!(reader.read(buf), None);
|
|
|
|
assert!(reader.eof());
|
|
|
|
}
|
2013-08-24 02:22:40 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_with_mem_writer() {
|
|
|
|
let buf = with_mem_writer(|wr| wr.write([1,2,3,4,5,6,7]));
|
|
|
|
assert_eq!(buf, ~[1,2,3,4,5,6,7]);
|
|
|
|
}
|
2013-04-20 19:25:00 -05:00
|
|
|
}
|