1

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:

  1. return data:,
  2. return an empty String
  3. throw an Exception

By my read, RFC 2397 doesn't explicitly address the notion of empty data.

2
  • Options #2 and #3 aren't URIs, and exceptions should only be used for exceptional circumstances or errors. I believe the question therefore answers itself.
    – amon
    Feb 24 '14 at 20:28
  • @amon, you're assuming a valid data URI can exist when there is no data. I'm not convinced of that. On what grounds do you make that assumption?
    – BDKosher
    Feb 24 '14 at 20:40
5

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.