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