-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
110 additions
and
5 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
openbook/src/main/java/com/mmorrell/openbook/model/OpenBookAnyEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.mmorrell.openbook.model; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
import java.util.Arrays; | ||
|
||
@Data | ||
@Builder | ||
public class OpenBookAnyEvent { | ||
|
||
public static final int SIZE = 144; | ||
|
||
// 0 = fill, 1 = out | ||
private byte eventType; | ||
private byte[] padding; | ||
|
||
public static OpenBookAnyEvent readOpenBookAnyEvent(byte[] data) { | ||
return OpenBookAnyEvent.builder() | ||
.eventType(data[0]) | ||
.padding(Arrays.copyOfRange(data, 1, SIZE)) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
openbook/src/main/java/com/mmorrell/openbook/model/OpenBookEventNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.mmorrell.openbook.model; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@Data | ||
@Builder | ||
public class OpenBookEventNode { | ||
|
||
// 2 shorts = 4 bytes | ||
// 4 bytes of padding | ||
// AnyEvent x 1 = 144 bytes | ||
// 152 bytes total | ||
|
||
public static final int SIZE = 2 + 2 + 4 + OpenBookAnyEvent.SIZE; | ||
private static final int NEXT_OFFSET = 0; | ||
private static final int PREV_OFFSET = 2; | ||
private static final int EVENT_OFFSET = 8; | ||
|
||
private short next; | ||
private short prev; | ||
private OpenBookAnyEvent event; | ||
|
||
public static List<OpenBookEventNode> readEventNodes(byte[] bytes) { | ||
List<OpenBookEventNode> results = new ArrayList<>(); | ||
int numNodes = bytes.length / SIZE; | ||
for (int i = 0; i < numNodes; i++) { | ||
byte[] nodeBytes = Arrays.copyOfRange(bytes, i * SIZE, (i + 1) * SIZE); | ||
OpenBookEventNode node = OpenBookEventNode.builder() | ||
.next(ByteBuffer.wrap(nodeBytes, NEXT_OFFSET, 2).getShort()) | ||
.prev(ByteBuffer.wrap(nodeBytes, PREV_OFFSET, 2).getShort()) | ||
.event( | ||
OpenBookAnyEvent.readOpenBookAnyEvent( | ||
Arrays.copyOfRange( | ||
nodeBytes, | ||
EVENT_OFFSET, | ||
nodeBytes.length | ||
) | ||
) | ||
) | ||
.build(); | ||
results.add(node); | ||
} | ||
|
||
return results; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters