Java Code Examples for cn.nukkit.utils.Zlib#deflate()
The following examples show how to use
cn.nukkit.utils.Zlib#deflate() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompressBatchedPacket.java From Jupiter with GNU General Public License v3.0 | 5 votes |
@Override public void onRun() { try { this.finalData = Zlib.deflate(data, level); this.data = null; } catch (Exception e) { //ignore } }
Example 2
Source File: CompressBatchedTask.java From Jupiter with GNU General Public License v3.0 | 5 votes |
@Override public void onRun() { try { this.finalData = Zlib.deflate(this.data, this.level); this.data = null; } catch (Exception e) { //ignore } }
Example 3
Source File: DataPacket.java From Nukkit with GNU General Public License v3.0 | 5 votes |
public BatchPacket compress(int level) { BatchPacket batch = new BatchPacket(); byte[][] batchPayload = new byte[2][]; byte[] buf = getBuffer(); batchPayload[0] = Binary.writeUnsignedVarInt(buf.length); batchPayload[1] = buf; byte[] data = Binary.appendBytes(batchPayload); try { batch.payload = Zlib.deflate(data, level); } catch (Exception e) { throw new RuntimeException(e); } return batch; }
Example 4
Source File: CompressBatchedPacket.java From Nukkit with GNU General Public License v3.0 | 5 votes |
@Override public void onRun() { try { this.finalData = Zlib.deflate(data, level); this.data = null; } catch (Exception e) { //ignore } }
Example 5
Source File: CompressBatchedTask.java From Nukkit with GNU General Public License v3.0 | 5 votes |
@Override public void onRun() { try { this.finalData = Zlib.deflate(this.data, this.level); this.data = null; } catch (Exception e) { //ignore } }
Example 6
Source File: ZlibTest.java From Nukkit with GNU General Public License v3.0 | 5 votes |
@DisplayName("Inflate and Deflate") @Test void testAll() throws Exception { byte[] in = "lmlstarqaq".getBytes(); byte[] compressed = Zlib.deflate(in); byte[] out = Zlib.inflate(compressed); assertArrayEquals(in, out); }
Example 7
Source File: DataPacket.java From Nukkit with GNU General Public License v3.0 | 5 votes |
public BatchPacket compress(int level) { BatchPacket batch = new BatchPacket(); byte[][] batchPayload = new byte[2][]; byte[] buf = getBuffer(); batchPayload[0] = Binary.writeUnsignedVarInt(buf.length); batchPayload[1] = buf; byte[] data = Binary.appendBytes(batchPayload); try { batch.payload = Zlib.deflate(data, level); } catch (Exception e) { throw new RuntimeException(e); } return batch; }
Example 8
Source File: CompressBatchedPacket.java From Nukkit with GNU General Public License v3.0 | 5 votes |
@Override public void onRun() { try { this.finalData = Zlib.deflate(data, level); this.data = null; } catch (Exception e) { //ignore } }
Example 9
Source File: CompressBatchedTask.java From Nukkit with GNU General Public License v3.0 | 5 votes |
@Override public void onRun() { try { this.finalData = Zlib.deflate(this.data, this.level); this.data = null; } catch (Exception e) { //ignore } }
Example 10
Source File: ChunkSection.java From Nukkit with GNU General Public License v3.0 | 5 votes |
public boolean compress() { if (!palette.compress()) { if (blockLight != null) { byte[] arr1 = blockLight; hasBlockLight = !Utils.isByteArrayEmpty(arr1); byte[] arr2; if (skyLight != null) { arr2 = skyLight; hasSkyLight = !Utils.isByteArrayEmpty(skyLight); } else { arr2 = EmptyChunkSection.EMPTY_LIGHT_ARR; hasSkyLight = false; } blockLight = null; skyLight = null; if (hasBlockLight && hasSkyLight) { try { compressedLight = Zlib.deflate(Binary.appendBytes(arr1, arr2), 1); } catch (Exception e) { e.printStackTrace(); } } return true; } return false; } return true; }
Example 11
Source File: ZlibTest.java From Nukkit with GNU General Public License v3.0 | 5 votes |
@DisplayName("Inflate and Deflate") @Test void testAll() throws Exception { byte[] in = "lmlstarqaq".getBytes(); byte[] compressed = Zlib.deflate(in); byte[] out = Zlib.inflate(compressed); assertTrue(Arrays.equals(in, out)); }
Example 12
Source File: Chunk.java From Jupiter with GNU General Public License v3.0 | 4 votes |
@Override public byte[] toBinary() { CompoundTag nbt = this.getNBT().copy(); nbt.putInt("xPos", this.x); nbt.putInt("zPos", this.z); if (this.isGenerated()) { nbt.putByteArray("Blocks", this.getBlockIdArray()); nbt.putByteArray("Data", this.getBlockDataArray()); nbt.putByteArray("SkyLight", this.getBlockSkyLightArray()); nbt.putByteArray("BlockLight", this.getBlockLightArray()); nbt.putIntArray("BiomeColors", this.getBiomeColorArray()); nbt.putIntArray("HeightMap", this.getHeightMapArray()); } ArrayList<CompoundTag> entities = new ArrayList<>(); for (Entity entity : this.getEntities().values()) { if (!(entity instanceof Player) && !entity.closed) { entity.saveNBT(); entities.add(entity.namedTag); } } ListTag<CompoundTag> entityListTag = new ListTag<>("Entities"); entityListTag.setAll(entities); nbt.putList(entityListTag); ArrayList<CompoundTag> tiles = new ArrayList<>(); for (BlockEntity blockEntity : this.getBlockEntities().values()) { blockEntity.saveNBT(); tiles.add(blockEntity.namedTag); } ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities"); tileListTag.setAll(tiles); nbt.putList(tileListTag); BinaryStream extraData = new BinaryStream(); Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray(); extraData.putInt(extraDataArray.size()); for (Integer key : extraDataArray.keySet()) { extraData.putInt(key); extraData.putShort(extraDataArray.get(key)); } nbt.putByteArray("ExtraData", extraData.getBuffer()); CompoundTag chunk = new CompoundTag(""); chunk.putCompound("Level", nbt); try { return Zlib.deflate(NBTIO.write(chunk, ByteOrder.BIG_ENDIAN), RegionLoader.COMPRESSION_LEVEL); } catch (Exception e) { throw new RuntimeException(e); } }
Example 13
Source File: Chunk.java From Jupiter with GNU General Public License v3.0 | 4 votes |
@Override public byte[] toBinary() { CompoundTag nbt = this.getNBT().copy(); nbt.putInt("xPos", this.x); nbt.putInt("zPos", this.z); ListTag<CompoundTag> sectionList = new ListTag<>("Sections"); for (cn.nukkit.level.format.ChunkSection section : this.getSections()) { if (section instanceof EmptyChunkSection) { continue; } CompoundTag s = new CompoundTag(null); s.putByte("Y", (section.getY())); s.putByteArray("Blocks", section.getIdArray()); s.putByteArray("Data", section.getDataArray()); s.putByteArray("BlockLight", section.getLightArray()); s.putByteArray("SkyLight", section.getSkyLightArray()); sectionList.add(s); } nbt.putList(sectionList); nbt.putIntArray("BiomeColors", this.getBiomeColorArray()); nbt.putIntArray("HeightMap", this.getHeightMapArray()); ArrayList<CompoundTag> entities = new ArrayList<>(); for (Entity entity : this.getEntities().values()) { if (!(entity instanceof Player) && !entity.closed) { entity.saveNBT(); entities.add(entity.namedTag); } } ListTag<CompoundTag> entityListTag = new ListTag<>("Entities"); entityListTag.setAll(entities); nbt.putList(entityListTag); ArrayList<CompoundTag> tiles = new ArrayList<>(); for (BlockEntity blockEntity : this.getBlockEntities().values()) { blockEntity.saveNBT(); tiles.add(blockEntity.namedTag); } ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities"); tileListTag.setAll(tiles); nbt.putList(tileListTag); List<BlockUpdateEntry> entries = this.provider.getLevel().getPendingBlockUpdates(this); if (entries != null) { ListTag<CompoundTag> tileTickTag = new ListTag<>("TileTicks"); long totalTime = this.provider.getLevel().getCurrentTick(); for (BlockUpdateEntry entry : entries) { CompoundTag entryNBT = new CompoundTag() .putString("i", entry.block.getClass().getSimpleName()) .putInt("x", entry.pos.getFloorX()) .putInt("y", entry.pos.getFloorY()) .putInt("z", entry.pos.getFloorZ()) .putInt("t", (int) (entry.delay - totalTime)) .putInt("p", entry.priority); tileTickTag.add(entryNBT); } nbt.putList(tileTickTag); } BinaryStream extraData = new BinaryStream(); Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray(); extraData.putInt(extraDataArray.size()); for (Integer key : extraDataArray.keySet()) { extraData.putInt(key); extraData.putShort(extraDataArray.get(key)); } nbt.putByteArray("ExtraData", extraData.getBuffer()); CompoundTag chunk = new CompoundTag(""); chunk.putCompound("Level", nbt); try { return Zlib.deflate(NBTIO.write(chunk, ByteOrder.BIG_ENDIAN), RegionLoader.COMPRESSION_LEVEL); } catch (Exception e) { throw new RuntimeException(e); } }
Example 14
Source File: Chunk.java From Nukkit with GNU General Public License v3.0 | 4 votes |
@Override public byte[] toBinary() { CompoundTag nbt = this.getNBT().copy(); nbt.remove("BiomeColors"); nbt.putInt("xPos", this.getX()); nbt.putInt("zPos", this.getZ()); if (this.isGenerated()) { nbt.putByteArray("Blocks", this.getBlockIdArray()); nbt.putByteArray("Data", this.getBlockDataArray()); nbt.putByteArray("SkyLight", this.getBlockSkyLightArray()); nbt.putByteArray("BlockLight", this.getBlockLightArray()); nbt.putByteArray("Biomes", this.getBiomeIdArray()); int[] heightInts = new int[256]; byte[] heightBytes = this.getHeightMapArray(); for (int i = 0; i < heightInts.length; i++) { heightInts[i] = heightBytes[i] & 0xFF; } nbt.putIntArray("HeightMap", heightInts); } ArrayList<CompoundTag> entities = new ArrayList<>(); for (Entity entity : this.getEntities().values()) { if (!(entity instanceof Player) && !entity.closed) { entity.saveNBT(); entities.add(entity.namedTag); } } ListTag<CompoundTag> entityListTag = new ListTag<>("Entities"); entityListTag.setAll(entities); nbt.putList(entityListTag); ArrayList<CompoundTag> tiles = new ArrayList<>(); for (BlockEntity blockEntity : this.getBlockEntities().values()) { blockEntity.saveNBT(); tiles.add(blockEntity.namedTag); } ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities"); tileListTag.setAll(tiles); nbt.putList(tileListTag); BinaryStream extraData = new BinaryStream(); Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray(); extraData.putInt(extraDataArray.size()); for (Integer key : extraDataArray.keySet()) { extraData.putInt(key); extraData.putShort(extraDataArray.get(key)); } nbt.putByteArray("ExtraData", extraData.getBuffer()); CompoundTag chunk = new CompoundTag(""); chunk.putCompound("Level", nbt); try { return Zlib.deflate(NBTIO.write(chunk, ByteOrder.BIG_ENDIAN), RegionLoader.COMPRESSION_LEVEL); } catch (Exception e) { throw new RuntimeException(e); } }
Example 15
Source File: Chunk.java From Nukkit with GNU General Public License v3.0 | 4 votes |
@Override public byte[] toBinary() { CompoundTag nbt = this.getNBT().copy(); nbt.remove("BiomeColors"); nbt.putInt("xPos", this.getX()); nbt.putInt("zPos", this.getZ()); ListTag<CompoundTag> sectionList = new ListTag<>("Sections"); for (cn.nukkit.level.format.ChunkSection section : this.getSections()) { if (section instanceof EmptyChunkSection) { continue; } CompoundTag s = new CompoundTag(null); s.putByte("Y", (section.getY())); s.putByteArray("Blocks", section.getIdArray()); s.putByteArray("Data", section.getDataArray()); s.putByteArray("BlockLight", section.getLightArray()); s.putByteArray("SkyLight", section.getSkyLightArray()); sectionList.add(s); } nbt.putList(sectionList); nbt.putByteArray("Biomes", this.getBiomeIdArray()); int[] heightInts = new int[256]; byte[] heightBytes = this.getHeightMapArray(); for (int i = 0; i < heightInts.length; i++) { heightInts[i] = heightBytes[i] & 0xFF; } nbt.putIntArray("HeightMap", heightInts); ArrayList<CompoundTag> entities = new ArrayList<>(); for (Entity entity : this.getEntities().values()) { if (!(entity instanceof Player) && !entity.closed) { entity.saveNBT(); entities.add(entity.namedTag); } } ListTag<CompoundTag> entityListTag = new ListTag<>("Entities"); entityListTag.setAll(entities); nbt.putList(entityListTag); ArrayList<CompoundTag> tiles = new ArrayList<>(); for (BlockEntity blockEntity : this.getBlockEntities().values()) { blockEntity.saveNBT(); tiles.add(blockEntity.namedTag); } ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities"); tileListTag.setAll(tiles); nbt.putList(tileListTag); Set<BlockUpdateEntry> entries = this.provider.getLevel().getPendingBlockUpdates(this); if (entries != null) { ListTag<CompoundTag> tileTickTag = new ListTag<>("TileTicks"); long totalTime = this.provider.getLevel().getCurrentTick(); for (BlockUpdateEntry entry : entries) { CompoundTag entryNBT = new CompoundTag() .putString("i", entry.block.getSaveId()) .putInt("x", entry.pos.getFloorX()) .putInt("y", entry.pos.getFloorY()) .putInt("z", entry.pos.getFloorZ()) .putInt("t", (int) (entry.delay - totalTime)) .putInt("p", entry.priority); tileTickTag.add(entryNBT); } nbt.putList(tileTickTag); } BinaryStream extraData = new BinaryStream(); Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray(); extraData.putInt(extraDataArray.size()); for (Integer key : extraDataArray.keySet()) { extraData.putInt(key); extraData.putShort(extraDataArray.get(key)); } nbt.putByteArray("ExtraData", extraData.getBuffer()); CompoundTag chunk = new CompoundTag(""); chunk.putCompound("Level", nbt); try { return Zlib.deflate(NBTIO.write(chunk, ByteOrder.BIG_ENDIAN), RegionLoader.COMPRESSION_LEVEL); } catch (Exception e) { throw new RuntimeException(e); } }
Example 16
Source File: Chunk.java From Nukkit with GNU General Public License v3.0 | 4 votes |
@Override public byte[] toBinary() { CompoundTag nbt = this.getNBT().copy(); nbt.putInt("xPos", this.getX()); nbt.putInt("zPos", this.getZ()); if (this.isGenerated()) { nbt.putByteArray("Blocks", this.getBlockIdArray()); nbt.putByteArray("Data", this.getBlockDataArray()); nbt.putByteArray("SkyLight", this.getBlockSkyLightArray()); nbt.putByteArray("BlockLight", this.getBlockLightArray()); nbt.putIntArray("BiomeColors", this.getBiomeColorArray()); int[] heightInts = new int[256]; byte[] heightBytes = this.getHeightMapArray(); for (int i = 0; i < heightInts.length; i++) { heightInts[i] = heightBytes[i] & 0xFF; } nbt.putIntArray("HeightMap", heightInts); } ArrayList<CompoundTag> entities = new ArrayList<>(); for (Entity entity : this.getEntities().values()) { if (!(entity instanceof Player) && !entity.closed) { entity.saveNBT(); entities.add(entity.namedTag); } } ListTag<CompoundTag> entityListTag = new ListTag<>("Entities"); entityListTag.setAll(entities); nbt.putList(entityListTag); ArrayList<CompoundTag> tiles = new ArrayList<>(); for (BlockEntity blockEntity : this.getBlockEntities().values()) { blockEntity.saveNBT(); tiles.add(blockEntity.namedTag); } ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities"); tileListTag.setAll(tiles); nbt.putList(tileListTag); BinaryStream extraData = new BinaryStream(); Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray(); extraData.putInt(extraDataArray.size()); for (Integer key : extraDataArray.keySet()) { extraData.putInt(key); extraData.putShort(extraDataArray.get(key)); } nbt.putByteArray("ExtraData", extraData.getBuffer()); CompoundTag chunk = new CompoundTag(""); chunk.putCompound("Level", nbt); try { return Zlib.deflate(NBTIO.write(chunk, ByteOrder.BIG_ENDIAN), RegionLoader.COMPRESSION_LEVEL); } catch (Exception e) { throw new RuntimeException(e); } }