Improve BinaryReader & BinaryWriter

This commit is contained in:
TheMode 2021-04-06 18:31:30 +02:00
parent 017bbf8a60
commit 0bb8144d33
2 changed files with 20 additions and 13 deletions

View File

@ -152,7 +152,7 @@ public class BinaryReader extends InputStream {
}
public byte[] readRemainingBytes() {
return readBytes(buffer.readableBytes());
return readBytes(available());
}
public BlockPosition readBlockPosition() {
@ -201,13 +201,13 @@ public class BinaryReader extends InputStream {
}
/**
* Creates a new object from the given supplier and calls its {@link Readable#read(BinaryReader)} method with this reader
* Creates a new object from the given supplier and calls its {@link Readable#read(BinaryReader)} method with this reader.
*
* @param supplier supplier to create new instances of your object
* @param <T>
* @param <T> the readable object type
* @return the read object
*/
public <T extends Readable> T read(Supplier<T> supplier) {
public <T extends Readable> T read(@NotNull Supplier<@NotNull T> supplier) {
T result = supplier.get();
result.read(this);
return result;
@ -218,16 +218,16 @@ public class BinaryReader extends InputStream {
* their respective {@link Readable#read(BinaryReader)} methods.
*
* @param supplier supplier to create new instances of your object
* @param <T>
* @param <T> the readable object type
* @return the read objects
*/
public <T extends Readable> T[] readArray(Supplier<T> supplier) {
T[] result = (T[]) new Object[readVarInt()];
public <T extends Readable> @NotNull T[] readArray(@NotNull Supplier<@NotNull T> supplier) {
Readable[] result = new Readable[readVarInt()];
for (int i = 0; i < result.length; i++) {
result[i] = supplier.get();
result[i].read(this);
}
return result;
return (T[]) result;
}
public ByteBuf getBuffer() {

View File

@ -180,6 +180,7 @@ public class BinaryWriter extends OutputStream {
/**
* Writes a JsonMessage to the buffer.
* Simply a writeSizedString with message.toString()
*
* @param message
*/
public void writeJsonMessage(JsonMessage message) {
@ -277,20 +278,26 @@ public class BinaryWriter extends OutputStream {
/**
* Writes the given writeable object into this writer.
*
* @param writeable the object to write
*/
public void write(Writeable writeable) {
public void write(@NotNull Writeable writeable) {
writeable.write(this);
}
public void write(@NotNull BinaryWriter writer) {
this.buffer.writeBytes(writer.getBuffer());
}
/**
* Writes an array of writeable objects to this writer. Will prepend the binary stream with a var int to denote the
* length of the array.
*
* @param writeables the array of writeables to write
*/
public void writeArray(Writeable[] writeables) {
public void writeArray(@NotNull Writeable[] writeables) {
writeVarInt(writeables.length);
for(Writeable w : writeables) {
for (Writeable w : writeables) {
write(w);
}
}
@ -340,7 +347,7 @@ public class BinaryWriter extends OutputStream {
*
* @return the raw buffer
*/
public ByteBuf getBuffer() {
public @NotNull ByteBuf getBuffer() {
return buffer;
}
@ -365,7 +372,7 @@ public class BinaryWriter extends OutputStream {
/**
* Returns a byte[] with the contents written via BinaryWriter
*/
public static byte[] makeArray(Consumer<BinaryWriter> writing) {
public static byte[] makeArray(@NotNull Consumer<@NotNull BinaryWriter> writing) {
BinaryWriter writer = new BinaryWriter();
writing.accept(writer);
return writer.toByteArray();