Uses 11335K of libraries. Click here for Pure Java version (6664L/40K).
1 | !include once #1027304 // Eclipse Collections |
2 | |
3 | import java.nio.*; |
4 | import java.nio.channels.*; |
5 | |
6 | // read-only, so far. should be thread-safe |
7 | // reads whole file into huge byte arrays on start |
8 | final sclass RAMByteMemory64 implements IByteMemory64, AutoCloseable { |
9 | File file; // the file we loaded |
10 | long size; // file size in ints |
11 | bool bigEndian = true; |
12 | bool debug; |
13 | |
14 | // byte buffers |
15 | int arrayShift = 30; // each buffer is 1 GB |
16 | int arraySize = 1 << arrayShift; |
17 | byte[][] arrays; |
18 | |
19 | *() {} |
20 | *(File file) { |
21 | load(file); |
22 | } |
23 | |
24 | void load(File file) ctex { |
25 | this.file = file; |
26 | size = fileSize(file); |
27 | temp RandomAccessFile raf = newRandomAccessFile(file, "r"); |
28 | |
29 | arrays = new byte[toInt(rightShift_ceil(size, arrayShift))][]; |
30 | FileChannel channel = raf.getChannel(); |
31 | print("Allocating " + n2(size) + " bytes"); |
32 | for i over arrays: { |
33 | long pos = (long) i << arrayShift; |
34 | int len = toInt(min(1 << arrayShift, size-pos)); |
35 | arrays[i] = new byte[len]; |
36 | } |
37 | |
38 | for i over arrays: { |
39 | long pos = (long) i << arrayShift; |
40 | int len = l(arrays[i]); |
41 | print("Loading bytes " + longToHex(pos) + "-" + longToHex(pos+len) + " of " + file); |
42 | MappedByteBuffer byteBuffer = channel.map(FileChannel.MapMode.READ_ONLY, pos, len); |
43 | byteBuffer.get(arrays[i]); |
44 | } |
45 | |
46 | print(size + " bytes loaded"); |
47 | } |
48 | |
49 | public void close {} |
50 | |
51 | public byte getByte(long idx) { |
52 | rangeCheck(idx, size); |
53 | ret arrays[(int) (idx >> arrayShift)][(((int) idx) & (arraySize-1))]; |
54 | } |
55 | |
56 | public int getInt(long idx) { |
57 | assertTrue(+bigEndian); |
58 | ret ubyteToInt(getByte(idx)) << 24 | |
59 | ubyteToInt(getByte(idx+1)) << 16 | |
60 | ubyteToInt(getByte(idx+2)) << 8 | |
61 | ubyteToInt(getByte(idx+3)); |
62 | } |
63 | |
64 | public void set(long idx, int val) { |
65 | checkWritable(); |
66 | fail("todo"); |
67 | } |
68 | |
69 | void checkWritable { |
70 | fail("read-only"); |
71 | } |
72 | |
73 | public long size() { |
74 | ret size; |
75 | } |
76 | |
77 | public void ensureSize(int size) { |
78 | this.size = max(this.size, size); |
79 | } |
80 | |
81 | void importVirtual(virtual RAMByteMemory64 mem) { |
82 | copyFields(mem, this); |
83 | } |
84 | } |
Began life as a copy of #1029532
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1029539 |
Snippet name: | RAMByteMemory64 |
Eternal ID of this version: | #1029539/12 |
Text MD5: | d6ba0148800dd3bbaf5237c6a1c6f52e |
Transpilation MD5: | 7f3b350d1825fbce638d4b51c2d9bc06 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-06-23 14:08:52 |
Source code size: | 2197 bytes / 84 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 328 / 639 |
Version history: | 11 change(s) |
Referenced in: | [show references] |