User: rednesto Date: 07 Jan 24 11:30 Revision: 93f5b9e4002f37b3534552d9a13d26a47a6e7286 Summary: Fix minecraft-dev/mcdev-error-report#1594 TeamCity URL: http://ci.mcdev.io:80/viewModification.html?tab=vcsModificationFiles&modId=8968&personal=false Index: src/main/kotlin/nbt/filetype/NbtFileTypeDetector.kt =================================================================== --- src/main/kotlin/nbt/filetype/NbtFileTypeDetector.kt (revision 09d6a08c615aca9f31374eaa7df258def63a899b) +++ src/main/kotlin/nbt/filetype/NbtFileTypeDetector.kt (revision 93f5b9e4002f37b3534552d9a13d26a47a6e7286) @@ -26,22 +26,27 @@ import com.intellij.openapi.fileTypes.FileTypeRegistry import com.intellij.openapi.util.io.ByteSequence import com.intellij.openapi.vfs.VirtualFile +import java.io.ByteArrayInputStream +import java.io.EOFException class NbtFileTypeDetector : FileTypeRegistry.FileTypeDetector { override fun detect(file: VirtualFile, firstBytes: ByteSequence, firstCharsIfText: CharSequence?): FileType? { return try { // 20 ms is plenty of time to parse most files // Won't parse very large files, but if we fail on timeout then those files probably are NBT anyways - Nbt.buildTagTree(file.inputStream, 20) + Nbt.buildTagTree(ByteArrayInputStream(firstBytes.toBytes()), 20) NbtFileType } catch (e: Throwable) { - if (e is NbtFileParseTimeoutException) { + when (e) { // If a timeout occurred then no file structure errors were detected in the parse time, so we can // probably assume it's a (very big) NBT file - NbtFileType - } else { - null + is NbtFileParseTimeoutException -> NbtFileType + // If we reach the end of the stream without another error then let's assume it is a valid NBT file + is EOFException -> NbtFileType + else -> null } } } + + override fun getDesiredContentPrefixLength(): Int = 1024 * 10 }