src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/MemoryData.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
equal deleted inserted replaced
284:88b85470e72c 286:f50545b5e2f1
1 /*
2 * Copyright (c) 1997, 2010, 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.Logger;
32
33 /**
34 * Keeps the Part's partial content data in memory.
35 *
36 * @author Kohsuke Kawaguchi
37 * @author Jitendra Kotamraju
38 */
39 final class MemoryData implements Data {
40 private static final Logger LOGGER = Logger.getLogger(MemoryData.class.getName());
41
42 private final byte[] data;
43 private final int len;
44 private final MIMEConfig config;
45
46 MemoryData(ByteBuffer buf, MIMEConfig config) {
47 data = buf.array();
48 len = buf.limit();
49 this.config = config;
50 }
51
52 // size of the chunk given by the parser
53 public int size() {
54 return len;
55 }
56
57 public byte[] read() {
58 return data;
59 }
60
61 public long writeTo(DataFile file) {
62 return file.writeTo(data, 0, len);
63 }
64
65 /**
66 *
67 * @param dataHead
68 * @param buf
69 * @return
70 */
71 public Data createNext(DataHead dataHead, ByteBuffer buf) {
72 if (!config.isOnlyMemory() && dataHead.inMemory >= config.memoryThreshold) {
73 try {
74 String prefix = config.getTempFilePrefix();
75 String suffix = config.getTempFileSuffix();
76 File dir = config.getTempDir();
77 File tempFile = (dir == null)
78 ? File.createTempFile(prefix, suffix)
79 : File.createTempFile(prefix, suffix, dir);
80 // delete the temp file when VM exits as a last resort for file clean up
81 tempFile.deleteOnExit();
82 LOGGER.fine("Created temp file = "+tempFile);
83 dataHead.dataFile = new DataFile(tempFile);
84 } catch(IOException ioe) {
85 throw new MIMEParsingException(ioe);
86 }
87
88 if (dataHead.head != null) {
89 for(Chunk c=dataHead.head; c != null; c=c.next) {
90 long pointer = c.data.writeTo(dataHead.dataFile);
91 c.data = new FileData(dataHead.dataFile, pointer, len);
92 }
93 }
94 return new FileData(dataHead.dataFile, buf);
95 } else {
96 return new MemoryData(buf, config);
97 }
98 }
99 }

mercurial