简体   繁体   English

在字节数组中的特定字节数中存储 integer - JAVA

[英]storing an integer in a specific number of bytes in an array of bytes - JAVA

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.我正在尝试使用开槽页面在 java 中实现一个数据库,所以基本上我想要做的是将我的数据存储在特定数量的字节中。 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.现在我想将 Integer 存储在 myData 的前 4 个字节中,当我这样做时,如果 Integer 不超过 255,它会自动存储在一个字节中,但我想要做的是为我的 ZA0FAEF0851B41CB204C 使用 4 个字节不管是 1 还是 10 亿。

my question is, is it possible to do that in java?我的问题是,是否可以在 java 中做到这一点? to control how many bytes my data must allocate, like I assign 3 to the first 4 bytes of my byte array?.控制我的数据必须分配多少字节,比如我将 3 分配给我的字节数组的前 4 个字节?

        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.我有一个固定大小的变量,我需要将它们存储在我的案例中,例如 12 个字节,我一直在使用 System.arraycopy() 但它与我的案例无关,在我执行代码后我超出了绑定异常“最后一个源索引 12 超出字节 [9] 的范围”,因为它仅使用 9 个字节来存储我的数据而不是 12。

This method creates an array of 32 bytes of any integer given - be it 1 or one billion:method创建一个包含 32 字节的任意 integer 的数组 - 无论是 1 还是 10 亿:

private static byte[] bigIntegerToBytes(BigInteger b, int numBytes) {
        byte[] src = b.toByteArray();
        byte[] dest = new byte[numBytes];
        boolean isFirstByteOnlyForSign = src[0] == 0;
        int length = isFirstByteOnlyForSign ? src.length - 1 : src.length;
        int srcPos = isFirstByteOnlyForSign ? 1 : 0;
        int destPos = numBytes - length;
        System.arraycopy(src, srcPos, dest, destPos, length);
        return dest;
    }

You have an array of byte ready to store:您有一个准备存储的byte array

    byte[] myData = new byte[PAGE_SIZE*1024];

You have a hand-picked integer as well:您还有一个精心挑选的integer

    BigInteger myInteger = new BigInteger("50000000000");

Then we change our integer to 32-length byte[]然后我们将我们的integer更改为 32-length byte[]

    byte[] bytesOfInteger = bigIntegerToBytes(myInteger,32);

Finally, you copy first 4 bytes of integer to your byte[] myData最后,将integer的前 4 bytes复制到byte[] myData

    System.arraycopy(bytesOfInteger, 0, myData, 0, 3);

So this shows that you can allocate any decent big integer into a fixed 32 byte[] .因此,这表明您可以将任何体面的大 integer 分配到固定的 32 byte[]中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM