Java array with more than 4gb elements

后端 未结 11 1870
心在旅途
心在旅途 2020-11-27 07:06

I have a big file, it\'s expected to be around 12 GB. I want to load it all into memory on a beefy 64-bit machine with 16 GB RAM, but I think Java does not support byte arra

11条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 07:59

    package com.deans.rtl.util;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    
    /**
     * 
     * @author william.deans@gmail.com
     *
     * Written to work with byte arrays requiring address space larger than 32 bits. 
     * 
     */
    
    public class ByteArray64 {
    
        private final long CHUNK_SIZE = 1024*1024*1024; //1GiB
    
        long size;
        byte [][] data;
    
        public ByteArray64( long size ) {
            this.size = size;
            if( size == 0 ) {
                data = null;
            } else {
                int chunks = (int)(size/CHUNK_SIZE);
                int remainder = (int)(size - ((long)chunks)*CHUNK_SIZE);
                data = new byte[chunks+(remainder==0?0:1)][];
                for( int idx=chunks; --idx>=0; ) {
                    data[idx] = new byte[(int)CHUNK_SIZE];
                }
                if( remainder != 0 ) {
                    data[chunks] = new byte[remainder];
                }
            }
        }
        public byte get( long index ) {
            if( index<0 || index>=size ) {
                throw new IndexOutOfBoundsException("Error attempting to access data element "+index+".  Array is "+size+" elements long.");
            }
            int chunk = (int)(index/CHUNK_SIZE);
            int offset = (int)(index - (((long)chunk)*CHUNK_SIZE));
            return data[chunk][offset];
        }
        public void set( long index, byte b ) {
            if( index<0 || index>=size ) {
                throw new IndexOutOfBoundsException("Error attempting to access data element "+index+".  Array is "+size+" elements long.");
            }
            int chunk = (int)(index/CHUNK_SIZE);
            int offset = (int)(index - (((long)chunk)*CHUNK_SIZE));
            data[chunk][offset] = b;
        }
        /**
         * Simulates a single read which fills the entire array via several smaller reads.
         * 
         * @param fileInputStream
         * @throws IOException
         */
        public void read( FileInputStream fileInputStream ) throws IOException {
            if( size == 0 ) {
                return;
            }
            for( int idx=0; idx

提交回复
热议问题