{"record":{"id":"c00c4d2148f0b3ba","repo":"SubtitleEdit/subtitleedit","slug":"mxf-klv-packet-stream-does-not-have-16-bytes-ava","errorCode":null,"errorMessage":"MXF KLV packet - stream does not have 16 bytes available for key","messagePattern":"MXF KLV packet - stream does not have 16 bytes available for key","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/libse/ContainerFormats/MaterialExchangeFormat/KlvPacket.cs","lineNumber":37,"sourceCode":"        public static readonly byte[] DataDefinitionAudio = { 0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x01, 0x01, 0x03, 0x02, 0x02, 0x02, 0xff, 0xff, 0x00 };\n        public static readonly byte[] Primer = { 0x06, 0xe, 0x2b, 0x34, 0x02, 0x05, 0x1, 0xff, 0x0d, 0x01, 0x02, 0x01, 0x01, 0x05, 0x01, 0xff };\n\n        private const int KeySize = 16;\n\n        public byte[] Key;\n        public long DataSize;\n        public long TotalSize;\n        public long DataPosition;\n        public PartitionStatus PartionStatus = PartitionStatus.Unknown;\n\n        public KlvPacket(Stream stream)\n        {\n            // read 16-bytes key\n            Key = new byte[KeySize];\n            int read = stream.Read(Key, 0, Key.Length);\n            if (read < Key.Length)\n            {\n                throw new Exception(\"MXF KLV packet - stream does not have 16 bytes available for key\");\n            }\n            int lengthSize;\n            DataSize = GetBasicEncodingRuleLength(stream, out lengthSize);\n            DataPosition = stream.Position;\n            TotalSize = KeySize + lengthSize + DataSize;\n            if (Key[14] >= 1 && Key[14] <= 4)\n            {\n                PartionStatus = (PartitionStatus)Key[14];\n            }\n        }\n\n        /// <summary>\n        /// Read length - never be more than 9 bytes in size (which means max 8 bytes of payload length)\n        /// There are four kinds of encoding for the Length field: 1-byte, 2-byte, 4-byte\n        /// </summary>\n        /// <param name=\"stream\"></param>\n        /// <param name=\"bytesInLength\"></param>\n        /// <returns></returns>","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/libse/ContainerFormats/MaterialExchangeFormat/KlvPacket.cs#L19-L55","documentation":"KlvPacket's constructor reads a 16-byte SMPTE KLV key from the stream; if Stream.Read returns fewer than 16 bytes the stream is truncated or out of sync, so parsing cannot continue and the constructor throws. This typically fires when an MXF parser is pointed at a position that is not actually the start of a KLV triplet.","triggerScenarios":"Calling new KlvPacket(stream) when the stream position is mid-record or past EOF; corrupted/truncated MXF file; wrong partition offset assumed by the caller; stream wrapping a non-MXF payload.","commonSituations":"Seeking to an offset derived from a corrupt header pack; iterating KLV triplets until EOF where the last packet is incomplete; reading an MXF captured mid-write; mismatch between expected and actual MXF flavor.","solutions":["Check stream.Length - stream.Position >= 16 before constructing KlvPacket.","Wrap the iteration in try/catch and treat a short key as end-of-index rather than fatal.","Validate the file signature (MXF header partition pack Universal Label) before iterating.","Re-transfer the MXF from the source if it is truncated."],"exampleFix":"// before\nKey = new byte[KeySize];\nint read = stream.Read(Key, 0, Key.Length);\nif (read < Key.Length) throw new Exception(\"MXF KLV packet - stream does not have 16 bytes available for key\");\n\n// after\nKey = new byte[KeySize];\nint read = stream.Read(Key, 0, Key.Length);\nif (read < Key.Length) throw new InvalidDataException($\"Short KLV key: read {read} of {Key.Length} bytes at offset {stream.Position}\");","handlingStrategy":"validation","validationCode":"if (stream == null || !stream.CanRead) throw new ArgumentException(\"stream must be readable\");\nif (stream.Length - stream.Position < 16) throw new InvalidDataException(\"Fewer than 16 bytes remain for a KLV key\");","typeGuard":null,"tryCatchPattern":"try { var pkt = new KlvPacket(stream); }\ncatch (Exception ex) when (ex.Message.Contains(\"16 bytes\")) { /* end of index */ }","preventionTips":["Validate stream position alignment with a known KLV triplet boundary.","Check the file signature before iterating triplets."],"tags":["mxf","klv","smpte","container-format","truncated"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}