|
| 1 | +package dotty.tools.tasty |
| 2 | + |
| 3 | +import java.util.UUID |
| 4 | + |
| 5 | +import TastyFormat.{MajorVersion, MinorVersion, header} |
| 6 | + |
| 7 | +class TastyHeaderUnpickler(reader: TastyReader) { |
| 8 | + import reader._ |
| 9 | + |
| 10 | + def this(bytes: Array[Byte]) = this(new TastyReader(bytes)) |
| 11 | + |
| 12 | + /**delegates to `readFullHeader`, expecting the following |
| 13 | + * - `majorVersion == TastyFormat.MajorVersion` |
| 14 | + * - `0 <= minorVersion <= TastyFormat.MinorVersion` |
| 15 | + */ |
| 16 | + def readHeader(): UUID = { |
| 17 | + val h = readFullHeader() |
| 18 | + val validVersion = ( |
| 19 | + h.majorVersion == MajorVersion |
| 20 | + && h.minorVersion >= 0 && h.minorVersion <= MinorVersion |
| 21 | + ) |
| 22 | + check(validVersion, |
| 23 | + s"""TASTy signature has wrong version. |
| 24 | + | expected: $MajorVersion.$MinorVersion |
| 25 | + | found : ${h.majorVersion}.${h.minorVersion}""".stripMargin |
| 26 | + ) |
| 27 | + h.uuid |
| 28 | + } |
| 29 | + |
| 30 | + /**Reads the header of a Tasty File |
| 31 | + */ |
| 32 | + def readFullHeader(): TastyHeader = { |
| 33 | + for (i <- 0 until header.length) |
| 34 | + check(readByte() == header(i), "not a TASTy file") |
| 35 | + val majorVersion = readNat() |
| 36 | + val minorVersion = readNat() |
| 37 | + val uuid = new UUID(readUncompressedLong(), readUncompressedLong()) |
| 38 | + TastyHeader(uuid, majorVersion, minorVersion) |
| 39 | + } |
| 40 | + |
| 41 | + def isAtEnd: Boolean = reader.isAtEnd |
| 42 | + |
| 43 | + private def check(cond: Boolean, msg: => String): Unit = { |
| 44 | + if (!cond) throw new UnpickleException(msg) |
| 45 | + } |
| 46 | +} |
0 commit comments