/*jslint node: true */
"use strict";

import stream = require('stream');
class WriteStream extends stream.Writable {

    private buffer;
    constructor() {
        super();
    }

    _write(chunk, encoding, done) {
        if (!this.buffer) {
            this.buffer = chunk;
        } else {
            this.buffer = Buffer.concat([this.buffer, chunk]);
        }
        done();
    }

    private toUint8Array(buffer) {
        let ab = new ArrayBuffer(buffer.length);
        let view = new Uint8Array(ab);
        for (let i = 0; i < buffer.length; ++i) {
            view[i] = buffer[i];
        }
        return view;
    }

    getData(): Uint8Array {
        let uint8data;
        if ( this.buffer ) { 
            uint8data = this.toUint8Array(this.buffer);
        } else {
            uint8data = this.toUint8Array([]);
        }
        return uint8data;
    }
}

class ReadStream extends stream.Readable {
    constructor(private data) {
        super();
    }

    _read() {
        this.push(this.data);
        this.push(null);
    }
}

export = {
    write: WriteStream,
    read: ReadStream
};

