【问题标题】:initializing Byte array with size of "Long" data type?初始化大小为“Long”数据类型的字节数组?
【发布时间】:2014-02-21 05:47:40
【问题描述】:

所以我有这种方法可以将文件中的数据读入一个字节数组,起点为“偏移”,长度为“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;
}

现在只要len 变量不超过允许的最大 int 值,它就可以正常工作。但是当我必须使用“long”作为变量 len 的数据类型才能传递更大的数字(即创建更大的数组)时,我收到以下错误:

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

所以基本上我需要做的就是创建一个大小为“long”数据类型的字节数组。有什么提示吗?

【问题讨论】:

  • 嗯;您知道任何不适合intlong 意味着它的值是2^31 或更多,不是吗?你为什么不直接做new byte[(int) len]
  • 我当然知道。事实上,我程序中的数字超过了 2^31-1,然后被转换为负数(在 int 范围的另一端)。转换为 int 并不能解决问题,因为转换后数字将再次为负数,或者充其量是 MAX_INT,这不是我想要的。我希望原始长数是字节数组的长度
  • 好的,等一下;你是认真的吗?你真的打算创建这么大的数组吗?在我看来,应该有另一种解决方案。如果你说你想做什么呢?
  • How to make a big array in java 的可能重复项
  • 谢谢大家。看来我得另谋出路了……

标签: java byte bytearray long-integer


【解决方案1】:

我刚刚试过。最大数组大小不能超过堆大小。所以如果可以的话,最好分几次处理文件。

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));
        }   
    }
}

【讨论】:

    【解决方案2】:

    您可能可以查看此链接stackOverflow

    请注意,在对它们进行类型转换之前,请务必检查原语的范围。 在这里,您希望字节数组大小采用 long 值,这在我看来是一个无效的场景,因为 long 可以在其范围内具有精度。精度值的地板/天花板肯定会导致精度损失。

    【讨论】:

    • 这不是 OP 所要求的。他们在问怎么办new byte[someLong]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多