meyfa/scratchlib

View on GitHub
src/main/java/scratchlib/objects/fixed/data/ScratchObjectByteArray.java

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
package scratchlib.objects.fixed.data;

import java.io.IOException;

import scratchlib.objects.IScratchReferenceType;
import scratchlib.objects.ScratchObject;
import scratchlib.objects.ScratchReferenceTable;
import scratchlib.project.ScratchProject;
import scratchlib.reader.ScratchInputStream;
import scratchlib.writer.ScratchOutputStream;


/**
 * Byte array reference type. The bytes are written exactly as specified.
 */
public class ScratchObjectByteArray extends ScratchObject implements IScratchReferenceType
{
    /**
     * Class ID in binary files.
     */
    public static final int CLASS_ID = 11;

    private byte[] value;

    /**
     * Empty constructor for yet-to-be-read instances.
     */
    public ScratchObjectByteArray()
    {
        super(CLASS_ID);
    }

    /**
     * @param value The bytes.
     */
    public ScratchObjectByteArray(byte[] value)
    {
        super(CLASS_ID);
        this.value = value;
    }

    /**
     * @return The bytes stored in this array.
     */
    public byte[] getValue()
    {
        return value;
    }

    @Override
    public void writeTo(ScratchOutputStream out, ScratchReferenceTable ref, ScratchProject project) throws IOException
    {
        super.writeTo(out, ref, project);

        out.write32bitUnsignedInt(value.length);
        out.write(value);
    }

    @Override
    public void readFrom(int id, ScratchInputStream in, ScratchProject project) throws IOException
    {
        super.readFrom(id, in, project);

        int length = in.read32bitUnsignedInt();
        this.value = in.readFully(length);
    }
}