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

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     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	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,253 @@
     1.4 +/*
     1.5 + * Copyright (c) 2009, 2012, 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 +import java.util.logging.Level;
    1.40 +import java.util.logging.Logger;
    1.41 +
    1.42 +/**
    1.43 + * Reads a input stream completely and creates a new stream
    1.44 + * by keeping some data in memory and the rest on the file system.
    1.45 + *
    1.46 + * @author Jitendra Kotamraju
    1.47 + */
    1.48 +public class ReadAllStream extends InputStream {
    1.49 +
    1.50 +    private final @NotNull MemoryStream memStream;
    1.51 +    private final @NotNull FileStream fileStream;
    1.52 +
    1.53 +    private boolean readAll;
    1.54 +    private boolean closed;
    1.55 +
    1.56 +    private static final Logger LOGGER = Logger.getLogger(ReadAllStream.class.getName());
    1.57 +
    1.58 +    public ReadAllStream() {
    1.59 +        memStream = new MemoryStream();
    1.60 +        fileStream = new FileStream();
    1.61 +    }
    1.62 +
    1.63 +    /**
    1.64 +     * Reads the data from input stream completely. It keeps
    1.65 +     * inMemory size in the memory, and the rest on the file
    1.66 +     * system.
    1.67 +     *
    1.68 +     * Caller's responsibility to close the InputStream. This
    1.69 +     * method can be called only once.
    1.70 +     *
    1.71 +     * @param in from which to be read
    1.72 +     * @param inMemory this much data is kept in the memory
    1.73 +     * @throws IOException in case of exception
    1.74 +     */
    1.75 +    public void readAll(InputStream in, long inMemory) throws IOException {
    1.76 +        assert !readAll;
    1.77 +        readAll = true;
    1.78 +
    1.79 +        boolean eof = memStream.readAll(in, inMemory);
    1.80 +        if (!eof) {
    1.81 +            fileStream.readAll(in);
    1.82 +        }
    1.83 +    }
    1.84 +
    1.85 +    @Override
    1.86 +    public int read() throws IOException {
    1.87 +        int ch = memStream.read();
    1.88 +        if (ch == -1) {
    1.89 +            ch = fileStream.read();
    1.90 +        }
    1.91 +        return ch;
    1.92 +    }
    1.93 +
    1.94 +    @Override
    1.95 +    public int read(byte b[], int off, int sz) throws IOException {
    1.96 +        int len = memStream.read(b, off, sz);
    1.97 +        if (len == -1) {
    1.98 +            len = fileStream.read(b, off, sz);
    1.99 +        }
   1.100 +        return len;
   1.101 +    }
   1.102 +
   1.103 +    @Override
   1.104 +    public void close() throws IOException {
   1.105 +        if (!closed) {
   1.106 +            memStream.close();
   1.107 +            fileStream.close();
   1.108 +            closed = true;
   1.109 +        }
   1.110 +    }
   1.111 +
   1.112 +    // Keeps the rest of the data on the file system
   1.113 +    private static class FileStream extends InputStream {
   1.114 +        private @Nullable File tempFile;
   1.115 +        private @Nullable FileInputStream fin;
   1.116 +
   1.117 +        void readAll(InputStream in) throws IOException {
   1.118 +            tempFile = File.createTempFile("jaxws",".bin");
   1.119 +            FileOutputStream fileOut = new FileOutputStream(tempFile);
   1.120 +            try {
   1.121 +                byte[] buf = new byte[8192];
   1.122 +                int len;
   1.123 +                while((len=in.read(buf)) != -1) {
   1.124 +                    fileOut.write(buf, 0, len);
   1.125 +                }
   1.126 +            } finally {
   1.127 +                fileOut.close();
   1.128 +            }
   1.129 +            fin = new FileInputStream(tempFile);
   1.130 +        }
   1.131 +
   1.132 +        @Override
   1.133 +        public int read() throws IOException {
   1.134 +            return (fin != null) ? fin.read() : -1;
   1.135 +        }
   1.136 +
   1.137 +        @Override
   1.138 +        public int read(byte b[], int off, int sz) throws IOException {
   1.139 +            return (fin != null) ? fin.read(b, off, sz) : -1;
   1.140 +        }
   1.141 +
   1.142 +        @Override
   1.143 +        public void close() throws IOException {
   1.144 +            if (fin != null) {
   1.145 +                fin.close();
   1.146 +            }
   1.147 +            if (tempFile != null) {
   1.148 +                boolean success = tempFile.delete();
   1.149 +                if (!success) {
   1.150 +                    LOGGER.log(Level.INFO, "File {0} could not be deleted", tempFile);
   1.151 +                }
   1.152 +            }
   1.153 +        }
   1.154 +    }
   1.155 +
   1.156 +    // Keeps data in memory until certain size
   1.157 +    private static class MemoryStream extends InputStream {
   1.158 +        private Chunk head, tail;
   1.159 +        private int curOff;
   1.160 +
   1.161 +        private void add(byte[] buf, int len) {
   1.162 +            if (tail != null) {
   1.163 +                tail = tail.createNext(buf, 0, len);
   1.164 +            } else {
   1.165 +                head = tail = new Chunk(buf, 0, len);
   1.166 +            }
   1.167 +        }
   1.168 +
   1.169 +        /**
   1.170 +         * Reads until the size specified
   1.171 +         *
   1.172 +         * @param in stream from which to be read
   1.173 +         * @param inMemory reads until this size
   1.174 +         * @return true if eof
   1.175 +         *         false otherwise
   1.176 +         * @throws IOException in case of exception
   1.177 +         */
   1.178 +        boolean readAll(InputStream in, long inMemory) throws IOException {
   1.179 +            long total = 0;
   1.180 +            while(true) {
   1.181 +                byte[] buf = new byte[8192];
   1.182 +                int read = fill(in, buf);
   1.183 +                total += read;
   1.184 +                if (read != 0) {
   1.185 +                    add(buf, read);
   1.186 +                }
   1.187 +                if (read != buf.length) {
   1.188 +                    return true;
   1.189 +                }        // EOF
   1.190 +                if (total > inMemory) {
   1.191 +                    return false; // Reached in-memory size
   1.192 +                }
   1.193 +            }
   1.194 +        }
   1.195 +
   1.196 +        private int fill(InputStream in, byte[] buf) throws IOException {
   1.197 +            int read;
   1.198 +            int total = 0;
   1.199 +            while(total < buf.length && (read=in.read(buf, total, buf.length-total)) != -1) {
   1.200 +                total += read;
   1.201 +            }
   1.202 +            return total;
   1.203 +        }
   1.204 +
   1.205 +        @Override
   1.206 +        public int read() throws IOException {
   1.207 +            if (!fetch()) {
   1.208 +                return -1;
   1.209 +            }
   1.210 +            return (head.buf[curOff++] & 0xff);
   1.211 +        }
   1.212 +
   1.213 +        @Override
   1.214 +        public int read(byte b[], int off, int sz) throws IOException {
   1.215 +            if (!fetch()) {
   1.216 +                return -1;
   1.217 +            }
   1.218 +            sz = Math.min(sz, head.len-(curOff-head.off));
   1.219 +            System.arraycopy(head.buf,curOff,b,off,sz);
   1.220 +            curOff += sz;
   1.221 +            return sz;
   1.222 +        }
   1.223 +
   1.224 +        // if eof, return false else true
   1.225 +        private boolean fetch() {
   1.226 +            if (head == null) {
   1.227 +                return false;
   1.228 +            }
   1.229 +            if (curOff == head.off+head.len) {
   1.230 +                head = head.next;
   1.231 +                if (head == null) {
   1.232 +                    return false;
   1.233 +                }
   1.234 +                curOff = head.off;
   1.235 +            }
   1.236 +            return true;
   1.237 +        }
   1.238 +
   1.239 +        private static final class Chunk {
   1.240 +            Chunk next;
   1.241 +            final byte[] buf;
   1.242 +            final int off;
   1.243 +            final int len;
   1.244 +
   1.245 +            public Chunk(byte[] buf, int off, int len) {
   1.246 +                this.buf = buf;
   1.247 +                this.off = off;
   1.248 +                this.len = len;
   1.249 +            }
   1.250 +
   1.251 +            public Chunk createNext(byte[] buf, int off, int len) {
   1.252 +                return next = new Chunk(buf, off, len);
   1.253 +            }
   1.254 +        }
   1.255 +    }
   1.256 +}

mercurial