Archiviert
13
0

Make people aware of NULLs in the StreamSerializer class.

Dieser Commit ist enthalten in:
Kristian S. Stangeland 2013-02-01 01:43:40 +01:00
Ursprung c7caf16a94
Commit ab5bc1fd25

Datei anzeigen

@ -6,6 +6,9 @@ import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import javax.annotation.Nonnull;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
@ -28,10 +31,12 @@ public class StreamSerializer {
* and {@link java.io.DataInputStream DataInputStream}. * and {@link java.io.DataInputStream DataInputStream}.
* *
* @param input - the target input stream. * @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. * @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) if (readItemMethod == null)
readItemMethod = FuzzyReflection.fromClass(MinecraftReflection.getPacketClass()). readItemMethod = FuzzyReflection.fromClass(MinecraftReflection.getPacketClass()).
getMethodByParameters("readPacket", getMethodByParameters("readPacket",
@ -51,10 +56,13 @@ public class StreamSerializer {
/** /**
* Deserialize an item stack from a base-64 encoded string. * Deserialize an item stack from a base-64 encoded string.
* @param input - 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. * @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)); ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(input));
return deserializeItemStack(new DataInputStream(inputStream)); return deserializeItemStack(new DataInputStream(inputStream));
@ -65,12 +73,18 @@ public class StreamSerializer {
* <p> * <p>
* To supply a byte array, wrap it in a {@link java.io.ByteArrayOutputStream ByteArrayOutputStream} * To supply a byte array, wrap it in a {@link java.io.ByteArrayOutputStream ByteArrayOutputStream}
* and {@link java.io.DataOutputStream DataOutputStream}. * and {@link java.io.DataOutputStream DataOutputStream}.
* <p>
* Note: An ItemStack can be written to a stream even if it's NULL.
* *
* @param output - the target output stream. * @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. * @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); Object nmsItem = MinecraftReflection.getMinecraftItemStack(stack);
if (writeItemMethod == null) if (writeItemMethod == null)
@ -87,7 +101,10 @@ public class StreamSerializer {
/** /**
* Serialize an item stack as a base-64 encoded string. * Serialize an item stack as a base-64 encoded string.
* @param stack - the item stack to serialize. * <p>
* 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. * @return A base-64 representation of the given item stack.
* @throws IOException If the operation fails due to reflection problems. * @throws IOException If the operation fails due to reflection problems.
*/ */