I have a method that generates a data URI from a byte array. In the event the input byte array is empty, what is the appropriate thing for the method to do? Here are the options I see:
- return
data:,
- return an empty String
- throw an Exception
By my read, RFC 2397 doesn’t explicitly address the notion of empty data.
2
RFC 2397 explicitly allows empty data. Let’s look at the relevant parts of the grammar:
dataurl := "data:" [ mediatype ] [ ";base64" ] "," data
...
data := *urlchar
The *
quantifier means zero or more repetitions, the square brackets contain optional parts. Therefore the shortest data-URL is indeed data:,
. In your context, you’ll probably use data:text/plain;base64,
or something like that instead.