BufReader error stream did not contain valid UTF-8

Hi guys,

I'm struggeling to read UTF-8 using a BufReader that by_ref().lines().next() returns some Option which fails to unwrap with stream did not contain valid UTF-8.

My question is how to continue debugging. First I had a closer look at the file itself that appears finest UTF-8 to me:

BuschBo:~/Rust/iCalendarR> head ~/Pi-Hole/pi5/local/radicale/collection-root/2f41b39a-eabb-58c4-e154-cb47276bc325/ec1it0rjvk31d7keqi653c2930@google.com.ics

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//PYVOBJECT//NONSGML Version 1//EN
BEGIN:VEVENT
UID:ec1it0rjvk31d7keqi653c2930@google.com
DTSTART;VALUE=DATE:20170616
DTEND;VALUE=DATE:20170617
CREATED:20160809T135103Z
DTSTAMP:20240131T173430Z
LAST-MODIFIED:20160809T135103Z

BuschBo:~/Rust/iCalendarR> xxd -p ~/Pi-Hole/pi5/local/radicale/collection-root/2f41b39a-eabb-58c4-e154-cb47276bc325/ec1it0rjvk31d7keqi653c2930@google.com.ics

424547494e3a5643414c454e4441520d0a56455253494f4e3a322e300d0a
50524f4449443a2d2f2f5059564f424a4543542f2f4e4f4e53474d4c2056
657273696f6e20312f2f454e0d0a424547494e3a564556454e540d0a5549
443a656331697430726a766b333164376b65716936353363323933304067
6f6f676c652e636f6d0d0a445453544152543b56414c55453d444154453a
32303137303631360d0a4454454e443b56414c55453d444154453a323031
37303631370d0a435245415445443a323031363038303954313335313033
5a0d0a44545354414d503a3230323430313331543137333433305a0d0a4c
4153542d4d4f4449464945443a3230313630383039543133353130335a0d
0a53455155454e43453a300d0a5354415455533a434f4e4649524d45440d
0a53554d4d4152593a5a6575676e69736175736761626520626c61756572
20426c6f636b0d0a5452414e53503a5452414e53504152454e540d0a454e
443a564556454e540d0a454e443a5643414c454e4441520d0a

All linefeeds are \r\n (0d0a) and the rest of the content seems OK as well. The BufReader delivers some non-UTF-8-bytes in the first row of that above file and I have no idea where to continue search.

Any ideas where to search for the course of (my) usage of BufReader failing with stream did not contain valid UTF-8?

read the raw bytes into a buffer and then call std::str::from_utf8(), it should give a Utf8Error, you can call valid_up_to() method to get the location of the first invalid utf8 sequence.

5 Likes

Thank you, I'll take that road searching.