|
34 | 34 | import org.slf4j.Logger; |
35 | 35 | import org.slf4j.LoggerFactory; |
36 | 36 |
|
37 | | -import java.io.BufferedInputStream; |
38 | | -import java.io.Closeable; |
39 | | -import java.io.DataInputStream; |
40 | | -import java.io.EOFException; |
41 | | -import java.io.File; |
42 | | -import java.io.FilterInputStream; |
43 | | -import java.io.IOException; |
44 | | -import java.io.InputStream; |
| 37 | +import java.io.*; |
| 38 | +import java.nio.channels.ClosedByInterruptException; |
45 | 39 | import java.util.Optional; |
46 | 40 | import java.util.zip.Checksum; |
47 | 41 |
|
@@ -169,23 +163,28 @@ public long skip(long amt) throws IOException { |
169 | 163 | */ |
170 | 164 | boolean verifyHeader() throws IOException { |
171 | 165 | final int headerLength = SegmentedRaftLogFormat.getHeaderLength(); |
172 | | - final int readLength = in.read(temp, 0, headerLength); |
173 | | - Preconditions.assertTrue(readLength <= headerLength); |
174 | | - final int matchLength = SegmentedRaftLogFormat.matchHeader(temp, 0, readLength); |
175 | | - Preconditions.assertTrue(matchLength <= readLength); |
176 | | - |
177 | | - if (readLength == headerLength && matchLength == readLength) { |
178 | | - // The header is matched successfully |
179 | | - return true; |
180 | | - } else if (SegmentedRaftLogFormat.isTerminator(temp, matchLength, readLength - matchLength)) { |
181 | | - // The header is partially written |
182 | | - return false; |
| 166 | + try{ |
| 167 | + final int readLength = in.read(temp, 0, headerLength); |
| 168 | + Preconditions.assertTrue(readLength <= headerLength); |
| 169 | + final int matchLength = SegmentedRaftLogFormat.matchHeader(temp, 0, readLength); |
| 170 | + Preconditions.assertTrue(matchLength <= readLength); |
| 171 | + |
| 172 | + if (readLength == headerLength && matchLength == readLength) { |
| 173 | + // The header is matched successfully |
| 174 | + return true; |
| 175 | + } else if (SegmentedRaftLogFormat.isTerminator(temp, matchLength, readLength - matchLength)) { |
| 176 | + // The header is partially written |
| 177 | + return false; |
| 178 | + } |
| 179 | + // The header is corrupted |
| 180 | + throw new CorruptedFileException(file, "Log header mismatched: expected header length=" |
| 181 | + + SegmentedRaftLogFormat.getHeaderLength() + ", read length=" + readLength + ", match length=" + matchLength |
| 182 | + + ", header in file=" + StringUtils.bytes2HexString(temp, 0, readLength) |
| 183 | + + ", expected header=" + StringUtils.bytes2HexString(SegmentedRaftLogFormat.getHeaderBytebuffer())); |
| 184 | + } catch (ClosedByInterruptException e) { |
| 185 | + Thread.currentThread().interrupt(); |
| 186 | + throw new IOException("Interrupted while reading the header of " + file, e); |
183 | 187 | } |
184 | | - // The header is corrupted |
185 | | - throw new CorruptedFileException(file, "Log header mismatched: expected header length=" |
186 | | - + SegmentedRaftLogFormat.getHeaderLength() + ", read length=" + readLength + ", match length=" + matchLength |
187 | | - + ", header in file=" + StringUtils.bytes2HexString(temp, 0, readLength) |
188 | | - + ", expected header=" + StringUtils.bytes2HexString(SegmentedRaftLogFormat.getHeaderBytebuffer())); |
189 | 188 | } |
190 | 189 |
|
191 | 190 | /** |
|
0 commit comments