forked from Cookies_Github_mirror/AquaDX
Initial Commit
This commit is contained in:
59
src/main/java/icu/samnyan/aqua/sega/util/ByteBufUtil.java
Normal file
59
src/main/java/icu/samnyan/aqua/sega/util/ByteBufUtil.java
Normal file
@@ -0,0 +1,59 @@
|
||||
package icu.samnyan.aqua.sega.util;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
/**
|
||||
* @author samnyan (privateamusement@protonmail.com)
|
||||
*/
|
||||
public class ByteBufUtil {
|
||||
|
||||
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
|
||||
|
||||
/**
|
||||
* Read bytes from start to current writer index
|
||||
* Not modifying the reader index
|
||||
*
|
||||
* @param byteBuf The input buffer
|
||||
* @return bytes
|
||||
*/
|
||||
public static byte[] toBytes(ByteBuf byteBuf) {
|
||||
|
||||
int readerPos = byteBuf.readerIndex();
|
||||
byteBuf.resetReaderIndex();
|
||||
byte[] result = new byte[byteBuf.readableBytes()];
|
||||
byteBuf.readBytes(result);
|
||||
|
||||
byteBuf.readerIndex(readerPos);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Move writer index to the capacity and read all the byte to the end
|
||||
* Not modifying the reader index
|
||||
*
|
||||
* @param byteBuf The input buffer
|
||||
* @return bytes
|
||||
*/
|
||||
public static byte[] toAllBytes(ByteBuf byteBuf) {
|
||||
|
||||
int readerPos = byteBuf.readerIndex();
|
||||
byteBuf.resetReaderIndex();
|
||||
byteBuf.writerIndex(byteBuf.capacity());
|
||||
byte[] result = new byte[byteBuf.capacity()];
|
||||
byteBuf.readBytes(result);
|
||||
|
||||
byteBuf.readerIndex(readerPos);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String bytesToHex(byte[] bytes) {
|
||||
char[] hexChars = new char[bytes.length * 2];
|
||||
for (int j = 0; j < bytes.length; j++) {
|
||||
int v = bytes[j] & 0xFF;
|
||||
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
|
||||
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
|
||||
}
|
||||
return new String(hexChars);
|
||||
}
|
||||
|
||||
}
|
||||
57
src/main/java/icu/samnyan/aqua/sega/util/Compression.java
Normal file
57
src/main/java/icu/samnyan/aqua/sega/util/Compression.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package icu.samnyan.aqua.sega.util;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
|
||||
import java.util.zip.DataFormatException;
|
||||
import java.util.zip.Deflater;
|
||||
import java.util.zip.Inflater;
|
||||
|
||||
/**
|
||||
* @author samnyan (privateamusement@protonmail.com)
|
||||
*/
|
||||
public class Compression {
|
||||
|
||||
public static byte[] decompress(byte[] src) {
|
||||
ByteBuf result = Unpooled.buffer();
|
||||
byte[] buffer = new byte[100];
|
||||
Inflater decompressor = new Inflater();
|
||||
decompressor.setInput(src);
|
||||
|
||||
try {
|
||||
while (!decompressor.finished()) {
|
||||
int count = decompressor.inflate(buffer);
|
||||
if (count == 0) {
|
||||
break;
|
||||
}
|
||||
result.writeBytes(buffer, result.readerIndex(), count);
|
||||
}
|
||||
decompressor.end();
|
||||
|
||||
return ByteBufUtil.toBytes(result);
|
||||
} catch (DataFormatException e) {
|
||||
e.printStackTrace();
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static byte[] compress(byte[] src) {
|
||||
ByteBuf result = Unpooled.buffer();
|
||||
byte[] buffer = new byte[100];
|
||||
Deflater compressor = new Deflater();
|
||||
compressor.setInput(src);
|
||||
compressor.finish();
|
||||
|
||||
while (!compressor.finished()) {
|
||||
int count = compressor.deflate(buffer);
|
||||
if (count == 0) {
|
||||
break;
|
||||
}
|
||||
result.writeBytes(buffer, result.readerIndex(), count);
|
||||
}
|
||||
compressor.end();
|
||||
|
||||
return ByteBufUtil.toBytes(result);
|
||||
}
|
||||
}
|
||||
14
src/main/java/icu/samnyan/aqua/sega/util/URIEncoder.java
Normal file
14
src/main/java/icu/samnyan/aqua/sega/util/URIEncoder.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package icu.samnyan.aqua.sega.util;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* @author samnyan (privateamusement@protonmail.com)
|
||||
*/
|
||||
public class URIEncoder {
|
||||
|
||||
public static String encode(String str) {
|
||||
return URLEncoder.encode(str, StandardCharsets.UTF_8).replaceAll("\\+", "%20");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package icu.samnyan.aqua.sega.util.jackson;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
import icu.samnyan.aqua.sega.general.model.Card;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author samnyan (privateamusement@protonmail.com)
|
||||
*/
|
||||
public class AccessCodeSerializer extends StdSerializer<Card> {
|
||||
|
||||
public AccessCodeSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public AccessCodeSerializer(Class<Card> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(Card value, JsonGenerator gen, SerializerProvider provider) throws IOException {
|
||||
gen.writeString(value.getLuid());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package icu.samnyan.aqua.sega.util.jackson;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author samnyan (privateamusement@protonmail.com)
|
||||
*/
|
||||
public class BooleanNumberDeserializer extends JsonDeserializer<Boolean> {
|
||||
|
||||
@Override
|
||||
public Boolean deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||
return !"0".equals(p.getText());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package icu.samnyan.aqua.sega.util.jackson;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author samnyan (privateamusement@protonmail.com)
|
||||
*/
|
||||
public class BooleanNumberSerializer extends StdSerializer<Boolean> {
|
||||
|
||||
public BooleanNumberSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public BooleanNumberSerializer(Class<Boolean> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(Boolean value, JsonGenerator gen, SerializerProvider provider) throws IOException {
|
||||
gen.writeNumber(value ? 1 : 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package icu.samnyan.aqua.sega.util.jackson;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author samnyan (privateamusement@protonmail.com)
|
||||
*/
|
||||
public class BooleanStringSerializer extends StdSerializer<Boolean> {
|
||||
|
||||
public BooleanStringSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public BooleanStringSerializer(Class<Boolean> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(Boolean value, JsonGenerator gen, SerializerProvider provider) throws IOException {
|
||||
gen.writeString(value.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package icu.samnyan.aqua.sega.util.jackson;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author samnyan (privateamusement@protonmail.com)
|
||||
*/
|
||||
public class ByteBufSerializer extends StdSerializer<ByteBuf> {
|
||||
|
||||
public ByteBufSerializer() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public ByteBufSerializer(Class<ByteBuf> t) {
|
||||
super(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(ByteBuf value, JsonGenerator gen, SerializerProvider provider) throws IOException {
|
||||
|
||||
gen.writeString(ByteBufUtil.hexDump(value));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package icu.samnyan.aqua.sega.util.jackson;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author samnyan (privateamusement@protonmail.com)
|
||||
*/
|
||||
@Component
|
||||
public class StringMapper {
|
||||
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
public StringMapper() {
|
||||
mapper = new ObjectMapper().configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
mapper.configure(SerializationFeature.WRITE_ENUMS_USING_INDEX, true);
|
||||
SimpleModule module = new SimpleModule();
|
||||
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
||||
module.addSerializer(Boolean.class, new BooleanStringSerializer());
|
||||
module.addSerializer(boolean.class, new BooleanStringSerializer());
|
||||
|
||||
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
||||
mapper.registerModule(module);
|
||||
}
|
||||
|
||||
public String write(Object o) throws JsonProcessingException {
|
||||
return mapper.writeValueAsString(o);
|
||||
|
||||
}
|
||||
|
||||
public <T> T convert(Map<String, Object> map, Class<T> toClass) {
|
||||
return mapper.convertValue(map, toClass);
|
||||
}
|
||||
|
||||
public LinkedHashMap<String, Object> toMap(Object object) {
|
||||
return mapper.convertValue(object, new TypeReference<>() {
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package icu.samnyan.aqua.sega.util.jackson;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
/**
|
||||
* @author samnyan (privateamusement@protonmail.com)
|
||||
*/
|
||||
public class ZonedDateTimeDeserializer extends JsonDeserializer<ZonedDateTime> {
|
||||
|
||||
@Override
|
||||
public ZonedDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||
return ZonedDateTime.parse(p.getText());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user