How to time-align two Streams of data
<p>i want to chronologically arrange two streams of data with overlapping timestamps. I have a rudimentary pipe and filter framework and am working with two PipedInputStreams. </p>

<p>This is the formatting of the byte stream. I have five pairs of (id, data point), then the next frame starts again with id 0:</p>

<pre><code>DATA ID (int) TIMESTAMP (long)  DATA ID (int)  ...   DATA ID (int)  ...

0             ...               1              ...   2              ...
</code></pre>

<p>I want to check the timestamp of the two data streams, decide which one is earlier and then read the whole frame. Then I want to get the next timestamp, compare it with the newer one from before, and continue.</p>

<p>Is there a best practice for this kind of alignment?</p>

<p>For example, I can extract the first row of ids from the input streams like this:</p>

<pre><code>// Read id
for (int i = 0; i &lt; this.getAmountOfInputPipes(); i++) {                    
    id = readIntFromInput(i);
    idList.add(id);
    bytesread = bytesread + 4;
}

// Read data (timestamp)
for (int i = 0; i &lt; this.getAmountOfInputPipes(); i++) {                    
    data = readLongFromInput(i);
    dataList.add(data);
    bytesread = bytesread + 8;
}
</code></pre>

<p>My idea is:
Now I got a List of TimeStamps. I compare them, find the earliest one and then proceed to read and write the complete rest of that frame with the earliest timeStamp. Then I read the first id and the next timeStamp of that inputStream. What should I do if one stream ended?</p>

<p>I have something like this:</p>

<pre><code>catch (EndOfStreamException e) {
    ClosePorts();
    System.out.println(this.getName() + "::Filter exiting...");
    break;

} // catch
</code></pre>

<p>Is it even a problem which stream ends first? Or do I just wait for the ArrayList to be empty?</p>

<p>I have a vague idea, but I have some trouble implementing it. Thank you in advance for any ideas.</p>

<p>Philipp</p>