0

Is there any way to write DataOutputStream content to an Array or a String regardless which type of data it contains?

DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(String dataPath)));

Thanks

Jürgen K.
  • 3,427
  • 9
  • 30
  • 66
  • Have you tried storing it in an array as an object or parsing whatever it contains to a string? – James Jan 28 '16 at 14:36
  • No, working on the second right now.Any idea how to parse? – Jürgen K. Jan 28 '16 at 14:41
  • You can use ByteArrayOutputStream as mentioned in the below link *http://stackoverflow.com/questions/2984538/how-to-use-bytearrayoutputstream-and-dataoutputstream-simultaneously-java* – Sunil Jan 28 '16 at 14:48
  • @JürgenK. I wasn't sure that they would work, I haven't tried them, it was just a quick thought. What is the kind of contents of the stream? Just characters of text? – James Jan 28 '16 at 14:51
  • @JürgenK. If they are sent as an actual double type, then this wouldn't work unless you explicitly knew when it was going to be a double or not. If you did, you would use: Double.parseDouble(input) and then for the strings you could simply cast them as a string (String) because they hold that type already. – James Jan 28 '16 at 14:55
  • @Sunil. I don't think this works. It a actually a solution to a different question – Jürgen K. Jan 28 '16 at 15:01
  • @James. Which input do you mean? – Jürgen K. Jan 28 '16 at 15:01
  • @JürgenK. whatever is coming from your stream. – James Jan 28 '16 at 15:05
  • @James. Well, thanks for that but it still doesn't solve the problem – Jürgen K. Jan 28 '16 at 15:07

1 Answers1

2

Use ByteArrrayOutputStream.

https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayOutputStream.html

ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream os = new DataOutputStream(baos);
os.write(...);
byte[] data = baos.toByteArray();
String dataAsString = new String(data, "UTF-8"); // or whatever encoding you are using

You may use the following strategy as well:

class CompositeOutputStream implements OutputStream {
    private OutputStream first,second;
    public CompositeOutputStream(OutputStream first, OutputStream second) {
        this.first = first;
        this.second=second;
    }

    public void write(int b) throws IOException {
        first.write(b);
        second.write(b);
    }

    // etc.
}

Use with:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream os = new CompositeOutputStream(new DataOutputStream(...), baos);
os.write(...);
byte[] data = baos.toByteArray();
String dataAsString = new String(data, "UTF-8"); // or whatever encoding you are using
// etc.

The "baos" is only a "mirror" of what's got written to your original DataOutputStream

You still need to handle exceptions correctly, and be carefull about the amount of data written (holding everything in memory may lead to out of memory), etc.

spi
  • 626
  • 4
  • 19
  • Is it possible to copy the content of the DataOutPutStream to the ByteArrayOutputStream? Using something else than the DataOutputStream is otherwise not an option – Jürgen K. Jan 28 '16 at 14:43
  • You mean, after everything has been written, you need to get a "log" of what has been produced? I'm afraid nothing like that exists in the base lib, but you may implement it yourself easily (with a composite pattern). See my edit – spi Jan 28 '16 at 14:44
  • Yes, the final result is already inside of the DataOutPutStream. I want to get it without writting it to a file. – Jürgen K. Jan 28 '16 at 14:48
  • Actually i don't understand how your compositeOutputStream can might work=( – Jürgen K. Jan 28 '16 at 14:54
  • 1
    Each time you write something to the first stream, you write the same thing to the other. after everything has been written, you may use the reference to the given OS to retrieve the data. The problem is, after a data has been "flushed" on a stream (either a file, a network, etc), the stream does not hold that data anymore (it got garbage collected) thus you can not get it anymore – spi Jan 28 '16 at 15:04
  • * only ByteArrayOutputStream provides you a way the get an history of what has been written – spi Jan 28 '16 at 15:36