17

I'm writing a Hadoop/HBase job. I needed to transform a Java String into a byte array. Is there any differences between Java's String.getBytes() and Hadoop's Bytes.toBytes()?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
victorunique
  • 310
  • 2
  • 3
  • 12

2 Answers2

23

According to its documentation Bytes.toBytes() converts the parameter to a byte[] using UTF-8.

String.getBytes() (without arguments) will convert the String to byte[] using the platform default encoding. That encoding can vary depending on the OS and user settings. Use of that method should generally be avoided.

You could use String.getBytes(String) (or the Charset variant) to specify the encoding to be used.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
8

Reading the Javadoc, it appear that String.getBytes() returns a byte[] using the default encoding and Bytes.toBytes() returns a byte[] using UTF-8

This could be the same thing, but it might not be.

Its always useful to read the Javadoc if you want to know something. ;)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130