-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
CoolLoong
committed
Aug 9, 2023
1 parent
a8b54d4
commit 2d38695
Showing
18 changed files
with
399 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
Allay-API/src/main/java/cn/allay/api/utils/VanillaBiomeIdUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cn.allay.api.utils; | ||
|
||
import cn.allay.api.data.VanillaBiomeId; | ||
import cn.allay.api.identifier.Identifier; | ||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; | ||
import lombok.experimental.UtilityClass; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.HashMap; | ||
|
||
/** | ||
* Allay Project 8/8/2023 | ||
* | ||
* @author Cool_Loong | ||
*/ | ||
@UtilityClass | ||
public class VanillaBiomeIdUtils { | ||
private final static Int2ObjectOpenHashMap<VanillaBiomeId> MAP1 = new Int2ObjectOpenHashMap<>(); | ||
private final static HashMap<Identifier, VanillaBiomeId> MAP2 = new HashMap<>(); | ||
|
||
static { | ||
for (var v : VanillaBiomeId.values()) { | ||
MAP1.put(v.getId(), v); | ||
MAP2.put(v.getIdentifier(), v); | ||
} | ||
} | ||
|
||
@Nullable | ||
public VanillaBiomeId fromId(int id) { | ||
return MAP1.get(id); | ||
} | ||
|
||
@Nullable | ||
public VanillaBiomeId fromIdentifier(Identifier identifier) { | ||
return MAP2.get(identifier); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
Allay-API/src/main/java/cn/allay/api/zlib/JavaZibThreadLocal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package cn.allay.api.zlib; | ||
|
||
import it.unimi.dsi.fastutil.io.FastByteArrayOutputStream; | ||
|
||
import java.io.IOException; | ||
import java.util.zip.DataFormatException; | ||
import java.util.zip.Deflater; | ||
import java.util.zip.Inflater; | ||
|
||
/** | ||
* Allay Project 2023/6/6 | ||
* | ||
* @author Cool_Loong | ||
*/ | ||
public final class JavaZibThreadLocal implements ZlibProvider { | ||
private static final ThreadLocal<FastByteArrayOutputStream> FBAO = ThreadLocal.withInitial(() -> new FastByteArrayOutputStream(1024)); | ||
private static final ThreadLocal<byte[]> BUFFER = ThreadLocal.withInitial(() -> new byte[8192]); | ||
private int level; | ||
private CompressionType type; | ||
private final ThreadLocal<Inflater> INFLATER = ThreadLocal.withInitial(Inflater::new); | ||
private final ThreadLocal<Deflater> DEFLATER = ThreadLocal.withInitial(() -> new Deflater(level)); | ||
|
||
JavaZibThreadLocal(CompressionType type, int level) { | ||
this.type = type; | ||
this.level = level; | ||
} | ||
|
||
@Override | ||
public void setCompressionType(CompressionType type) { | ||
this.type = type; | ||
} | ||
|
||
@Override | ||
public void setCompressionLevel(int level) { | ||
this.level = level; | ||
} | ||
|
||
@Override | ||
public byte[] deflate(byte[] data) throws IOException { | ||
try (var bos = FBAO.get()) { | ||
if (type == CompressionType.GZIP) { | ||
throw new UnsupportedOperationException(this.getClass().getSimpleName() + " dont support GZIP"); | ||
} else { | ||
Deflater deflater = DEFLATER.get(); | ||
try { | ||
deflater.reset(); | ||
deflater.setInput(data); | ||
deflater.finish(); | ||
bos.reset(); | ||
byte[] buffer = BUFFER.get(); | ||
int length = 0; | ||
while (!deflater.finished()) { | ||
int i = deflater.deflate(buffer); | ||
bos.write(buffer, 0, i); | ||
length += i; | ||
} | ||
byte[] output = new byte[length]; | ||
System.arraycopy(bos.array, 0, output, 0, length); | ||
return output; | ||
} finally { | ||
deflater.reset(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public byte[] inflate(byte[] data, int maxSize) throws IOException { | ||
try (var bos = FBAO.get()) { | ||
if (type == CompressionType.GZIP) { | ||
throw new UnsupportedOperationException(this.getClass().getSimpleName() + " dont support GZIP"); | ||
} else { | ||
Inflater inflater = INFLATER.get(); | ||
try { | ||
inflater.reset(); | ||
inflater.setInput(data); | ||
bos.reset(); | ||
byte[] buffer = BUFFER.get(); | ||
try { | ||
int length = 0; | ||
while (!inflater.finished()) { | ||
int i = inflater.inflate(buffer); | ||
length += i; | ||
if (maxSize > 0 && length > maxSize) { | ||
throw new IOException("Inflated data exceeds maximum size"); | ||
} | ||
bos.write(buffer, 0, i); | ||
} | ||
byte[] output = new byte[length]; | ||
System.arraycopy(bos.array, 0, output, 0, length); | ||
return output; | ||
} catch (DataFormatException e) { | ||
throw new IOException("Unable to inflate zlib stream", e); | ||
} | ||
} finally { | ||
inflater.end(); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.