5
public byte[] toByteArray() {
    try {
        ByteArrayOutputStream objectStream = dataObject.toByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(objectStream);
        dout.writeUTF(recordid);    

        dout.close();
        objectStream.close();
        return objectStream.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

There is a problem with the code above. I first create an objectStream (in another class). And then I manually add the recordid to the ByteArrayOutputStream. But is there a way to first add the recordId & then append the ByteArrayOutputStream to it? Basically I have 2 ByteArrayoutputStreams which need to be concatenated (and remain a ByteArrayOutputStream).

edit: My new version should work but it does not. When I print out the hashcode of dout, it is the same before and after the flush. It's like it stays empty? Is that possible?

public byte[] toByteArray() {
        try {

            ByteArrayOutputStream realOutputStream = new ByteArrayOutputStream();
            DataOutputStream dout = new DataOutputStream(realOutputStream);
            dout.writeUTF(dataObject.getClass().toString());
            dout.writeUTF(recordid);
            System.out.println("Recordid: " + recordid + "|" +  dout.hashCode());
            dout.flush();
            System.out.println("Recordid: " + recordid + "|" +  dout.hashCode());

            ByteArrayOutputStream objectStream = dataObject.toByteArrayOutputStream();
            dout.write(objectStream.toByteArray());

            dout.close();
            objectStream.close();
            return objectStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    } 
Vincent
  • 6,058
  • 15
  • 52
  • 94

4 Answers4

5

try the following to place the recordid first.

ByteArrayOutputStream objectStream = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(objectStream);
dout.writeUTF(recordid);    
dout.write(dataObject.toByteArrayOutputStream().toByteArray());
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

The method writeTo() will let you append the contents of a ByteArrayOutputStream to any other OutputStream.

biziclop
  • 48,926
  • 12
  • 77
  • 104
  • 1
    This method is not present in J2ME: http://download.oracle.com/javame/config/cldc/ref-impl/cldc1.1/jsr139/java/io/ByteArrayOutputStream.html – BalusC Jan 19 '11 at 13:26
  • @BalusC How was I to know it was J2ME? In J2ME you can simply say `baos1.write( baos2.toByteArray(), 0, baos2.toByteArray().length );` – biziclop Jan 19 '11 at 13:29
  • The OP explicitly commented it. – BalusC Jan 19 '11 at 13:30
  • @BalusC Yes, a bit later than I started writing the answer though. The OP didn't mention it in the question and didn't tag it properly either. – biziclop Jan 19 '11 at 13:37
0

I don't know what the API of ByteArrayOutputStream on J2ME is like but try:

ByteArrayOutputStream realOutput = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(realOutput);
dout.writeUTF(recordid);
dout.flush();

ByteArrayOutputStream objectStream = dataObject.toByteArrayOutputStream();
objectStream.writeTo(realOutput);

return realOutput.toByteArray();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    For your reference, API docs are in JSR139: http://download.oracle.com/javame/config/cldc/ref-impl/cldc1.1/jsr139/index.html – BalusC Jan 19 '11 at 13:26
0

You could write dataObject and recordid in the current (wrong) order then rotate them into place:

public byte[] toByteArray() {
    try {
        ByteArrayOutputStream objectStream = dataObject.toByteArrayOutputStream();
        int pos = objectStream.size();
        DataOutputStream dout = new DataOutputStream(objectStream);
        dout.writeUTF(recordid);    
        dout.close();
        objectStream.close();
        byte[] array = return objectStream.toByteArray();
        rotate(array, pos);
        return array;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

private static void rotate(byte[] array, int pos) {
    reverse(array, 0, pos);
    reverse(array, pos, array.length);
    reverse(array, 0, array.length);
}

private static void reverse(byte[] array, int start, int end) {
    while (start < --end) {
        byte t = array[start];
        array[start] = array[end];
        array[end] = t;
        ++ start;
    }
}
finnw
  • 47,861
  • 24
  • 143
  • 221