bytearray - Reading bytes from large files in Java cause Java heap space error -


awhile ago seeking on code below , started working on again. basically, i've narrowed error down size of file causing error:

exception in thread "main" java.lang.outofmemoryerror: java heap space

the line right below error in stack trace is: at java.util.arrays.copyof(arrays.java:2786)

i can pass program large directory thousands of smaller files, file on 50 mb size tends crash. haven't traced exact size program crashes on know @ least 50 mb file causes issues.

below primary snippets , stack trace tells my code breaking.

private void handlefile(file source) {     fileinputstream fis = null;      try     {         if(source.isfile())         {             fis = new fileinputstream(source);             handlefile(source.getabsolutepath(), fis);         }         else if(source.isdirectory())         {             for(file file:source.listfiles())             {                if(file.isfile())                {                    fis = new fileinputstream(file);                    handlefile(file, fis);                }                else                {                    handlefile(file);                }             }          }      }      catch(ioexception ioe)      {          ioe.printstacktrace();      }           {          try          {              if(fis != null) { fis.close(); }          }          catch(ioexception ioe) { ioe.printstacktrace(); }      } }  private handlefile(string filename, inputstream inputstream) {     byte[] startingbytes = null;      try     {        startingbytes = inputstreamtobytearray(inputstream);         if(startingbytes.length == 0) return;         if(isbytestypeb(startingbytes))        {           stuff           return;        }      }      catch(ioexception ioe)      {          ioe.printstacktrace();      } }  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);         }     }     { baos.close(); }      return baos.tobytearray();  }   private boolean isbytestypeb(byte[] filebytes)  {      // checks if these bytes match particular type      if(bytesmatcher.matches(filebytes, filebytes.length))      {          return true;      }      return false;  } 

so there in above code causing error. ideas i'm doing wrong here?

arrays.copyof invoked each time bytearrayoutputstream's internal array needs resizing. moment of highest memory demand. can avoid array resizing specifying initial size of array equal file size.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -