From 5f7a18392b505bded9d3e687a5b9dd0a2044c05d Mon Sep 17 00:00:00 2001 From: xiaowuler Date: Mon, 21 Mar 2022 16:32:59 +0800 Subject: [PATCH] modify some codes --- .../custom/impl/CustomRadarInputStream.java | 19 ++++++++++++++++--- .../core/weather/domain/RadialBlock.java | 9 +++++++-- .../radartest/RadarTestApplicationTests.java | 2 +- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/04.系统编码/03.radar-resolver/radar-core/src/main/java/com/xiaowuler/radar/core/custom/impl/CustomRadarInputStream.java b/04.系统编码/03.radar-resolver/radar-core/src/main/java/com/xiaowuler/radar/core/custom/impl/CustomRadarInputStream.java index 542e4e7..c276fb3 100644 --- a/04.系统编码/03.radar-resolver/radar-core/src/main/java/com/xiaowuler/radar/core/custom/impl/CustomRadarInputStream.java +++ b/04.系统编码/03.radar-resolver/radar-core/src/main/java/com/xiaowuler/radar/core/custom/impl/CustomRadarInputStream.java @@ -22,13 +22,15 @@ import com.xiaowuler.radar.core.custom.ICustomInputStream; */ public class CustomRadarInputStream implements ICustomInputStream, Closeable{ + private long pos = 0; + private long available = 0; 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 { + public CustomRadarInputStream(String filepath) throws IOException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { String fileSuffix = filepath.substring(filepath.lastIndexOf(".") + 1).trim().toLowerCase(Locale.ROOT); switch (fileSuffix){ @@ -43,13 +45,15 @@ public class CustomRadarInputStream implements ICustomInputStream, Closeable{ } } - private void customRadarInputStream(String filepath, Class clazz) throws NoSuchMethodException, FileNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException { + private void customRadarInputStream(String filepath, Class clazz) throws NoSuchMethodException, IOException, InvocationTargetException, InstantiationException, IllegalAccessException { fileInputStream = new FileInputStream(filepath); + available = new File(filepath).length(); inputStream = clazz.getConstructor(InputStream.class).newInstance(fileInputStream); } @Override public int readInt() throws IOException { +// addPos(4); int ch1 = inputStream.read(); int ch2 = inputStream.read(); int ch3 = inputStream.read(); @@ -70,12 +74,14 @@ public class CustomRadarInputStream implements ICustomInputStream, Closeable{ */ @Override public short readShort() throws IOException{ +// addPos(2); byte[] bytes = inputStream.readNBytes(2); return ByteUtils.bytes2Short(bytes); } @Override public long readLong() throws IOException{ +// addPos(8); byte[] bytes = inputStream.readNBytes(8); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); byteBuffer.order(ByteOrder.LITTLE_ENDIAN); @@ -84,17 +90,20 @@ public class CustomRadarInputStream implements ICustomInputStream, Closeable{ @Override public float readFloat() throws IOException { +// addPos(4); return Float.intBitsToFloat(readInt()); } @Override public byte[] readNBytes(int len) throws IOException{ +// addPos(len); return inputStream.readNBytes(len); } @Override public int available() throws IOException { - return inputStream.available(); + return (int)(available - pos); +// return fileInputStream.available(); } @Override @@ -115,4 +124,8 @@ public class CustomRadarInputStream implements ICustomInputStream, Closeable{ } } } + + private synchronized void addPos(int len){ + pos += len; + } } diff --git a/04.系统编码/03.radar-resolver/radar-core/src/main/java/com/xiaowuler/radar/core/weather/domain/RadialBlock.java b/04.系统编码/03.radar-resolver/radar-core/src/main/java/com/xiaowuler/radar/core/weather/domain/RadialBlock.java index 3e8c6f5..c4129e4 100644 --- a/04.系统编码/03.radar-resolver/radar-core/src/main/java/com/xiaowuler/radar/core/weather/domain/RadialBlock.java +++ b/04.系统编码/03.radar-resolver/radar-core/src/main/java/com/xiaowuler/radar/core/weather/domain/RadialBlock.java @@ -1,5 +1,6 @@ package com.xiaowuler.radar.core.weather.domain; +import java.io.EOFException; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -19,8 +20,12 @@ public class RadialBlock { public RadialBlock(ICustomInputStream ICustomInputStream) throws IOException { this.radials = new ArrayList<>(); - while (ICustomInputStream.available() > 0){ - this.radials.add(new Radial(ICustomInputStream)); + try{ + while (true){ + this.radials.add(new Radial(ICustomInputStream)); + } + }catch (EOFException e){ + } } } diff --git a/04.系统编码/03.radar-resolver/radar-test/src/test/java/com/xiaowuler/radartest/RadarTestApplicationTests.java b/04.系统编码/03.radar-resolver/radar-test/src/test/java/com/xiaowuler/radartest/RadarTestApplicationTests.java index 7586f55..3c9b75c 100644 --- a/04.系统编码/03.radar-resolver/radar-test/src/test/java/com/xiaowuler/radartest/RadarTestApplicationTests.java +++ b/04.系统编码/03.radar-resolver/radar-test/src/test/java/com/xiaowuler/radartest/RadarTestApplicationTests.java @@ -11,7 +11,7 @@ class RadarTestApplicationTests { @Test void contextLoads() throws RadarReadException { RadarReader radarReader = new RadarReader(); - radarReader.read("C:\\SaveFile\\Source\\202203\\RADAR\\SAD\\CAP\\Z_RADR_I_Z9558_20220321065017_O_DOR_SAD_CAP_FMT.bin.bz2"); + radarReader.read("C:\\Users\\xiaowuler\\Desktop\\test\\Z_RADR_I_Z9551_20210712000134_O_DOR_SA_CAP_FMT.bin.bz2"); System.out.println(""); }