mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2024-11-28 05:35:28 +01:00
Switched to a better base64 encoding and decoding method.
This commit is contained in:
parent
f791e2726f
commit
0597084c3d
@ -6,9 +6,8 @@ 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 java.math.BigInteger;
|
|
||||||
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||||
|
|
||||||
import com.comphenix.protocol.reflect.FuzzyReflection;
|
import com.comphenix.protocol.reflect.FuzzyReflection;
|
||||||
|
|
||||||
@ -50,21 +49,15 @@ public class StreamSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deserialize an item stack from a base-32 encoded string.
|
* Deserialize an item stack from a base-64 encoded string.
|
||||||
* @param input - base-32 encoded string.
|
* @param input - base-64 encoded string.
|
||||||
* @return A deserialized item stack.
|
* @return A deserialized item stack.
|
||||||
* @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(String input) throws IOException {
|
||||||
try {
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(input));
|
||||||
BigInteger base32 = new BigInteger(input, 32);
|
|
||||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(base32.toByteArray());
|
return deserializeItemStack(new DataInputStream(inputStream));
|
||||||
|
|
||||||
return deserializeItemStack(new DataInputStream(inputStream));
|
|
||||||
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
throw new IOException("Input is not valid base 32.", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -93,9 +86,9 @@ public class StreamSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Serialize an item stack as a base-32 encoded string.
|
* Serialize an item stack as a base-64 encoded string.
|
||||||
* @param stack - the item stack to serialize.
|
* @param stack - the item stack to serialize.
|
||||||
* @return A base-32 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.
|
||||||
*/
|
*/
|
||||||
public String serializeItemStack(ItemStack stack) throws IOException {
|
public String serializeItemStack(ItemStack stack) throws IOException {
|
||||||
@ -105,6 +98,6 @@ public class StreamSerializer {
|
|||||||
serializeItemStack(dataOutput, stack);
|
serializeItemStack(dataOutput, stack);
|
||||||
|
|
||||||
// Serialize that array
|
// Serialize that array
|
||||||
return new BigInteger(1, outputStream.toByteArray()).toString(32);
|
return Base64Coder.encodeLines(outputStream.toByteArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,8 @@ import java.io.ByteArrayOutputStream;
|
|||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.math.BigInteger;
|
|
||||||
|
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
||||||
|
|
||||||
import com.comphenix.protocol.wrappers.nbt.NbtBase;
|
import com.comphenix.protocol.wrappers.nbt.NbtBase;
|
||||||
import com.comphenix.protocol.wrappers.nbt.NbtCompound;
|
import com.comphenix.protocol.wrappers.nbt.NbtCompound;
|
||||||
@ -13,36 +14,28 @@ import com.comphenix.protocol.wrappers.nbt.NbtList;
|
|||||||
import com.comphenix.protocol.wrappers.nbt.NbtWrapper;
|
import com.comphenix.protocol.wrappers.nbt.NbtWrapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Serializes NBT to a base N (default 32) encoded string and back.
|
* Serializes NBT to a base-64 encoded string and back.
|
||||||
*
|
*
|
||||||
* @author Kristian
|
* @author Kristian
|
||||||
*/
|
*/
|
||||||
public class NbtTextSerializer {
|
public class NbtTextSerializer {
|
||||||
/**
|
|
||||||
* The default radix to use while converting to text.
|
|
||||||
*/
|
|
||||||
public static final int STANDARD_BASE = 32;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A default instance of this serializer.
|
* A default instance of this serializer.
|
||||||
*/
|
*/
|
||||||
public static final NbtTextSerializer DEFAULT = new NbtTextSerializer();
|
public static final NbtTextSerializer DEFAULT = new NbtTextSerializer();
|
||||||
|
|
||||||
private NbtBinarySerializer binarySerializer;
|
private NbtBinarySerializer binarySerializer;
|
||||||
private int baseRadix;
|
|
||||||
|
|
||||||
public NbtTextSerializer() {
|
public NbtTextSerializer() {
|
||||||
this(new NbtBinarySerializer(), STANDARD_BASE);
|
this(new NbtBinarySerializer());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a serializer with a custom binary serializer and base radix.
|
* Construct a serializer with a custom binary serializer.
|
||||||
* @param binary - binary serializer.
|
* @param binary - binary serializer.
|
||||||
* @param baseRadix - base radix in the range 2 - 32.
|
|
||||||
*/
|
*/
|
||||||
public NbtTextSerializer(NbtBinarySerializer binary, int baseRadix) {
|
public NbtTextSerializer(NbtBinarySerializer binary) {
|
||||||
this.binarySerializer = binary;
|
this.binarySerializer = binary;
|
||||||
this.baseRadix = baseRadix;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -54,17 +47,9 @@ public class NbtTextSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the base radix.
|
* Serialize a NBT tag to a base-64 encoded string.
|
||||||
* @return The base radix.
|
|
||||||
*/
|
|
||||||
public int getBaseRadix() {
|
|
||||||
return baseRadix;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Serialize a NBT tag to a String.
|
|
||||||
* @param value - the NBT tag to serialize.
|
* @param value - the NBT tag to serialize.
|
||||||
* @return The NBT tag in base N form.
|
* @return The NBT tag in base-64 form.
|
||||||
*/
|
*/
|
||||||
public <TType> String serialize(NbtBase<TType> value) {
|
public <TType> String serialize(NbtBase<TType> value) {
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
@ -73,30 +58,24 @@ public class NbtTextSerializer {
|
|||||||
binarySerializer.serialize(value, dataOutput);
|
binarySerializer.serialize(value, dataOutput);
|
||||||
|
|
||||||
// Serialize that array
|
// Serialize that array
|
||||||
return new BigInteger(1, outputStream.toByteArray()).toString(baseRadix);
|
return Base64Coder.encodeLines(outputStream.toByteArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deserialize a NBT tag from a base N encoded string.
|
* Deserialize a NBT tag from a base-64 encoded string.
|
||||||
* @param input - the base N string.
|
* @param input - the base-64 string.
|
||||||
* @return The NBT tag contained in the string.
|
* @return The NBT tag contained in the string.
|
||||||
* @throws IOException If we are unable to parse the input.
|
* @throws IOException If we are unable to parse the input.
|
||||||
*/
|
*/
|
||||||
public <TType> NbtWrapper<TType> deserialize(String input) throws IOException {
|
public <TType> NbtWrapper<TType> deserialize(String input) throws IOException {
|
||||||
try {
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(input));
|
||||||
BigInteger baseN = new BigInteger(input, baseRadix);
|
|
||||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(baseN.toByteArray());
|
return binarySerializer.deserialize(new DataInputStream(inputStream));
|
||||||
|
|
||||||
return binarySerializer.deserialize(new DataInputStream(inputStream));
|
|
||||||
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
throw new IOException("Input is not valid base " + baseRadix + ".", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deserialize a NBT compound from a base N encoded string.
|
* Deserialize a NBT compound from a base-64 encoded string.
|
||||||
* @param input - the base N string.
|
* @param input - the base-64 string.
|
||||||
* @return The NBT tag contained in the string.
|
* @return The NBT tag contained in the string.
|
||||||
* @throws IOException If we are unable to parse the input.
|
* @throws IOException If we are unable to parse the input.
|
||||||
*/
|
*/
|
||||||
@ -107,8 +86,8 @@ public class NbtTextSerializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deserialize a NBT list from a base N encoded string.
|
* Deserialize a NBT list from a base-64 encoded string.
|
||||||
* @param input - the base N string.
|
* @param input - the base-64 string.
|
||||||
* @return The NBT tag contained in the string.
|
* @return The NBT tag contained in the string.
|
||||||
* @throws IOException If we are unable to parse the input.
|
* @throws IOException If we are unable to parse the input.
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user