title: optimizing code that writes byte array image to disk
body: <p>I have the following code that that writes large images to disk. My application server is running out of memory and I'm wondering if I could somehow optimize the following:</p>
  
  <pre><code>public void writeImgToDisk(byte[] base64AttachmentInBytes, String dmxi){
  
          String destinationAndFileName  = "";
          String fileNameMinusExtension = getCurrentTimeStampForFileNaming();
          String extension="";
          try {
  
  
                 TikaConfig config = TikaConfig.getDefaultConfig();
                 InputStream stream = new ByteArrayInputStream(base64AttachmentInBytes);
  
                 MediaType mediaType = config.getMimeRepository().detect(stream, new Metadata());
                 MimeType mimeType;
  
  
              try {
                  mimeType = config.getMimeRepository().forName(mediaType.toString());
                  extension = mimeType.getExtension();
                  System.out.println("Extension is:"+ extension);
  
                  File folder  = new File("Z:\\images\\"+ sref);
                  if (!folder.exists()){
                      folder.mkdir();
                  }
                  destinationAndFileName  = "Z:\\images\\"+ dmxi + "\\" + fileNameMinusExtension+ extension;
  
                  System.out.println("destinationAndFileName is:"+ destinationAndFileName);
              } catch (MimeTypeException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
              InputStream input = new ByteArrayInputStream(base64AttachmentInBytes);
              OutputStream output = new FileOutputStream(destinationAndFileName);
              IOUtils.copy(input, output);
  
  
              System.out.println("It was written..");  
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
      }
  </code></pre>
  
  <p>This works but its is the cause of a bottleneck during high volume calls.</p>