1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/DataHead.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,276 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.org.jvnet.mimepull; 1.30 + 1.31 +import java.io.*; 1.32 +import java.nio.ByteBuffer; 1.33 + 1.34 +/** 1.35 + * Represents an attachment part in a MIME message. MIME message parsing is done 1.36 + * lazily using a pull parser, so the part may not have all the data. {@link #read} 1.37 + * and {@link #readOnce} may trigger the actual parsing the message. In fact, 1.38 + * parsing of an attachment part may be triggered by calling {@link #read} methods 1.39 + * on some other attachment parts. All this happens behind the scenes so the 1.40 + * application developer need not worry about these details. 1.41 + * 1.42 + * @author Jitendra Kotamraju 1.43 + */ 1.44 +final class DataHead { 1.45 + 1.46 + /** 1.47 + * Linked list to keep the part's content 1.48 + */ 1.49 + volatile Chunk head, tail; 1.50 + 1.51 + /** 1.52 + * If the part is stored in a file, non-null. 1.53 + */ 1.54 + DataFile dataFile; 1.55 + 1.56 + private final MIMEPart part; 1.57 + 1.58 + boolean readOnce; 1.59 + volatile long inMemory; 1.60 + 1.61 + /** 1.62 + * Used only for debugging. This records where readOnce() is called. 1.63 + */ 1.64 + private Throwable consumedAt; 1.65 + 1.66 + DataHead(MIMEPart part) { 1.67 + this.part = part; 1.68 + } 1.69 + 1.70 + void addBody(ByteBuffer buf) { 1.71 + synchronized(this) { 1.72 + inMemory += buf.limit(); 1.73 + } 1.74 + if (tail != null) { 1.75 + tail = tail.createNext(this, buf); 1.76 + } else { 1.77 + head = tail = new Chunk(new MemoryData(buf, part.msg.config)); 1.78 + } 1.79 + } 1.80 + 1.81 + void doneParsing() { 1.82 + } 1.83 + 1.84 + void moveTo(File f) { 1.85 + if (dataFile != null) { 1.86 + dataFile.renameTo(f); 1.87 + } else { 1.88 + try { 1.89 + OutputStream os = new FileOutputStream(f); 1.90 + try { 1.91 + InputStream in = readOnce(); 1.92 + byte[] buf = new byte[8192]; 1.93 + int len; 1.94 + while((len=in.read(buf)) != -1) { 1.95 + os.write(buf, 0, len); 1.96 + } 1.97 + } finally { 1.98 + if (os != null) { 1.99 + os.close(); 1.100 + } 1.101 + } 1.102 + } catch(IOException ioe) { 1.103 + throw new MIMEParsingException(ioe); 1.104 + } 1.105 + } 1.106 + } 1.107 + 1.108 + void close() { 1.109 + head = tail = null; 1.110 + if (dataFile != null) { 1.111 + dataFile.close(); 1.112 + } 1.113 + } 1.114 + 1.115 + 1.116 + /** 1.117 + * Can get the attachment part's content multiple times. That means 1.118 + * the full content needs to be there in memory or on the file system. 1.119 + * Calling this method would trigger parsing for the part's data. So 1.120 + * do not call this unless it is required(otherwise, just wrap MIMEPart 1.121 + * into a object that returns InputStream for e.g DataHandler) 1.122 + * 1.123 + * @return data for the part's content 1.124 + */ 1.125 + public InputStream read() { 1.126 + if (readOnce) { 1.127 + throw new IllegalStateException("readOnce() is called before, read() cannot be called later."); 1.128 + } 1.129 + 1.130 + // Trigger parsing for the part 1.131 + while(tail == null) { 1.132 + if (!part.msg.makeProgress()) { 1.133 + throw new IllegalStateException("No such MIME Part: "+part); 1.134 + } 1.135 + } 1.136 + 1.137 + if (head == null) { 1.138 + throw new IllegalStateException("Already read. Probably readOnce() is called before."); 1.139 + } 1.140 + return new ReadMultiStream(); 1.141 + } 1.142 + 1.143 + /** 1.144 + * Used for an assertion. Returns true when readOnce() is not already called. 1.145 + * or otherwise throw an exception. 1.146 + * 1.147 + * <p> 1.148 + * Calling this method also marks the stream as 'consumed' 1.149 + * 1.150 + * @return true if readOnce() is not called before 1.151 + */ 1.152 + @SuppressWarnings("ThrowableInitCause") 1.153 + private boolean unconsumed() { 1.154 + if (consumedAt != null) { 1.155 + AssertionError error = new AssertionError("readOnce() is already called before. See the nested exception from where it's called."); 1.156 + error.initCause(consumedAt); 1.157 + throw error; 1.158 + } 1.159 + consumedAt = new Exception().fillInStackTrace(); 1.160 + return true; 1.161 + } 1.162 + 1.163 + /** 1.164 + * Can get the attachment part's content only once. The content 1.165 + * will be lost after the method. Content data is not be stored 1.166 + * on the file system or is not kept in the memory for the 1.167 + * following case: 1.168 + * - Attachement parts contents are accessed sequentially 1.169 + * 1.170 + * In general, take advantage of this when the data is used only 1.171 + * once. 1.172 + * 1.173 + * @return data for the part's content 1.174 + */ 1.175 + public InputStream readOnce() { 1.176 + assert unconsumed(); 1.177 + if (readOnce) { 1.178 + throw new IllegalStateException("readOnce() is called before. It can only be called once."); 1.179 + } 1.180 + readOnce = true; 1.181 + // Trigger parsing for the part 1.182 + while(tail == null) { 1.183 + if (!part.msg.makeProgress() && tail == null) { 1.184 + throw new IllegalStateException("No such Part: "+part); 1.185 + } 1.186 + } 1.187 + InputStream in = new ReadOnceStream(); 1.188 + head = null; 1.189 + return in; 1.190 + } 1.191 + 1.192 + class ReadMultiStream extends InputStream { 1.193 + Chunk current; 1.194 + int offset; 1.195 + int len; 1.196 + byte[] buf; 1.197 + boolean closed; 1.198 + 1.199 + public ReadMultiStream() { 1.200 + this.current = head; 1.201 + len = current.data.size(); 1.202 + buf = current.data.read(); 1.203 + } 1.204 + 1.205 + @Override 1.206 + public int read(byte b[], int off, int sz) throws IOException { 1.207 + if (!fetch()) { 1.208 + return -1; 1.209 + } 1.210 + 1.211 + sz = Math.min(sz, len-offset); 1.212 + System.arraycopy(buf,offset,b,off,sz); 1.213 + offset += sz; 1.214 + return sz; 1.215 + } 1.216 + 1.217 + @Override 1.218 + public int read() throws IOException { 1.219 + if (!fetch()) { 1.220 + return -1; 1.221 + } 1.222 + return (buf[offset++] & 0xff); 1.223 + } 1.224 + 1.225 + void adjustInMemoryUsage() { 1.226 + // Nothing to do in this case. 1.227 + } 1.228 + 1.229 + /** 1.230 + * Gets to the next chunk if we are done with the current one. 1.231 + * @return true if any data available 1.232 + * @throws IOException when i/o error 1.233 + */ 1.234 + private boolean fetch() throws IOException { 1.235 + if (closed) { 1.236 + throw new IOException("Stream already closed"); 1.237 + } 1.238 + if (current == null) { 1.239 + return false; 1.240 + } 1.241 + 1.242 + while(offset==len) { 1.243 + while(!part.parsed && current.next == null) { 1.244 + part.msg.makeProgress(); 1.245 + } 1.246 + current = current.next; 1.247 + 1.248 + if (current == null) { 1.249 + return false; 1.250 + } 1.251 + adjustInMemoryUsage(); 1.252 + this.offset = 0; 1.253 + this.buf = current.data.read(); 1.254 + this.len = current.data.size(); 1.255 + } 1.256 + return true; 1.257 + } 1.258 + 1.259 + @Override 1.260 + public void close() throws IOException { 1.261 + super.close(); 1.262 + current = null; 1.263 + closed = true; 1.264 + } 1.265 + } 1.266 + 1.267 + final class ReadOnceStream extends ReadMultiStream { 1.268 + 1.269 + @Override 1.270 + void adjustInMemoryUsage() { 1.271 + synchronized(DataHead.this) { 1.272 + inMemory -= current.data.size(); // adjust current memory usage 1.273 + } 1.274 + } 1.275 + 1.276 + } 1.277 + 1.278 + 1.279 +}