LoginSignup
0

More than 5 years have passed since last update.

How to convert `org.apache.hadoop.io.Text` to byte array?

Last updated at Posted at 2014-12-05

How to convert org.apache.hadoop.io.Text to byte array?

This class has a "sweet" method named getBytes. Seems we can directly convert it to byte array using this method.

If you have similar idea, don't do that! It takes me almost an hour to find this stupid bug.

According to official documentation:

getBytes():

Returns the raw bytes; however, only data up to getLength() is valid

So the bytes you get may contain invalid data!

I think one solution is through Bytes.toByte

stupid.java

Bytes.toByte(text.toString())

Who or what kind of application will use the getBytes method? I totally have no idea.

What can we learn from this?

As library developer,

  • Don't expect everyone will read the whole documentation especially when your library contains tens of thousands of methods.

  • Never expose dangerous methods like getBytes. Or you can name it differently like getBytesUnsafe to call the attention of library users.

As programmer,

  • Be familiar with every method you have called.

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0