Вопрос

I understand that this can be done using wrapper pattern, but i am having a hard time understanding, how does the following code work.

ByteArrayOutputStream bytearray = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(bytearray);

passing ByteArrayOutputStream reference into the constructor of DataOutputStream means that the DataOutputStream gets converted to ByteArrayOutputStream, but how?

After this, os.writeUTF("String"); How does DataOutputStream convert to ByteArrayOutputStream.

What is happening behind the scenes? Can somebody please explain the details.

Это было полезно?

Решение

The DataOutputStream only requires any implementation of an OutputStream in its constructor. It doesn't care how it's implemented only that it satisfies the interface. A ByteArrayOutputStream provides an output stream backed by a byte array. Similarly, you could use a FileOutputStream to get a stream backed by a file on disk. From the DataOutputStream's perspective, they are both streams that used with an interface. OutputStream is a really simple interface, and just defines some methods that write byte arrays to some kind of storage device.

There's no magic going on under the scenes, it just uses an OutputStream and doesn't care how it's implemented.

Другие советы

Actually this can be called Decorator pattern. DataOutputStream takes any existing OutputStream (ByteArrayOutputStream in our case) and stores a reference to it in

protected OutputStream out;

(Actually it is in java.io.FilterOutputStream that DataOutputStream extends).

Now lets see how DataOutputStream.writeInt works (writeUTF is too heavy)

public final void writeInt(int v) throws IOException {
    out.write((v >>> 24) & 0xFF);
    out.write((v >>> 16) & 0xFF);
    out.write((v >>>  8) & 0xFF);
    out.write((v >>>  0) & 0xFF);
...

it simply takes an int arg and writes its 4 bytes, byte by byte, using the regular OutputStream.write(byte). In our case these bytes will be written into ByteArrayOutputStream internal byte array. If can change ByteArryOutputStream to FileOuputStream and the data will be written to a file, DataOutuputStream does not need to know about the actual implementation.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top