0

My program needs to do calculations against the entire bytes of a file and it breaks whenever the file gets above a certain size.

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

I know I can allocate the amount of memory to my program using command line switches, but I'm wondering if there is a more effective way of handling this in my program?

I'm basically trying to figure out a way to read the file in chunks and pass those chunks to another method and essentially rebuild the file in that method.

This is the problem method. I need these bytes to be used in another method.

This method converts the stream to a byte array:

private byte[] inputStreamToByteArray(InputStream inputStream) {
   BufferedInputStream bis = null;
   ByteArrayOutputStream baos = null;

   try {
      bis = new BufferedInputStream(inputStream);
      baos = new ByteArrayOutputStream(bis);

      byte[] buffer = new byte[1024];

      int nRead;
      while((nRead = bis.read(buffer)) != -1) {
         baos.write(buffer, 0, nRead);
      }
   } catch(IOException ioe) {
      ioe.printStackTrace();
   }

   return baos.toByteArray();
}

This method checks the file type:

private final boolean isMyFileType(byte[] bytes) { 
   // do stuff

   return theBoolean;
}

The reason it is breaking makes sense to me - the byte array ends up being gigantic if I have a gigantic file AND I'm passing around a gigantic byte array.

My goal, I want to read the bytes from a file, determine what type of file it is using another method I wrote, run compression/decompression method against those bytes after determining the file type.

I have most of my goal completed, I just don't know how to handle file streams and large byte arrays effectively.

4

1 回答 1

1

您已经在使用 BufferedInputStream。使用“标记”方法在蒸汽中放置标记。确保“mark”的“readlimit”参数足够大,以便您检测文件类型。从流中读取前 X 个字节(但不超过 readlimit)并尝试找出内容。然后调用 reset() 将流设置回开头并继续执行您想要对流执行的任何操作。

于 2013-07-23T17:41:17.720 回答