{"record":{"id":"854d64508b37cdfb","repo":"SixLabors/ImageSharp","slug":"input-stream-does-not-have-enough-bytes-to-parse-declared","errorCode":null,"errorMessage":"Input stream does not have enough bytes to parse declared contents of the {marker:X2} marker.","messagePattern":"Input stream does not have enough bytes to parse declared contents of the (.+?) marker\\.","errorType":"exception","errorClass":"InvalidImageContentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs","lineNumber":14,"sourceCode":"// Copyright (c) Six Labors.\n// Licensed under the Six Labors Split License.\n\nnamespace SixLabors.ImageSharp.Formats.Jpeg;\n\ninternal static class JpegThrowHelper\n{\n    public static void ThrowNotSupportedException(string errorMessage) => throw new NotSupportedException(errorMessage);\n\n    public static void ThrowInvalidImageContentException(string errorMessage) => throw new InvalidImageContentException(errorMessage);\n\n    public static void ThrowBadMarker(string marker, int length) => throw new InvalidImageContentException($\"Marker {marker} has bad length {length}.\");\n\n    public static void ThrowNotEnoughBytesForMarker(byte marker) => throw new InvalidImageContentException($\"Input stream does not have enough bytes to parse declared contents of the {marker:X2} marker.\");\n\n    public static void ThrowBadQuantizationTableIndex(int index) => throw new InvalidImageContentException($\"Bad Quantization Table index {index}.\");\n\n    public static void ThrowBadQuantizationTablePrecision(int precision) => throw new InvalidImageContentException($\"Unknown Quantization Table precision {precision}.\");\n\n    public static void ThrowBadSampling() => throw new InvalidImageContentException(\"Bad sampling factor.\");\n\n    public static void ThrowBadSampling(int factor) => throw new InvalidImageContentException($\"Bad sampling factor: {factor}\");\n\n    public static void ThrowBadProgressiveScan(int ss, int se, int ah, int al) => throw new InvalidImageContentException($\"Invalid progressive parameters Ss={ss} Se={se} Ah={ah} Al={al}.\");\n\n    public static void ThrowInvalidImageDimensions(int width, int height) => throw new InvalidImageContentException($\"Invalid image dimensions: {width}x{height}.\");\n\n    public static void ThrowDimensionsTooLarge(int width, int height) => throw new ImageFormatException($\"Image is too large to encode at {width}x{height} for JPEG format.\");\n\n    public static void ThrowNotSupportedComponentCount(int componentCount) => throw new NotSupportedException($\"Images with {componentCount} components are not supported.\");\n\n    public static void ThrowNotSupportedColorSpace() => throw new NotSupportedException(\"Image color space could not be deduced.\");","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs#L1-L32","documentation":"JpegThrowHelper.ThrowNotEnoughBytesForMarker throws InvalidImageContentException when the input stream ends before the full declared contents of a marker segment could be read — the marker promised N bytes but the stream delivered fewer. This is a truncation signature for JPEG input.","triggerScenarios":"Image.Load/LoadAsync on a truncated JPEG: download/upload interrupted mid-file, stream closed early, or a seekable stream whose length was misreported so segment reads run past the end.","commonSituations":"Partial HTTP downloads; interrupted file copies; streaming a JPEG while it is still being written; reading from a truncated database blob or archive entry.","solutions":["Check the byte stream is complete (compare length to Content-Length/expected size) before decoding.","Re-download or re-copy the file; verify with a checksum.","Catch InvalidImageContentException and retry once with a fresh, fully-buffered copy of the data.","Avoid decoding files still being written; wait for write completion first."],"exampleFix":"// before\nusing var image = Image.Load(partialStream);\n// after\nusing var buffered = new MemoryStream();\npartialStream.CopyTo(buffered); // ensure fully read\buffered.Position = 0;\nusing var image = Image.Load(buffered);","handlingStrategy":"try-catch","validationCode":"// Ensure the stream is fully buffered and its length matches expectations before decoding.\nusing var buffered = new MemoryStream();\nsource.CopyTo(buffered);\nif (buffered.Length < expectedMinBytes)\n    throw new IOException(\"Source JPEG is truncated.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    using var image = Image.Load(stream);\n}\ncatch (InvalidImageContentException ex) when (ex.Message.Contains(\"does not have enough bytes\"))\n{\n    // truncated input: re-fetch or reject\n}","preventionTips":["Compare downloaded length to Content-Length before decoding.","Never decode files that are still being written; wait for completion signals.","Fully buffer network streams before handing them to the decoder."],"tags":["jpeg","decoder","truncated","invalid-image"],"backgroundTag":"corrupt-jpeg-data","analyzedSha":"59ce6af6fc29027cda277ef62d4d1694a8acce91","analyzedAt":"2026-09-13T18:34:59.331Z","contentChangedAt":"2026-09-13T18:34:59.331Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}