0

I have a String converted with org.apache.axis2.databinding.utils.ConverterUtil to a Base64Binary (ByteArrayDataSource inside a DataHandler).

When I try to convert it back to the string, it doestn work. I cant figure out why. What am I missing?

Here`s the code:

@Test
  public void testBase64() {
    DataHandler test = ConverterUtil.convertToBase64Binary("TEST");
    try {
      BufferedReader br = new BufferedReader(new InputStreamReader(test.getDataSource().getInputStream()));
      StringBuilder sb = new StringBuilder();
      String line;

      while ((line = br.readLine()) != null) {
        sb.append(line);
      }
      br.close();

      String result = new String(Base64.decode(sb.toString()));

    } catch (IOException e) {
      e.printStackTrace();
    }

As you can see.. result string is empty.. I hope someone can help me with this.

Thanks

heiningair
  • 441
  • 1
  • 6
  • 23
  • Base64 transforms binary bytes into a readable string, and vice-versa. You're trying to decode a base64-encoded string into a string. That doesn't make much sense. It should be `byte[] bytes = Base64.decode(sb.toString())` – JB Nizet Aug 23 '13 at 11:03
  • I dont really get it.. so how do I get a string containing the value "TEST" out of it? – heiningair Aug 23 '13 at 11:14
  • Oh, I missed the part where you encoded a string. Forget what I said. I had a look at the javadoc of ConverterUtil.convertToBase64Binary(), but it doesn't have any. So I can't even tell what it does. Sorry. – JB Nizet Aug 23 '13 at 11:42
  • No problem.. I just found out what I was looking for.. The thing is that the convertToBase64Binary() method does a Base64.decode("string") inside it! So I have to encode the byte version of the string first and then decode it back.. that is stupid, but it works.. thanks anyway – heiningair Aug 23 '13 at 11:58

0 Answers0