Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2009, 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.ws.util; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.istack.internal.NotNull; |
aoqi@0 | 29 | import com.sun.istack.internal.Nullable; |
aoqi@0 | 30 | |
aoqi@0 | 31 | import java.io.File; |
aoqi@0 | 32 | import java.io.FileInputStream; |
aoqi@0 | 33 | import java.io.FileOutputStream; |
aoqi@0 | 34 | import java.io.IOException; |
aoqi@0 | 35 | import java.io.InputStream; |
aoqi@0 | 36 | import java.util.logging.Level; |
aoqi@0 | 37 | import java.util.logging.Logger; |
aoqi@0 | 38 | |
aoqi@0 | 39 | /** |
aoqi@0 | 40 | * Reads a input stream completely and creates a new stream |
aoqi@0 | 41 | * by keeping some data in memory and the rest on the file system. |
aoqi@0 | 42 | * |
aoqi@0 | 43 | * @author Jitendra Kotamraju |
aoqi@0 | 44 | */ |
aoqi@0 | 45 | public class ReadAllStream extends InputStream { |
aoqi@0 | 46 | |
aoqi@0 | 47 | private final @NotNull MemoryStream memStream; |
aoqi@0 | 48 | private final @NotNull FileStream fileStream; |
aoqi@0 | 49 | |
aoqi@0 | 50 | private boolean readAll; |
aoqi@0 | 51 | private boolean closed; |
aoqi@0 | 52 | |
aoqi@0 | 53 | private static final Logger LOGGER = Logger.getLogger(ReadAllStream.class.getName()); |
aoqi@0 | 54 | |
aoqi@0 | 55 | public ReadAllStream() { |
aoqi@0 | 56 | memStream = new MemoryStream(); |
aoqi@0 | 57 | fileStream = new FileStream(); |
aoqi@0 | 58 | } |
aoqi@0 | 59 | |
aoqi@0 | 60 | /** |
aoqi@0 | 61 | * Reads the data from input stream completely. It keeps |
aoqi@0 | 62 | * inMemory size in the memory, and the rest on the file |
aoqi@0 | 63 | * system. |
aoqi@0 | 64 | * |
aoqi@0 | 65 | * Caller's responsibility to close the InputStream. This |
aoqi@0 | 66 | * method can be called only once. |
aoqi@0 | 67 | * |
aoqi@0 | 68 | * @param in from which to be read |
aoqi@0 | 69 | * @param inMemory this much data is kept in the memory |
aoqi@0 | 70 | * @throws IOException in case of exception |
aoqi@0 | 71 | */ |
aoqi@0 | 72 | public void readAll(InputStream in, long inMemory) throws IOException { |
aoqi@0 | 73 | assert !readAll; |
aoqi@0 | 74 | readAll = true; |
aoqi@0 | 75 | |
aoqi@0 | 76 | boolean eof = memStream.readAll(in, inMemory); |
aoqi@0 | 77 | if (!eof) { |
aoqi@0 | 78 | fileStream.readAll(in); |
aoqi@0 | 79 | } |
aoqi@0 | 80 | } |
aoqi@0 | 81 | |
aoqi@0 | 82 | @Override |
aoqi@0 | 83 | public int read() throws IOException { |
aoqi@0 | 84 | int ch = memStream.read(); |
aoqi@0 | 85 | if (ch == -1) { |
aoqi@0 | 86 | ch = fileStream.read(); |
aoqi@0 | 87 | } |
aoqi@0 | 88 | return ch; |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | @Override |
aoqi@0 | 92 | public int read(byte b[], int off, int sz) throws IOException { |
aoqi@0 | 93 | int len = memStream.read(b, off, sz); |
aoqi@0 | 94 | if (len == -1) { |
aoqi@0 | 95 | len = fileStream.read(b, off, sz); |
aoqi@0 | 96 | } |
aoqi@0 | 97 | return len; |
aoqi@0 | 98 | } |
aoqi@0 | 99 | |
aoqi@0 | 100 | @Override |
aoqi@0 | 101 | public void close() throws IOException { |
aoqi@0 | 102 | if (!closed) { |
aoqi@0 | 103 | memStream.close(); |
aoqi@0 | 104 | fileStream.close(); |
aoqi@0 | 105 | closed = true; |
aoqi@0 | 106 | } |
aoqi@0 | 107 | } |
aoqi@0 | 108 | |
aoqi@0 | 109 | // Keeps the rest of the data on the file system |
aoqi@0 | 110 | private static class FileStream extends InputStream { |
aoqi@0 | 111 | private @Nullable File tempFile; |
aoqi@0 | 112 | private @Nullable FileInputStream fin; |
aoqi@0 | 113 | |
aoqi@0 | 114 | void readAll(InputStream in) throws IOException { |
aoqi@0 | 115 | tempFile = File.createTempFile("jaxws",".bin"); |
aoqi@0 | 116 | FileOutputStream fileOut = new FileOutputStream(tempFile); |
aoqi@0 | 117 | try { |
aoqi@0 | 118 | byte[] buf = new byte[8192]; |
aoqi@0 | 119 | int len; |
aoqi@0 | 120 | while((len=in.read(buf)) != -1) { |
aoqi@0 | 121 | fileOut.write(buf, 0, len); |
aoqi@0 | 122 | } |
aoqi@0 | 123 | } finally { |
aoqi@0 | 124 | fileOut.close(); |
aoqi@0 | 125 | } |
aoqi@0 | 126 | fin = new FileInputStream(tempFile); |
aoqi@0 | 127 | } |
aoqi@0 | 128 | |
aoqi@0 | 129 | @Override |
aoqi@0 | 130 | public int read() throws IOException { |
aoqi@0 | 131 | return (fin != null) ? fin.read() : -1; |
aoqi@0 | 132 | } |
aoqi@0 | 133 | |
aoqi@0 | 134 | @Override |
aoqi@0 | 135 | public int read(byte b[], int off, int sz) throws IOException { |
aoqi@0 | 136 | return (fin != null) ? fin.read(b, off, sz) : -1; |
aoqi@0 | 137 | } |
aoqi@0 | 138 | |
aoqi@0 | 139 | @Override |
aoqi@0 | 140 | public void close() throws IOException { |
aoqi@0 | 141 | if (fin != null) { |
aoqi@0 | 142 | fin.close(); |
aoqi@0 | 143 | } |
aoqi@0 | 144 | if (tempFile != null) { |
aoqi@0 | 145 | boolean success = tempFile.delete(); |
aoqi@0 | 146 | if (!success) { |
aoqi@0 | 147 | LOGGER.log(Level.INFO, "File {0} could not be deleted", tempFile); |
aoqi@0 | 148 | } |
aoqi@0 | 149 | } |
aoqi@0 | 150 | } |
aoqi@0 | 151 | } |
aoqi@0 | 152 | |
aoqi@0 | 153 | // Keeps data in memory until certain size |
aoqi@0 | 154 | private static class MemoryStream extends InputStream { |
aoqi@0 | 155 | private Chunk head, tail; |
aoqi@0 | 156 | private int curOff; |
aoqi@0 | 157 | |
aoqi@0 | 158 | private void add(byte[] buf, int len) { |
aoqi@0 | 159 | if (tail != null) { |
aoqi@0 | 160 | tail = tail.createNext(buf, 0, len); |
aoqi@0 | 161 | } else { |
aoqi@0 | 162 | head = tail = new Chunk(buf, 0, len); |
aoqi@0 | 163 | } |
aoqi@0 | 164 | } |
aoqi@0 | 165 | |
aoqi@0 | 166 | /** |
aoqi@0 | 167 | * Reads until the size specified |
aoqi@0 | 168 | * |
aoqi@0 | 169 | * @param in stream from which to be read |
aoqi@0 | 170 | * @param inMemory reads until this size |
aoqi@0 | 171 | * @return true if eof |
aoqi@0 | 172 | * false otherwise |
aoqi@0 | 173 | * @throws IOException in case of exception |
aoqi@0 | 174 | */ |
aoqi@0 | 175 | boolean readAll(InputStream in, long inMemory) throws IOException { |
aoqi@0 | 176 | long total = 0; |
aoqi@0 | 177 | while(true) { |
aoqi@0 | 178 | byte[] buf = new byte[8192]; |
aoqi@0 | 179 | int read = fill(in, buf); |
aoqi@0 | 180 | total += read; |
aoqi@0 | 181 | if (read != 0) { |
aoqi@0 | 182 | add(buf, read); |
aoqi@0 | 183 | } |
aoqi@0 | 184 | if (read != buf.length) { |
aoqi@0 | 185 | return true; |
aoqi@0 | 186 | } // EOF |
aoqi@0 | 187 | if (total > inMemory) { |
aoqi@0 | 188 | return false; // Reached in-memory size |
aoqi@0 | 189 | } |
aoqi@0 | 190 | } |
aoqi@0 | 191 | } |
aoqi@0 | 192 | |
aoqi@0 | 193 | private int fill(InputStream in, byte[] buf) throws IOException { |
aoqi@0 | 194 | int read; |
aoqi@0 | 195 | int total = 0; |
aoqi@0 | 196 | while(total < buf.length && (read=in.read(buf, total, buf.length-total)) != -1) { |
aoqi@0 | 197 | total += read; |
aoqi@0 | 198 | } |
aoqi@0 | 199 | return total; |
aoqi@0 | 200 | } |
aoqi@0 | 201 | |
aoqi@0 | 202 | @Override |
aoqi@0 | 203 | public int read() throws IOException { |
aoqi@0 | 204 | if (!fetch()) { |
aoqi@0 | 205 | return -1; |
aoqi@0 | 206 | } |
aoqi@0 | 207 | return (head.buf[curOff++] & 0xff); |
aoqi@0 | 208 | } |
aoqi@0 | 209 | |
aoqi@0 | 210 | @Override |
aoqi@0 | 211 | public int read(byte b[], int off, int sz) throws IOException { |
aoqi@0 | 212 | if (!fetch()) { |
aoqi@0 | 213 | return -1; |
aoqi@0 | 214 | } |
aoqi@0 | 215 | sz = Math.min(sz, head.len-(curOff-head.off)); |
aoqi@0 | 216 | System.arraycopy(head.buf,curOff,b,off,sz); |
aoqi@0 | 217 | curOff += sz; |
aoqi@0 | 218 | return sz; |
aoqi@0 | 219 | } |
aoqi@0 | 220 | |
aoqi@0 | 221 | // if eof, return false else true |
aoqi@0 | 222 | private boolean fetch() { |
aoqi@0 | 223 | if (head == null) { |
aoqi@0 | 224 | return false; |
aoqi@0 | 225 | } |
aoqi@0 | 226 | if (curOff == head.off+head.len) { |
aoqi@0 | 227 | head = head.next; |
aoqi@0 | 228 | if (head == null) { |
aoqi@0 | 229 | return false; |
aoqi@0 | 230 | } |
aoqi@0 | 231 | curOff = head.off; |
aoqi@0 | 232 | } |
aoqi@0 | 233 | return true; |
aoqi@0 | 234 | } |
aoqi@0 | 235 | |
aoqi@0 | 236 | private static final class Chunk { |
aoqi@0 | 237 | Chunk next; |
aoqi@0 | 238 | final byte[] buf; |
aoqi@0 | 239 | final int off; |
aoqi@0 | 240 | final int len; |
aoqi@0 | 241 | |
aoqi@0 | 242 | public Chunk(byte[] buf, int off, int len) { |
aoqi@0 | 243 | this.buf = buf; |
aoqi@0 | 244 | this.off = off; |
aoqi@0 | 245 | this.len = len; |
aoqi@0 | 246 | } |
aoqi@0 | 247 | |
aoqi@0 | 248 | public Chunk createNext(byte[] buf, int off, int len) { |
aoqi@0 | 249 | return next = new Chunk(buf, off, len); |
aoqi@0 | 250 | } |
aoqi@0 | 251 | } |
aoqi@0 | 252 | } |
aoqi@0 | 253 | } |