ohair@286: /* mkos@384: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.org.jvnet.mimepull; ohair@286: ohair@286: import java.io.*; ohair@286: import java.nio.ByteBuffer; ohair@286: ohair@286: /** ohair@286: * Represents an attachment part in a MIME message. MIME message parsing is done ohair@286: * lazily using a pull parser, so the part may not have all the data. {@link #read} ohair@286: * and {@link #readOnce} may trigger the actual parsing the message. In fact, ohair@286: * parsing of an attachment part may be triggered by calling {@link #read} methods alanb@368: * on some other attachment parts. All this happens behind the scenes so the ohair@286: * application developer need not worry about these details. ohair@286: * ohair@286: * @author Jitendra Kotamraju ohair@286: */ ohair@286: final class DataHead { ohair@286: ohair@286: /** ohair@286: * Linked list to keep the part's content ohair@286: */ ohair@286: volatile Chunk head, tail; ohair@286: ohair@286: /** ohair@286: * If the part is stored in a file, non-null. ohair@286: */ ohair@286: DataFile dataFile; ohair@286: ohair@286: private final MIMEPart part; ohair@286: ohair@286: boolean readOnce; ohair@286: volatile long inMemory; ohair@286: ohair@286: /** ohair@286: * Used only for debugging. This records where readOnce() is called. ohair@286: */ ohair@286: private Throwable consumedAt; ohair@286: ohair@286: DataHead(MIMEPart part) { ohair@286: this.part = part; ohair@286: } ohair@286: ohair@286: void addBody(ByteBuffer buf) { ohair@286: synchronized(this) { ohair@286: inMemory += buf.limit(); ohair@286: } ohair@286: if (tail != null) { ohair@286: tail = tail.createNext(this, buf); ohair@286: } else { ohair@286: head = tail = new Chunk(new MemoryData(buf, part.msg.config)); ohair@286: } ohair@286: } ohair@286: ohair@286: void doneParsing() { ohair@286: } ohair@286: ohair@286: void moveTo(File f) { ohair@286: if (dataFile != null) { ohair@286: dataFile.renameTo(f); ohair@286: } else { ohair@286: try { ohair@286: OutputStream os = new FileOutputStream(f); alanb@368: try { alanb@368: InputStream in = readOnce(); alanb@368: byte[] buf = new byte[8192]; alanb@368: int len; alanb@368: while((len=in.read(buf)) != -1) { alanb@368: os.write(buf, 0, len); alanb@368: } alanb@368: } finally { alanb@368: if (os != null) { alanb@368: os.close(); alanb@368: } ohair@286: } ohair@286: } catch(IOException ioe) { ohair@286: throw new MIMEParsingException(ioe); ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: void close() { mkos@384: head = tail = null; ohair@286: if (dataFile != null) { ohair@286: dataFile.close(); ohair@286: } ohair@286: } ohair@286: ohair@286: ohair@286: /** ohair@286: * Can get the attachment part's content multiple times. That means ohair@286: * the full content needs to be there in memory or on the file system. ohair@286: * Calling this method would trigger parsing for the part's data. So ohair@286: * do not call this unless it is required(otherwise, just wrap MIMEPart ohair@286: * into a object that returns InputStream for e.g DataHandler) ohair@286: * ohair@286: * @return data for the part's content ohair@286: */ ohair@286: public InputStream read() { ohair@286: if (readOnce) { ohair@286: throw new IllegalStateException("readOnce() is called before, read() cannot be called later."); ohair@286: } ohair@286: ohair@286: // Trigger parsing for the part ohair@286: while(tail == null) { ohair@286: if (!part.msg.makeProgress()) { ohair@286: throw new IllegalStateException("No such MIME Part: "+part); ohair@286: } ohair@286: } ohair@286: ohair@286: if (head == null) { ohair@286: throw new IllegalStateException("Already read. Probably readOnce() is called before."); ohair@286: } ohair@286: return new ReadMultiStream(); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Used for an assertion. Returns true when readOnce() is not already called. ohair@286: * or otherwise throw an exception. ohair@286: * ohair@286: *

ohair@286: * Calling this method also marks the stream as 'consumed' ohair@286: * ohair@286: * @return true if readOnce() is not called before ohair@286: */ alanb@368: @SuppressWarnings("ThrowableInitCause") ohair@286: private boolean unconsumed() { ohair@286: if (consumedAt != null) { ohair@286: AssertionError error = new AssertionError("readOnce() is already called before. See the nested exception from where it's called."); ohair@286: error.initCause(consumedAt); ohair@286: throw error; ohair@286: } ohair@286: consumedAt = new Exception().fillInStackTrace(); ohair@286: return true; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Can get the attachment part's content only once. The content ohair@286: * will be lost after the method. Content data is not be stored ohair@286: * on the file system or is not kept in the memory for the ohair@286: * following case: ohair@286: * - Attachement parts contents are accessed sequentially ohair@286: * ohair@286: * In general, take advantage of this when the data is used only ohair@286: * once. ohair@286: * ohair@286: * @return data for the part's content ohair@286: */ ohair@286: public InputStream readOnce() { ohair@286: assert unconsumed(); ohair@286: if (readOnce) { ohair@286: throw new IllegalStateException("readOnce() is called before. It can only be called once."); ohair@286: } ohair@286: readOnce = true; ohair@286: // Trigger parsing for the part ohair@286: while(tail == null) { ohair@286: if (!part.msg.makeProgress() && tail == null) { ohair@286: throw new IllegalStateException("No such Part: "+part); ohair@286: } ohair@286: } ohair@286: InputStream in = new ReadOnceStream(); ohair@286: head = null; ohair@286: return in; ohair@286: } ohair@286: ohair@286: class ReadMultiStream extends InputStream { ohair@286: Chunk current; ohair@286: int offset; ohair@286: int len; ohair@286: byte[] buf; ohair@286: boolean closed; ohair@286: ohair@286: public ReadMultiStream() { ohair@286: this.current = head; ohair@286: len = current.data.size(); ohair@286: buf = current.data.read(); ohair@286: } ohair@286: ohair@286: @Override ohair@286: public int read(byte b[], int off, int sz) throws IOException { alanb@368: if (!fetch()) { alanb@368: return -1; alanb@368: } ohair@286: ohair@286: sz = Math.min(sz, len-offset); ohair@286: System.arraycopy(buf,offset,b,off,sz); ohair@286: offset += sz; ohair@286: return sz; ohair@286: } ohair@286: alanb@368: @Override ohair@286: public int read() throws IOException { ohair@286: if (!fetch()) { ohair@286: return -1; ohair@286: } ohair@286: return (buf[offset++] & 0xff); ohair@286: } ohair@286: ohair@286: void adjustInMemoryUsage() { ohair@286: // Nothing to do in this case. ohair@286: } ohair@286: ohair@286: /** ohair@286: * Gets to the next chunk if we are done with the current one. ohair@286: * @return true if any data available ohair@286: * @throws IOException when i/o error ohair@286: */ ohair@286: private boolean fetch() throws IOException { ohair@286: if (closed) { ohair@286: throw new IOException("Stream already closed"); ohair@286: } ohair@286: if (current == null) { ohair@286: return false; ohair@286: } ohair@286: ohair@286: while(offset==len) { ohair@286: while(!part.parsed && current.next == null) { ohair@286: part.msg.makeProgress(); ohair@286: } ohair@286: current = current.next; ohair@286: ohair@286: if (current == null) { ohair@286: return false; ohair@286: } ohair@286: adjustInMemoryUsage(); ohair@286: this.offset = 0; ohair@286: this.buf = current.data.read(); ohair@286: this.len = current.data.size(); ohair@286: } ohair@286: return true; ohair@286: } ohair@286: alanb@368: @Override ohair@286: public void close() throws IOException { ohair@286: super.close(); ohair@286: current = null; ohair@286: closed = true; ohair@286: } ohair@286: } ohair@286: ohair@286: final class ReadOnceStream extends ReadMultiStream { ohair@286: ohair@286: @Override ohair@286: void adjustInMemoryUsage() { ohair@286: synchronized(DataHead.this) { ohair@286: inMemory -= current.data.size(); // adjust current memory usage ohair@286: } ohair@286: } ohair@286: ohair@286: } ohair@286: ohair@286: ohair@286: }