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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

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

mercurial