//
// Generated by dtk
// Translation Unit: JSUInputStream.cpp
//

#include "JSystem/JSystem.h" // IWYU pragma: keep

#include "JSystem/JSupport/JSUInputStream.h"
#include "JSystem/JSupport/JSURandomInputStream.h"
#include "dolphin/os/OS.h"

/* 802BF3F4-802BF47C       .text __dt__14JSUInputStreamFv */
JSUInputStream::~JSUInputStream() {
    if (!isGood())
        OSReport("JSUInputStream: occur error.\n");
}

/* 802BF47C-802BF4D4       .text read__14JSUInputStreamFPvl */
s32 JSUInputStream::read(void* buffer, s32 numBytes) {
    s32 bytesRead = readData(buffer, numBytes);
    if (bytesRead != numBytes) {
        setState(IOS_STATE_1);
    }
    return bytesRead;
}

/* 802BF4D4-802BF554       .text skip__14JSUInputStreamFl */
s32 JSUInputStream::skip(s32 count) {
    s32 skipCount = 0;
    u8 buffer[1];
    while (count > skipCount) {
        if (readData(&buffer, 1) != 1) {
            setState(IOS_STATE_1);
            break;
        }
        skipCount++;
    }
    return skipCount;
}

/* 802BF554-802BF5E0       .text align__20JSURandomInputStreamFl */
s32 JSURandomInputStream::align(s32 alignment) {
    s32 currentPos = getPosition();
    s32 offset = (alignment + currentPos);
    offset -= 1;
    offset &= ~(alignment - 1);
    s32 alignmentOffset = offset - currentPos;
    if (alignmentOffset != 0) {
        s32 seekLen = seekPos(offset, JSUStreamSeekFrom_SET);
        if (seekLen != alignmentOffset) {
            setState(IOS_STATE_1);
        }
    }
    return alignmentOffset;
}

/* 802BF5E0-802BF63C       .text skip__20JSURandomInputStreamFl */
s32 JSURandomInputStream::skip(s32 param_0) {
    s32 val = seekPos(param_0, JSUStreamSeekFrom_CUR);
    if (val != param_0) {
        setState(IOS_STATE_1);
    }
    return val;
}

/* 802BF63C-802BF6C0       .text peek__20JSURandomInputStreamFPvl */
s32 JSURandomInputStream::peek(void* buffer, s32 numBytes) {
    s32 oldPos = getPosition();
    s32 bytesRead = read(buffer, numBytes);
    if (bytesRead != 0) {
        seekPos(oldPos, JSUStreamSeekFrom_SET);
    }
    return bytesRead;
}

/* 802BF6C0-802BF704       .text seek__20JSURandomInputStreamFl17JSUStreamSeekFrom */
s32 JSURandomInputStream::seek(s32 param_0, JSUStreamSeekFrom param_1) {
    s32 seekResult = seekPos(param_0, param_1);
    clrState(IOS_STATE_1);
    return seekResult;
}
