Concatenate ByteArrayOutputStream
Asked Answered
O

4

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;
        }
    } 
Omnipresent answered 19/1, 2011 at 13:15 Comment(0)
D
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());
Deauville answered 19/1, 2011 at 13:25 Comment(2)
I have tried this (even with dout.flush() between the writeUTF and the write. But the recordid is not added to the stream :SOmnipresent
This won't alter the original stream, you can't prepend data to that.Deauville
S
2

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

Swithbart answered 19/1, 2011 at 13:24 Comment(4)
This method is not present in J2ME: download.oracle.com/javame/config/cldc/ref-impl/cldc1.1/jsr139/…Lilytrotter
@Lilytrotter How was I to know it was J2ME? In J2ME you can simply say baos1.write( baos2.toByteArray(), 0, baos2.toByteArray().length );Swithbart
The OP explicitly commented it.Lilytrotter
@Lilytrotter 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.Swithbart
H
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();
Hayleyhayloft answered 19/1, 2011 at 13:23 Comment(1)
For your reference, API docs are in JSR139: download.oracle.com/javame/config/cldc/ref-impl/cldc1.1/jsr139/…Lilytrotter
M
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;
    }
}
Masterpiece answered 19/1, 2011 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.