aoqi@0: /* aoqi@0: * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.ws.util; aoqi@0: aoqi@0: import com.sun.istack.internal.NotNull; aoqi@0: import com.sun.istack.internal.Nullable; aoqi@0: aoqi@0: import java.io.File; aoqi@0: import java.io.FileInputStream; aoqi@0: import java.io.FileOutputStream; aoqi@0: import java.io.IOException; aoqi@0: import java.io.InputStream; aoqi@0: import java.util.logging.Level; aoqi@0: import java.util.logging.Logger; aoqi@0: aoqi@0: /** aoqi@0: * Reads a input stream completely and creates a new stream aoqi@0: * by keeping some data in memory and the rest on the file system. aoqi@0: * aoqi@0: * @author Jitendra Kotamraju aoqi@0: */ aoqi@0: public class ReadAllStream extends InputStream { aoqi@0: aoqi@0: private final @NotNull MemoryStream memStream; aoqi@0: private final @NotNull FileStream fileStream; aoqi@0: aoqi@0: private boolean readAll; aoqi@0: private boolean closed; aoqi@0: aoqi@0: private static final Logger LOGGER = Logger.getLogger(ReadAllStream.class.getName()); aoqi@0: aoqi@0: public ReadAllStream() { aoqi@0: memStream = new MemoryStream(); aoqi@0: fileStream = new FileStream(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Reads the data from input stream completely. It keeps aoqi@0: * inMemory size in the memory, and the rest on the file aoqi@0: * system. aoqi@0: * aoqi@0: * Caller's responsibility to close the InputStream. This aoqi@0: * method can be called only once. aoqi@0: * aoqi@0: * @param in from which to be read aoqi@0: * @param inMemory this much data is kept in the memory aoqi@0: * @throws IOException in case of exception aoqi@0: */ aoqi@0: public void readAll(InputStream in, long inMemory) throws IOException { aoqi@0: assert !readAll; aoqi@0: readAll = true; aoqi@0: aoqi@0: boolean eof = memStream.readAll(in, inMemory); aoqi@0: if (!eof) { aoqi@0: fileStream.readAll(in); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public int read() throws IOException { aoqi@0: int ch = memStream.read(); aoqi@0: if (ch == -1) { aoqi@0: ch = fileStream.read(); aoqi@0: } aoqi@0: return ch; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public int read(byte b[], int off, int sz) throws IOException { aoqi@0: int len = memStream.read(b, off, sz); aoqi@0: if (len == -1) { aoqi@0: len = fileStream.read(b, off, sz); aoqi@0: } aoqi@0: return len; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void close() throws IOException { aoqi@0: if (!closed) { aoqi@0: memStream.close(); aoqi@0: fileStream.close(); aoqi@0: closed = true; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Keeps the rest of the data on the file system aoqi@0: private static class FileStream extends InputStream { aoqi@0: private @Nullable File tempFile; aoqi@0: private @Nullable FileInputStream fin; aoqi@0: aoqi@0: void readAll(InputStream in) throws IOException { aoqi@0: tempFile = File.createTempFile("jaxws",".bin"); aoqi@0: FileOutputStream fileOut = new FileOutputStream(tempFile); aoqi@0: try { aoqi@0: byte[] buf = new byte[8192]; aoqi@0: int len; aoqi@0: while((len=in.read(buf)) != -1) { aoqi@0: fileOut.write(buf, 0, len); aoqi@0: } aoqi@0: } finally { aoqi@0: fileOut.close(); aoqi@0: } aoqi@0: fin = new FileInputStream(tempFile); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public int read() throws IOException { aoqi@0: return (fin != null) ? fin.read() : -1; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public int read(byte b[], int off, int sz) throws IOException { aoqi@0: return (fin != null) ? fin.read(b, off, sz) : -1; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void close() throws IOException { aoqi@0: if (fin != null) { aoqi@0: fin.close(); aoqi@0: } aoqi@0: if (tempFile != null) { aoqi@0: boolean success = tempFile.delete(); aoqi@0: if (!success) { aoqi@0: LOGGER.log(Level.INFO, "File {0} could not be deleted", tempFile); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Keeps data in memory until certain size aoqi@0: private static class MemoryStream extends InputStream { aoqi@0: private Chunk head, tail; aoqi@0: private int curOff; aoqi@0: aoqi@0: private void add(byte[] buf, int len) { aoqi@0: if (tail != null) { aoqi@0: tail = tail.createNext(buf, 0, len); aoqi@0: } else { aoqi@0: head = tail = new Chunk(buf, 0, len); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Reads until the size specified aoqi@0: * aoqi@0: * @param in stream from which to be read aoqi@0: * @param inMemory reads until this size aoqi@0: * @return true if eof aoqi@0: * false otherwise aoqi@0: * @throws IOException in case of exception aoqi@0: */ aoqi@0: boolean readAll(InputStream in, long inMemory) throws IOException { aoqi@0: long total = 0; aoqi@0: while(true) { aoqi@0: byte[] buf = new byte[8192]; aoqi@0: int read = fill(in, buf); aoqi@0: total += read; aoqi@0: if (read != 0) { aoqi@0: add(buf, read); aoqi@0: } aoqi@0: if (read != buf.length) { aoqi@0: return true; aoqi@0: } // EOF aoqi@0: if (total > inMemory) { aoqi@0: return false; // Reached in-memory size aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private int fill(InputStream in, byte[] buf) throws IOException { aoqi@0: int read; aoqi@0: int total = 0; aoqi@0: while(total < buf.length && (read=in.read(buf, total, buf.length-total)) != -1) { aoqi@0: total += read; aoqi@0: } aoqi@0: return total; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public int read() throws IOException { aoqi@0: if (!fetch()) { aoqi@0: return -1; aoqi@0: } aoqi@0: return (head.buf[curOff++] & 0xff); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public int read(byte b[], int off, int sz) throws IOException { aoqi@0: if (!fetch()) { aoqi@0: return -1; aoqi@0: } aoqi@0: sz = Math.min(sz, head.len-(curOff-head.off)); aoqi@0: System.arraycopy(head.buf,curOff,b,off,sz); aoqi@0: curOff += sz; aoqi@0: return sz; aoqi@0: } aoqi@0: aoqi@0: // if eof, return false else true aoqi@0: private boolean fetch() { aoqi@0: if (head == null) { aoqi@0: return false; aoqi@0: } aoqi@0: if (curOff == head.off+head.len) { aoqi@0: head = head.next; aoqi@0: if (head == null) { aoqi@0: return false; aoqi@0: } aoqi@0: curOff = head.off; aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: private static final class Chunk { aoqi@0: Chunk next; aoqi@0: final byte[] buf; aoqi@0: final int off; aoqi@0: final int len; aoqi@0: aoqi@0: public Chunk(byte[] buf, int off, int len) { aoqi@0: this.buf = buf; aoqi@0: this.off = off; aoqi@0: this.len = len; aoqi@0: } aoqi@0: aoqi@0: public Chunk createNext(byte[] buf, int off, int len) { aoqi@0: return next = new Chunk(buf, off, len); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: }