1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.org.jvnet.mimepull;
  27 
  28 import java.nio.ByteBuffer;
  29 import java.io.File;
  30 import java.io.IOException;
  31 import java.util.logging.Level;
  32 import java.util.logging.Logger;
  33 
  34 /**
  35  * Keeps the Part's partial content data in memory.
  36  *
  37  * @author Kohsuke Kawaguchi
  38  * @author Jitendra Kotamraju
  39  */
  40 final class MemoryData implements Data {
  41     private static final Logger LOGGER = Logger.getLogger(MemoryData.class.getName());
  42 
  43     private final byte[] data;
  44     private final int len;
  45     private final MIMEConfig config;
  46 
  47     MemoryData(ByteBuffer buf, MIMEConfig config) {
  48         data = buf.array();
  49         len = buf.limit();
  50         this.config = config;
  51     }
  52 
  53     // size of the chunk given by the parser
  54     @Override
  55     public int size() {
  56         return len;
  57     }
  58 
  59     @Override
  60     public byte[] read() {
  61         return data;
  62     }
  63 
  64     @Override
  65     public long writeTo(DataFile file) {
  66         return file.writeTo(data, 0, len);
  67     }
  68 
  69     /**
  70      *
  71      * @param dataHead
  72      * @param buf
  73      * @return
  74      */
  75     @Override
  76     public Data createNext(DataHead dataHead, ByteBuffer buf) {
  77         if (!config.isOnlyMemory() && dataHead.inMemory >= config.memoryThreshold) {
  78             try {
  79                 String prefix = config.getTempFilePrefix();
  80                 String suffix = config.getTempFileSuffix();
  81                 File dir = config.getTempDir();
  82                 File tempFile = (dir == null)
  83                         ? File.createTempFile(prefix, suffix)
  84                         : File.createTempFile(prefix, suffix, dir);
  85                 // delete the temp file when VM exits as a last resort for file clean up
  86                 tempFile.deleteOnExit();
  87                 if (LOGGER.isLoggable(Level.FINE)) {LOGGER.log(Level.FINE, "Created temp file = {0}", tempFile);}
  88                 dataHead.dataFile = new DataFile(tempFile);
  89             } catch(IOException ioe) {
  90                 throw new MIMEParsingException(ioe);
  91             }
  92 
  93             if (dataHead.head != null) {
  94                 for(Chunk c=dataHead.head; c != null; c=c.next) {
  95                     long pointer = c.data.writeTo(dataHead.dataFile);
  96                     c.data = new FileData(dataHead.dataFile, pointer, len);
  97                 }
  98             }
  99             return new FileData(dataHead.dataFile, buf);
 100         } else {
 101             return new MemoryData(buf, config);
 102         }
 103     }
 104 }