OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE.md file. | |
4 | |
5 package dartino; | |
6 | |
7 import java.nio.ByteBuffer; | |
8 import java.nio.CharBuffer; | |
9 | |
10 class Builder { | |
11 public Builder() { } | |
12 | |
13 public Builder(BuilderSegment segment, int base) { | |
14 this.segment = segment; | |
15 this.base = base; | |
16 } | |
17 | |
18 public BuilderSegment segment() { return segment; } | |
19 | |
20 public Object[] getSegments() { | |
21 Object[] result = new Object[2]; | |
22 int segments = segment().builder().segments(); | |
23 byte[][] segmentArray = new byte[segments][]; | |
24 int[] sizeArray = new int[segments]; | |
25 BuilderSegment current = segment; | |
26 for (int i = 0; i < segments; i++) { | |
27 segmentArray[i] = current.buffer().array(); | |
28 sizeArray[i] = current.used(); | |
29 current = current.next(); | |
30 } | |
31 result[0] = segmentArray; | |
32 result[1] = sizeArray; | |
33 return result; | |
34 } | |
35 | |
36 public Builder newStruct(Builder builder, int offset, int size) { | |
37 offset += base; | |
38 BuilderSegment s = segment; | |
39 while (true) { | |
40 int result = s.allocate(size); | |
41 ByteBuffer memory = s.buffer(); | |
42 if (result >= 0) { | |
43 memory.putInt(offset + 0, (result << 2) | 1); | |
44 memory.putInt(offset + 4, 0); | |
45 builder.segment = s; | |
46 builder.base = result; | |
47 return builder; | |
48 } | |
49 | |
50 BuilderSegment other = s.builder().findSegmentForBytes(size + 8); | |
51 int target = other.allocate(8); | |
52 memory.putInt(offset + 0, (target << 2) | 3); | |
53 memory.putInt(offset + 4, other.id()); | |
54 | |
55 s = other; | |
56 offset = target; | |
57 } | |
58 } | |
59 | |
60 public ListBuilder newList(ListBuilder list, | |
61 int offset, | |
62 int length, | |
63 int size) { | |
64 list.length = length; | |
65 offset += base; | |
66 size *= length; | |
67 BuilderSegment s = segment; | |
68 while (true) { | |
69 int result = s.allocate(size); | |
70 ByteBuffer memory = s.buffer(); | |
71 if (result >= 0) { | |
72 memory.putInt(offset + 0, (result << 2) | 2); | |
73 memory.putInt(offset + 4, length); | |
74 list.segment = s; | |
75 list.base = result; | |
76 return list; | |
77 } | |
78 | |
79 BuilderSegment other = s.builder().findSegmentForBytes(size + 8); | |
80 int target = other.allocate(8); | |
81 memory.putInt(offset + 0, (target << 2) | 3); | |
82 memory.putInt(offset + 4, other.id()); | |
83 | |
84 s = other; | |
85 offset = target; | |
86 } | |
87 } | |
88 | |
89 | |
90 public void newString(int offset, String value) { | |
91 int length = value.length(); | |
92 ListBuilder stringBuilder = new ListBuilder(); | |
93 newList(stringBuilder, offset, length, 2); | |
94 CharBuffer cb = stringBuilder.segment.buffer().asCharBuffer(); | |
95 int base = stringBuilder.base / 2; | |
96 for (int i = 0; i < length; i++) { | |
97 cb.put(base + i, value.charAt(i)); | |
98 } | |
99 } | |
100 | |
101 protected BuilderSegment segment; | |
102 protected int base; | |
103 } | |
OLD | NEW |