handle exception
diff --git a/java/tsfile/src/main/java/org/apache/tsfile/exception/NotCompatibleTsFileException.java b/java/tsfile/src/main/java/org/apache/tsfile/exception/NotCompatibleTsFileException.java index abcb628..71e1f32 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/exception/NotCompatibleTsFileException.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/exception/NotCompatibleTsFileException.java
@@ -26,4 +26,8 @@ public NotCompatibleTsFileException(String message) { super(message); } + + public NotCompatibleTsFileException(Throwable cause) { + super(cause); + } }
diff --git a/java/tsfile/src/main/java/org/apache/tsfile/exception/TsFileRuntimeException.java b/java/tsfile/src/main/java/org/apache/tsfile/exception/TsFileRuntimeException.java index fe9fa93..85b0161 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/exception/TsFileRuntimeException.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/exception/TsFileRuntimeException.java
@@ -34,4 +34,8 @@ public TsFileRuntimeException(Throwable cause) { super(cause); } + + public TsFileRuntimeException(String message, Throwable cause) { + super(message, cause); + } }
diff --git a/java/tsfile/src/main/java/org/apache/tsfile/read/TsFileSequenceReader.java b/java/tsfile/src/main/java/org/apache/tsfile/read/TsFileSequenceReader.java index 29fc5e8..5af148f 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/read/TsFileSequenceReader.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/read/TsFileSequenceReader.java
@@ -29,6 +29,7 @@ import org.apache.tsfile.encrypt.EncryptUtils; import org.apache.tsfile.encrypt.IDecryptor; import org.apache.tsfile.enums.TSDataType; +import org.apache.tsfile.exception.NotCompatibleTsFileException; import org.apache.tsfile.exception.StopReadTsFileByInterruptException; import org.apache.tsfile.exception.TsFileRuntimeException; import org.apache.tsfile.exception.TsFileStatisticsMistakesException; @@ -157,9 +158,9 @@ } this.file = file; tsFileInput = FSFactoryProducer.getFileInputFactory().getTsFileInput(file); - loadFileVersion(); try { + loadFileVersion(); if (loadMetadataSize) { loadMetadataSize(); } @@ -223,16 +224,21 @@ } private void loadFileVersion() throws IOException { - tsFileInput.position(TSFileConfig.MAGIC_STRING.getBytes(TSFileConfig.STRING_CHARSET).length); - final ByteBuffer buffer = ByteBuffer.allocate(1); - tsFileInput.read(buffer); - buffer.flip(); - fileVersion = buffer.get(); + try { + tsFileInput.position(TSFileConfig.MAGIC_STRING.getBytes(TSFileConfig.STRING_CHARSET).length); + final ByteBuffer buffer = ByteBuffer.allocate(1); + tsFileInput.read(buffer); + buffer.flip(); + fileVersion = buffer.get(); - checkFileVersion(); - configDeserializer(); + checkFileVersion(); + configDeserializer(); - tsFileInput.position(0); + tsFileInput.position(0); + } catch (Exception e) { + tsFileInput.close(); + throw new NotCompatibleTsFileException(e); + } } private void configDeserializer() {
diff --git a/java/tsfile/src/main/java/org/apache/tsfile/write/writer/RestorableTsFileIOWriter.java b/java/tsfile/src/main/java/org/apache/tsfile/write/writer/RestorableTsFileIOWriter.java index 2083708..7b1d123 100644 --- a/java/tsfile/src/main/java/org/apache/tsfile/write/writer/RestorableTsFileIOWriter.java +++ b/java/tsfile/src/main/java/org/apache/tsfile/write/writer/RestorableTsFileIOWriter.java
@@ -107,29 +107,35 @@ return; } - if (file.exists()) { - try (TsFileSequenceReader reader = new TsFileSequenceReader(file.getAbsolutePath(), false)) { - schema.setEnabledUpdateSchema(false); - truncatedSize = reader.selfCheck(schema, chunkGroupMetadataList, true); - minPlanIndex = reader.getMinPlanIndex(); - maxPlanIndex = reader.getMaxPlanIndex(); - if (truncatedSize == TsFileCheckStatus.COMPLETE_FILE) { - crashed = false; - canWrite = false; - out.close(); - } else if (truncatedSize == TsFileCheckStatus.INCOMPATIBLE_FILE) { - out.close(); - throw new NotCompatibleTsFileException( - String.format("%s is not in TsFile format.", file.getAbsolutePath())); - } else { - crashed = true; - canWrite = true; - // remove broken data - if (truncate) { - out.truncate(truncatedSize); + try { + if (file.exists()) { + try (TsFileSequenceReader reader = + new TsFileSequenceReader(file.getAbsolutePath(), false)) { + schema.setEnabledUpdateSchema(false); + truncatedSize = reader.selfCheck(schema, chunkGroupMetadataList, true); + minPlanIndex = reader.getMinPlanIndex(); + maxPlanIndex = reader.getMaxPlanIndex(); + if (truncatedSize == TsFileCheckStatus.COMPLETE_FILE) { + crashed = false; + canWrite = false; + out.close(); + } else if (truncatedSize == TsFileCheckStatus.INCOMPATIBLE_FILE) { + out.close(); + throw new NotCompatibleTsFileException( + String.format("%s is not in TsFile format.", file.getAbsolutePath())); + } else { + crashed = true; + canWrite = true; + // remove broken data + if (truncate) { + out.truncate(truncatedSize); + } } } } + } catch (Exception e) { + out.close(); + throw e; } }