/*
 * SDK Pullenti Unitext, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software. 
 * This class is generated using the converter UniSharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

const Utils = require("./../../../unisharp/Utils");
const Stream = require("./../../../unisharp/Stream");

// Provides simple <see cref="Stream"/>" utilities.
class StreamUtils {
    
    static readFully(stream, buffer) {
        StreamUtils.readFullyEx(stream, buffer, 0, buffer.length);
    }
    
    static readFullyEx(stream, buffer, offset, count) {
        if (stream === null) 
            throw new Error("stream");
        if (buffer === null) 
            throw new Error("buffer");
        if (((offset < 0)) || (offset > buffer.length)) 
            throw new Error("offset");
        if (((count < 0)) || ((offset + count) > buffer.length)) 
            throw new Error("count");
        while (count > 0) {
            let readCount = stream.read(buffer, offset, count);
            if (readCount <= 0) 
                throw new Error();
            offset += readCount;
            count -= readCount;
        }
    }
    
    static copy(source, destination, buffer) {
        if (source === null) 
            throw new Error("source");
        if (destination === null) 
            throw new Error("destination");
        if (buffer === null) 
            throw new Error("buffer");
        if (buffer.length < 128) 
            throw new Error("Buffer is too small", "buffer");
        let copying = true;
        while (copying) {
            let bytesRead = source.read(buffer, 0, buffer.length);
            if (bytesRead > 0) 
                destination.write(buffer, 0, bytesRead);
            else {
                destination.flush();
                copying = false;
            }
        }
    }
    
    constructor() {
        
    }
}


module.exports = StreamUtils