简体   繁体   English

初始化大小为“ Long”数据类型的字节数组?

[英]initializing Byte array with size of “Long” data type?

So i have this method to read the data in a file into a byte array with starting point "offset" and length o "len" : 所以我有这种方法可以将文件中的数据读入字节数组,起始点为“ offset”,长度为o“ len”:

public static byte[] readFileDataToByteArray( File inFile, long offset, int len ) {

    byte[] buffer = new byte[ len ];

    try {
        RandomAccessFile in = new RandomAccessFile( inFile, "r" );
        in.seek( off );
        in.read( buffer );          
        in.close();
    } catch ( IOException e ) {
        System.err.println("Error readSentence: Error reading " + inFile ); 
        System.exit(1);
    }
    return buffer;
}

now this is working just fine as long as the len variable does not exceed the maximum allowed int value. 现在,只要len变量不超过允许的最大int值,此方法就可以正常工作。 but when I have to use "long" as data type of variable len to be able to pass larger numbers (ie to create lager arrays), I get the following error : 但是当我必须使用“ long”作为变量len数据类型以便能够传递更大的数字(即创建更大的数组)时,出现以下错误:

../util/Common.java:564: error: possible loss of precision
    byte[] buffer = new byte[ len ];
                              ^
required: int
found:    long
1 error

So basically all I need to do is to create a byte array with a size of a "long" data type. 因此,基本上我要做的就是创建一个字节数组,其大小为“长”数据类型。 any hints ? 有什么提示吗?

I have just tried. 我刚刚尝试过。 That the biggest array size can't exceed the heap size. 最大数组大小不能超过堆大小。 So it's better to process the file in several time if it could be. 因此,如果可能的话,最好多次处理文件。

public class RandomAccessFileTest {
    static public class LargeArray {
        public long offset;
        public long len;
        public int arraySize; // can't exceed JVM heap size
        byte[] byte_arr;

        public LargeArray(long offset, long len, int arraySize) {
            this.offset = offset;
            this.len    = len;
            this.arraySize = arraySize;
        }
    }

    public static LargeArray readFileDataToByteArray(File inFile, LargeArray  array) {
        long count = array.len/array.arraySize;
        if (array.len > 0 ) {
            try{
                int arr_len = (count == 0) ? (int)array.len:array.arraySize;
                array.byte_arr = new byte[arr_len];
                RandomAccessFile in = new RandomAccessFile( inFile, "r" );
                in.seek( array.offset );
                in.read( array.byte_arr );          
                in.close();

                array.offset += arr_len;
                array.len    -= arr_len;
                return array;
            } catch ( IOException e ) {
                System.err.println("Error readSentence: Error reading " + inFile ); 
                System.exit(1);
            }       
        }
        return null;
    }

    public static void main(String[] args) {

        LargeArray array = new LargeArray(5,1000000, 10000);
        File file = new File("test.txt");

        while((array = readFileDataToByteArray(file, array)) != null) {
            System.out.println(new String(array.byte_arr));
        }   
    }
}

You can probably check out this link stackOverflow 您可能可以检查出此链接stackOverflow

A word of cauuion, always check out for the range of primitives before tycasting them. 小心一点,在对原型进行分类之前,请务必先检查它们的范围。 Here in you want a byte array size to take the long value which seems to me a invalid scenario since long can have precision in it's scope. 在这里,您希望字节数组的大小为long值,这对我来说似乎是无效的情况,因为long可以在其范围内保持精度。 Flooring/ceiling of the precision value would certainly result in loss of precision. 精度值的下限/上限肯定会导致精度损失。

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

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