Java: Memory efficient ByteArrayOutputStream

前端 未结 9 573
一个人的身影
一个人的身影 2021-02-04 06:15

I\'ve got a 40MB file in the disk and I need to \"map\" it into memory using a byte array.

At first, I thought writing the file to a ByteArrayOutputStream would be the b

9条回答
  •  星月不相逢
    2021-02-04 06:46

    ... came here with the same observation when reading a 1GB file: Oracle's ByteArrayOutputStream has a lazy memory management. A byte-Array is indexed by an int and such anyway limited to 2GB. Without dependency on 3rd-party you might find this useful:

    static public byte[] getBinFileContent(String aFile) 
    {
        try
        {
            final int bufLen = 32768;
            final long fs = new File(aFile).length();
            final long maxInt = ((long) 1 << 31) - 1;
            if (fs > maxInt)
            {
                System.err.println("file size out of range");
                return null;
            }
            final byte[] res = new byte[(int) fs];
            final byte[] buffer = new byte[bufLen];
            final InputStream is = new FileInputStream(aFile);
            int n;
            int pos = 0;
            while ((n = is.read(buffer)) > 0)
            {
                System.arraycopy(buffer, 0, res, pos, n);
                pos += n;
            }
            is.close();
            return res;
        }
        catch (final IOException e)
        {
            e.printStackTrace();
            return null;
        }
        catch (final OutOfMemoryError e)
        {
            e.printStackTrace();
            return null;
        }
    }
    

提交回复
热议问题