src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/staxex/StreamingDataHandler.java

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

ohair@286 1 /*
mkos@408 2 * Copyright (c) 2010, 2012, 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.staxex;
ohair@286 27
ohair@286 28 import javax.activation.DataHandler;
ohair@286 29 import javax.activation.DataSource;
ohair@286 30 import java.io.BufferedInputStream;
ohair@286 31 import java.io.Closeable;
ohair@286 32 import java.io.File;
ohair@286 33 import java.io.IOException;
ohair@286 34 import java.io.InputStream;
ohair@286 35 import java.net.URL;
ohair@286 36
ohair@286 37 /**
ohair@286 38 * {@link DataHandler} extended to offer better buffer management
ohair@286 39 * in a streaming environment.
ohair@286 40 *
ohair@286 41 * <p>
ohair@286 42 * {@link DataHandler} is used commonly as a data format across
ohair@286 43 * multiple systems (such as JAXB/WS.) Unfortunately, {@link DataHandler}
ohair@286 44 * has the semantics of "read as many times as you want", so this makes
ohair@286 45 * it difficult for involving parties to handle a BLOB in a streaming fashion.
ohair@286 46 *
ohair@286 47 * <p>
ohair@286 48 * {@link StreamingDataHandler} solves this problem by offering methods
ohair@286 49 * that enable faster bulk "consume once" read operation.
ohair@286 50 *
ohair@286 51 * @author Jitendra Kotamraju
ohair@286 52 */
ohair@286 53 public abstract class StreamingDataHandler extends DataHandler implements Closeable {
ohair@286 54
ohair@286 55 public StreamingDataHandler(Object o, String s) {
ohair@286 56 super(o, s);
ohair@286 57 }
ohair@286 58
ohair@286 59 public StreamingDataHandler(URL url) {
ohair@286 60 super(url);
ohair@286 61 }
ohair@286 62
ohair@286 63 public StreamingDataHandler(DataSource dataSource) {
ohair@286 64 super(dataSource);
ohair@286 65 }
ohair@286 66
ohair@286 67 /**
ohair@286 68 * Works like {@link #getInputStream()} except that this method
ohair@286 69 * can be invoked only once.
ohair@286 70 *
ohair@286 71 * <p>
ohair@286 72 * This is used as a signal from the caller that there will
ohair@286 73 * be no further {@link #getInputStream()} invocation nor
ohair@286 74 * {@link #readOnce()} invocation on this object (which would
ohair@286 75 * result in {@link IOException}.)
ohair@286 76 *
ohair@286 77 * <p>
ohair@286 78 * When {@link DataHandler} is backed by a streaming BLOB
ohair@286 79 * (such as an attachment in a web service read from the network),
ohair@286 80 * this allows the callee to avoid unnecessary buffering.
ohair@286 81 *
ohair@286 82 * <p>
ohair@286 83 * Note that it is legal to call {@link #getInputStream()}
ohair@286 84 * multiple times and then call {@link #readOnce()} afterward.
ohair@286 85 * Streams created such a way can be read in any order &mdash;
ohair@286 86 * there's no requirement that streams created earlier must be read
ohair@286 87 * first.
ohair@286 88 *
ohair@286 89 * @return
ohair@286 90 * always non-null. Represents the content of this BLOB.
ohair@286 91 * The returned stream is generally not buffered, so for
ohair@286 92 * better performance read in a big batch or wrap this into
ohair@286 93 * {@link BufferedInputStream}.
ohair@286 94 * @throws IOException
ohair@286 95 * if any i/o error
ohair@286 96 */
ohair@286 97 public abstract InputStream readOnce() throws IOException;
ohair@286 98
ohair@286 99 /**
ohair@286 100 * Obtains the BLOB into a specified file.
ohair@286 101 *
ohair@286 102 * <p>
ohair@286 103 * Semantically, this method is roughly equivalent to the following
ohair@286 104 * code, except that the actual implementation is likely to be a lot faster.
ohair@286 105 *
ohair@286 106 * <pre>
ohair@286 107 * InputStream i = getInputStream();
ohair@286 108 * OutputStream o = new FileOutputStream(dst);
ohair@286 109 * int ch;
ohair@286 110 * while((ch=i.read())!=-1) o.write(ch);
ohair@286 111 * i.close();
ohair@286 112 * o.close();
ohair@286 113 * </pre>
ohair@286 114 *
ohair@286 115 * <p>
ohair@286 116 * The main motivation behind this method is that often
ohair@286 117 * {@link DataHandler} that reads data from a streaming source
ohair@286 118 * will use a temporary file as a data store to hold data
ohair@286 119 * (think of commons-fileupload.) In such case this method
ohair@286 120 * can be as fast as calling {@link File#renameTo(File)}.
ohair@286 121 *
ohair@286 122 * <p>
ohair@286 123 * This method shouldn't be called when there are any
ohair@286 124 * open streams.
ohair@286 125 *
ohair@286 126 * <p>
ohair@286 127 * After this method is invoked, {@link #readOnce()} and
ohair@286 128 * {@link #getInputStream()} will simply open the destination
ohair@286 129 * file you've specified as an argument. So if you further
ohair@286 130 * move the file or delete this file, those methods will
ohair@286 131 * behave in undefined fashion. For a simliar reason,
ohair@286 132 * calling this method multiple times will cause
ohair@286 133 * undefined behavior.
ohair@286 134 */
ohair@286 135 public abstract void moveTo(File dst) throws IOException;
ohair@286 136
ohair@286 137 /**
ohair@286 138 * Releases any resources associated with this DataHandler.
ohair@286 139 * (such as an attachment in a web service read from a temp
ohair@286 140 * file will be deleted.) After calling this method, it is
ohair@286 141 * illegal to call any other methods.
ohair@286 142 */
ohair@286 143 public abstract void close() throws IOException;
ohair@286 144
ohair@286 145 }

mercurial