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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

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

mercurial