title: Axis2 ByteArray Web Service Return as String
body: <p>I am developing an Android application which suppose to call Axis2 Web Service (already implemented and running) where the previous developer made in such a way that Byte[] is the common form of passing Object around. Suppose there is a need to invoke web service in the Axis2 which should return List, the web service will convert the instance into Byte[] and return the Byte[], where the client will convert it back.</p>
  
  <p>Here is the snippet from the service:</p>
  
  <pre><code>public byte[] getMyPersonList() throws Exception {
      java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
      java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(baos);
      oos.writeObject(new List&lt;Person&gt;());
      return (baos.toByteArray());
  }
  </code></pre>
  
  <p>In the client (not my android application but the existing desktop application which are developed by the same people making the web service)</p>
  
  <pre><code>byte[] buffer = (byte[]) serviceClient.invokeBlocking(methodName,
              new Object[] {}, new Class[] { byte[].class })[0];
  java.io.ObjectInputStream ois = new java.io.ObjectInputStream(
              new java.io.ByteArrayInputStream(buffer));
  return (List&lt;Person&gt;) ois.readObject();
  </code></pre>
  
  <p>The invokeBlocking() method seems to automatically convert the returned value from the service automatically back to byte[], but I am having trouble doing the same thing in my application.</p>
  
  <p>This is what the return value looks like when I invoke the service manually over browser:</p>
  
  <p><code>&lt;return&gt;
  rO0ABXNyABNqYXZhLnV0aWwuQXJyYXlMaXN0eIHSHZnHYZ0DAAFJAARzaXpleHAAAAAJdwQAAAAKc3IAJ3dvcmtmbG9.......................
  &lt;/return&gt;</code></p>
  
  <p>And what I have done so far is to directly get the return as string and use getBytes():</p>
  
  <pre><code>byte [] soapReturnBytes = resultsRequestSOAP.getPropertyAsString("return").getBytes();
  ObjectInputStream ois = new ObjectInputStream(bais);
  myPersonList= (List&lt;Person&gt;) ois.readObject();
  </code></pre>
  
  <p>But I received stream corrupted error which could mean that it simply not the same content when I convert it back.</p>
  
  <p>So my question is:</p>
  
  <ol>
  <li>When returning Byte[], what does Axis2 engine do to convert the values to fit it into the SOAP return</li>
  <li>What are the methods of converting this 'stringified' value back to Byte[], in case I can't do anything about the web service (no modification on the web service)</li>
  </ol>
  
  <p>Any suggestion is greatly appreciated.</p>
  
  <p>EDIT:</p>
  
  <p>As suggested, I am using a decoder to get the result, however still no luck:</p>
  
  <pre><code>String result = resultsRequestSOAP.getPropertyAsString("return");           
  byte[] soapReturnBytes = Base64.decode(result, Base64.DEFAULT);
  ByteArrayInputStream bais = new ByteArrayInputStream(soapReturnBytes);
  ObjectInputStream ois = new ObjectInputStream(bais);
  orgs = (List&lt;Person&gt;) ois.readObject();
  </code></pre>
  
  <p>I still received the same error: </p>
  
  <pre><code>java.io.StreamCorruptedException: Wrong format: 0
  </code></pre>
  
  <p>Any suggestion? <em>getting frustrated</em></p>
  
  <p>EDIT 2:</p>
  
  <p>I created another service which returns String as byte[], and in my Android application I received the byte[] and convert it back to the original String. So perhaps it has something to do with the way I am using the Stream to convert my Object &lt;=> byte [].</p>