All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.fritteli.maze.generator.serialization.AbstractMazeInputStream Maven / Gradle / Ivy

package ch.fritteli.maze.generator.serialization;

import ch.fritteli.maze.generator.model.Maze;
import org.jetbrains.annotations.NotNull;

import java.io.ByteArrayInputStream;
import java.io.IOException;

public abstract class AbstractMazeInputStream extends ByteArrayInputStream {

    public AbstractMazeInputStream(@NotNull final byte[] buf) {
        super(buf);
    }

    public abstract void checkHeader();

    @NotNull
    public abstract Maze readMazeData() throws IOException;

    public byte readByte() {
        final int read = this.read();
        if (read == -1) {
            // end of stream reached
            throw new ArrayIndexOutOfBoundsException("End of stream reached. Cannot read more bytes.");
        }
        return (byte) read;
    }

    public int readByteAsInt() {
        return 0xff & this.readByte();
    }

    public int readInt() {
        int result = 0;
        result |= (0xff & this.readByte()) << 24;
        result |= (0xff & this.readByte()) << 16;
        result |= (0xff & this.readByte()) << 8;
        result |= 0xff & this.readByte();
        return result;
    }

    public long readLong() {
        long result = 0;
        result |= ((long) this.readInt()) << 32;
        result |= 0xffffffffL & this.readInt();
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy