Repository navigation
Expand file tree
/
Copy pathDataSourceArray.java
More file actions
80 lines (67 loc) · 2.33 KB
/
Copy pathDataSourceArray.java
File metadata and controls
80 lines (67 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/
package org.tron.storage;
import org.spongycastle.util.encoders.Hex;
import org.tron.utils.ByteUtil;
import java.util.AbstractList;
public class DataSourceArray<V> extends AbstractList<V> {
private static final byte[] SIZE_KEY = Hex.decode("FFFFFFFFFFFFFFFF");
private ObjectDataSource<V> src;
private int size = -1;
public DataSourceArray(ObjectDataSource<V> src) {
this.src = src;
}
public synchronized boolean flush() {
return src.flush();
}
@Override
public synchronized V set(int idx, V value) {
if (idx >= size()) {
setSize(idx + 1);
}
src.putData(ByteUtil.intToBytes(idx), value);
return value;
}
@Override
public synchronized void add(int index, V element) {
set(index, element);
}
@Override
public synchronized V remove(int index) {
throw new RuntimeException("Not supported yet.");
}
@Override
public synchronized V get(int idx) {
if (idx < 0 || idx >= size()) {
throw new IndexOutOfBoundsException(idx + " > " + size);
}
return src.getData(ByteUtil.intToBytes(idx));
}
@Override
public synchronized int size() {
if (size < 0) {
byte[] sizeBB = src.getSourceInter().getData(SIZE_KEY);
size = sizeBB == null ? 0 : ByteUtil.byteArrayToInt(sizeBB);
}
return size;
}
private synchronized void setSize(int newSize) {
size = newSize;
src.getSourceInter().putData(SIZE_KEY, ByteUtil.intToBytes(newSize));
}
}