/*
 * 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
 */

package com.pullenti.unitext.internal.zip;

// Provides simple <see cref="Stream"/>" utilities.
public class StreamUtils {

    public static void readFully(com.pullenti.unisharp.Stream stream, byte[] buffer) throws IllegalArgumentException, IndexOutOfBoundsException, java.io.EOFException, java.io.IOException {
        readFullyEx(stream, buffer, 0, buffer.length);
    }

    public static void readFullyEx(com.pullenti.unisharp.Stream stream, byte[] buffer, int offset, int count) throws IllegalArgumentException, IndexOutOfBoundsException, java.io.EOFException, java.io.IOException {
        if (stream == null) 
            throw new IllegalArgumentException("stream");
        if (buffer == null) 
            throw new IllegalArgumentException("buffer");
        if (((offset < 0)) || (offset > buffer.length)) 
            throw new IndexOutOfBoundsException("offset");
        if (((count < 0)) || ((offset + count) > buffer.length)) 
            throw new IndexOutOfBoundsException("count");
        while (count > 0) {
            int readCount = stream.read(buffer, offset, count);
            if (readCount <= 0) 
                throw new java.io.EOFException();
            offset += readCount;
            count -= readCount;
        }
    }

    public static void copy(com.pullenti.unisharp.Stream source, com.pullenti.unisharp.Stream destination, byte[] buffer) throws IllegalArgumentException, IllegalArgumentException, java.io.IOException {
        if (source == null) 
            throw new IllegalArgumentException("source");
        if (destination == null) 
            throw new IllegalArgumentException("destination");
        if (buffer == null) 
            throw new IllegalArgumentException("buffer");
        if (buffer.length < 128) 
            throw new IllegalArgumentException("Buffer is too small" + " " + "buffer");
        boolean copying = true;
        while (copying) {
            int bytesRead = source.read(buffer, 0, buffer.length);
            if (bytesRead > 0) 
                destination.write(buffer, 0, bytesRead);
            else {
                destination.flush();
                copying = false;
            }
        }
    }

    private StreamUtils() {
    }
}
