33import org .bukkit .craftbukkit .util .CraftMagicNumbers ;
44import org .bukkit .inventory .ItemStack ;
55import org .jspecify .annotations .Nullable ;
6- import xyz .xenondevs .invui .InvUI ;
76import xyz .xenondevs .invui .internal .util .DataUtils ;
87import xyz .xenondevs .invui .util .ItemUtils ;
98
109import java .io .*;
1110import java .util .Arrays ;
1211import java .util .BitSet ;
1312import java .util .UUID ;
14- import java .util .logging .Level ;
1513import java .util .zip .GZIPInputStream ;
1614import java .util .zip .GZIPOutputStream ;
1715
@@ -174,56 +172,58 @@ public VirtualInventory(VirtualInventory inventory) {
174172 * @return The deserialized {@link VirtualInventory}.
175173 */
176174 public static VirtualInventory deserialize (byte [] bytes ) {
177- return deserialize (new ByteArrayInputStream (bytes ));
175+ try {
176+ return deserialize (new ByteArrayInputStream (bytes ));
177+ } catch (IOException e ) {
178+ // ByteArrayInputStream should not throw IOException
179+ throw new RuntimeException (e );
180+ }
178181 }
179182
180183 /**
181184 * Deserializes a {@link VirtualInventory} from an {@link InputStream}.
182185 *
183186 * @param in The {@link InputStream} to deserialize from.
184187 * @return The deserialized {@link VirtualInventory}.
188+ * @throws IOException If an I/O error has occurred.
185189 */
186- public static VirtualInventory deserialize (InputStream in ) {
187- try {
188- DataInputStream din = new DataInputStream (in );
189- UUID uuid = new UUID (din .readLong (), din .readLong ());
190- @ Nullable ItemStack [] items ;
191-
192- byte id = din .readByte (); // id, pre v1.0: 3, v1.0: 4, v2.0: 5
193- switch (id ) {
194- case 3 , 4 -> {
195- if (id == 3 ) {
196- // stack sizes are no longer serialized
197- DataUtils .readByteArray (din );
198- }
199-
200- items = Arrays .stream (DataUtils .read2DByteArray (din ))
201- .map (data -> data .length != 0 ? ItemStack .deserializeBytes (data ) : null )
202- .toArray (ItemStack []::new );
190+ public static VirtualInventory deserialize (InputStream in ) throws IOException {
191+ DataInputStream din = new DataInputStream (in );
192+ UUID uuid = new UUID (din .readLong (), din .readLong ());
193+ @ Nullable ItemStack [] items ;
194+
195+ byte id = din .readByte (); // id, pre v1.0: 3, v1.0: 4, v2.0: 5
196+ switch (id ) {
197+ case 3 , 4 -> {
198+ if (id == 3 ) {
199+ // stack sizes are no longer serialized
200+ DataUtils .readByteArray (din );
203201 }
204202
205- case 5 -> {
206- int dataVersion = din .readInt ();
207- int size = din .readInt ();
208- var itemsMask = BitSet .valueOf (din .readNBytes ((size + 7 ) / 8 )); // ceil(size / 8)
209- var itemsIn = new BufferedInputStream (new GZIPInputStream (din ));
203+ items = Arrays .stream (DataUtils .read2DByteArray (din ))
204+ .map (data -> data .length != 0 ? ItemStack .deserializeBytes (data ) : null )
205+ .toArray (ItemStack []::new );
206+ }
207+
208+ case 5 -> {
209+ int dataVersion = din .readInt ();
210+ int size = din .readInt ();
211+ var itemsMask = BitSet .valueOf (din .readNBytes ((size + 7 ) / 8 )); // ceil(size / 8)
212+ var itemsIn = new BufferedInputStream (new GZIPInputStream (din ));
213+
214+ items = new ItemStack [size ];
215+ for (int i = 0 ; i < size ; i ++) {
216+ if (!itemsMask .get (i ))
217+ continue ;
210218
211- items = new ItemStack [size ];
212- for (int i = 0 ; i < size ; i ++) {
213- if (!itemsMask .get (i ))
214- continue ;
215-
216- items [i ] = DataUtils .deserializeItemStack (dataVersion , itemsIn );
217- }
219+ items [i ] = DataUtils .deserializeItemStack (dataVersion , itemsIn );
218220 }
219-
220- default -> throw new UnsupportedOperationException ("Unsupported VirtualInventory version: " + id );
221221 }
222222
223- return new VirtualInventory (uuid , items );
224- } catch (IOException e ) {
225- throw new RuntimeException ("Failed to deserialize VirtualInventory" , e );
223+ default -> throw new UnsupportedOperationException ("Unsupported VirtualInventory version: " + id );
226224 }
225+
226+ return new VirtualInventory (uuid , items );
227227 }
228228
229229 /**
@@ -234,9 +234,14 @@ public static VirtualInventory deserialize(InputStream in) {
234234 * @return The serialized data.
235235 */
236236 public byte [] serialize () {
237- ByteArrayOutputStream out = new ByteArrayOutputStream ();
238- serialize (out );
239- return out .toByteArray ();
237+ try {
238+ ByteArrayOutputStream out = new ByteArrayOutputStream ();
239+ serialize (out );
240+ return out .toByteArray ();
241+ } catch (IOException e ) {
242+ // ByteArrayOutputStream should not throw IOException
243+ throw new RuntimeException (e );
244+ }
240245 }
241246
242247 /**
@@ -245,38 +250,35 @@ public byte[] serialize() {
245250 * This method only serializes the {@link UUID} and {@link ItemStack ItemStacks}.
246251 *
247252 * @param out The {@link OutputStream} to write serialized data to.
253+ * @throws IOException If an I/O error has occurred.
248254 */
249- public void serialize (OutputStream out ) {
250- try {
251- var dos = new DataOutputStream (out );
252- dos .writeLong (uuid .getMostSignificantBits ());
253- dos .writeLong (uuid .getLeastSignificantBits ());
254- dos .writeByte ((byte ) 5 ); // id, pre v1.0: 3, v1.0: 4, v2.0: 5
255-
256- dos .writeInt (CraftMagicNumbers .INSTANCE .getDataVersion ());
257- dos .writeInt (items .length );
258-
259- var itemMask = new BitSet (items .length );
260- var itemsBin = new ByteArrayOutputStream ();
261- var itemsOut = new BufferedOutputStream (new GZIPOutputStream (itemsBin ));
262-
263- for (int i = 0 ; i < items .length ; i ++) {
264- var itemStack = items [i ];
265- if (ItemUtils .isEmpty (itemStack ))
266- continue ;
267-
268- itemMask .set (i , true );
269- DataUtils .serializeItemStack (itemStack , itemsOut );
270- }
271-
272- itemsOut .close ();
273- dos .write (Arrays .copyOf (itemMask .toByteArray (), (items .length + 7 ) / 8 )); // ceil(size / 8)
274- dos .write (itemsBin .toByteArray ());
255+ public void serialize (OutputStream out ) throws IOException {
256+ var dos = new DataOutputStream (out );
257+ dos .writeLong (uuid .getMostSignificantBits ());
258+ dos .writeLong (uuid .getLeastSignificantBits ());
259+ dos .writeByte ((byte ) 5 ); // id, pre v1.0: 3, v1.0: 4, v2.0: 5
260+
261+ dos .writeInt (CraftMagicNumbers .INSTANCE .getDataVersion ());
262+ dos .writeInt (items .length );
263+
264+ var itemMask = new BitSet (items .length );
265+ var itemsBin = new ByteArrayOutputStream ();
266+ var itemsOut = new BufferedOutputStream (new GZIPOutputStream (itemsBin ));
267+
268+ for (int i = 0 ; i < items .length ; i ++) {
269+ var itemStack = items [i ];
270+ if (ItemUtils .isEmpty (itemStack ))
271+ continue ;
275272
276- dos .flush ();
277- } catch (IOException e ) {
278- InvUI .getInstance ().getLogger ().log (Level .SEVERE , "Failed to serialize VirtualInventory" , e );
273+ itemMask .set (i , true );
274+ DataUtils .serializeItemStack (itemStack , itemsOut );
279275 }
276+
277+ itemsOut .close ();
278+ dos .write (Arrays .copyOf (itemMask .toByteArray (), (items .length + 7 ) / 8 )); // ceil(size / 8)
279+ dos .write (itemsBin .toByteArray ());
280+
281+ dos .flush ();
280282 }
281283
282284 /**
0 commit comments