src/share/jaxws_classes/com/sun/xml/internal/ws/util/ReadAllStream.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial