Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

84
LINES

< > BotCompany Repo | #1029539 // RAMByteMemory64

JavaX fragment (include) [tags: use-pretranspiled]

Uses 11335K of libraries. Click here for Pure Java version (6664L/40K).

!include once #1027304 // Eclipse Collections

import java.nio.*;
import java.nio.channels.*;

// read-only, so far. should be thread-safe
// reads whole file into huge byte arrays on start
final sclass RAMByteMemory64 implements IByteMemory64, AutoCloseable {
  File file; // the file we loaded
  long size; // file size in ints
  bool bigEndian = true;
  bool debug;

  // byte buffers
  int arrayShift = 30; // each buffer is 1 GB
  int arraySize = 1 << arrayShift;
  byte[][] arrays;
  
  *() {}
  *(File file) {
    load(file);
  }
  
  void load(File file) ctex {
    this.file = file;
    size = fileSize(file);
    temp RandomAccessFile raf = newRandomAccessFile(file, "r");
    
    arrays = new byte[toInt(rightShift_ceil(size, arrayShift))][];
    FileChannel channel = raf.getChannel();
    print("Allocating " + n2(size) + " bytes");
    for i over arrays: {
      long pos = (long) i << arrayShift;
      int len = toInt(min(1 << arrayShift, size-pos));
      arrays[i] = new byte[len];
    }
    
    for i over arrays: {
      long pos = (long) i << arrayShift;
      int len = l(arrays[i]);
      print("Loading bytes " + longToHex(pos) + "-" + longToHex(pos+len) + " of " + file);
      MappedByteBuffer byteBuffer = channel.map(FileChannel.MapMode.READ_ONLY, pos, len);
      byteBuffer.get(arrays[i]);
    }
    
    print(size + " bytes loaded");
  }
  
  public void close {}
  
  public byte getByte(long idx) {
    rangeCheck(idx, size);
    ret arrays[(int) (idx >> arrayShift)][(((int) idx) & (arraySize-1))];
  }
  
  public int getInt(long idx) {
    assertTrue(+bigEndian);
    ret ubyteToInt(getByte(idx)) << 24 |
      ubyteToInt(getByte(idx+1)) << 16 |
      ubyteToInt(getByte(idx+2)) << 8 |
      ubyteToInt(getByte(idx+3));
  }
  
  public void set(long idx, int val) {
    checkWritable();
    fail("todo");
  }
  
  void checkWritable {
    fail("read-only");
  }
  
  public long size() {
    ret size;
  }
  
  public void ensureSize(int size) {
    this.size = max(this.size, size);
  }
  
  void importVirtual(virtual RAMByteMemory64 mem) {
    copyFields(mem, this);
  }
}

Author comment

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: 327 / 636
Version history: 11 change(s)
Referenced in: [show references]