Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FBNS:About replacement of deprecated methods

 
Jianhua Ren
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I just started the assignment and I am working on the replacement of two deprecated methods: public String(byte[] bytes, int hibyte, int offset, int length) and public void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin) .
I prefer to use the methods which takes the platform's default character-encoding, since I think if explicitly giving the encoding name, it may happens that the given encoding may not work under various environments. But I am not sure if platform's default character-encoding is always work for the mothods public byte[] getBytes() and public String(byte[] bytes, int offset, int length)?
For example, if platform's default character-encoding is UTF-16 bit, what happens if I use the getBytes() methods to convert bytes to string, only 8 lower-order bits in the string got copied in this case?
Could somebody explain a little bit to me?
Thanks a lot!
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jianhua,
Welcome to JavaRanch.
Originally posted by Jianhua Ren:
since I think if explicitly giving the encoding name, it may happens that the given encoding may not work under various environments.

The API documentation states "Every implementation of the Java platform is required to support the following standard charsets" (and then goes on to list which ones are always available) - as long as you use one of them, you should be fine
Originally posted by Jianhua Ren:
But I am not sure if platform's default character-encoding is always work for the mothods public byte[] getBytes() and public String(byte[] bytes, int offset, int length)?
For example, if platform's default character-encoding is UTF-16 bit, what happens if I use the getBytes() methods to convert bytes to string, only 8 lower-order bits in the string got copied in this case?

You have just hit on why you should not use the default character encoding - in the case you mentioned, the bytes will get translated into the system's default character set, which may render the data unreadable.
Regards, Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic