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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 384
8f2986ff0235
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.org.jvnet.mimepull;
aoqi@0 27
aoqi@0 28 import java.io.*;
aoqi@0 29 import java.nio.ByteBuffer;
aoqi@0 30
aoqi@0 31 /**
aoqi@0 32 * Represents an attachment part in a MIME message. MIME message parsing is done
aoqi@0 33 * lazily using a pull parser, so the part may not have all the data. {@link #read}
aoqi@0 34 * and {@link #readOnce} may trigger the actual parsing the message. In fact,
aoqi@0 35 * parsing of an attachment part may be triggered by calling {@link #read} methods
aoqi@0 36 * on some other attachment parts. All this happens behind the scenes so the
aoqi@0 37 * application developer need not worry about these details.
aoqi@0 38 *
aoqi@0 39 * @author Jitendra Kotamraju
aoqi@0 40 */
aoqi@0 41 final class DataHead {
aoqi@0 42
aoqi@0 43 /**
aoqi@0 44 * Linked list to keep the part's content
aoqi@0 45 */
aoqi@0 46 volatile Chunk head, tail;
aoqi@0 47
aoqi@0 48 /**
aoqi@0 49 * If the part is stored in a file, non-null.
aoqi@0 50 */
aoqi@0 51 DataFile dataFile;
aoqi@0 52
aoqi@0 53 private final MIMEPart part;
aoqi@0 54
aoqi@0 55 boolean readOnce;
aoqi@0 56 volatile long inMemory;
aoqi@0 57
aoqi@0 58 /**
aoqi@0 59 * Used only for debugging. This records where readOnce() is called.
aoqi@0 60 */
aoqi@0 61 private Throwable consumedAt;
aoqi@0 62
aoqi@0 63 DataHead(MIMEPart part) {
aoqi@0 64 this.part = part;
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 void addBody(ByteBuffer buf) {
aoqi@0 68 synchronized(this) {
aoqi@0 69 inMemory += buf.limit();
aoqi@0 70 }
aoqi@0 71 if (tail != null) {
aoqi@0 72 tail = tail.createNext(this, buf);
aoqi@0 73 } else {
aoqi@0 74 head = tail = new Chunk(new MemoryData(buf, part.msg.config));
aoqi@0 75 }
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 void doneParsing() {
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 void moveTo(File f) {
aoqi@0 82 if (dataFile != null) {
aoqi@0 83 dataFile.renameTo(f);
aoqi@0 84 } else {
aoqi@0 85 try {
aoqi@0 86 OutputStream os = new FileOutputStream(f);
aoqi@0 87 try {
aoqi@0 88 InputStream in = readOnce();
aoqi@0 89 byte[] buf = new byte[8192];
aoqi@0 90 int len;
aoqi@0 91 while((len=in.read(buf)) != -1) {
aoqi@0 92 os.write(buf, 0, len);
aoqi@0 93 }
aoqi@0 94 } finally {
aoqi@0 95 if (os != null) {
aoqi@0 96 os.close();
aoqi@0 97 }
aoqi@0 98 }
aoqi@0 99 } catch(IOException ioe) {
aoqi@0 100 throw new MIMEParsingException(ioe);
aoqi@0 101 }
aoqi@0 102 }
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 void close() {
aoqi@0 106 head = tail = null;
aoqi@0 107 if (dataFile != null) {
aoqi@0 108 dataFile.close();
aoqi@0 109 }
aoqi@0 110 }
aoqi@0 111
aoqi@0 112
aoqi@0 113 /**
aoqi@0 114 * Can get the attachment part's content multiple times. That means
aoqi@0 115 * the full content needs to be there in memory or on the file system.
aoqi@0 116 * Calling this method would trigger parsing for the part's data. So
aoqi@0 117 * do not call this unless it is required(otherwise, just wrap MIMEPart
aoqi@0 118 * into a object that returns InputStream for e.g DataHandler)
aoqi@0 119 *
aoqi@0 120 * @return data for the part's content
aoqi@0 121 */
aoqi@0 122 public InputStream read() {
aoqi@0 123 if (readOnce) {
aoqi@0 124 throw new IllegalStateException("readOnce() is called before, read() cannot be called later.");
aoqi@0 125 }
aoqi@0 126
aoqi@0 127 // Trigger parsing for the part
aoqi@0 128 while(tail == null) {
aoqi@0 129 if (!part.msg.makeProgress()) {
aoqi@0 130 throw new IllegalStateException("No such MIME Part: "+part);
aoqi@0 131 }
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 if (head == null) {
aoqi@0 135 throw new IllegalStateException("Already read. Probably readOnce() is called before.");
aoqi@0 136 }
aoqi@0 137 return new ReadMultiStream();
aoqi@0 138 }
aoqi@0 139
aoqi@0 140 /**
aoqi@0 141 * Used for an assertion. Returns true when readOnce() is not already called.
aoqi@0 142 * or otherwise throw an exception.
aoqi@0 143 *
aoqi@0 144 * <p>
aoqi@0 145 * Calling this method also marks the stream as 'consumed'
aoqi@0 146 *
aoqi@0 147 * @return true if readOnce() is not called before
aoqi@0 148 */
aoqi@0 149 @SuppressWarnings("ThrowableInitCause")
aoqi@0 150 private boolean unconsumed() {
aoqi@0 151 if (consumedAt != null) {
aoqi@0 152 AssertionError error = new AssertionError("readOnce() is already called before. See the nested exception from where it's called.");
aoqi@0 153 error.initCause(consumedAt);
aoqi@0 154 throw error;
aoqi@0 155 }
aoqi@0 156 consumedAt = new Exception().fillInStackTrace();
aoqi@0 157 return true;
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 /**
aoqi@0 161 * Can get the attachment part's content only once. The content
aoqi@0 162 * will be lost after the method. Content data is not be stored
aoqi@0 163 * on the file system or is not kept in the memory for the
aoqi@0 164 * following case:
aoqi@0 165 * - Attachement parts contents are accessed sequentially
aoqi@0 166 *
aoqi@0 167 * In general, take advantage of this when the data is used only
aoqi@0 168 * once.
aoqi@0 169 *
aoqi@0 170 * @return data for the part's content
aoqi@0 171 */
aoqi@0 172 public InputStream readOnce() {
aoqi@0 173 assert unconsumed();
aoqi@0 174 if (readOnce) {
aoqi@0 175 throw new IllegalStateException("readOnce() is called before. It can only be called once.");
aoqi@0 176 }
aoqi@0 177 readOnce = true;
aoqi@0 178 // Trigger parsing for the part
aoqi@0 179 while(tail == null) {
aoqi@0 180 if (!part.msg.makeProgress() && tail == null) {
aoqi@0 181 throw new IllegalStateException("No such Part: "+part);
aoqi@0 182 }
aoqi@0 183 }
aoqi@0 184 InputStream in = new ReadOnceStream();
aoqi@0 185 head = null;
aoqi@0 186 return in;
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 class ReadMultiStream extends InputStream {
aoqi@0 190 Chunk current;
aoqi@0 191 int offset;
aoqi@0 192 int len;
aoqi@0 193 byte[] buf;
aoqi@0 194 boolean closed;
aoqi@0 195
aoqi@0 196 public ReadMultiStream() {
aoqi@0 197 this.current = head;
aoqi@0 198 len = current.data.size();
aoqi@0 199 buf = current.data.read();
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 @Override
aoqi@0 203 public int read(byte b[], int off, int sz) throws IOException {
aoqi@0 204 if (!fetch()) {
aoqi@0 205 return -1;
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 sz = Math.min(sz, len-offset);
aoqi@0 209 System.arraycopy(buf,offset,b,off,sz);
aoqi@0 210 offset += sz;
aoqi@0 211 return sz;
aoqi@0 212 }
aoqi@0 213
aoqi@0 214 @Override
aoqi@0 215 public int read() throws IOException {
aoqi@0 216 if (!fetch()) {
aoqi@0 217 return -1;
aoqi@0 218 }
aoqi@0 219 return (buf[offset++] & 0xff);
aoqi@0 220 }
aoqi@0 221
aoqi@0 222 void adjustInMemoryUsage() {
aoqi@0 223 // Nothing to do in this case.
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 /**
aoqi@0 227 * Gets to the next chunk if we are done with the current one.
aoqi@0 228 * @return true if any data available
aoqi@0 229 * @throws IOException when i/o error
aoqi@0 230 */
aoqi@0 231 private boolean fetch() throws IOException {
aoqi@0 232 if (closed) {
aoqi@0 233 throw new IOException("Stream already closed");
aoqi@0 234 }
aoqi@0 235 if (current == null) {
aoqi@0 236 return false;
aoqi@0 237 }
aoqi@0 238
aoqi@0 239 while(offset==len) {
aoqi@0 240 while(!part.parsed && current.next == null) {
aoqi@0 241 part.msg.makeProgress();
aoqi@0 242 }
aoqi@0 243 current = current.next;
aoqi@0 244
aoqi@0 245 if (current == null) {
aoqi@0 246 return false;
aoqi@0 247 }
aoqi@0 248 adjustInMemoryUsage();
aoqi@0 249 this.offset = 0;
aoqi@0 250 this.buf = current.data.read();
aoqi@0 251 this.len = current.data.size();
aoqi@0 252 }
aoqi@0 253 return true;
aoqi@0 254 }
aoqi@0 255
aoqi@0 256 @Override
aoqi@0 257 public void close() throws IOException {
aoqi@0 258 super.close();
aoqi@0 259 current = null;
aoqi@0 260 closed = true;
aoqi@0 261 }
aoqi@0 262 }
aoqi@0 263
aoqi@0 264 final class ReadOnceStream extends ReadMultiStream {
aoqi@0 265
aoqi@0 266 @Override
aoqi@0 267 void adjustInMemoryUsage() {
aoqi@0 268 synchronized(DataHead.this) {
aoqi@0 269 inMemory -= current.data.size(); // adjust current memory usage
aoqi@0 270 }
aoqi@0 271 }
aoqi@0 272
aoqi@0 273 }
aoqi@0 274
aoqi@0 275
aoqi@0 276 }

mercurial