43 lines
1.1 KiB
Java
43 lines
1.1 KiB
Java
package ch.fritteli.maze.generator.serialization;
|
|
|
|
import ch.fritteli.maze.generator.model.Maze;
|
|
import java.io.ByteArrayInputStream;
|
|
import lombok.NonNull;
|
|
|
|
public abstract class AbstractMazeInputStream extends ByteArrayInputStream {
|
|
|
|
public AbstractMazeInputStream(@NonNull final byte[] buf) {
|
|
super(buf);
|
|
}
|
|
|
|
public abstract void checkHeader();
|
|
|
|
@NonNull
|
|
public abstract Maze readMazeData();
|
|
|
|
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 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;
|
|
}
|
|
}
|