Java String.getBytes( charsetName ) vs String.getBytes ( Charset object )

后端 未结 4 2070
后悔当初
后悔当初 2020-12-15 18:00

I need to encode a String to byte array using UTF-8 encoding. I am using Google guava, it has Charsets class already define Charset instance for UTF-8 encoding. I have 2 way

相关标签:
4条回答
  • 2020-12-15 18:45

    The first API is for situations when you do not know the charset at compile time; the second one is for situations when you do. Since it appears that your code needs UTF-8 specifically, you should prefer the second API:

    byte[] bytes = my_input.getBytes ( Charsets.UTF_8 ); // <<== UTF-8 is known at compile time
    

    The first API is for situations when the charset comes from outside your program - for example, from the configuration file, from user input, as part of a client request to the server, and so on. That is why there is a checked exception thrown from it - for situations when the charset specified in the configuration or through some other means is not available.

    0 讨论(0)
  • 2020-12-15 18:49

    If you already have the Charset, then use the 2nd version as it's less error prone.

    0 讨论(0)
  • 2020-12-15 18:58

    If you are going to use a string literal (e.g. "UTF-8") ... you shouldn't. Instead use the second version and supply the constant value from StandardCharsets (specifically, StandardCharsets.UTF_8, in this case).

    The first version is used when the charset is dynamic. This is going to be the case when you don't know what the charset is at compile time; it's being supplied by an end user, read from a config file or system property, etc.

    Internally, both methods are calling a version of StringCoding.encode(). The first version of encode() is simply looking up the Charset by the supplied name first, and throwing an exception if that charset is unknown / not available.

    0 讨论(0)
  • 2020-12-15 18:59

    Since they return the same result, you should use method 2 because it generally safer and more efficient to avoid asking the library to parse and possibly break on a user-supplied string. Also, avoiding the try-catch will make your own code cleaner as well.

    The Charsets.UTF_8 can be more easily checked at compile-time, which is most likely the reason you do not need a try-catch.

    0 讨论(0)
提交回复
热议问题