public class CommonUtils {
public static boolean isNotNull(String s){
if(s==null || "".equals(s)) {
return false;
}
return true;
}
public static boolean isNull(String s){
return !isNotNull(s) || "null".equals(s);
}
public static byte[] compress(String s) throws IOException, DataFormatException{
byte[] input = s.getBytes("UTF-8");
// 创建一个压缩器,进行压缩处理
Deflater compressor = new Deflater();
compressor.setLevel(Deflater.BEST_COMPRESSION);
compressor.setInput(input);
compressor.finish();
// 压缩的数据写入内存数组中
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
byte[] buf = new byte[1024];
int len=0;
GZIPOutputStream out;
while(!compressor.finished()) {
len = compressor.deflate(buf);
bos.write(buf, 0, len);
}
bos.close();
return bos.toByteArray();
}
public static byte[] decompress(byte[] compressData) throws IOException, DataFormatException{
// 创建解压缩器
Inflater decompressor = new Inflater();
decompressor.setInput(compressData);
// 解压缩后的数据写入内存数组中
ByteArrayOutputStream bos = new ByteArrayOutputStream(compressData.length);
byte[] buf = new byte[1024];
int len=0;
while(!decompressor.finished()) {
len = decompressor.inflate(buf);
bos.write(buf, 0, len);
}
bos.close();
return bos.toByteArray();
}
public static void main(String[] args)throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
String name = "{test:111,test1:222,test1:22312}";
int age = 84;
dout.writeUTF(name);
dout.writeInt(age);
byte[] buff = bout.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(buff);
DataInputStream dis = new DataInputStream(bin);
String newName = dis.readUTF();
int newAge = dis.readInt();
System.out.println(newName+":"+newAge);
}
private final static double EARTH_RADIUS = 6378137.0;
@SuppressWarnings("unused")
public static double gps2m(double lat_a, double lng_a, double lat_b, double lng_b) {
double radLat1 = (lat_a * Math.PI / 180.0);
double radLat2 = (lat_b * Math.PI / 180.0);
double a = radLat1 - radLat2;
double b = (lng_a - lng_b) * Math.PI / 180.0;
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
+ Math.cos(radLat1) * Math.cos(radLat2)
* Math.pow(Math.sin(b / 2), 2)));
s = s * EARTH_RADIUS;
s = Math.round(s * 10000) / 10000;
return s;
}
}
public static boolean isNotNull(String s){
if(s==null || "".equals(s)) {
return false;
}
return true;
}
public static boolean isNull(String s){
return !isNotNull(s) || "null".equals(s);
}
public static byte[] compress(String s) throws IOException, DataFormatException{
byte[] input = s.getBytes("UTF-8");
// 创建一个压缩器,进行压缩处理
Deflater compressor = new Deflater();
compressor.setLevel(Deflater.BEST_COMPRESSION);
compressor.setInput(input);
compressor.finish();
// 压缩的数据写入内存数组中
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
byte[] buf = new byte[1024];
int len=0;
GZIPOutputStream out;
while(!compressor.finished()) {
len = compressor.deflate(buf);
bos.write(buf, 0, len);
}
bos.close();
return bos.toByteArray();
}
public static byte[] decompress(byte[] compressData) throws IOException, DataFormatException{
// 创建解压缩器
Inflater decompressor = new Inflater();
decompressor.setInput(compressData);
// 解压缩后的数据写入内存数组中
ByteArrayOutputStream bos = new ByteArrayOutputStream(compressData.length);
byte[] buf = new byte[1024];
int len=0;
while(!decompressor.finished()) {
len = decompressor.inflate(buf);
bos.write(buf, 0, len);
}
bos.close();
return bos.toByteArray();
}
public static void main(String[] args)throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
String name = "{test:111,test1:222,test1:22312}";
int age = 84;
dout.writeUTF(name);
dout.writeInt(age);
byte[] buff = bout.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(buff);
DataInputStream dis = new DataInputStream(bin);
String newName = dis.readUTF();
int newAge = dis.readInt();
System.out.println(newName+":"+newAge);
}
private final static double EARTH_RADIUS = 6378137.0;
@SuppressWarnings("unused")
public static double gps2m(double lat_a, double lng_a, double lat_b, double lng_b) {
double radLat1 = (lat_a * Math.PI / 180.0);
double radLat2 = (lat_b * Math.PI / 180.0);
double a = radLat1 - radLat2;
double b = (lng_a - lng_b) * Math.PI / 180.0;
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
+ Math.cos(radLat1) * Math.cos(radLat2)
* Math.pow(Math.sin(b / 2), 2)));
s = s * EARTH_RADIUS;
s = Math.round(s * 10000) / 10000;
return s;
}
}