I'm trying to implement a database in java using slotted pages , so basically what I want to do is to store my data in a specific number of bytes . so this is the page where I have to store it .
protected byte[] myData = new byte[PAGE_SIZE*1024]; //array for storing my data
now I want to store an Integer in the first 4 bytes of myData , when I do that automatically is stored in just one byte if the Integer doesn't exceed 255 , but what I want to do is use 4 bytes for my Integer it doesn't matter if it's 1 or one billion .
my question is , is it possible to do that in java ? to control how many bytes my data must allocate , like I assign 3 to the first 4 bytes of my byte array ?.
if (recordFitsIntoPage(record)) {
byte [] fix_rec = new byte [record.getFixedLength()];
byte [] var_rec= new byte [record.getVariableLength()];
var_rec = var_rec(record);
fix_rec = fix_rec(record);
byte [] box = { (byte) record.getVariableLength() ,(byte) offsetEnd };
System.arraycopy(fix_rec, 0,data,offset,record.getFixedLength());
System.arraycopy(var_rec, 0,data,offsetEnd,record.getVariableLength());
read_bytes(data);
this.numRecords++;
}else {
throw new Exception("no more space left");
}
I have a fixed-sized variables that I need to store them in my case for example in 12 bytes , I have been using System.arraycopy() but it's not relevant in my case , after I execute the code I get out of bound exception "last source index 12 out of bounds for byte[9]" because it uses just 9 bytes to store my Data not 12 .