连接ByteArrayOutputStream

时间:2011-01-19 13:15:17

标签: java java-me outputstream bytearrayoutputstream

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

上面的代码存在问题。我首先创建一个objectStream(在另一个类中)。然后我手动将recordid添加到ByteArrayOutputStream。但有没有办法首先添加recordId&然后将ByteArrayOutputStream附加到它?基本上我有2个ByteArrayoutputStreams需要连接(并保持ByteArrayOutputStream)。

编辑: 我的新版本应该有效,但事实并非如此。当我打印出dout的哈希码时,它在刷新之前和之后是相同的。它就像是空着的?这可能吗?

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

4 个答案:

答案 0 :(得分:4)

尝试以下方法将recordid放在首位。

ByteArrayOutputStream objectStream = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(objectStream);
dout.writeUTF(recordid);    
dout.write(dataObject.toByteArrayOutputStream().toByteArray());

答案 1 :(得分:2)

方法writeTo()可让您将ByteArrayOutputStream的内容附加到任何其他OutputStream

答案 2 :(得分:0)

我不知道J2ME上ByteArrayOutputStream的API是什么样的,但请尝试:

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

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

return realOutput.toByteArray();

答案 3 :(得分:0)

您可以按当前(错误)顺序编写dataObjectrecordid,然后将其旋转到位:

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