diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/StreamSerializer.java b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/StreamSerializer.java index ac4fe360..2a441149 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/utility/StreamSerializer.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/utility/StreamSerializer.java @@ -6,6 +6,9 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.lang.reflect.Method; + +import javax.annotation.Nonnull; + import org.bukkit.inventory.ItemStack; import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; @@ -28,10 +31,12 @@ public class StreamSerializer { * and {@link java.io.DataInputStream DataInputStream}. * * @param input - the target input stream. - * @return The resulting item stack. + * @return The resulting item stack, or NULL if the serialized item stack was NULL. * @throws IOException If the operation failed due to reflection or corrupt data. */ - public ItemStack deserializeItemStack(DataInputStream input) throws IOException { + public ItemStack deserializeItemStack(@Nonnull DataInputStream input) throws IOException { + if (input == null) + throw new IllegalArgumentException("Input stream cannot be NULL."); if (readItemMethod == null) readItemMethod = FuzzyReflection.fromClass(MinecraftReflection.getPacketClass()). getMethodByParameters("readPacket", @@ -51,10 +56,13 @@ public class StreamSerializer { /** * Deserialize an item stack from a base-64 encoded string. * @param input - base-64 encoded string. - * @return A deserialized item stack. + * @return A deserialized item stack, or NULL if the serialized ItemStack was also NULL. * @throws IOException If the operation failed due to reflection or corrupt data. */ - public ItemStack deserializeItemStack(String input) throws IOException { + public ItemStack deserializeItemStack(@Nonnull String input) throws IOException { + if (input == null) + throw new IllegalArgumentException("Input text cannot be NULL."); + ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(input)); return deserializeItemStack(new DataInputStream(inputStream)); @@ -65,12 +73,18 @@ public class StreamSerializer { *

* To supply a byte array, wrap it in a {@link java.io.ByteArrayOutputStream ByteArrayOutputStream} * and {@link java.io.DataOutputStream DataOutputStream}. + *

+ * Note: An ItemStack can be written to a stream even if it's NULL. * * @param output - the target output stream. - * @param stack - the item stack that will be written. + * @param stack - the item stack that will be written, or NULL to represent air/nothing. * @throws IOException If the operation fails due to reflection problems. */ - public void serializeItemStack(DataOutputStream output, ItemStack stack) throws IOException { + public void serializeItemStack(@Nonnull DataOutputStream output, ItemStack stack) throws IOException { + if (output == null) + throw new IllegalArgumentException("Output stream cannot be NULL."); + + // Get the NMS version of the ItemStack Object nmsItem = MinecraftReflection.getMinecraftItemStack(stack); if (writeItemMethod == null) @@ -87,7 +101,10 @@ public class StreamSerializer { /** * Serialize an item stack as a base-64 encoded string. - * @param stack - the item stack to serialize. + *

+ * Note: An ItemStack can be written to the serialized text even if it's NULL. + * + * @param stack - the item stack to serialize, or NULL to represent air/nothing. * @return A base-64 representation of the given item stack. * @throws IOException If the operation fails due to reflection problems. */