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

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/util/ReadAllStream.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,239 @@
     1.4 +/*
     1.5 + * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.util;
    1.30 +
    1.31 +import com.sun.istack.internal.NotNull;
    1.32 +import com.sun.istack.internal.Nullable;
    1.33 +
    1.34 +import java.io.File;
    1.35 +import java.io.FileInputStream;
    1.36 +import java.io.FileOutputStream;
    1.37 +import java.io.IOException;
    1.38 +import java.io.InputStream;
    1.39 +
    1.40 +/**
    1.41 + * Reads a input stream completely and creates a new stream
    1.42 + * by keeping some data in memory and the rest on the file system.
    1.43 + *
    1.44 + * @author Jitendra Kotamraju
    1.45 + */
    1.46 +public class ReadAllStream extends InputStream {
    1.47 +
    1.48 +    private final @NotNull MemoryStream memStream;
    1.49 +    private final @NotNull FileStream fileStream;
    1.50 +
    1.51 +    private boolean readAll;
    1.52 +    private boolean closed;
    1.53 +
    1.54 +    public ReadAllStream() {
    1.55 +        memStream = new MemoryStream();
    1.56 +        fileStream = new FileStream();
    1.57 +    }
    1.58 +
    1.59 +    /**
    1.60 +     * Reads the data from input stream completely. It keeps
    1.61 +     * inMemory size in the memory, and the rest on the file
    1.62 +     * system.
    1.63 +     *
    1.64 +     * Caller's responsibility to close the InputStream. This
    1.65 +     * method can be called only once.
    1.66 +     *
    1.67 +     * @param in from which to be read
    1.68 +     * @param inMemory this much data is kept in the memory
    1.69 +     * @throws IOException in case of exception
    1.70 +     */
    1.71 +    public void readAll(InputStream in, long inMemory) throws IOException {
    1.72 +        assert !readAll;
    1.73 +        readAll = true;
    1.74 +
    1.75 +        boolean eof = memStream.readAll(in, inMemory);
    1.76 +        if (!eof) {
    1.77 +            fileStream.readAll(in);
    1.78 +        }
    1.79 +    }
    1.80 +
    1.81 +    public int read() throws IOException {
    1.82 +        int ch = memStream.read();
    1.83 +        if (ch == -1) {
    1.84 +            ch = fileStream.read();
    1.85 +        }
    1.86 +        return ch;
    1.87 +    }
    1.88 +
    1.89 +    @Override
    1.90 +    public int read(byte b[], int off, int sz) throws IOException {
    1.91 +        int len = memStream.read(b, off, sz);
    1.92 +        if (len == -1) {
    1.93 +            len = fileStream.read(b, off, sz);
    1.94 +        }
    1.95 +        return len;
    1.96 +    }
    1.97 +
    1.98 +    public void close() throws IOException {
    1.99 +        if (!closed) {
   1.100 +            memStream.close();
   1.101 +            fileStream.close();
   1.102 +            closed = true;
   1.103 +        }
   1.104 +    }
   1.105 +
   1.106 +    // Keeps the rest of the data on the file system
   1.107 +    private static class FileStream extends InputStream {
   1.108 +        private @Nullable File tempFile;
   1.109 +        private @Nullable FileInputStream fin;
   1.110 +
   1.111 +        void readAll(InputStream in) throws IOException {
   1.112 +            tempFile = File.createTempFile("jaxws",".bin");
   1.113 +            FileOutputStream fileOut = new FileOutputStream(tempFile);
   1.114 +            try {
   1.115 +                byte[] buf = new byte[8192];
   1.116 +                int len;
   1.117 +                while((len=in.read(buf)) != -1) {
   1.118 +                    fileOut.write(buf, 0, len);
   1.119 +                }
   1.120 +            } finally {
   1.121 +                fileOut.close();
   1.122 +            }
   1.123 +            fin = new FileInputStream(tempFile);
   1.124 +        }
   1.125 +
   1.126 +        public int read() throws IOException {
   1.127 +            return (fin != null) ? fin.read() : -1;
   1.128 +        }
   1.129 +
   1.130 +        @Override
   1.131 +        public int read(byte b[], int off, int sz) throws IOException {
   1.132 +            return (fin != null) ? fin.read(b, off, sz) : -1;
   1.133 +        }
   1.134 +
   1.135 +        @Override
   1.136 +        public void close() throws IOException {
   1.137 +            if (fin != null) {
   1.138 +                fin.close();
   1.139 +            }
   1.140 +            if (tempFile != null) {
   1.141 +                tempFile.delete();
   1.142 +            }
   1.143 +        }
   1.144 +    }
   1.145 +
   1.146 +    // Keeps data in memory until certain size
   1.147 +    private static class MemoryStream extends InputStream {
   1.148 +        private Chunk head, tail;
   1.149 +        private int curOff;
   1.150 +
   1.151 +        private void add(byte[] buf, int len) {
   1.152 +            if (tail != null) {
   1.153 +                tail = tail.createNext(buf, 0, len);
   1.154 +            } else {
   1.155 +                head = tail = new Chunk(buf, 0, len);
   1.156 +            }
   1.157 +        }
   1.158 +
   1.159 +        /**
   1.160 +         * Reads until the size specified
   1.161 +         *
   1.162 +         * @param in stream from which to be read
   1.163 +         * @param inMemory reads until this size
   1.164 +         * @return true if eof
   1.165 +         *         false otherwise
   1.166 +         * @throws IOException in case of exception
   1.167 +         */
   1.168 +        boolean readAll(InputStream in, long inMemory) throws IOException {
   1.169 +            long total = 0;
   1.170 +            while(true) {
   1.171 +                byte[] buf = new byte[8192];
   1.172 +                int read = fill(in, buf);
   1.173 +                total += read;
   1.174 +                if (read != 0)
   1.175 +                    add(buf, read);
   1.176 +                if (read != buf.length)
   1.177 +                    return true;        // EOF
   1.178 +                if (total > inMemory)
   1.179 +                    return false;       // Reached in-memory size
   1.180 +            }
   1.181 +        }
   1.182 +
   1.183 +        private int fill(InputStream in, byte[] buf) throws IOException {
   1.184 +            int read;
   1.185 +            int total = 0;
   1.186 +            while(total < buf.length && (read=in.read(buf, total, buf.length-total)) != -1) {
   1.187 +                total += read;
   1.188 +            }
   1.189 +            return total;
   1.190 +        }
   1.191 +
   1.192 +        public int read() throws IOException {
   1.193 +            if (!fetch()) {
   1.194 +                return -1;
   1.195 +            }
   1.196 +            return (head.buf[curOff++] & 0xff);
   1.197 +        }
   1.198 +
   1.199 +        @Override
   1.200 +        public int read(byte b[], int off, int sz) throws IOException {
   1.201 +            if (!fetch()) {
   1.202 +                return -1;
   1.203 +            }
   1.204 +            sz = Math.min(sz, head.len-(curOff-head.off));
   1.205 +            System.arraycopy(head.buf,curOff,b,off,sz);
   1.206 +            curOff += sz;
   1.207 +            return sz;
   1.208 +        }
   1.209 +
   1.210 +        // if eof, return false else true
   1.211 +        private boolean fetch() {
   1.212 +            if (head == null) {
   1.213 +                return false;
   1.214 +            }
   1.215 +            if (curOff == head.off+head.len) {
   1.216 +                head = head.next;
   1.217 +                if (head == null) {
   1.218 +                    return false;
   1.219 +                }
   1.220 +                curOff = head.off;
   1.221 +            }
   1.222 +            return true;
   1.223 +        }
   1.224 +
   1.225 +        private static final class Chunk {
   1.226 +            Chunk next;
   1.227 +            final byte[] buf;
   1.228 +            final int off;
   1.229 +            final int len;
   1.230 +
   1.231 +            public Chunk(byte[] buf, int off, int len) {
   1.232 +                this.buf = buf;
   1.233 +                this.off = off;
   1.234 +                this.len = len;
   1.235 +            }
   1.236 +
   1.237 +            public Chunk createNext(byte[] buf, int off, int len) {
   1.238 +                return next = new Chunk(buf, off, len);
   1.239 +            }
   1.240 +        }
   1.241 +    }
   1.242 +}

mercurial