/*
 * 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.word;

public class ReadUtils {

    public static final int byteSize = 1;

    public static final int wordSize = 2;

    public static final int dWordSize = 4;

    public static byte readByte(com.pullenti.unisharp.Stream s) throws Exception, java.io.IOException {
        int b = s.read();
        if (b < 0) 
            throw new Exception("Unexpected EOF");
        return (byte)b;
    }

    public static byte readByteRef(com.pullenti.unisharp.Stream s, com.pullenti.unisharp.Outargwrapper<Integer> read) throws Exception, java.io.IOException {
        byte b = readByte(s);
        ++read.value;
        return b;
    }

    private static byte[] buf;

    public static int readInt(com.pullenti.unisharp.Stream s) throws java.io.IOException {
        int res = 0;
        int i = s.read(buf, 0, 4);
        if (i < 0) 
            return -1;
        if (i < 4) 
            return 0;
        for (i = 3; i >= 0; i--) {
            res = (res << 8) | (Byte.toUnsignedInt(buf[i]));
        }
        return res;
    }

    public static short readShort(com.pullenti.unisharp.Stream s) throws java.io.IOException {
        short res = (short)0;
        int i = s.read(buf, 0, 2);
        if (i < 2) 
            return (short)0;
        for (i = 1; i >= 0; i--) {
            res = (short)(((((int)res) << 8) | (Byte.toUnsignedInt(buf[i]))));
        }
        return res;
    }

    public static byte[] readExact(com.pullenti.unisharp.Stream s, int count) throws Exception, java.io.IOException {
        byte[] data = new byte[count];
        int read = s.read(data, 0, count);
        if (read != count) 
            throw new Exception("Unexpected EOF");
        return data;
    }

    public static byte[] readExactRef(com.pullenti.unisharp.Stream s, int count, com.pullenti.unisharp.Outargwrapper<Integer> read) throws Exception, java.io.IOException {
        byte[] data = readExact(s, count);
        read.value += data.length;
        return data;
    }

    public static void skip(com.pullenti.unisharp.Stream s, int count) throws java.io.IOException {
        s.seek((long)count, 1);
    }

    public ReadUtils() {
    }
    
    static {
        buf = new byte[32];
    }
}
