/* * Copyright (C) 2016 eccentric_nz * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package me.eccentric_nz.TARDIS.arch; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import org.bukkit.inventory.ItemStack; import org.bukkit.util.io.BukkitObjectInputStream; import org.bukkit.util.io.BukkitObjectOutputStream; import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; /** * * @author eccentric_nz */ public class TARDISArchSerialization { public static String toDatabase(ItemStack[] inventory) { try { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream); // Write the size of the inventory dataOutput.writeInt(inventory.length); // Save every element in the list for (ItemStack is : inventory) { dataOutput.writeObject(is); } // Serialize that array dataOutput.close(); return Base64Coder.encodeLines(outputStream.toByteArray()); } catch (IOException e) { throw new IllegalStateException("Unable to save item stacks.", e); } } public static ItemStack[] fromDatabase(String data) throws IOException { try { ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data)); BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream); int size = dataInput.readInt(); ItemStack[] inventory = new ItemStack[size]; // Read the serialized inventory for (int i = 0; i < size; i++) { inventory[i] = (ItemStack) dataInput.readObject(); } dataInput.close(); return inventory; } catch (ClassNotFoundException e) { throw new IOException("Unable to decode class type.", e); } } }