下面列出了org.bukkit.util.io.BukkitObjectOutputStream#close ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static String toBase64(Inventory inventory) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
// Write the size of the inventory
dataOutput.writeInt(inventory.getSize());
// Save every element in the list
for (int i = 0; i < inventory.getSize(); i++) {
dataOutput.writeObject(inventory.getItem(i));
}
// Serialize that array
dataOutput.close();
return Base64Coder.encodeLines(outputStream.toByteArray());
} catch (Exception e) {
throw new IllegalStateException("Cannot into itemstacksz!", e);
}
}
public static String toBase64(Inventory inventory) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
// Write the size of the inventory
dataOutput.writeInt(inventory.getSize());
// Save every element in the list
for (int i = 0; i < inventory.getSize(); i++) {
dataOutput.writeObject(inventory.getItem(i));
}
// Serialize that array
dataOutput.close();
return Base64Coder.encodeLines(outputStream.toByteArray());
} catch (Exception e) {
throw new IllegalStateException("Cannot into itemstacksz!", e);
}
}
/**
*
* A method to serialize an {@link ItemStack} array to Base64 String.
*
* <p />
*
* Based off of {@link #toBase64(Inventory)}.
*
* @param items to turn into a Base64 String.
* @return Base64 string of the items.
* @throws IllegalStateException
*/
public static String itemStackArrayToBase64(ItemStack[] items){
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
// Write the size of the inventory
dataOutput.writeInt(items.length);
// Save every element in the list
for (int i = 0; i < items.length; i++) {
dataOutput.writeObject(items[i]);
}
// Serialize that array
dataOutput.close();
return Base64Coder.encodeLines(outputStream.toByteArray());
} catch (Exception e) {
throw new IllegalStateException("Unable to save item stacks.", e);
}
}
public static String InventoryToString(ItemStack[] items) throws IllegalStateException {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
dataOutput.writeInt(items.length);
for (ItemStack item : items) {
dataOutput.writeObject(item);
}
dataOutput.close();
return Base64Coder.encodeLines(outputStream.toByteArray());
} catch (Exception e) {
throw new IllegalStateException("Unable to save item stacks.", e);
}
}
public static String toBase64(Inventory inventory, int size, String target) {
try {
ByteArrayOutputStream finalOutputStream = new ByteArrayOutputStream();
ByteArrayOutputStream temporaryOutputStream = new ByteArrayOutputStream();
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(temporaryOutputStream);
int failedItems = 0;
// Write the size of the inventory
dataOutput.writeInt(size);
// Save every element in the list
for (int i = 0; i < inventory.getSize(); i++) {
try {
dataOutput.writeObject(inventory.getItem(i));
} catch (Exception ignored) {
failedItems++;
temporaryOutputStream.reset();
} finally {
if (temporaryOutputStream.size() == 0) {
dataOutput.writeObject(null);
}
finalOutputStream.write(temporaryOutputStream.toByteArray());
temporaryOutputStream.reset();
}
}
if (failedItems > 0) {
PlayerVaults.getInstance().getLogger().severe("Failed to save " + failedItems + " invalid items to vault " + target);
}
PlayerVaults.debug("Serialized " + inventory.getSize() + " items");
// Serialize that array
dataOutput.close();
return Base64Coder.encodeLines(finalOutputStream.toByteArray());
} catch (Exception e) {
throw new IllegalStateException("Cannot into itemstacksz!", e);
}
}