Add an option to read from enum

This commit is contained in:
Camotoy 2021-11-15 14:38:17 -05:00
parent 1188c1486f
commit 2a8d18a3f8
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
2 changed files with 72 additions and 47 deletions

View file

@ -13,7 +13,7 @@ public interface NetInput {
* @return The next boolean. * @return The next boolean.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public boolean readBoolean() throws IOException; boolean readBoolean() throws IOException;
/** /**
* Reads the next byte. * Reads the next byte.
@ -21,7 +21,7 @@ public interface NetInput {
* @return The next byte. * @return The next byte.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public byte readByte() throws IOException; byte readByte() throws IOException;
/** /**
* Reads the next unsigned byte. * Reads the next unsigned byte.
@ -29,7 +29,7 @@ public interface NetInput {
* @return The next unsigned byte. * @return The next unsigned byte.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public int readUnsignedByte() throws IOException; int readUnsignedByte() throws IOException;
/** /**
* Reads the next short. * Reads the next short.
@ -45,7 +45,7 @@ public interface NetInput {
* @return The next unsigned short. * @return The next unsigned short.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public int readUnsignedShort() throws IOException; int readUnsignedShort() throws IOException;
/** /**
* Reads the next char. * Reads the next char.
@ -53,7 +53,7 @@ public interface NetInput {
* @return The next char. * @return The next char.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public char readChar() throws IOException; char readChar() throws IOException;
/** /**
* Reads the next integer. * Reads the next integer.
@ -61,7 +61,7 @@ public interface NetInput {
* @return The next integer. * @return The next integer.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public int readInt() throws IOException; int readInt() throws IOException;
/** /**
* Reads the next varint. A varint is a form of integer where only necessary bytes are written. This is done to save bandwidth. * Reads the next varint. A varint is a form of integer where only necessary bytes are written. This is done to save bandwidth.
@ -69,7 +69,7 @@ public interface NetInput {
* @return The next varint. * @return The next varint.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public int readVarInt() throws IOException; int readVarInt() throws IOException;
/** /**
* Reads the next long. * Reads the next long.
@ -77,7 +77,7 @@ public interface NetInput {
* @return The next long. * @return The next long.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public long readLong() throws IOException; long readLong() throws IOException;
/** /**
* Reads the next varlong. A varlong is a form of long where only necessary bytes are written. This is done to save bandwidth. * Reads the next varlong. A varlong is a form of long where only necessary bytes are written. This is done to save bandwidth.
@ -85,7 +85,7 @@ public interface NetInput {
* @return The next varlong. * @return The next varlong.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public long readVarLong() throws IOException; long readVarLong() throws IOException;
/** /**
* Reads the next float. * Reads the next float.
@ -93,7 +93,7 @@ public interface NetInput {
* @return The next float. * @return The next float.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public float readFloat() throws IOException; float readFloat() throws IOException;
/** /**
* Reads the next double. * Reads the next double.
@ -101,7 +101,7 @@ public interface NetInput {
* @return The next double. * @return The next double.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public double readDouble() throws IOException; double readDouble() throws IOException;
/** /**
* Reads the next byte array. * Reads the next byte array.
@ -110,7 +110,7 @@ public interface NetInput {
* @return The next byte array. * @return The next byte array.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public byte[] readBytes(int length) throws IOException; byte[] readBytes(int length) throws IOException;
/** /**
* Reads as much data as possible into the given byte array. * Reads as much data as possible into the given byte array.
@ -119,7 +119,7 @@ public interface NetInput {
* @return The amount of bytes read, or -1 if no bytes could be read. * @return The amount of bytes read, or -1 if no bytes could be read.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public int readBytes(byte b[]) throws IOException; int readBytes(byte b[]) throws IOException;
/** /**
* Reads the given amount of bytes into the given array at the given offset. * Reads the given amount of bytes into the given array at the given offset.
@ -130,7 +130,7 @@ public interface NetInput {
* @return The amount of bytes read, or -1 if no bytes could be read. * @return The amount of bytes read, or -1 if no bytes could be read.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public int readBytes(byte b[], int offset, int length) throws IOException; int readBytes(byte b[], int offset, int length) throws IOException;
/** /**
* Reads the next short array. * Reads the next short array.
@ -139,7 +139,7 @@ public interface NetInput {
* @return The next short array. * @return The next short array.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public short[] readShorts(int length) throws IOException; short[] readShorts(int length) throws IOException;
/** /**
* Reads as much data as possible into the given short array. * Reads as much data as possible into the given short array.
@ -148,7 +148,7 @@ public interface NetInput {
* @return The amount of shorts read, or -1 if no shorts could be read. * @return The amount of shorts read, or -1 if no shorts could be read.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public int readShorts(short s[]) throws IOException; int readShorts(short s[]) throws IOException;
/** /**
* Reads the given amount of shorts into the given array at the given offset. * Reads the given amount of shorts into the given array at the given offset.
@ -159,7 +159,7 @@ public interface NetInput {
* @return The amount of shorts read, or -1 if no shorts could be read. * @return The amount of shorts read, or -1 if no shorts could be read.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public int readShorts(short s[], int offset, int length) throws IOException; int readShorts(short s[], int offset, int length) throws IOException;
/** /**
* Reads the next int array. * Reads the next int array.
@ -168,7 +168,7 @@ public interface NetInput {
* @return The next int array. * @return The next int array.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public int[] readInts(int length) throws IOException; int[] readInts(int length) throws IOException;
/** /**
* Reads as much data as possible into the given int array. * Reads as much data as possible into the given int array.
@ -177,7 +177,7 @@ public interface NetInput {
* @return The amount of ints read, or -1 if no ints could be read. * @return The amount of ints read, or -1 if no ints could be read.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public int readInts(int i[]) throws IOException; int readInts(int i[]) throws IOException;
/** /**
* Reads the given amount of ints into the given array at the given offset. * Reads the given amount of ints into the given array at the given offset.
@ -188,7 +188,7 @@ public interface NetInput {
* @return The amount of ints read, or -1 if no ints could be read. * @return The amount of ints read, or -1 if no ints could be read.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public int readInts(int i[], int offset, int length) throws IOException; int readInts(int i[], int offset, int length) throws IOException;
/** /**
* Reads the next long array. * Reads the next long array.
@ -197,7 +197,7 @@ public interface NetInput {
* @return The next long array. * @return The next long array.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public long[] readLongs(int length) throws IOException; long[] readLongs(int length) throws IOException;
/** /**
* Reads as much data as possible into the given long array. * Reads as much data as possible into the given long array.
@ -206,7 +206,7 @@ public interface NetInput {
* @return The amount of longs read, or -1 if no longs could be read. * @return The amount of longs read, or -1 if no longs could be read.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public int readLongs(long l[]) throws IOException; int readLongs(long l[]) throws IOException;
/** /**
* Reads the given amount of longs into the given array at the given offset. * Reads the given amount of longs into the given array at the given offset.
@ -217,7 +217,7 @@ public interface NetInput {
* @return The amount of longs read, or -1 if no longs could be read. * @return The amount of longs read, or -1 if no longs could be read.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public int readLongs(long l[], int offset, int length) throws IOException; int readLongs(long l[], int offset, int length) throws IOException;
/** /**
* Reads the next string. * Reads the next string.
@ -225,7 +225,7 @@ public interface NetInput {
* @return The next string. * @return The next string.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public String readString() throws IOException; String readString() throws IOException;
/** /**
* Reads the next UUID. * Reads the next UUID.
@ -233,7 +233,22 @@ public interface NetInput {
* @return The next UUID. * @return The next UUID.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public UUID readUUID() throws IOException; UUID readUUID() throws IOException;
/**
* Reads the next enum.
*
* @param values the collection of enums to read from
* @return the next enum.
* @throws IOException If an I/O error occurs.
*/
default <T extends Enum<T>> T readEnum(T[] values) throws IOException {
int index = readVarInt();
if (index >= values.length) {
throw new IndexOutOfBoundsException("Enum class " + values.getClass() + " does not have a value with index " + index);
}
return values[index];
}
/** /**
* Gets the number of available bytes. * Gets the number of available bytes.
@ -241,5 +256,5 @@ public interface NetInput {
* @return The number of available bytes. * @return The number of available bytes.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public int available() throws IOException; int available() throws IOException;
} }

View file

@ -13,7 +13,7 @@ public interface NetOutput {
* @param b Boolean to write. * @param b Boolean to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeBoolean(boolean b) throws IOException; void writeBoolean(boolean b) throws IOException;
/** /**
* Writes a byte. * Writes a byte.
@ -21,7 +21,7 @@ public interface NetOutput {
* @param b Byte to write. * @param b Byte to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeByte(int b) throws IOException; void writeByte(int b) throws IOException;
/** /**
* Writes a short. * Writes a short.
@ -29,7 +29,7 @@ public interface NetOutput {
* @param s Short to write. * @param s Short to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeShort(int s) throws IOException; void writeShort(int s) throws IOException;
/** /**
* Writes a char. * Writes a char.
@ -37,7 +37,7 @@ public interface NetOutput {
* @param c Char to write. * @param c Char to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeChar(int c) throws IOException; void writeChar(int c) throws IOException;
/** /**
* Writes a integer. * Writes a integer.
@ -45,7 +45,7 @@ public interface NetOutput {
* @param i Integer to write. * @param i Integer to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeInt(int i) throws IOException; void writeInt(int i) throws IOException;
/** /**
* Writes a varint. A varint is a form of integer where only necessary bytes are written. This is done to save bandwidth. * Writes a varint. A varint is a form of integer where only necessary bytes are written. This is done to save bandwidth.
@ -53,7 +53,7 @@ public interface NetOutput {
* @param i Varint to write. * @param i Varint to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeVarInt(int i) throws IOException; void writeVarInt(int i) throws IOException;
/** /**
* Writes a long. * Writes a long.
@ -61,7 +61,7 @@ public interface NetOutput {
* @param l Long to write. * @param l Long to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeLong(long l) throws IOException; void writeLong(long l) throws IOException;
/** /**
* Writes a varlong. A varlong is a form of long where only necessary bytes are written. This is done to save bandwidth. * Writes a varlong. A varlong is a form of long where only necessary bytes are written. This is done to save bandwidth.
@ -69,7 +69,7 @@ public interface NetOutput {
* @param l Varlong to write. * @param l Varlong to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeVarLong(long l) throws IOException; void writeVarLong(long l) throws IOException;
/** /**
* Writes a float. * Writes a float.
@ -77,7 +77,7 @@ public interface NetOutput {
* @param f Float to write. * @param f Float to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeFloat(float f) throws IOException; void writeFloat(float f) throws IOException;
/** /**
* Writes a double. * Writes a double.
@ -85,7 +85,7 @@ public interface NetOutput {
* @param d Double to write. * @param d Double to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeDouble(double d) throws IOException; void writeDouble(double d) throws IOException;
/** /**
* Writes a byte array. * Writes a byte array.
@ -93,7 +93,7 @@ public interface NetOutput {
* @param b Byte array to write. * @param b Byte array to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeBytes(byte b[]) throws IOException; void writeBytes(byte b[]) throws IOException;
/** /**
* Writes a byte array, using the given amount of bytes. * Writes a byte array, using the given amount of bytes.
@ -102,7 +102,7 @@ public interface NetOutput {
* @param length Bytes to write. * @param length Bytes to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeBytes(byte b[], int length) throws IOException; void writeBytes(byte b[], int length) throws IOException;
/** /**
* Writes a short array. * Writes a short array.
@ -110,7 +110,7 @@ public interface NetOutput {
* @param s Short array to write. * @param s Short array to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeShorts(short s[]) throws IOException; void writeShorts(short s[]) throws IOException;
/** /**
* Writes a short array, using the given amount of bytes. * Writes a short array, using the given amount of bytes.
@ -119,7 +119,7 @@ public interface NetOutput {
* @param length Shorts to write. * @param length Shorts to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeShorts(short s[], int length) throws IOException; void writeShorts(short s[], int length) throws IOException;
/** /**
* Writes an int array. * Writes an int array.
@ -127,7 +127,7 @@ public interface NetOutput {
* @param i Int array to write. * @param i Int array to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeInts(int i[]) throws IOException; void writeInts(int i[]) throws IOException;
/** /**
* Writes an int array, using the given amount of bytes. * Writes an int array, using the given amount of bytes.
@ -136,7 +136,7 @@ public interface NetOutput {
* @param length Ints to write. * @param length Ints to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeInts(int i[], int length) throws IOException; void writeInts(int i[], int length) throws IOException;
/** /**
* Writes a long array. * Writes a long array.
@ -144,7 +144,7 @@ public interface NetOutput {
* @param l Long array to write. * @param l Long array to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeLongs(long l[]) throws IOException; void writeLongs(long l[]) throws IOException;
/** /**
* Writes a long array, using the given amount of bytes. * Writes a long array, using the given amount of bytes.
@ -153,7 +153,7 @@ public interface NetOutput {
* @param length Longs to write. * @param length Longs to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeLongs(long l[], int length) throws IOException; void writeLongs(long l[], int length) throws IOException;
/** /**
* Writes a string. * Writes a string.
@ -161,7 +161,7 @@ public interface NetOutput {
* @param s String to write. * @param s String to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeString(String s) throws IOException; void writeString(String s) throws IOException;
/** /**
* Writes a UUID. * Writes a UUID.
@ -169,12 +169,22 @@ public interface NetOutput {
* @param uuid UUID to write. * @param uuid UUID to write.
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void writeUUID(UUID uuid) throws IOException; void writeUUID(UUID uuid) throws IOException;
/**
* Writes an enum.
*
* @param e Enum to write.
* @throws IOException
*/
default void writeEnum(Enum<?> e) throws IOException {
writeVarInt(e.ordinal());
}
/** /**
* Flushes the output. * Flushes the output.
* *
* @throws java.io.IOException If an I/O error occurs. * @throws java.io.IOException If an I/O error occurs.
*/ */
public void flush() throws IOException; void flush() throws IOException;
} }