|
| 1 | +package dotty.tools.tasty |
| 2 | + |
| 3 | +import java.util.UUID |
| 4 | + |
| 5 | +import TastyFormat.{MajorVersion, MinorVersion, header} |
| 6 | + |
| 7 | +sealed abstract case class TastyHeader(uuid: UUID, majorVersion: Int, minorVersion: Int) |
| 8 | + |
| 9 | +class TastyHeaderUnpickler(reader: TastyReader) { |
| 10 | + import reader._ |
| 11 | + |
| 12 | + def this(bytes: Array[Byte]) = this(new TastyReader(bytes)) |
| 13 | + |
| 14 | + /** delegates to `readFullHeader`, extracting the UUID */ |
| 15 | + def readHeader(): UUID = |
| 16 | + readFullHeader().uuid |
| 17 | + |
| 18 | + /**Reads the header of a Tasty File, the returned header, `h`, |
| 19 | + * has the following properties: |
| 20 | + * - `h.majorVersion == TastyFormat.MajorVersion` |
| 21 | + * - `0 <= h.minorVersion <= TastyFormat.MinorVersion` |
| 22 | + */ |
| 23 | + def readFullHeader(): TastyHeader = { |
| 24 | + for (i <- 0 until header.length) |
| 25 | + check(readByte() == header(i), "not a TASTy file") |
| 26 | + val majorVersion = readNat() |
| 27 | + val minorVersion = readNat() |
| 28 | + val validVersion = ( |
| 29 | + majorVersion == MajorVersion |
| 30 | + && minorVersion >= 0 && minorVersion <= MinorVersion |
| 31 | + ) |
| 32 | + check(validVersion, |
| 33 | + s"""TASTy signature has wrong version. |
| 34 | + | expected: $MajorVersion.$MinorVersion |
| 35 | + | found : ${majorVersion}.${minorVersion}""".stripMargin |
| 36 | + ) |
| 37 | + val uuid = new UUID(readUncompressedLong(), readUncompressedLong()) |
| 38 | + new 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