From 24de8354407f9cb854b21437caca991f1222b912 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Tue, 1 Apr 2014 23:20:24 +0200 Subject: [PATCH] Add the ability to load or save NBT compounds from files. --- .../protocol/wrappers/nbt/NbtFactory.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java index e6ab163a..596da076 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/wrappers/nbt/NbtFactory.java @@ -17,10 +17,17 @@ package com.comphenix.protocol.wrappers.nbt; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; import java.lang.reflect.Method; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; import javax.annotation.Nonnull; @@ -32,6 +39,9 @@ import com.comphenix.protocol.reflect.FuzzyReflection; import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.utility.MinecraftReflection; import com.comphenix.protocol.wrappers.BukkitConverters; +import com.comphenix.protocol.wrappers.nbt.io.NbtBinarySerializer; +import com.google.common.base.Preconditions; +import com.google.common.io.Closeables; /** * Factory methods for creating NBT elements, lists and compounds. @@ -155,6 +165,56 @@ public class NbtFactory { return fromBase(result); } + /** + * Load a NBT compound from a GZIP compressed file. + * @param file - the source file. + * @return The compound. + * @throws IOException Unable to load file. + */ + public static NbtCompound fromFile(String file) throws IOException { + Preconditions.checkNotNull(file, "file cannot be NULL"); + FileInputStream stream = null; + DataInputStream input = null; + boolean swallow = true; + + try { + stream = new FileInputStream(file); + NbtCompound result = NbtBinarySerializer.DEFAULT. + deserializeCompound(input = new DataInputStream(new GZIPInputStream(stream))); + swallow = false; + return result; + } finally { + // Would be nice to avoid this, but alas - we have to use Java 6 + if (input != null) Closeables.close(input, swallow); + else if (stream != null) Closeables.close(stream, swallow); + } + } + + /** + * Save a NBT compound to a new compressed file, overwriting any existing files in the process. + * @param compound - the compound to save. + * @param file - the destination file. + * @throws IOException Unable to save compound. + */ + public static void toFile(NbtCompound compound, String file) throws IOException { + Preconditions.checkNotNull(compound, "compound cannot be NULL"); + Preconditions.checkNotNull(file, "file cannot be NULL"); + FileOutputStream stream = null; + DataOutputStream output = null; + boolean swallow = true; + + try { + stream = new FileOutputStream(file); + NbtBinarySerializer.DEFAULT. + serialize(compound, output = new DataOutputStream(new GZIPOutputStream(stream))); + swallow = false; + } finally { + // Note the order + if (output != null) Closeables.close(output, swallow); + else if (stream != null) Closeables.close(stream, swallow); + } + } + /** * Ensure that the given stack can store arbitrary NBT information. * @param stack - the stack to check.