Re: UTF - 8 / Byte Stream
What do you mean by validate? Really the only thing you can do if you're trying to read from a file is to see if interpreting the file as UTF-8 encoded is what it's suppose to look like.
If you have a stream, send a known character and see if you get the same expected character when you try to read it (I would recommend sending multiple known characters as many character encoding formats have similar layouts).
Re: UTF - 8 / Byte Stream
Quote:
Originally Posted by
helloworld922
What do you mean by validate? Really the only thing you can do if you're trying to read from a file is to see if interpreting the file as UTF-8 encoded is what it's suppose to look like.
If you have a stream, send a known character and see if you get the same expected character when you try to read it (I would recommend sending multiple known characters as many character encoding formats have similar layouts).
no, actually, this is security problem. I would take in a stream byte. And I would have to check it if its valid UTF-8. There is some way to do that. Probably i would need to go byte by byte or bit by bit to see its valid UTF-8 format. The main logic is to not to accept anything else then UTF-8, and if it does encounter something thats not UTF-8, then say, "NOT Valid Stream"
Re: UTF - 8 / Byte Stream
Take a look at Wikipedia: UTF-8. The only way you would know if you had an invalid UTF-8 character is if it's one of those who's hex sequence is invalid to the UTF-8 encoding. However, if the stream somehow passed another valid UTF-8 character in place of another UTF-8 character, you would have no way of knowing if that character was what the sender intended to send.
You're probably better off asking for something like a MD5 hash from the sender and then checking that against the data you received.
Re: UTF - 8 / Byte Stream
Quote:
Originally Posted by
helloworld922
Take a look at
Wikipedia: UTF-8. The only way you would know if you had an invalid UTF-8 character is if it's one of those who's hex sequence is invalid to the UTF-8 encoding. However, if the stream somehow passed another valid UTF-8 character in place of another UTF-8 character, you would have no way of knowing if that character was what the sender intended to send.
You're probably better off asking for something like a MD5 hash from the sender and then checking that against the data you received.
I was thinking the same thing, but I have to write a java program as Exercise, I did look that wiki article, but there were many different articles that said something different then wiki did.
I was hoping anyone here would know how to properly validate that.
Re: UTF - 8 / Byte Stream
You could also try opening a character stream to use UTF-8 encoding.
See: Byte Encodings and Strings (The Java™ Tutorials > Internationalization > Working with Text)
I don't know if the Java stream will complain if there's an invalid character, you'll need to check on this.
I believe the reason why there are many different listings for what is a valid UTF-8 and what isn't is because the UTF-8 standard has changed a few times. Make sure you're using an up-to-date listing (I'm pretty sure the Wikipedia listing is current).