开发者

Concatenate ByteArrayOutputStream

开发者 https://www.devze.com 2023-02-05 17:54 出处:网络
public byte[] toByteArray(开发者_C百科) { try { ByteArrayOutputStream objectStream = dataObject.toByteArrayOutputStream();
public byte[] toByteArray(开发者_C百科) {
    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;
        }
    } 


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());


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


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();


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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消