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

     1 /*
     2  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.xml.internal.ws.util;
    28 import com.sun.istack.internal.NotNull;
    29 import com.sun.istack.internal.Nullable;
    31 import java.io.File;
    32 import java.io.FileInputStream;
    33 import java.io.FileOutputStream;
    34 import java.io.IOException;
    35 import java.io.InputStream;
    37 /**
    38  * Reads a input stream completely and creates a new stream
    39  * by keeping some data in memory and the rest on the file system.
    40  *
    41  * @author Jitendra Kotamraju
    42  */
    43 public class ReadAllStream extends InputStream {
    45     private final @NotNull MemoryStream memStream;
    46     private final @NotNull FileStream fileStream;
    48     private boolean readAll;
    49     private boolean closed;
    51     public ReadAllStream() {
    52         memStream = new MemoryStream();
    53         fileStream = new FileStream();
    54     }
    56     /**
    57      * Reads the data from input stream completely. It keeps
    58      * inMemory size in the memory, and the rest on the file
    59      * system.
    60      *
    61      * Caller's responsibility to close the InputStream. This
    62      * method can be called only once.
    63      *
    64      * @param in from which to be read
    65      * @param inMemory this much data is kept in the memory
    66      * @throws IOException in case of exception
    67      */
    68     public void readAll(InputStream in, long inMemory) throws IOException {
    69         assert !readAll;
    70         readAll = true;
    72         boolean eof = memStream.readAll(in, inMemory);
    73         if (!eof) {
    74             fileStream.readAll(in);
    75         }
    76     }
    78     public int read() throws IOException {
    79         int ch = memStream.read();
    80         if (ch == -1) {
    81             ch = fileStream.read();
    82         }
    83         return ch;
    84     }
    86     @Override
    87     public int read(byte b[], int off, int sz) throws IOException {
    88         int len = memStream.read(b, off, sz);
    89         if (len == -1) {
    90             len = fileStream.read(b, off, sz);
    91         }
    92         return len;
    93     }
    95     public void close() throws IOException {
    96         if (!closed) {
    97             memStream.close();
    98             fileStream.close();
    99             closed = true;
   100         }
   101     }
   103     // Keeps the rest of the data on the file system
   104     private static class FileStream extends InputStream {
   105         private @Nullable File tempFile;
   106         private @Nullable FileInputStream fin;
   108         void readAll(InputStream in) throws IOException {
   109             tempFile = File.createTempFile("jaxws",".bin");
   110             FileOutputStream fileOut = new FileOutputStream(tempFile);
   111             try {
   112                 byte[] buf = new byte[8192];
   113                 int len;
   114                 while((len=in.read(buf)) != -1) {
   115                     fileOut.write(buf, 0, len);
   116                 }
   117             } finally {
   118                 fileOut.close();
   119             }
   120             fin = new FileInputStream(tempFile);
   121         }
   123         public int read() throws IOException {
   124             return (fin != null) ? fin.read() : -1;
   125         }
   127         @Override
   128         public int read(byte b[], int off, int sz) throws IOException {
   129             return (fin != null) ? fin.read(b, off, sz) : -1;
   130         }
   132         @Override
   133         public void close() throws IOException {
   134             if (fin != null) {
   135                 fin.close();
   136             }
   137             if (tempFile != null) {
   138                 tempFile.delete();
   139             }
   140         }
   141     }
   143     // Keeps data in memory until certain size
   144     private static class MemoryStream extends InputStream {
   145         private Chunk head, tail;
   146         private int curOff;
   148         private void add(byte[] buf, int len) {
   149             if (tail != null) {
   150                 tail = tail.createNext(buf, 0, len);
   151             } else {
   152                 head = tail = new Chunk(buf, 0, len);
   153             }
   154         }
   156         /**
   157          * Reads until the size specified
   158          *
   159          * @param in stream from which to be read
   160          * @param inMemory reads until this size
   161          * @return true if eof
   162          *         false otherwise
   163          * @throws IOException in case of exception
   164          */
   165         boolean readAll(InputStream in, long inMemory) throws IOException {
   166             long total = 0;
   167             while(true) {
   168                 byte[] buf = new byte[8192];
   169                 int read = fill(in, buf);
   170                 total += read;
   171                 if (read != 0)
   172                     add(buf, read);
   173                 if (read != buf.length)
   174                     return true;        // EOF
   175                 if (total > inMemory)
   176                     return false;       // Reached in-memory size
   177             }
   178         }
   180         private int fill(InputStream in, byte[] buf) throws IOException {
   181             int read;
   182             int total = 0;
   183             while(total < buf.length && (read=in.read(buf, total, buf.length-total)) != -1) {
   184                 total += read;
   185             }
   186             return total;
   187         }
   189         public int read() throws IOException {
   190             if (!fetch()) {
   191                 return -1;
   192             }
   193             return (head.buf[curOff++] & 0xff);
   194         }
   196         @Override
   197         public int read(byte b[], int off, int sz) throws IOException {
   198             if (!fetch()) {
   199                 return -1;
   200             }
   201             sz = Math.min(sz, head.len-(curOff-head.off));
   202             System.arraycopy(head.buf,curOff,b,off,sz);
   203             curOff += sz;
   204             return sz;
   205         }
   207         // if eof, return false else true
   208         private boolean fetch() {
   209             if (head == null) {
   210                 return false;
   211             }
   212             if (curOff == head.off+head.len) {
   213                 head = head.next;
   214                 if (head == null) {
   215                     return false;
   216                 }
   217                 curOff = head.off;
   218             }
   219             return true;
   220         }
   222         private static final class Chunk {
   223             Chunk next;
   224             final byte[] buf;
   225             final int off;
   226             final int len;
   228             public Chunk(byte[] buf, int off, int len) {
   229                 this.buf = buf;
   230                 this.off = off;
   231                 this.len = len;
   232             }
   234             public Chunk createNext(byte[] buf, int off, int len) {
   235                 return next = new Chunk(buf, off, len);
   236             }
   237         }
   238     }
   239 }

mercurial