19 changed files with 392 additions and 281 deletions
@ -0,0 +1,59 @@ |
|||||
|
package com.xiaowuler.radarresolver.custom; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
|
||||
|
/** |
||||
|
* @describe: 自定义读取字节流 |
||||
|
* @author: xiaowuler |
||||
|
* @createTime: 2022-03-17 17:07 |
||||
|
*/ |
||||
|
public interface ICustomInputStream { |
||||
|
/** |
||||
|
* 转换 int |
||||
|
* @return |
||||
|
* @throws IOException |
||||
|
*/ |
||||
|
int readInt() throws IOException; |
||||
|
|
||||
|
/** |
||||
|
* 转换 short |
||||
|
* @return |
||||
|
* @throws IOException |
||||
|
*/ |
||||
|
short readShort() throws IOException; |
||||
|
|
||||
|
/** |
||||
|
* 转换 long |
||||
|
* @return |
||||
|
* @throws IOException |
||||
|
*/ |
||||
|
long readLong() throws IOException; |
||||
|
|
||||
|
/** |
||||
|
* 转换 float |
||||
|
* @return |
||||
|
* @throws IOException |
||||
|
*/ |
||||
|
float readFloat() throws IOException; |
||||
|
|
||||
|
/** |
||||
|
* 读取 指定个数 字节 |
||||
|
* @param len |
||||
|
* @return |
||||
|
* @throws IOException |
||||
|
*/ |
||||
|
byte[] readNBytes(int len) throws IOException; |
||||
|
|
||||
|
/** |
||||
|
* 判断是否存在字节 |
||||
|
* @return |
||||
|
* @throws IOException |
||||
|
*/ |
||||
|
int available() throws IOException; |
||||
|
|
||||
|
/** |
||||
|
* 释放资源 |
||||
|
* @throws IOException |
||||
|
*/ |
||||
|
void close() throws IOException; |
||||
|
} |
@ -0,0 +1,116 @@ |
|||||
|
package com.xiaowuler.radarresolver.custom.impl; |
||||
|
|
||||
|
import java.io.*; |
||||
|
import java.lang.reflect.InvocationTargetException; |
||||
|
import java.nio.ByteBuffer; |
||||
|
import java.nio.ByteOrder; |
||||
|
import java.util.Locale; |
||||
|
import java.util.Objects; |
||||
|
|
||||
|
import org.python.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; |
||||
|
import com.xiaowuler.radarresolver.custom.ICustomInputStream; |
||||
|
import com.xiaowuler.radarresolver.util.ByteUtils; |
||||
|
|
||||
|
/** |
||||
|
* @describe: 自定义 雷达 输入流 |
||||
|
* Big-Endian 和 Little-Endian 字节排序 |
||||
|
* * Big-Endian 一个Word中的高位的Byte放在内存中这个Word区域的低地址处。 |
||||
|
* * Little-Endian 一个Word中的低位的Byte放在内存中这个Word区域的低地址处。 |
||||
|
* * python写入的二进制文件和java二进制文件编码读取顺序不同,java 是从左到右编码,python是从右到左 |
||||
|
* @author: xiaowuler |
||||
|
* @createTime: 2022-03-18 09:09 |
||||
|
*/ |
||||
|
public class CustomRadarInputStream implements ICustomInputStream, Closeable{ |
||||
|
|
||||
|
private static final String BZIP_SUFFIX = "bz2"; |
||||
|
private static final String BIN_SUFFIX = "bin"; |
||||
|
|
||||
|
private InputStream inputStream; |
||||
|
private FileInputStream fileInputStream; |
||||
|
|
||||
|
public CustomRadarInputStream(String filepath) throws FileNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { |
||||
|
String fileSuffix = filepath.substring(filepath.lastIndexOf(".") + 1).trim().toLowerCase(Locale.ROOT); |
||||
|
switch (fileSuffix){ |
||||
|
case BIN_SUFFIX: |
||||
|
customRadarInputStream(filepath, DataInputStream.class); |
||||
|
break; |
||||
|
case BZIP_SUFFIX: |
||||
|
customRadarInputStream(filepath, BZip2CompressorInputStream.class); |
||||
|
default: |
||||
|
throw new IllegalAccessException("无效的文件格式类型"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private <T extends InputStream> void customRadarInputStream(String filepath, Class<T> clazz) throws NoSuchMethodException, FileNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException { |
||||
|
fileInputStream = new FileInputStream(filepath); |
||||
|
inputStream = clazz.getConstructor(InputStream.class).newInstance(fileInputStream); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public int readInt() throws IOException { |
||||
|
int ch1 = inputStream.read(); |
||||
|
int ch2 = inputStream.read(); |
||||
|
int ch3 = inputStream.read(); |
||||
|
int ch4 = inputStream.read(); |
||||
|
if ((ch1 | ch2 | ch3 | ch4) < 0) { |
||||
|
throw new EOFException(); |
||||
|
} |
||||
|
return ((ch1 << 0) + (ch2 << 8) + (ch3 << 16) + (ch4 << 24)); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Big-Endian 和 Little-Endian 字节排序 |
||||
|
* |
||||
|
* Big-Endian 一个Word中的高位的Byte放在内存中这个Word区域的低地址处。 |
||||
|
* Little-Endian 一个Word中的低位的Byte放在内存中这个Word区域的低地址处。 |
||||
|
* @return |
||||
|
* @throws IOException |
||||
|
*/ |
||||
|
@Override |
||||
|
public short readShort() throws IOException{ |
||||
|
byte[] bytes = inputStream.readNBytes(2); |
||||
|
return ByteUtils.bytes2Short(bytes); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public long readLong() throws IOException{ |
||||
|
byte[] bytes = inputStream.readNBytes(8); |
||||
|
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); |
||||
|
byteBuffer.order(ByteOrder.LITTLE_ENDIAN); |
||||
|
return byteBuffer.getLong(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public float readFloat() throws IOException { |
||||
|
return Float.intBitsToFloat(readInt()); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public byte[] readNBytes(int len) throws IOException{ |
||||
|
return inputStream.readNBytes(len); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public int available() throws IOException { |
||||
|
return inputStream.available(); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void close() { |
||||
|
if (Objects.nonNull(fileInputStream)){ |
||||
|
try { |
||||
|
fileInputStream.close(); |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (Objects.nonNull(inputStream)){ |
||||
|
try { |
||||
|
inputStream.close(); |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
@ -1,4 +1,4 @@ |
|||||
package com.xiaowuler.radarresolver.customer; |
package com.xiaowuler.radarresolver.custom.impl; |
||||
|
|
||||
import java.io.DataOutputStream; |
import java.io.DataOutputStream; |
||||
import java.io.FilterOutputStream; |
import java.io.FilterOutputStream; |
@ -1,89 +0,0 @@ |
|||||
package com.xiaowuler.radarresolver.customer; |
|
||||
|
|
||||
import java.io.*; |
|
||||
import java.nio.ByteBuffer; |
|
||||
import java.nio.ByteOrder; |
|
||||
|
|
||||
import lombok.Data; |
|
||||
import com.xiaowuler.radarresolver.util.ByteUtils; |
|
||||
|
|
||||
/** |
|
||||
* @describe: Big-Endian 和 Little-Endian 字节排序 |
|
||||
* Big-Endian 一个Word中的高位的Byte放在内存中这个Word区域的低地址处。 |
|
||||
* Little-Endian 一个Word中的低位的Byte放在内存中这个Word区域的低地址处。 |
|
||||
* python写入的二进制文件和java二进制文件编码读取顺序不同,java 是从左到右编码,python是从右到左 |
|
||||
* @author: xiaowuler |
|
||||
* @createTime: 2021-05-19 09:01 |
|
||||
*/ |
|
||||
public class CustomerDataInputStream extends DataInputStream { |
|
||||
|
|
||||
// private int pos = 0;
|
|
||||
|
|
||||
/** |
|
||||
* Creates a DataInputStream that uses the specified |
|
||||
* underlying InputStream. |
|
||||
* |
|
||||
* @param in the specified input stream |
|
||||
*/ |
|
||||
public CustomerDataInputStream(InputStream in) { |
|
||||
super(in); |
|
||||
} |
|
||||
|
|
||||
public final int readCInt() throws IOException { |
|
||||
// addPos(4);
|
|
||||
int ch1 = in.read(); |
|
||||
int ch2 = in.read(); |
|
||||
int ch3 = in.read(); |
|
||||
int ch4 = in.read(); |
|
||||
if ((ch1 | ch2 | ch3 | ch4) < 0) { |
|
||||
throw new EOFException(); |
|
||||
} |
|
||||
return ((ch1 << 0) + (ch2 << 8) + (ch3 << 16) + (ch4 << 24)); |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* Big-Endian 和 Little-Endian 字节排序 |
|
||||
* |
|
||||
* Big-Endian 一个Word中的高位的Byte放在内存中这个Word区域的低地址处。 |
|
||||
* Little-Endian 一个Word中的低位的Byte放在内存中这个Word区域的低地址处。 |
|
||||
* @return |
|
||||
* @throws IOException |
|
||||
*/ |
|
||||
public final short readCShort() throws IOException{ |
|
||||
// addPos(2);
|
|
||||
byte[] bytes = in.readNBytes(2); |
|
||||
return ByteUtils.bytes2Short(bytes); |
|
||||
} |
|
||||
|
|
||||
public final long readCLong() throws IOException{ |
|
||||
// addPos(8);
|
|
||||
byte[] bytes = in.readNBytes(8); |
|
||||
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); |
|
||||
byteBuffer.order(ByteOrder.LITTLE_ENDIAN); |
|
||||
return byteBuffer.getLong(); |
|
||||
} |
|
||||
|
|
||||
public final float readCFloat() throws IOException { |
|
||||
// addPos(4);
|
|
||||
return Float.intBitsToFloat(readCInt()); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public byte[] readNBytes(int len) throws IOException{ |
|
||||
// addPos(len);
|
|
||||
return super.readNBytes(len); |
|
||||
} |
|
||||
|
|
||||
public byte readCByte() throws IOException{ |
|
||||
// addPos(1);
|
|
||||
return super.readByte(); |
|
||||
} |
|
||||
|
|
||||
// public int getPos() {
|
|
||||
// return pos;
|
|
||||
// }
|
|
||||
//
|
|
||||
// private synchronized void addPos(int value) {
|
|
||||
// this.pos += value;
|
|
||||
// }
|
|
||||
} |
|
@ -0,0 +1,24 @@ |
|||||
|
package com.xiaowuler.radarresolver.exceptions; |
||||
|
|
||||
|
/** |
||||
|
* @describe: 雷达读取异常 |
||||
|
* @author: xiaowuler |
||||
|
* @createTime: 2022-03-18 09:47 |
||||
|
*/ |
||||
|
public class RadarReadException extends Exception { |
||||
|
public RadarReadException(){ |
||||
|
super(); |
||||
|
} |
||||
|
|
||||
|
public RadarReadException(String message){ |
||||
|
super(message); |
||||
|
} |
||||
|
|
||||
|
public RadarReadException(String message, Throwable cause){ |
||||
|
super(message, cause); |
||||
|
} |
||||
|
|
||||
|
public RadarReadException(Throwable cause){ |
||||
|
super(cause); |
||||
|
} |
||||
|
} |
@ -0,0 +1,27 @@ |
|||||
|
package com.xiaowuler.radarresolver.weather.enums; |
||||
|
|
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Data; |
||||
|
import lombok.Getter; |
||||
|
import lombok.Setter; |
||||
|
|
||||
|
/** |
||||
|
* @describe: 雷达文件枚举 |
||||
|
* @author: xiaowuler |
||||
|
* @createTime: 2022-03-18 09:19 |
||||
|
*/ |
||||
|
@AllArgsConstructor |
||||
|
public enum RadarSuffixEnum { |
||||
|
/** |
||||
|
* 二进制 文件 后缀名 |
||||
|
*/ |
||||
|
BIN(".bin"), |
||||
|
|
||||
|
/** |
||||
|
* 压缩文件 后缀名 |
||||
|
*/ |
||||
|
BZIP2(".bz2"); |
||||
|
|
||||
|
@Getter |
||||
|
private final String type; |
||||
|
} |
Loading…
Reference in new issue